Tuesday 22 March 2011

Using Lego Mindstorms NXT with Ubuntu Linux

The Lego Mindstorms NXT brick is an incredibly versatile programmable brick consisting of a microcontroller inside a plastic casing with an LCD display, four buttons, a speaker and eight external connections. Unfortunately, though, it only ships with compilers for Lego's "NXT-G" graphical language that run on Microsoft Windows and Apple Macintosh platforms. There is, however, a multitude of text-based languages available for the main platforms (Linux/Unix included). The variety of languages available is beyond the scope of this article, but there is an excellent page here that should provide enough information to get started.

In this article I will be setting up the NBC/NXC compiler for use on Ubuntu Maverick (it should work on most current *nix releases, too - if not, please comment and we'll look into it).

The first thing to do is make sure the NXT brick is recognised by the system. To do this we run:

david@pinatubo:~$ lsusb|grep Lego
Bus 002 Device 002: ID 0694:0002 Lego Group Mindstorms NXT
david@pinatubo:~$


If you get similar output, you are ready to go onto the next stage. If not, then there is probably a misconfiguration or fault somewhere in the USB communications, or the brick isn't turned on.
To be able to compile and download NXC or NBC programs, all you need is the nbc executable. Download the latest release from here and untar. Make sure the binary is executable by cd'ing to where you untarred it and run chmod +x nbc. You can now run it by typing ./nbc at the terminal. This isn't very convenient unless you happen to be in the directory whenever you need it. I prefer to move the compiler to /usr/local/bin/ so it is on the system include path and the './' can be omitted.
david@pinatubo:~/nbc$ ls
nbc
david@pinatubo:~/nbc$ sudo mv nbc /usr/local/bin
[sudo] password for david:
david@pinatubo:~/nbc$ which nbc
/usr/local/bin/nbc
david@pinatubo:~/nbc$

You can now compile and download NXC and NBC programs by running:
david@pinatubo:~/nbc$ sudo nbc -d -EF test.nxc
[sudo] password for david:
david@pinatubo:~/nbc$

Flags used are -d to make it download the compiled binary and -EF to tell it to compile it for the enhanced firmware (see nbc download link). For a full overview of available options, run nbc -help.
You may have noticed that the compiler needed to be run as superuser in order to work. This is due to the way Linux handles device files and a udev rule will need to be made to let it run at normal permissions. udev is the utility in charge of dynamic devices (e.g. USB devices which must allow hotplugging) on Linux systems. When a new device is connected, it looks through a set of predefined rules to tell it what to do with the device and who is allowed to access it. The details of how to set up the necessary udev rule are a tad too long to include here, but there are excellent instructions here on the NBC website.

That should be enough to get the NXT brick talking to your Linux box. If there are any errors or omissions, or you have problems following these instructions or configuring your system, please comment below and we will try to help.
Not got one? Buy LEGO Mindstorms NXT 2.0 (8547)

10 comments:

  1. Wow...will try it very soon. My son has LEGO Mindstorm and he wants to try it on his laptop which has been installed with Ubuntu.
    I'll keep you inform how it goes ! Thanks in advance !

    ReplyDelete
  2. I want to program it in Python. :|

    ReplyDelete
  3. There are Python modules for controlling NXT from a computer, there might be some firmware that uses python syntax too.

    ReplyDelete
  4. I wanted to program in python

    ReplyDelete
  5. Excellent post.
    I have one question. I am working with the Dexter GPS sensor and that gives me errors. This one for an example

    Error: Undefined Identifier SensorDIGPSLatitude

    Any ideas?

    Thanks for your work

    ReplyDelete
  6. Thanks sciguy, appreciate the info!

    The mindstorm software crashes on startup om my wifes apple, so I've followed your instructions to get my nine year old daughter up and running on my Linux box using nbc (the software not working on apple has been a blessing in disguise).

    As a side note, I'm astounded at how easily my daughter is taking to using vim, copying and pasting code examples from the web, compiling and running on the device. I'm even getting full attention when loosely describing what the code is doing.

    All possible because of the wonderful guys who put nbc together and of course for pages like this that make it so simple for the rest of us!

    ReplyDelete
  7. If you are looking for a graphical user interface:
    nxceditor.sourceforge.net ...

    ReplyDelete
  8. in the meantime nbc is a standard program in ubuntu repository and can be installed simply with 'sudo apt install nbc'
    It seems it does not need to be run as root anymore

    ReplyDelete
    Replies
    1. Update: sudo nbc is needed to download the SW to the NXT!

      Delete