Using Lego Mindstorms on Ubuntu

When I was growing up I loved playing with Lego, mechanical construction sets, chemistry sets (with proper dangerous chemicals kids), wood, metal, basically anything where I could build something with my hands. I even remember going though a phase of building lamp shades with ice-cream sticks. I don’t know how many dog houses and go-karts my brothers and I built. Thankfully I grew up in the Irish countryside where we had lots of space. It also helped that my father was a builder so there was always lots of material and tools about the house he was happy to let us borrow (we didn’t like to bother him by asking him for any of it of course :))

I think it was all this building stuff that led me into a career in programming. Weather you’re using your mind or your hands it’s the process of making something out of nothing I really love.

I miss using my hands and so after hearing recently about Lego’s new EV3 platform I pulled out my old Mindstorms Robotics Invention System 2.0. This is pretty old set but it’s still perfectly useabable. It comes with two motors, two touch sensors, a light sensor, an RCX (the computer) and more bits of Lego than you can shake a stick at.

I run Ubuntu so the Windows based program that came in the kit would have to stay there (besides, the cover says it only supports Windows 98/ME so what are the chances it’ll work on Windows 7).

What I needed was to do was get the infrared tower used to transmit programs to the RCX working. I wasn’t all that hopeful but after plugging it in I was more than surprised to see a new device appear, /dev/usb/legousbtower0. Apparently the Lego USB tower kernel module has been part of base Linux for some time now. How cool is that.

Now I needed a programming language. A quick google and I came across Not Quite C. It has a C-like syntax but given the restrictions of the RCX it’s pretty straight forward. What’s even better is it’s available from the Ubuntu repositories.

$ sudo apt-get install nqc

The next step was the trickiest, getting nqc to recognize the device. The first step was to give my user access to the /dev/usb/legousbtower0 device. This can be done with the command sudo chmod 666 /dev/usb/legousbtower0. For a more permanent solution create the file /etc/udev/rules.d/90-legotower.rules with the following contents,

ATTRS{idVendor}=="0694",ATTRS{idProduct}=="0001",MODE="0666",GROUP="<group>"

You can use any group you’re a member of in place of <group>. You can find a list of the groups you’re a member of using ‘id -a‘. I used the group “adrian”. On a lot of systems there’ll be a group with the same name as your used id. This is fine so long as you’re the only one who’ll need access to the device. Otherwise you’ll need to find a common group or perhaps create a new one. By the way, the vendor and product ids in the udev rules file came from running lsusb.

The first time I tried to transfer a program to the RCX I got an error saying there was no firmware on the device. Apparently it only lasts as long as the batteries are powered. The nqc command can be used to install the firmware so all I needed to do was locate the firmware file. A few sites talk about the file /firm/firm0309.lgo on the system CD. My CD has no such file. I knew the firmware had to be on there somewhere though. Eventually I found it in the file data2.cab. This is an InstallShield archive. To extract the file install unshield and then expand the CAB file.

$ sudo apt-get install unshield
$ unshield -d /tmp/lego x <path to CD>/RIS2/data1.cab

The firmware file will be located at /tmp/lego/Script/Firmware/firm0328.lgo.

To install the firmware run,

$ nqc -Susb -firmware /tmp/lego/Script/Firmware/firm0328.lgo

Needless to say the USB tower has to be connected and pointing towards your powered on RCX. The transfer takes a few minutes to complete.

Finally, here’s how to transfer a simple program (source file prog1.nqc) to the RCX and have it run automatically,

$ nqc -Susb -d prog1.nqc -run

Now that everything is up and running I can get on with building something.

Links