Skip to content
Dec 27 / kkrizka

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 Tutorialand 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:

  1. Downloading and setting up the tools
  2. Transferring code over USB
  3. 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.

Downloading and Setting Up The Tools

  1. Install the required Free Pascal Compiler. The tools are written in PASCAL.
    sudo aptitude install fpc libusb-dev
  2. 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
  3. Enter the  bricxcc source code.
    cd bricxcc
  4. Build NeXTTool, used for transferring files to and from the brick, and other miscellaneous operations. 
    make -f ./nexttool.mak
  5. Enter the NXT directory, inside of bricxcc source code.
    cd NXT
  6. Build the NCB/NXC compilers.
    make -f nbcunix.mak
  7. 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:

  1. 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.
  2. 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
  3. 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:

    000000000000=USB0::0o0000::000000::000000000000::RAW
    BTH::NXT=BTH::NXT::00:00:00:00:00:00::5

    The first line is my brick connected via USB, and the second line is my brick connected via Bluetooth.

  4. Change the part before the equal sign, the alias, to something that you will remember. Save your changes. The following is my ~/nxt.dat file after my changes:

    usb=USB0::0o0000::000000::000000000000::RAW
    bt=BTH::NXT::00:00:00:00:00:00::5

  5. 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

  1. 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

     

    group.SUBSYSTEM==”usb_device”, ACTION==”add”, SYSFS{idVendor}==”0694″,
    SYSFS{idProduct}==”0002″, SYMLINK+=”legonxt-%k”, GROUP=”plugdev”, MODE=”0660″, RUN+=”/etc/udev/legonxt.sh”

  2. Create a file called legonxt.sh with the following contents and place it into the /etc/udev directory. Don’t forget to edit the GROUP variable here too.
    #!/bin/bash
     
    GROUP=plugdev
    if [ "${ACTION}" = "add" ] && [ -f "${DEVICE}" ]
    then
     chmod o-rwx "${DEVICE}"
     chgrp "${GROUP}" "${DEVICE}"
     chmod g+rw "${DEVICE}"
    fi
  3. Make the legonxt.sh file executable
    sudo chmod +x /etc/udev/legonxt.sh
  4. 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
  5. 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.
  6. Download my dancer code (or write your own), unpack it and run the following command.
    nbc -d -v=128 dancer.nxc
    • 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.
  7. 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.
  8. 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

  1. 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.
  2. Install Bluetooth support.
    sudo apt-get install bluetooth
  3. Figure out the MAC address of your NXT brick by running the following command.
    hcitool scan
    The output should look like the following:

    kkrizka@sein:~/Projects/NXT$ hcitool scan
    Scanning …
    00:00:00:00:00:00    NXT

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

  4. 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.

    rfcomm0 {
    # Automatically bind the device at startup
    bind yes;
    # Bluetooth address of the device
    device 00:00:00:00:00:00;
    # RFCOMM channel for the connection
    channel 1;
    # Description of the connection
    comment “Mindstorm NXT”;
    }

  5. Restart bluetooth to force it to reread the new rfcomm.conf file
    sudo /etc/init.d/bluetooth restart
  6. 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
  7. You should now see the NXT brick when you run rfcomm. Also you should see a device named /dev/rfcomm0

    kkrizka@sein:/var/lib/bluetooth$ rfcomm
    rfcomm0: 00:00:00:00:00:00 channel 1 clean
    kkrizka@sein:/var/lib/bluetooth$ ls /dev/ | grep rfcomm
    rfcomm0

  8. Setup an alias for your brick, as shown in the Setup Aliases For Your NXT Bricks section.
  9. Download my dancer code (or write your own), unpack it and run the following command.

    nbc -S=bt -d -v=128 dancer.nxc

    • 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.
  10. 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.
  11. 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.
  12. 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.
  13. 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

14 Comments

Leave a comment
  1. John Hansen / Dec 27 2009

    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:

    ./nbc -S=alias -d filename.nxc

    John Hansen

  2. Karol Krizka / Dec 29 2009

    Thank you very much for the tip!

    It works without my patch. So I’ve updated my guide with that information.

  3. Aapo / Jan 24 2010

    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)

  4. Ewen / Mar 1 2010

    Great article, thanks a lot. The steps are very clearly described.

    Managed to get it working without too much trouble.

    A couple of notes for others –
    in the legonxt.sh script make sure you replace the && with &&.
    make sure you restart udev after adding your rules

    Also thanks to Aapo, for his comment which I also needed to do.

  5. Timur / Mar 22 2010

    Thank you for the post – very helpful.

  6. Mildred Ki'Lya / Apr 8 2010

    Note: if you copy/paste the udev rule directly, it might not work because the quote characters aren’t the ASCII characters but some beautiful unicode characters.

  7. Karol Krizka / Apr 8 2010

    Mildred: Thank you for pointing that out. WordPress is very annoying when it comes to formatting code.

  8. Marco / Oct 22 2011

    You write about the tool NeXTTool but I was not able to find it after my compilation. However I found nexttool which is the same I assume. On windows this might not be a problem but on Linux this is.

    What IDE do you use to write your NXT applications?

    • Karol Krizka / Oct 25 2011

      I just used Emacs and typed in everything by hand.

  9. lego mindstorm nxt / Dec 19 2011

    I’m a lego brainiac lol

  10. lego mindstorm nxt / Dec 19 2011

    I don’t think so… when are you going to do it?

  11. lego mindstorm nxt / Dec 19 2011

    When do you think you’ll do that? Today?

Trackbacks and Pingbacks

  1. Announcing libmidi, A Very Simple MIDI File Library | Karol Krizka
  2. Lego for Kids and Parents | meinetechnik
Leave a comment