This is an old revision of the document!
Table of Contents
First steps with Lego Mindstorms
Intro
My brother has lent me a Lego Mindstorms version 1.5. It is quite old, by the specifications I think that the product may be dated of 1998. My objective is to make it work under linux in a modern computer without a serial cable, to let my son work with it.
I suppose that if you come to this page you are somewhat familiar with Lego Mindstorms, however, I will describe some of the main elements of the kit:
- The RCX is the “brain” of the robot. Is a yellow box that can be programmed. The good part is that you can replace the firmware easily, in fact there are several solutions out there that replace the firmware.
- A device that you can connect to the computer via an RS232. I call in this document “the black box” because of this color.
- Motors, sensors, etc. The usual robot stuff.
Options to have it work under Linux
I will try the following options in the same order depicted here:
- Use of Not Quite C (nqc): it's a programming language similar to C, that can be used to generate code compatible with the RCX (the brain that goes into the robot)
- Use of Lejos: it's a Java Virtual Machine for the RCX. This replaces completely the firmware in the RCX as long as the batteries are connected to the unit (RCX). For me it's a good solution because I am quite familiar with Java, so it's a natural way to go. Unfortunately, this is a major obstacle for my son and presumably I will have to do all the programming stuff…
Of course, there are other options, but I won't discuss here.
RS232 Communication in the XXI Century
Modern computers doesn't have an RS232 interface anymore. The replacement for this are the USB ports, that any computer have lots of them. You can simulate an RS232 with an adapter. For my purposes I've bought an adapter from Star Tech called ICUSB232. Check it out:
Checking that it works under Linux
One step is to check that it works properly under linux.
Before Everything
Don't plug the USB-RS232 adaptor yet!!! Before this, run a tail -f /var/log/messages
or tail -f /var/log/syslog
(depending of your version of Linux). To be sure, just run both at a time:
$ sudo tail -f /var/log/messages /var/log/syslog
Now, plug your cable. You should view something like this:
Dec 30 12:29:18 petrus kernel: [17198.868053] usb 4-1: new full speed USB device number 3 using uhci_hcd Dec 30 12:29:18 petrus mtp-probe: checking bus 4, device 3: "/sys/devices/pci0000:00/0000:00:1a.1/usb4/4-1" Dec 30 12:29:18 petrus kernel: [17199.032177] pl2303 4-1:1.0: pl2303 converter detected Dec 30 12:29:18 petrus kernel: [17199.044237] usb 4-1: pl2303 converter now attached to ttyUSB0 Dec 30 12:29:19 petrus mtp-probe: bus: 4, device: 3 was not an MTP device
This pl2303 converter now attached to ttyUSB0
means that your USB device is /dev/ttyUSB0
.
First Attempt: Not Quite C
This is my first attempt and my first try to make the USB-RS232 adaptor to work. I know that Java doesn't have a good USB or serial support, so the second try will be a major problem. My hopes are that NQC, as probably is made natively in C or similar, will have a native support for USB/RS-232 more robust.
You can find documentation about NQC here: http://bricxcc.sourceforge.net/nqc/.
Hands on
You can install nqc in Ubuntu by typing:
> sudo apt-get install nqc
You can make a hello world to make sure that the USB-RS232 is working properly.
First, connect the unit to the PC and make sure it's on. The “on” in this unit means to move the switch to the biggest triangle:
Next, turn on the RCX and point it to the black unit, so both can see each other. Remember that the connection is via infrared.
Next, run the following code:
nqc -S/dev/ttyUSB0 -msg 123456
When running it, I've observed no error messages and no Could not open serial port or USB device
message. This is important.
Other commands to test proper running
I've run this command:
$ nqc -S/dev/ttyUSB0 -pgm 1 No firmware installed on RCX
So I'm afraid further investigation is needed.
Last try
I've attached a motor to the “C” square of the RCX:
And this is my first program:
task main() { OnFwd( OUT_C ); Wait( 400 ); OnRev( OUT_C ); Wait( 400 ); Off( OUT_C ); }
When I try to run it, I get the following error:
$ nqc -S/dev/ttyUSB0 -run Hello.nqc No firmware installed on RCX
So, I definitely think that the RCX is completely blank, and I have to install some sort of firmware to have it properly working.
Where is the firmware???
Check out the box of mindstorm: it should be a CD which contains the firmware. To get the firmware from the CD, you should face the following scenarios:
- The firmware file is in the cd and you can extract directly without hassle. It should be a file like
firm0309.lgo
or something like this. - The firmware file is in the CD, but apparently when trying to extract using linux, it fails. This is because the file format of the CD is very old. Try to extract the file using a windows machine or a virtual windows machine. If it is your case, I recommend to make a backup copy of the contents of the CD in another Cd. But copy the contents, not make a copy of the whole cd, because the older filesystem will be kept too.
- The firmware file is not in the cd, but it is in one of the *cab files. In that case, you can find instruction in this page:http://www.17od.com/2013/01/13/using-lego-mindstorms-on-ubuntu/. In a nutshell,
sudo apt-get install unshield
and thenunshield x PATH-TO-YOUR-CAB-FILE
.
Downloading the firmware
In the argot, placing the firmware file in the yellow box (the RCX) is called “download the firmware”. I don't know why, because for me is more proper the opposite term (upload), but the terms were set when I came to this world.
Run the following command:
$ nqc -S/dev/ttyUSB0 -firmware firm0309.lgo Downloading firmware:.....................
You should see a message in the RCX about the progress of the download:
and a green diode lighting in the black box (it's not easy to see it):
After the process is finished, the LCD screen of the RCX should show a 0000 message, and a sound is reproduced:
The installation of the firmware will be there until the batteries are removed.
For example, the following command:
nqc -S/dev/ttyUSB0 -pgm 2
Will have the effect that in the RCX the program number 2 is selected:
Executing the program
To run and download the program into the RCX, you should do the following:
$ nqc -S/dev/ttyUSB0 -d -run Hello.nqc Downloading Program:..complete Battery Level = 8.7 V
Now, click on the “run” button on the RCX: the program will start, moving the motor in one direction and then in the opposite direction.
Second Attempt: LEJOS
Lejos is a Java Virtual Machine for the RCX (the yellow box). It's a replacement for the Lego firmware, so, if your CD of mindstorms is broken, it's the perfect solution.
The web page has a fantastic tutorial that shows the steps to be taken to have this stuff running.