O'Reilly Network    
 Published on O'Reilly Network (http://www.oreillynet.com/)
 See this if you're having trouble printing code examples

Lego MindStorms: RCX Programming

by Jonathan Knudsen

RCX: A lingua franca for robot builders

RCX Programming

• Lego is a big enough player to define a standard for hobby robotics.

• You can program the MindStorms RCX brain with Legos' visual development environment or with newly developed programming languages like NQC, legOS, and pbFORTH.

• It's a challenge working with software and hardware together.

Hobbyists have been building robots for decades. But the Robotics Invention System offers a standard.

If someone builds a robot with the pieces from the Robotics Invention System, anyone else with the same set can build it too. Robots with nonstandard parts are much harder to reproduce.

Also, programs you write for the RCX can run on anyone else's RCX. Homebrew robots tend to have software that's hard to reuse in other robots.

The time was ripe for a product like the Robotics Invention System. Lego bricks are a good, standard, mechanical prototyping toolkit. And some standard robot brains have been made, such as MIT's HandyBoard or the BASIC Stamp from Parallax. The beauty of the Robotics Invention System is that it combines a standard mechanical toolkit with a standard software platform. If you build something cool, someone else can easily build it too. This makes it easy to exchange ideas and programs with other Lego robot builders.

The forgotten challenge of mechanical design

One of the joys of creating Lego robots is the challenge of mechanical design. Mechanical design is something kids generally understand better than adults; after all, they get to play with wooden blocks, Lego bricks, and other physical toys. Many adults spend their workdays in front of a computer and have forgotten the rewards of mechanical design. Building robots with the Robotics Invention System is a great way to reacquaint yourself with the physical world.

Lego bricks are a great prototyping tool because they are so versatile. It's easy to take something apart and rebuild it as many times as you like.

Here's one of my robots, Minerva 1.5. She can drive around and pick things up with her hand.

Minerva 1.5
Figure 5. Minerva 1.5, a robot with an arm and gripper.

Other people have built tank-style robots, walking robots (with two, four, six, or even eight legs), a model of a VCR, models of natural disasters, robots that slither, robots that play games, robots that play tic-tac-toe, copying machines, and almost anything else you can imagine.

Developing with RCX

In This Column

What is MindStorms?
• RCX Programming
Lego and MIT
Lego Glasnost

How do programs get from a computer to the RCX? The answer is an infrared light link. The Robotics Invention System includes an infrared tower that attaches to a serial port on your computer. The RCX itself has an infrared port. When you're ready to download a program to the RCX, you just point the infrared tower at the RCX, just like pointing a remote control at a television.

Special software on the RCX, called the firmware, acts as a kind of RCX operating system. It can receive programs from the infrared port and respond to the front panel buttons to start and stop programs.

The development software that comes with the Robotics Invention System runs on Windows platforms. Programs are created by snapping together different functional blocks of RCX code, just as you create a robot by snapping together Lego bricks. (See Figure 6.)

The RCX code graphic programming environment.
Figure 6. The RCX code graphic programming environment.

Palettes of program blocks are on the left. Here, I've created a simple program (the green blocks) that turns outputs A and C on for 4 seconds, then turns them off and plays a sound.

Development Alternatives

Development Alternatives include NQC, legOS, and pbFORTH. Although RCX code is good for people who haven't programmed before, it has some serious limitations. The biggest problem is a lack of variables. In response to this, dedicated and innovative fans have created a dozen alternate programming environments for the RCX. The most important of these are NQC, legOS, and pbFORTH.

NQC is based on text source code files with a C-like syntax. It's the most popular and widespread alternate development tool for the RCX, but it is still subject to the limitations imposed by the default RCX firmware. To give you an idea what NQC source code looks like, here's a program that does the same thing as the RCX code program shown above:

task main() {
  SetPower(OUT_A + OUT_C, OUT_FULL);
  Fwd(OUT_A + OUT_C);
  On(OUT_A + OUT_C);
  Off(OUT_A + OUT_C);
With legOS, you can write robot programs in real C using GNU compiler tools. And pbFORTH is a Forth language interpreter that runs on the RCX. Both legOS and pbFORTH both offer more power than NQC, but involve replacing the firmware on the RCX, which make them harder to use.

Copyright © 2009 O'Reilly Media, Inc.