Starting MindStorm NXT 2.0 Development On Linux

For Christmas, I’ve got a fun little toy: The LEGO MindStorm NXT 2.0 Kit. It came with a NXT brick and a CD containing software for programming it. However that software only runs on a Windows and Mac, neither of which I use regularly. But the MindStorm franchise has a large hobbyist community, so large that LEGO even saw it fit to to open source the firmware on the NXT brick! This made it easy for people to develop tools that run on Linux.

After some quick searching, I’ve decided to settle with Next Byte Codes (NCB) and Not eXactly C (NXC). These are two text-based (as opposed to the official graphics-based) programming languages. NCB is very similar to assembly, and NXC is very similar to C. Of the two, I use NXT, because I find that C is better at making complex code more readable. Of course, that is just my personal preference.

In this tutorial, I want to provide a quick guide about settting up and using the NCB/NXC compilers to write code for the NXT brick. This is not a programming guide, as several already exist and are listed on the NCB/NXC site (NXC Guide, NBC Guide, NBC Tutorial, and also a book can be purchased!). But those tutorial suppose that you are using the Windows-only bricxcc IDE to transfer code to the NXT brick. They do not document the steps that you have to take by using the tools directly, which is what you have to do on Linux.

There are several parts to this tutorial:

Downloading and setting up the tools

Transferring code over USB

Transferring code over Bluetooth

This tutorial was written and tested using Ubuntu Karmic Koala, but it should work fine on other distributions. In fact I mainly use a Gentoo desktop to program my NXT brick, but it does not have Bluetooth so I would not be able to compile this entire tutorial on it.

Finally, during the tutorial I use a sample program called “dancer”, that can be downloaded here. The package contains a version written in NCB and a version written in NXC. It makes a car-bot, built by following the instructions in the booklet from the Mindstorm box, dance by following random instructions (move, turn, make sounds). The result are very boring, but very simple and thus serve their purpose in this tutorial.

If you have any questions about this tutorial or how to accomplish other things, do not hesitate to contact me.

Download the bricxcc source from SVN. Only the SVN version (as of now) contains support for some of the new sensors included with the NXT 2.0 brick and works with 64-bit Linux. svn co https://bricxcc.svn.sourceforge.net/svnroot/bricxcc/ bricxcc

Enter the bricxcc source code. cd bricxcc

Build NeXTTool, used for transferring files to and from the brick, and other miscellaneous operations. make -f ./nexttool.mak

Enter the NXT directory, inside of bricxcc source code. cd NXT

Build the NCB/NXC compilers. make -f nbcunix.mak

Install the build tools. I copy my to ${HOME}/bin, which means that you need to have it included in your PATH. cp nbc ../NeXTTool ~/bin

Setup Aliases For Your NXT Bricks

Using the NBC/NXC tools, you can setup aliases for your NXT bricks. This means that if you connect two bricks at the same time, you can pick which one to communicate to by specifying it’s alias. If you are only connecting one brick via USB, you do not have to setup any aliases as a default one called “usb” is created. However, if you plan to use Bluetooth, you have to setup an alias by following these steps:

Connect your brick in any way you want to communicate with it, either via Bluetooth (see beginning of “Transferring Code Over Bluetooth“), USB or both.

Dump all found bricks to a file called nxt.dat, located in your home directory. The following command will create a new file, overwriting an existing file, and fill it with all found bricks. If you do not want to overwrite the existing file, because you already have some bricks listed, just run the command before > to display the list of found bricks and append them manually. NeXTTool -listbricks > ~/nxt.dat

Open the ~/nxt.dat file using your favourite text editor. Each line lists a found device, with the part before = being the alias and the part after being the device ID. The device ID will be unique, and automatically generated by your brick. Don’t touch it! Mine looks like the following:

Now you can use either of the aliases above for communicating with your brick. When I am connected via usb, I use the alias usb. When I am connected via Bluetooth, I use the alias bt. You use these aliases by adding the flag -S=alias to nbc or /COM=alias to NeXTTool.

Transferring Code Over USB

Create a file called 45-legonxt.rules with the following contents and place it into the /etc/udev/rules.d directory. Make sure to set the GROUP variable to a group that exists. On Ubuntu, plugdev is the group that all users that can use pluggable devices belong to. On Gentoo, I use the usb

Plug in the NXT brick via USB and turn it on. You should now see a new file under /dev corresponding to it named /dev/legonxt-#-#. If it does not exist, then something is wrong. ls /dev | grep legonxt

Find out your NXT firmware version, which is found by navigating the NXT brick LCD display using the arrow buttons. The version is found under: Settings -> NXT Version. It is the first row. My version is 1.28, as seen in the picture below.

