Lego Mindstorms

Back to Physical and Embedded Computer


Inside LEGO mindstorms RCX

RCX

 

RCX with devices

 

Inside the RCX packaging, there are …

(image source http://graphics.stanford.edu/~kekoa/rcx/)

 

Functional components

(image source http://orionrobots.co.uk/knowledgecenter/rcximages/)

CPU - Hitachi H8/3292 (3)

(image source http://orionrobots.co.uk/knowledgecenter/rcximages/)

Some information

  • 8 bits data
  • 512 bytes RAM
  • 32K external RAM
  • 16K ROM (driver)
  • 16K firmware (e.g. leJOS)
  • 6K region memory (bytecode programs)
  • 10 MHz clock speed

 

Input devices

  • 4 buttons - ON/OFF, RUN, VIEW, PRGM
  • 3 input ports (sensor) - 1, 2, 3
  • Battery level
  • IR receiver
  • Timers

 

Output devices

  • 3 output ports (motor) - A, B, C
  • LCD display
  • Speaker
  • IR transmitter

 

Sensors and Motor come with Lego Mindstorms

Touch sensor

 

Touch sensor with wire

 

ON/OFF switch by touching the yellow part

 

Input port specification (RAW reading)

  • 0 volt - 0
  • 5 volt - 1023

 

The ON/OFF (boolean mode) reading of a touch sensor will be

  • RAW < 450 gives 1
  • RAW > 565 gives 0

 

 

Light sensor

 

Input port specification (RAW reading)

  • 0 volt - 0
  • 5 volt - 1023

 

Light sensor reading

  • 146 - RAW / 7

 

 

Motor

 

Motor with wire

 

Download Firmware

Place the RCX in front of your IR tower .

 

It has to be done once only.

 

Check the leJOS preference settings.

 

Download the firmware.

 

Wait for about 10 mins.
Using Lejos with Eclipse

Create a new leJOS project

 

Add external JAR (Java archive file) libraries for the project

 

Choose the classes.jar from the C:\lejos\lib folder

 

Create a new package and a new class

 

Select Project -> Rebuild All to compile your program

 

Place your RCX in front of your IR tower

 

Choose leJOS -> Bytecode Download

 

Press the RUN button on your RCX to test the program

 

Download the first bytecode sample
Your First Lejos Program

Create a LeJOS project in Eclipse IDE

 

Create a package and give it a name 'class01'

 

Create a class called 'Try02' with the following codes

 

package class01;
import josx.platform.rcx.*;         
public class Try02 { 
  public static void main(String[] args) 
   throws InterruptedException {
    TextLCD.print("HELLO");
    Button.RUN.waitForPressAndRelease();
  } 
}
 

Follow the steps in previous section to build, download and run the program in the RCX.

 

Check the display in the LCD screen. Press the RUN button when you are done.

 

Common objects for LeJOS

  • TextLCD, LCD
  • Button
  • Sensor
  • Motor

 

Help for the API

Additional Java Concept you need to know

Exception and Throws

An exception is an 'error' that occurs in run time. We are going to study the

InterruptedException

 

Throws is a command to create such exception. We can specify the main function may create this exception beforehand like,

public static void main(String[] args) throws InterruptedException {

 

In our case, pressing any buttons will create such exception. We need to have some codes to cater for it.

 

Sample code in our previous example,

public class Try02 { 
  public static void main(String[] args) 
   throws InterruptedException {
    Motor.A.forward();
    TextLCD.print("HELLO");
    Button.RUN.waitForPressAndRelease();
  } 
}

The program will stop after the TextLCD.print command and wait there until some one presses the RUN button again to stop the program.


Listener

We'll see examples using traditional method POLLING and LISTENER.

 

Requirements

Display the text 'HAPPY' when the VIEW button is pressed; otherwise display 'SAD'.

 

The traditional approach is POLLING, where your program runs in an infinite loop to check constantly the status of the VIEW button. If it is pressed, display HAPPY, otherwise, display SAD.

package class01;
import josx.platform.rcx.*;
public class SensorTest01 {
   public static void main(String[] args) {
      int senVal = 0;
      Sensor.S1.setTypeAndMode(SensorConstants.SENSOR_TYPE_TOUCH,
       SensorConstants.SENSOR_MODE_BOOL);
      Sensor.S1.activate();
   
      while (true) {
         senVal = Sensor.S1.readValue();
         if (senVal==1) {
            TextLCD.print("HAPPY");
         } else {
            TextLCD.print("SAD");
         }
      }
   }
}
 
            

In the Listener way, you do not have to care about to check the button status. You can do your useful stuffs inside the while loop.

 

You need to create a SensorListener object and modify the stateChanged function to fit your purpose.

package class01;
import josx.platform.rcx.*;
public class SensorTest02 {
   public static void main(String[] args) {
      Sensor.S1.setTypeAndMode(SensorConstants.SENSOR_TYPE_TOUCH,
       SensorConstants.SENSOR_MODE_BOOL);
      Sensor.S1.activate();
   
      Sensor.S1.addSensorListener(new SensorListener() {
         public void stateChanged(Sensor s, int o, int n) {
            if (n==1) {
               TextLCD.print("HAPPY");
            } else {
               TextLCD.print("SAD");
            }
         }
      });
   
      while (true) {
      // do your useful things here.
      }
   }
}

 

Note the following,

  • Use the Sensor.S1.addSensorListener(… ) to let the sensor at S1 know the Listener object.
  • Use the new SensorListener() to create the new SensorListener object.
  • Define a function named stateChanged to handle the event when the sensor detects something changes. The function takes 3 parameters; the 1st is the Sensor object; the 2nd is the old sensor value; the 3rd is the new sensor value. Usually you have to check the 3rd parameter in order to do something relevant.
  • Whenever the sensor has status change, the stateChanged function will execute automatically without calling it explicitly.

 

Exercise time

    • Use the Listener method to display the touch sensor status value on the LCD.
    • Play tone with two touch sensors
    • Controlling motors with sensors

Simple tutorials