Lego Mindstorm

[ Quick Start | Beginning | Freeware | NQC | BrickOS | Perl | Links | References | HRP Home]

Quick Start

There are many options from which a development environment can be chosen for the Lego Mindstorm. If your host is a Windows PC then the best place to start is BricxCC. If you are happy with Not Quite C (NQC) which uses the Lego firmware and is one of the easiest ways to program Lego Mindstorm then the only thing you need is BricxCC (bricxcc_setup_3377.exe). To use BrickOS which comes with its own firmware you will need to install Hitachi cross-compilers (and cygwin utilities if you don't already have them) and a BrickOS install.

All that is required is clicking three executables and watching them install. Nothing could be simpler.

If you already have cygwin installed on your system then you need to choose custom setup and install only Hitachi cross-compilers from BricxCCSetupCygwin.exe.
A link to local copies of files to install BricxCC, cross-compliers, BrickOS, and lejOS is provided above. For other information please click through to the BricxCC homepage.

BirckOS supports C and C++ compliers. This means that if you wish to write a C program you can include all the .h files like math.h and use the functions therein in your C program for Rcx. Perhaps you can copy the header files you most need from the gcc include to the BrickOS include area. The mighty gcc compiler is used to produce machine code for the h8300-hitachi processor so I presume one can use all what one uses with the standard C.

Do take tiime and read a very good HOWTO.

First successful cycle of tasks to set control experiments using BrickOS-Notes on 28 April 2005

After a few attempts I have finally succeeded in getting a motor to rotate by plus or minus 90 degrees, read back the position history into a PC and plot it. This is the simplest of the simple experiments but gives a good understanding of the entire cycle of operation needed for setting control experiments.

The position control system consists of motor_a, rotation_sensor_2, touch_sensor_1, and touch_sensor_2 (the last two sensors are only to repeat commands after the motor has once rotated by 90 degrees). From a control point of view the transfer function between the input voltage and position already has an integrator so only a position control, directly proportional to the position error, has been implemented with satisfactory result.

The position data is stored in an array called posarray. The data is collected at 0.1 s interval for 3 s. After three seconds of motor operation the data is sent to the PC via lnp_integrity_write. To receive the data at the PC ir.exe is run (in a separte PC console window) with:
ir --tty=com1 -p posdata.txt
The above command puts the data in posdata.txt and this is processed using data2sce.pl which creates a Scilab file ready for plotting the data. All these files are in motorpos.zip and the ir.exe is in ir27April2005.zip.

For getting this simple cycle working, a big thanks to John Hansen (for help with installing BricxCC), Pavel Petrovic (for the help with ir.exe) and Prof Peter Gawthrop for their excllent IEEE CS Oct 2004 paper and the bundle of software made available at their site.

Lego Network Protocol for Windows

Notes in reverse chronological order.
(Notes on Thur 28 April 2005)
Pavel sent a modified ir.c and ir.exe and using that the following command puts all the data coming from RCX in file log.txt till a final "\bye" comes from the RCX:
ir3 --tty=com1 -p log.txt
We are almost there now. The testfile is testlnp.c. To use this feature one has to use the version of ir3 which Pavel modified at my request.

(Notes on 27 April 2005)
FAQ - With Java 1.5: "javac: target release 1.1 conflicts with default source release 1.5" or similar.
This is caused by using JDK 1.5; new versions of Lejos will fix this. As a workaround you can use the option '-source 1.2' with the lejosc command:
lejosc -source 1.2 View.java
Linux users should also add this to JAVAC in the main Makefile if you are recompiling the library.

Something Working
cd C:\cygwin\lejos\rcxcomm\examples\outstream
lejosc -source 1.2 Write
lejos Write
javac Read
java Read
(Press the Run button on the brick and then Read will read the value sent by Write and display on the console.)

WriteInt (on RCX) and ReadInt (on PC) are also working. The files are in C:\cygwin\lejos\rcxcomm\examples\read_write_int.

(Notes on Tue 26 April 2006)
Taking a hint from talker.c in ir/examples (in ir3 distribution) I created a file testlnp.c. While testlnp.lx is running on RCX, I can use the program ir3.exe
ir3 --tty=com1 -p
to print on the console the data sent from the RCX, but
ir3 --tty=com1 -r 1000 filename
doesn't create the file filename correctly. It seems to have only the last string transmitted from the RCX. And many strings sent from the RCX don't reach the PC, they get lost. I have to find a way so that the communication is cent-percent reliable.

It seems lejos_2.1.0 doesn't work with jdk_1.5. (It does with -source 1.2 as suggested in the FAQ and the note above.)

(Notes on Mon 25 April 2005)
For small tasks a program can be loaded on the RCX and that's sufficient. But when developing complex program, to debug or to analyse the performance of algorithms or to identify the system, it's imperative that we have a mechanism to transfer data between the brick and PC. For this BrickOS has a network protocol known as Lego Network Protocol (LNP). The LNP is always running on the brick but it has to be initiated on the PC. As of today (25 April 2005) I have sourced two programs ir3.exe and legoshrink.exe which can receive (and send) data to the brick. To receive data on the PC two things should happen: (a) a program like ir3.exe or legoshrink.exe should run on the PC and (b) the program running on the brick should transmit data using lnp functions. As I understand these two programs only receive strings so if integers have to be sent some extra work needs to be done to convert between the two types.

Actually one should be able to write programs like ir3.c especially suited to ones requirements. I am working on it right now (25 April 2005).

Can Java Help?

For Linux there is LNPD which permits an easy way to develop c programs for communication between the RCX and PC but in spite of references to WinLNPD there doesn't exist any link to it. I will try and compile the code with cygwin but with my limited knowledge this is difficult but I can see myself wasting a lot of time on that (25 April 2005). In the meantime it seems to me that it might be much easier to use java on the PC since java is platform independent and there are java programs for communicating. There is a good tutotrial on leJOS and a fully working mobile robot with vision which uses a combination of matlab and java. I think the mobile robot has used exactly the idea of using BrickOS on the brick and matlab plus java to communicate with the brick. But this has only functions to send data from the PC to the brick and not the other way round.

Another helpful tutorial Using leJOS.

Hopefully Read.java in C:\downloads\lejos\rcxcomm\examples\outstream should do the trick. It needs to be complied, etc., so I should get a java compiler.

BricxCC

A good solution seems to be to get:

  1. BricxCC latest version
  2. setup executable for cygwin
  3. minimal brickOS and leJOS for BricxCC for BrickOS

Hopefully after installing BricxCC you will not need the following information but may find its use in understanding the whole business of development environment for Lego Mindstorm.

Beginning at the Beginning

Lego Mindstrom is a commercially available product from Lego. It started with RCX 1.0 and RIS 1.0. It's now RCX 2.0 and RIS 2.0. If you have RIS 1.0 then you have to use only Win 95 or 98, the development environment provided by Lego doesn't work on NT or 2000 or ME or XP. But people have hacked through the system and freeware is available to develop programs for RCX.

There is another system Lego Technic which has similar development environments but a different brick.

But first let's look at the spirit.OCX provided by Lego.

  1. A visual basic development environment is needed. Lego's spirit.OCX provides objects to this environment. The Robotics Invention System LEGO Programming Bricks Technical Reference and a few visual basic scripts to get started are helpful in getting some idea of the Spirit.ocx.
  2. These objects are routines to communicate, control, and sense RX. So one can write a visual basic program where the program runs on the host computer and as and when a subroutine or a function for RCX comes, it is executed. All the variables and other visual basic functions can be used to manipulate data which comes from the RCX. This is the easiest way to program but they are not stand alone functions. Simple visual basic programs can be made up of subroutine or function calls such as:
    PBrickCtrl.SetFwd MOTOR_A + MOTOR_C
    PBrickCtrl.On MOTOR_A + MOTOR_C
    This way of control using the host computer is called immediate control and the commands which achieve this are called immediate commands. Some commands are only immediate commands, some others can also be downloaded in addition to being used as immediate commands and some can only be downloaded.
  3. To develop stand alone functions only the downloadable commands are used with the variable set (0-31) and other registers like eight constants (0-7) which can fit on RCX can be used. An example:
    With PBrickCtrl .SelectPrgm SLOT_3
    .BeginOfTask MAIN
    .SetPower MOTOR_A + MOTOR_C, CON, 7
    .SetFwd MOTOR_A + MOTOR_C
    .On MOTOR_A + MOTOR_C
    .Wait CON, SEC_2
    .Off MOTOR_A + MOTOR_C
    .EndOfTask
    End With
    Two generous souls, David Hanley and Sean Hearne, have put a good book, Lego Mindstorms Programming with Visual Basic, on Internet which can be downloaded from here.

Freeware

Like me if you have an RIS 1.5 and XP then you are stuck. But fortunate for us, people have written development environments which don't depend on the RIS system supplied by Lego. I suspect the initial motivation for these alternative systems was to use Lego Mindstorm with Linux or Mac for which Lego didn't provide any support.

NQC

Not Quite C is a cinch to install and can do powerful stuff. To install the windows version all it needs is to unzip a file. A simple FAQ helps.
NQC Homepage RcxCC Homepage LUGNET NQC discussion page. The best option is to install BricxCC which has support for the NQC and also an integrated development environment. You get BrickOS and leJOS as a bonus with BricxCC.

An easier way is to install BricxCC directly from the executables and the standard option has all the tools (including help files) for NQC.

LegOS/BrickOS

The best thing I can suggest is install BricxCC and the other two utilities (cygwin utilities including gcc and BrickOS) directly using the executables on the BricxCC site. For further discussions see LUGNET NQC discussion page

Perl

If you can do anything in perl better do it in perl. There is a perl package rcx.pm (last updated in 2000) for Lego Mindstorm and it seems to me that it was written with Linux as the host operating system. I tried it with XP but it didn't work. Hopefully someone will come up with a Windows version soon.

Links

Australian Supplier of Lego Products in Newcastle
RoboticsIntroduction - A very useful site with links to many functional and complex robots, sensors, and software.
BrickOS source page
Mario Ferrari's Home page

Using a PID-based Technique For Competitive Odometry and Dead-Reckoning
The Rossum Project
Lego Mindstorm
Constructopedia
NQC Home Page
LEGO Lab
LEGO on My Mind
Lego Mindstorm Internals
Lego Robotics Course
MindStorms in Education
LegOS on lugnet
Robolab - the educational version of Mindstorm software.
LDraw - LDraw Family of LEGO CAD Software

http://www.mech.gla.ac.uk/~peterg/Lego/
http://users.ece.gatech.edu/~bonnie/Legos/
http://graphics.stanford.edu/%7Ekekoa/rcx/
http://today.java.net/pub/a/today/2005/02/21/robotics.html
http://lejos.sourceforge.net/tutorial/
http://tarpit.rmc.ca/lesauvage/eee243/eee243.html
http://hoenicke.ath.cx/rcx/

A good reference on Lego Technic and Interactive C is [1].
References on Lego Mindstorm are: [2], [3].
Inverted Pendulum experiment using Lego Mindstorm: [4].

References

[1]
Fred G. Martin. Robotic Explorations: A Hands-on Introduction to Engineering. Prentice-Hall, Inc., Sydney, 2001.
[2]
D. Baum. Definitive Guide to Lego Mindstorms. Apress, Berkley, CA, 2000.
[3]
D. Baum, M. Gasperi, R. Hempel, and L. Villa. Extreme Mindstorms: An Advanced Guide to Lego Mindstorms. Apress, Berkley, CA, 2000.
[4]
Frank Klassner and Scott D. Anderson. LEGO mindstorms: not just for K-12 anymore. IEEE Robotics and Automation Magazine, pages 12-18, June 2003.
[5]
Peter J. Gawthrop and Evan McGookin. A LEGO-based control experiment. IEEE Control Systems Magazine, 24(5):43-56, October 2004.
[6]
Bonnie S. Heck, N. Scott Clements, and Aldo A. Ferri. A LEGO experiment for embedded control system design. IEEE Control Systems Magazine, 24(5):61-64, October 2004.

Himanshu Pota <pota@adfa.edu.au>
Last modified: Monday February 13, 2006 2:49 PM