The -v=128 tells nbc to compile the code for firmware version 1.28. Without this, I couldn’t get the transfer to work. Replace 128 with any other version that you are using, and make sure to drop the dot (Notice I wrote 128 and NOT 1.28. That is not a typo.)

dancer.nxc is the file to compile and transfer. You can also also type dancer.nbc to transfer the byte-code file. The command (both NBC and NXC use the nbc command) and other options are exactly the same for both.

If everything worked correctly, then wait for a beep from the NXT brick. No beep means that there was a problem, even if no error messages were printer.

On the NXT brick, get to the main menu by pressing the gray button repeatedly. Then navigate to My Files->Software files and find the file that you compiled, without the extension. Press the orange button to select it, and press the orange button once more to run it. Make sure the car-bot is now on the ground (and the NXT brick is connected to it), because this will make it dance!

Transferring Code Over Bluetooth

Turn on Bluetooth on the NXT brick. This is done by going under Bluetooth item in the NXT’s LCD menu and clicking the orange button until you get the message that Bluetooth was turned on.

Install Bluetooth support. sudo apt-get install bluetooth

Figure out the MAC address of your NXT brick by running the following command. hcitool scan The output should look like the following:

Where 00:00:00:00:00:00 (or some other numbers/letters) is NXT’s MAC address.

Add the following to the end of the /etc/bluetooth/rfcomm.conf file. If you already have entries in the file, increment rfcomm0 to the next available number (ei: rfcomm1). This will connect to your NXT brick as soon as you start it up. Make sure to change the string of 00′s after device to the MAC address you found in step 3.

Restart bluetooth to force it to reread the new rfcomm.conf file sudo /etc/init.d/bluetooth restart

Due to a bug in Ubuntu Karmic Koala (9.10), rfcomm doesn’t bind to the specified devices automatically. As a workaround, add the following command to the end of /etc/rc.local (but before exit 0) file. This will be only run next time you restart your computer, so run the same command in command line manually. rfcomm bind all

You should now see the NXT brick when you run rfcomm. Also you should see a device named /dev/rfcomm0

The -S=bt flag tells nbc to connect to the brick described by the alias bt. Replace bt with whatever you set your brick’s alias to.

The -d flag tells the nbc to download the compiled code to the brick.

The -v=128 tells nbc to compile the code for firmware version 1.28. Without this, I couldn’t get the transfer to work. Replace 128 with any other version that you are using, and make sure to drop the dot (Notice I wrote 128 and NOT 1.28. That is not a typo.)

dancer.nxc is the file to compile and transfer. You can also also type dancer.nbc to transfer the byte-code file. The command (both NBC and NXC use the nbc command) and other options are exactly the same for both.

If everything worked correctly, wait for a beep from the NXT brick. No beep means that there was a problem, even if no error messages were printer.

On your brick, you should see a message asking you to enter a passkey, giving you a default option of 1234. Just use this default option, and press the orange button.

If you are using Ubuntu, a window should pop out asking you to type in the passkey. Type in 1234 or whatever else you specified in step 10. Press Ok. I am not sure what will happen on systems not running Ubuntu, but you should have some way to enter in the passkey.

On the NXT brick, get to the home directory by pressing the gray button repeatedly. Then navigate to My Files->Software files and find the file that you compiled, without the extension. Press the orange button to select it, and press the orange button once more to run it. Make sure the car-bot is now on the ground (and the NXT brick is connected to it), because this will make it dance!

Note: If you run the nbc or NeXTTool with the -help flag, you might have seen a flag called -BT that talks about connecting via Bluetooth. This flag has been depreciated, and I didn’t even get it to work without applying this patch: nobluetooth-r274.patch. In short, don’t use the -BT flag.

Credits

The following sites talk about what I just described here, but in less detail. I used them as a starting point for this tutorial, so a lot of thanks goes to their authors

John Hansen for pointing out that the -BT flag is deprecated and for creating the NBC/NXC tool set.

14 Comments

The -BT switch is sort of deprecated. Instead you should start using brick resource strings or aliases for brick resource strings in a file called nxt.dat which should be stored in the ~ directory. That may be why you had to patch the source code to get the -BT switch to work. If you use a full brick resource string or an alias for one then you should not need to use -BT. There are several posts on the nxtasy forums as well as a post on the nxtasy blog that describes the format for the brick resource strings that go in the nxt.dat file. Once you have the nxt.dat file populated then you simply use the short alias (or the full brick resource string) on the nbc command line like this:

Finally I got this working, thanks.
I compiled nexttool and nxc from svn (must change ppc386 to ppcx64 on nexttool.mak).
Then I mistyped ~/nxt.dat couple of times. (Any error message would make things easier)