This page is about remote-controlling the old Lego RCX system. For the new NXT system, please click here


Introduction

The Lego Mindstorms robotics kit provides a cheap and easy way to build simple robot applications. However, as robot control programs are usually uploaded to the Lego RCX computer to run standalone, it suffers from some major disadvantages: To overcome these problems, I have programmed a small direct interface that remote-controls the RCX from within a Windows C-program on the host computer. Data exchange with the RCX is done by the interface, which runs in an extra task in the background and provides simple data exchange using C-variables. Data is transmitted to and from the robot by setting and reading out these variables.
This interface directly accesses the Lego tower via the serial port or the USB port (which simulates a serial port) directly via Win32 functions, which means that neither spirit.ocx nor MFC are needed. On the RCX, only the standard Lego firmware is needed. It supports direct read- and write-commands via the IR-tower, and these functions are used by the interface.

The second important part for advanced robot control is gaining access to the Lego camera (or any other video camera) from within the robot-controlling VC++ program. For this, the Video-for-Windows standard interface is used (see below).

This tutorial contains the following example programs:


The rcx21.cpp V2.1 Interface (for serial port and USB)

Here is the newest version of the RCX interface program. It supports both the serial port version and the USB version of the Lego tower. Since version 2.0, the robustness of the interface to message loss has been improved, and it now supports to set sensor types and modes (which is needed for light and rotation sensors). Also, RCX_error now reports communication failures.

This is a direct interfacing library, which means that sensor/motor data is transferred between the RCX and the host computer in realtime. Downloading programs onto the RCX to make it work stand-alone is not supported. This library focuses on direct robotic control from C++ programs and uses the RCX only as an interface between the sensors/motors and the host computer.

Features:


Short description

Starting and stopping the interface

To start the RCX interface, call the routine RCX_start() with the port type and port name as parameters, for instance RCX_start(1, "COM1"); for the serial tower, or RCX_start(2, "\\\\.\\LEGOTOWER1"); for the USB tower.

To stop the RCX interface, use the following commands:
RCX_stop=1;
while(RCX_stop);

This signals the RCX interfacing task to quit and waits until it has finished.

Sensor control

Possible values for RCX_sensor_on variables:
0 (polling off) and 1 (polling on).

RCX_sensor_on[0] is alive signal,
RCX_sensor_on[1] is value of sensor 1,
RCX_sensor_on[2] is value of sensor 2,
RCX_sensor_on[3] is value of sensor 3,
RCX_sensor_on[4] is battery power (in mV).

Please note that polling sensors slows the transmissions down, so don't switch on sensors if you don't need them.

RCX_sensor_val[] contain the current sensor values, if polling for the respective sensor is switched on. The value range depends on the current sensor mode (see below), but is coded as an integer.

RCX_sensor_type[5]={0,0,0,0,0};
Types for sensors. Only the values for sensors 1,2,3 are used. Sensor types are: 0: raw, 1: touch, 2: temperature, 3: light, 4: rotation.

RCX_sensor_mode[5]={0,0,0,0,0};
Modes for sensors. Only the values for sensors 1,2,3 are used. Valid sensor modes are:
0: Raw - value in 0..1023
1: Boolean - Either 0 or 1
2: Edge count - Number of boolean transitions
3: Pulse count - Number of boolean transitions divided by two
4: Percentage - Raw value scaled to 0..100
5: Temperature in C - 1/10ths of a degree, -19.8 .. 69.5 (coded as an int)
6: Temperature in F - 1/10ths of a degree, -3.6 .. 157.1 (coded as an int)
7: Angle - 1/16ths of a rotation, represented as a signed short

Not all of the sensor modes are tested, and some of the values might need additional conversion (for example temperature).

Motor control

Possible values for RCX_motor_on variables:
0 (off) and 1 (on).

RCX_motor_on[0] is motor A,
RCX_motor_on[1] is motor B,
RCX_motor_on[2] is motor C.

A motor which is switched off will block. If you want a motor to be able to turn passively without much resistance, switch it on and set its RCX_motor_val to 0 (see below).

Possible values for RCX_motor_val variables:
-8 (maximal power backwards), ... -1, 0 (no power, free float), 1, ... , 8 (maximal power forwards).

Sound control

Possible values for RCX_sound:
The variable is 0 in the beginning. Setting the variable to a value 1,2,3,4,5 or 6 triggers the according sound effect (blip, beep-beep, downward tones, upward tones, low buzz and fast upward tones). After the sound command has been transferred successfully, the variable is set back to 0 automatically.

For a demonstration how to use the rcx21.cpp interface, please refer to the "roam" example below.


Click to download the RCX V2.1 interface source code (also included in roam.zip and vehicle.zip):

rcx21.cpp
rcx21.h

Click to download older versions of the interface source code (for backwards compatibility only):

rcx.cpp
rcx.h
rcx2.cpp
rcx2.h


Roam


This is a small Visual C++ console application that demonstrates how to use the RCX interface to interact with the robot. The behavior implemented in this example program is to go forward until the touch sensor connected to sensor input 1 signals that an obstacle is hit, then turn and continue forward.

Click here to download roam.zip (9 kb)


Videostream


This application shows how to set up the video for windows interface to enable video input to your VC++ program. It also shows the incoming video in the window and provides a dialog for selecting a video source. Uses standard video-for-windows library vfw32.lib.

Click here to download videostream.zip (17 kb)


Vehicle


This more advanced application demonstrates how one can combine video input and the rcx21.cpp interface to create a Braitenberg-vehicle-like "bull robot" that is attracted by red things. The incoming video images are scanned for red objects, and the robot motors are controlled according to the position and size of the red objects in the robot's view.

Click here to download vehicle.zip (17 kb)


Links

Note: these links point to pages outside of the Max Planck Institute's webspace. We take no responsibility for their content.

This software is provided as-is. Please remember that you use this software on your own risk.
I am currently not working on Lego robotics any more, but you can contact me for questions and comments at: dberger@mit.edu
Thank you!
Last updated: September 7th, 2012. (Version of September 15th, 2005)