Quick Start


These code examples are for v 1.0 and 1.1 of the API. In v1.2 beta and newer I have switched to using nullable types and the code has to be modified accordingly.

Prerequisites

Before you can do anything with the MindSqualls library, you need to have established a bluetooth partnership between your computer and your LEGO MINDSTORMS NXT robot.

Secondly you'll need to know which COM-port it connects to. Open your bluetooth control panel, right-click on the icon representing the NXT robot, and chose to see details. It should say something like "COM40" somewhere. Remember the number 40. The examples below will assume that you have 40.

Example 1 - Hole Through; Reading the Name of Your Robot

In the following I'll assume that you are using Visual Studio 2005, but it should work just as well with another editor like say SharpDevelop.

Create a console application project.

Create a reference to the NKH.MindSqualls DLL, and add the line:

using NKH.MindSqualls;

at the top with the other using-directives.

Now write the following code in the Main()-method:

// Create a bluetooth connection on COM40.
NxtBluetoothConnection conn = new NxtBluetoothConnection(40);

// Connect to the NXT.
conn.Connect();

// Query the device info, and write out the NXT's name.
Console.WriteLine(conn.GetDeviceInfo().nxtName);

// Disconnect for the NXT.
conn.Disconnect();

Example 2 - Renaming Your NXT Robot

Keep the rest of the code, but replace the code block:

// Query the device info, and write out the NXT's name.
Console.WriteLine(conn.GetDeviceInfo().nxtName);

with this:

// Rename the NXT brick to "MindSqualls".
conn.SetBrickName("MindSqualls");

When you run it you should see the name in the NXT bricks display change to "MindSqua"; Even though the name can be up to 15 characters in length, there is not enough space in the display to actually show it.

Example 3 - Reading the Firmware Version

Change the code block to this, if you want the firmware version:

// Write out the firmware version of the NXT-brick.
Console.WriteLine(conn.GetFirmwareVersion().firmwareVersion);

It should say something like 1.4 depending on the firmware in your NXT.

BTW: If you have not upgraded to at least 1.4 by now, I strongly recommend it.

Example 4 - List the Files in Your NXT Robot

The following code will list all the files that you've got on your NXT brick:

// Create a bluetooth connection on COM40.
NxtBluetoothConnection conn = new NxtBluetoothConnection(40);

// Connect to the NXT.
conn.Connect();

// Find the first file on the NXT brick.
NxtFindFileReply fileInfo = conn.FindFirst("*.*");

// Continue for as long as a file was found.
while (fileInfo.FileFound)
{
    // Write the name of the file.
    Console.WriteLine(fileInfo.fileName);

    // Go to the next file.
    fileInfo = conn.FindNext(fileInfo.handle);
}

// Disconnect from the NXT.
conn.Disconnect();

You may recognize some of them as programs you've downloaded to the NXT brick. They are listed with the .rxe extension.

There are probably other as well:

Extension File type
.rdt Datalog files.
.ric Graphics files.
.rfw Firmware files.
.rpg Programs that you've programmed directly on the NXT brick.
.rso Sound files.
.rtm Try-Me programs.
.rxe Your downloaded NXT-G programs.
.sys System files.

Simply use the filemask *.rxe if you are only interested in the NXT-G programs.

Example 5 - Running a Program on the NXT Robot

Lets say that you have experimented with the TriBot and that you've downloaded the TriBotProgram6 program to it.

You can then tell the robot to run it with this line of code:

// Run the TriBotProgram6 program.
conn.StartProgram("TriBotProgram6.rxe");
Stop any running program with:
// Stop any running program.
conn.StopProgram();

On to the Serious Stuff!

You can do all sorts of nifty things directly through the direct bluetooth link to your NXT robot.

However, lets face it: You want to be able to run your robot, and get sensor-readings. Even though you can do that through the bluetooth (using the commands SetInputMode(), GetInputValues(), SetOutputState(), GetOutputState(), LsGetStatus(), LsRead() and LsWrite()) it is properly more than you would like to deal with right from the start.

The NxtBrick-, NxtMotor- and NxtSensor-classes provide a package for this.

Example 1 - Take 2

Revisiting example 1, the below code does the same as above:

// Create a NXT brick on COM40.
NxtBrick brick = new NxtBrick(40);

// Connect to the NXT.
brick.Connect();

// Write out the NXT's name.
Console.WriteLine(brick.Name);

// Disconnect from the NXT.
brick.Disconnect();

Example 2 - Take 2

// Rename the NXT brick to "MindSqualls".
brick.Name = "MindSqualls";

Nice, eh?

Example 6 - Running Motor B

Ok, ok, OK! You want to run your robot! Simply create a motor, attach it to the appropriate port, and run it:

// Create a NXT brick on COM40.
NxtBrick brick = new NxtBrick(40);

// Create a motor.
NxtMotor motor = new NxtMotor();

// Attach it to port B of the NXT brick.
brick.MotorB = motor;

