LMICSE: Lego Mindstorms in Computer Science Education

Site Map | Contact Us
Project Overview | Staff | Grant Information
Short Workshops | Primary Workshops
CS 1 | Data Str. & Algo. | Prog. Languages | Architecture | Intelligent Sys. | Operating Sys. | Net-centric
Ada | C | C++ | Java | Lisp

Intro to Mindstorms

small logo

Perquisites

Basic knowledge of the techniques and concepts behind programming in java. This lab will introduce you to all the commands needed to program the mindstorm to do basic functions but advanced structures will also be needed to create interesting and useful programs.

Materials

To complete this lab you will need the basic RCX robot, which can be built following our Turtle Construction Instructions or designed to your specifications. This turtle will require a bumper assembly as well as a light sensor assembly for doing input work. When you're done, the lejos firmware will need to be downloaded onto the robot. Your instructor can help you with this. Once the firmware is downloaded a decimal number will appear on the LCD display when a program is not running, this is the current battery voltage available. When this number falls below 6 than your batteries need to be replaced.

Task One

First off we need to learn how to create a program that can tell the mindstorm to turn on and off motors so that we can tell it to move around. The most basic way to do this is to tell each motor to turn on forward/backward for a certain amount of time. This can be completed by using the following command (that can be modified depending on which output you want to turn on):

Motor.A.forward();
Motor.C.backward();

This set of commands will turn the motor connected to the left-most connection on forward and the motor connected to the right-most motor on backwards. In this particular instance this set of commands will cause the bot to turn clockwise (if the left motor is in channel a and the right motor is in channel c). If you wanted to have it move forward or backward only you would just set both motors to do that command. To have the bot do commands for a certain amount of time is the next useful tool. This can allow us to move forward for a certain distance. To do this we add a sleep command that looks like:

Thread.sleep(n);

Here n would represent some time in milliseconds that you want the robot to not continue reading lines of code.

Use these simple commands to create a program that will drive your robot in a square. Note: Using a loop structure might work well here.

Task Two

Next we will discuss some of the sensors that are available to the standard RCX robot kits. The simplest of these is the bump sensors. They return an integer of 0(not bumped) or 1(bumped) to describe the state it is in. To use these sensors we must first declare them to be bump sensors so the robot knows what its seeing. To do this we use the following code:

Sensor.S1.setTypeAndMode(SensorConstants.SENSOR_TYPE_TOUCH,
							   SensorConstants.SENSOR_MODE_BOOL); 

As you can see there are two parameters to the setTypeAndMode() call these are what type of sensor that input is connected to and what kind of return it sends. In this case we are using a sensor of type touch and return of mode bool. From this point on in the code we can make calls to the variable Sensor.S1 using the readValue() method built into the sensor variables.

Now sensors are most useful when they cause some action to take place when they are hit. To do this there are two methods. There is what is known as polling which is when you have a loop that continues to check the sensor every time it goes through the loop to see if its bumped. This would be coded similar to the following:

while(Sensor.S1.readValue() != 1){
  //code to be executed while waiting for bump
  } 

However, the problem that you will run into with this method of sensor checks is that your loop will have to complete every time before the sensor gets checked so if you have some other code inside the loop going that takes time it may not check it very often. Another form of sensor checks is called using a sensorListener which runs in a separate thread (or process) and checks the sensors constantly even while other code is running. This can allow you to have a robot that whenever it gets hit it reacts. This has a slightly more complicate format though which would look like:

public class listening implements SensorListener{
	boolean stopFlag = false;
	
	public static void main (String[] args) throws Exception {
		listening rock = new listening();		
		  }
	
	public listening () throws InterruptedException{
		Sensor.S2.setTypeAndMode(SensorConstants.SENSOR_TYPE_TOUCH,
								   SensorConstants.SENSOR_MODE_BOOL);
		Sensor.S2.addSensorListener(this);
		//code for what to do while waiting for sensor
	}

	//this method needs to be implemented due to SensorListener	
	public void stateChanged(Sensor s, int oldValue, int newValue) {
	   if (newValue == 1) {
		//	some code to occure when the bumper gets pressed
		}
	}

} 

Now implement code that will utilize the polling idea to move forward until it hits a wall, turns away from the wall, and continues on. Then use it some to see where its downfalls are in your implementation then recreate it to use the listener idea. (Note: When the listener does fire your current code will be interrupted so your turns and or forward motions may be shortened of missed due to this. How might one go about avoiding this?)

Task Three

What if we wanted to utilize the light sensor to say find dark/light areas on a board. We can utilize similar structures as above but the sensor will need to be declared slightly differently. The light sensor is a sensor of type light and return mode raw and after it is declared it returns an integer value. This integer value can be determined by using the following code in a simple program that has the light sensor declared:

Sensor.S2.activate();
while (true){		
	LCD.showNumber(Sensor.S2.readRawValue());
}

You will notice that there is an activate command in the previous code. This is because the light sensor is known as an active sensor and thus needs to be activated before it gets called.

Now implement the new light sensor into a program that can follow a curvy black line.

Conclusion

In this lab, you learned the following:

  • How to write code for the RCX
  • The use of the following RCX devices and calls: Motor, Sensor, forward, backward, and Thread.sleep().
  • How to use sensor listeners to utilize sensor inputs.