RCX Programming


Lesson 1

In this lesson we show how to compile and download simple c programs to LEGO MindStorms RCX. Furthermore, we describe how to obtain input from LEGO sensors connected to the input ports 1, 2 and 3. Finally, we will demonstrate how to calibrate a light sensor to distinguish different colours.

Cross-compilation and download

The first c program that we are going to execute on the RCX is the following useful test template (TestTemplate.c):
/* Test template for test programs. Make sure to include files that are used in the test program. Last updated 3.1.01. */ #include "RCX_String.h" #include "RCX_Control.h" #include "H8.h" #include "Time.h" #include "LCD.h" #include "Battery.h" #include "Buttons.h" void _start(void) { ButtonsInit(); while ( ! Running ){ lcd_show_int16(BatteryLevel()); BusyPauseMS(300); } while ( Running); RCX_Reset(); }
First, the program should be compiled and downloaded onto the RCX: Follow the description in The RCX UNIX/Linux c programming Environment.

Second, when the download is completed the TestTemplate program starts execution on the RCX. The BatteryLevel is shown in the LCD until the Run button is pressed. When pressed the LEGO man change from standing to walking and the program waits until Run is pressed again. When pressed the RCX resets. The OnOff button can also be used during the program execution. The first time OnOff is pressed the RCX goes to sleep (low power consumption mode). The second time the button is pressed the RCX resets and goes to sleep. If the Run button is pressed after the first OnOff then the program restarts from _start.

Input sensor values

To input sensor values from the input ports include InputPorts.h and use the routines Port1Raw, Port2Raw and Port3Raw, e.g. to show raw values from passive sensors (touch sensors, temperature sensors, ...) connected to the three ports as in the following loop:
while ( Running) { if ( View ) { lcd_show_int16(Port1Raw()); lcd_show_digit(1); } else if ( Prgm ) { lcd_show_int16(Port2Raw()); lcd_show_digit(2); } else { lcd_show_int16(Port3Raw()); lcd_show_digit(3); } BusyPauseMS(100); }
The buttons View and Prgm are used to switch between the input ports that deliver the raw sensor values to be shown in the LCD. Use the different passive sensors to fill in a table like the following:

Sensors Raw values
Touch sensor, pressed --
Touch sensor, released --
Temperature sensor --

You may also try to connect two or three passive sensors to the same input port and read ( and perhaps explain ) the raw values obtained.

To show values from the active sensors (light- and rotation sensor) use the routines Port1SetActive, Port2SetActive and Port3SetActive to supply power to the LED's of the active sensors. Fill out a similar table for the active sensors. Try to connect a touch sensor and a light sensor to the same input port. Can both stimuli, touch and light, be detected even if the sensors are connected to the same input port ?

There are two light sensors in the LEGO Lab: The active LEGO sensor and a home build passive light sensitive resistor. A detailed investigation of the two sensors are available in Properties of two light sensors.

Calibration of a light sensor to interprete colours

When a light sensor is used to input sensor values, the 10 bit integer sensor value in the range 0 to 1023 can be interpreted as different colours. This can be accomplished through initial calibration and determination of thresholds to be used in the interpretation of the input values.

A module ( LightSense.h ) shows how this can be implemented. In this module three different colours can be distinguished through a light sensor connected to Port 1. The thresholds for the three different colours are determined by means of the function LightSenseCalibrate( void ). These thresholds are then used in the function LightSenseColour( void ) to interprete the sensor values in terms of the three different colours. Try this module in the testprogram ( TestLightSense.c ). The Prgm button is used to start detection of each colour, the View is used to signal end of calibration.

Try to use the module to detect the floor colour underneath a car with a light sensor pointing to the floor. Building instructions for a car with a light sensor pointing to the floor can be found on Jonathan B. Knudsen, The Unofficial Guide to LEGOŽ MINDSTORMS[tm] Robots.


Access to the other include and source files.
Last update: 26-01-06