nxt

We’ve been doing some Lego workshops at Gold Coast TechSpace lately and with the older kids we’ve been spending a lot of time on Lego Mindstorms.

I quite like the Lego NXT software that comes with the kits for a simple introduction, it’s easy to teach and there are plenty of resources available to help.

However 2 things really irk me a bit about it:

  • It’s a bit limiting at times, I start the workshop talking about programming and what I and others do for a job - but when students ask me about how to do more advanced things I’m sometimes stumped on how to do it in the GUI - even sometimes quite simple things that would be ridiculously easy in REAL code.
  • It only runs on Windows (and Mac). I purged my machines of Windows a long time ago to take a free software path and it really pains me to have to dual boot perfectly good Linux machines to run the Lego software. I could run it in wine but that makes bluetooth connectivity a little difficult.

So I am embarking on a bit of discovery mission on how to work with Lego on a platform of my choice - GNU Linux, and for the sake of keeping things simple and accessible - Ubuntu 12.04 LTS (in my case Lubuntu - but it doesn’t matter for the purposes of this). I’ll blog my discoveries here as I go along.

There’s many alternative programming paradigms on Lego LXT, and as a Java programmer for many years you would think I might jump for something like LeJOS which runs a JVM on the NXT brick, but I did want to keep the NXT bricks with clean firmware so that they could be used with the stock software if necessary. I was a C programmer before Java and a lot of the kids seem to like Python so I feel myself searching for something different.

Your first NXC program

I’ll start by installing the NBC NXC compiler from Bricx Command Center. The project is a little old and hasn’t had an update in a while, but appears stable and well used. NBC is a simple byte code like language similar to assembly language, NXC stands for Not Exactly C and is a C like like language that we will use on our way to Python.

If you want to go to the exact version I downloaded - try here, just extract it to a directory somewhere and put the NXT/nbc executable in the path. You can also have a look at the source code. It’s a shame the code isn’t in a source repository - if someone reading this knows of the source of truth, it would be good to know.

Now that you have the compiler you can write little C programs that you can run on the NXT. Here is the classic Hello World example:

// HelloWorld.nxc
task main() {
    TextOut(0, 0, "HelloWorld!");
    Wait(2000);
}

You can then compile this up using

nbc -O=HelloWorld HelloWorld.nxc

Note: If you are curious to see what the intermediate NBC code looks like you can also use the -nbc flag to write it to a file.

Now you need to copy your program to the NXT brick, but to do this you’ll first need to do a couple more steps.

Connecting your NXT Brick

To test your NXT connectivity I use the rather excellent Next Tools. Even if you don’t want to program the NXT like this Next Tools is still a lot of fun, you can see all sorts of information about your NXT brick and even test all the sensors and motors in a more flexible way than the “Try” built in feature on NXT. I usually go straight to the music section to test I can connect to my brick.

Running this tool would normally be a no-brainer, just plug it into the USB port and use it, and it is if you run as root via sudo, but to do it properly you should add udev rules to allow your user to access the USB port properly.

If you want a script that will do all of this, feel free to check out this thread, but I like to keep it simple and do the bare minimum of changes, so this is what I did:

First run “lsusb” to see what the id of the NXT is, you should see a line of text something like this:

Bus 002 Device 006: ID 0694:0002 Lego Group Mindstorms NXT

Now create a file in the /etc/udev/rules.d directory with rules like this:

# /etc/udev/rules.d/45-legonxt.rules
# Lego NXT brick in normal mode
SUBSYSTEM=="usb", DRIVER=="usb", ATTRS{idVendor}=="0694", ATTRS{idProduct}=="0002", MODE="0660", OWNER="dalts"
# Lego NXT brick in firmware update mode (Atmel SAM-BA mode)
SUBSYSTEM=="usb", DRIVER=="usb", ATTRS{idVendor}=="03eb", ATTRS{idProduct}=="6124", MODE="0660", OWNER="dalts"

You don’t need the 2nd line - but it’s useful for firmware updates, replace “dalts” with your own user id. Some of the examples on the web create groups and add your user to the group but I like to keep things simple and just add my regular user.

Now restart udev

service udev restart

You should now be able to run Next Tools from your own user id!

Uploading your Program

Now you are ready to upload your program that you created at start. You have a few choices here. One is to use the file browser in Next Tools - there is an upload feature. Another is to use the nxt_push command that is found in the python-nxt package. To run it just install using

sudo apt-get install python-nxt

then upload your HelloWorld program

nxt_push HelloWorld

You can then run the program using the buttons on the NXT

Another final option is to use the the built in download feature of nbc, you can do something like this when you compile:

nbc -d -S=usb HelloWorld.nxc

This will automatically download a successful build to the NXT brick, there is also a -r option to download and run.

In the next section, we’ll learn how to write NXT programs in Python - see you then!