// Connect to the NXT.
brick.Connect();

// Run it at 75% power, for a 360 degree run.
motor.Run(75, 360);

// Disconnect from the NXT.
brick.Disconnect();

Example 7 - Running both Motors B and C

To drive straight forward, you probably need two motors:

// Create a NXT brick on COM40.
NxtBrick brick = new NxtBrick(40);

// Attach motors to port B and C on the NXT.
brick.MotorB = new NxtMotor();
brick.MotorC = new NxtMotor();

// Connect to the NXT.
brick.Connect();

// Run them at 75% power, for a 3600 degree run.
brick.MotorB.Run(75, 3600);
brick.MotorC.Run(75, 3600);

// Disconnect from the NXT.
brick.Disconnect();

However, the above do not guarantee that the NXT robot will actually go straight; One motor may get ahead of the other. It is even worse if you want your NXT robot to make a controlled turn. Two motors are not synchronized.

// Create a NXT brick on COM40.
NxtBrick brick = new NxtBrick(40);

// Attach motors to port B and C on the NXT.
brick.MotorB = new NxtMotor();
brick.MotorC = new NxtMotor();

// Syncronize the two motors.
NxtMotorSync motorPair = new NxtMotorSync(brick.MotorB, brick.MotorC);

// Connect to the NXT.
brick.Connect();

// Run them at 75% power, for a 3600 degree run.
motorPair.Run(75, 3600, 0);
Console.WriteLine("Running...");

// Wait 8 seconds before putting the motors into idle-mode.
System.Threading.Thread.Sleep(8 * 1000);
motorPair.Idle();

// Disconnect from the NXT.
brick.Disconnect();

The 3rd parameter, turnRatio, will determine which way the NXT robot is turning. The turnRatio can be in the interval [-100, 100]. Try the various values, and see what the result is:

motorPair.Run(75, 3600, -100);
motorPair.Run(75, 3600, -50);
motorPair.Run(75, 3600, -5);
motorPair.Run(75, 3600, 0);
motorPair.Run(75, 3600, 5);
motorPair.Run(75, 3600, 50);
motorPair.Run(75, 3600, -100);

Example 8 - Adding a Touch Sensor

This is just a little bit more difficult than adding a motor:

  1. Create the sensor
  2. Attach it to the appropriate port
  3. Define a poll-interval
  4. Set up an eventlistner for the OnPolled-event

and then ... listen:

static void Main(string[] args)
{
    // Create a NXT brick on COM40.
    NxtBrick brick = new NxtBrick(40);

    // Create a touch sensor.
    NxtTouchSensor touchSensor = new NxtTouchSensor();

    // Attach it to port 1.
    brick.Sensor1 = touchSensor;

    // Poll it every 50 milliseconds.
    touchSensor.PollInterval = 50;

    // Handle the OnPolled-event of the sensor.
    touchSensor.OnPolled += new Polled(touchSensor_OnPolled);

    // Connect to the NXT brick.
    brick.Connect();

    // Now wait...
    Console.WriteLine("Press any key to stop.");
    Console.ReadKey();

    // Disconnect from the NXT.
    brick.Disconnect();
}

static void touchSensor_OnPolled(NxtPollable polledItem)
{
    NxtTouchSensor touchSensor = (NxtTouchSensor) polledItem;
    if (touchSensor.IsPressed)
        Console.WriteLine("Pressed");
    else
        Console.WriteLine("Please press the sensor!");
}

Example 9 - Putting it all Together

// Create a NXT brick on COM40.
NxtBrick brick = new NxtBrick(40);

// Attach motors to port B and C on the NXT.
brick.MotorB = new NxtMotor();
brick.MotorC = new NxtMotor();

// Synchronize the two motors.
NxtMotorSync motorPair = new NxtMotorSync(brick.MotorB, brick.MotorC);

// Attach a touch sensor to port 1.
brick.Sensor1 = new NxtTouchSensor();

// Poll it every 50 milliseconds.
brick.Sensor1.PollInterval = 50;

// Connect to the NXT brick.
brick.Connect();

// Run the motors forever ...
motorPair.Run(75, 0, 0);

Console.WriteLine("Running...");
// ... and wait
do
{
    // ... and wait...
}
while (!((NxtTouchSensor) brick.Sensor1).IsPressed);

// Put the motors into idle-state.
motorPair.Idle();

// Disconnect from the NXT.
brick.Disconnect();

Menu

Home
Introduction
Quick Start v1.1 (C#)
Quick Start v1.2 (C#)
Quick Start v2.0 (C#)
MotorControl
Samples (C#)
User Contributions
Documentation
Downloads
Updates
Future?
You People Using It
Credits
Legal Stuff

Links

MINDSTORMS
Tech stuff
HiTechnic

Visit Steve Hassenplug's
NXT site for a list of similar tools.


The NXT STEP
MindBOARDS

Feedback

Forum
Guestbook
E-Mail