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

Lab 1: Sequential Control

small logo

Making the Turtle Move

This lab is your first chance to program the Lego Mindstorms robot we call the Turtle, so we will keep it simple. You will program it using Java, specifically LeJOS, an extension of Java which can used to program Mindstorms robots. In this lab we will restrict ourselves to a set of methods specially designed to control the Turtle. Each of the programs you will create consists of a series of commands which are executed one after the other. This constitutes what is called sequential control in programming parlance.

Grading Requirements

For each of the tasks in this lab:

  • Demonstrate to the instructor or lab TA that you robot successfully completes its mission, and have them initial the lab activity sheet.
  • Print off the code for that task, making sure that you have commented that code appropriately.
  • Turn in the initialed checkoff sheet and the code for each of the tasks by 5 pm on the Friday following lab.

Prerequisite Knowledge

This lab assumes very little, but it would help if you were familiar with the general structure of a simple Java program.

Materials

To complete this lab you will need the basic Turtle robot, without any bumpers or sensors. Power the robot on. If the RCX beeps twice and the LCD screen says something like 7.6 0, with an icon of a little person between the x.y number and the single digit, you're all set. If not, you'll have to download the Lejos firmware - the software which makes the robot understand LeJOS. To do that, see your instructor or lab TA.

Tip: That x.y number, by the way, is an estimate of the battery power level (voltage). If it drops below 5.4 or thereabouts (your mileage may vary), it's probably time to change or recharge the batteries. Battery levels certainly affect the speed of the motors (which is not a big deal, but can be annoying if your programs are calibrated for a different speed), and sometimes can affect whether the downloads are successful (which is highly frustrating).

Basic Turtle Methods

Turtles know how to do a number of things (its methods), and in this lab we introduce a few of them. Basically, we can ask a Turtle to move forward, backwards, turn for an amount of time, or stop moving all together. The details:

forward(int n)  : Move forward for n milliseconds.

backward(int n) : Move backward for n milliseconds.

turn(int n)     : Turn counterclockwise (clockwise if negative) for
                  n milliseconds.

stop()          : Stop the motors and wait for them to stop.

Task One  

Task 1 is to download and run a file in an existing project. Download it from this link, and store it in your home directory. It will be a compressed file (a "zip" file) which you can uncompress by double clicking on it. After you have uncompressed it, startup the BlueJ program and open that project. You should see a window like the following:

The two icons (Task1 and Turtle) represent two Java classes. Turtle is a class definition provided by your instructor which defines the Turtle robot. You never need to (or should!) change this code. Task1 is a simple example program which makes the turtle move in a "U" like shape.

Let's run this program. Four steps:

  1. Get Ready: Plug the Lego infrared tower into a usb port on the computer. A good place in our lab is the back of the monitor (if there is one, unplug the zip drive). Place the robot near the tower with its infrared port facing the tower and turn it on (press the on-off button).
  2. Compile: We need to make should that the files have been compiled, i.e., translated into a form the robot can understand. For each of the two classes, hold down the "control key" and click the mouse on the class icon. (we call this a "control-click"). A popup menu appears. Choose "MindstormsTool" and then "Compile". (Don't just choose "Compile", that translates it to run the computer itself, which is not what we want.)
  3. Download: Then control-click on the Task1 icon, and choose "MindstormsTool" and then "download". A status window will pop up showing the download progress. Yes, it does take a while!
  4. Run It: Take the robot over to one of the robot tables, put it down and press the run button. It should start moving.

Ok, if that worked, let's start examining the code. (If not, talk to your instructor or TA about the problem). Back at the computer, double click the Task1 class icon to reveal its code. Minus the comments it should look like this:

public class Task1 {
    public static void main(String args[]) {

 
        Turtle.forward(2000);
        Turtle.turn(1000);
        Turtle.forward(2000);
        Turtle.turn(1000);
        Turtle.forward(2000);
 
        Turtle.stop();
    }
}
Some Review: You probably already know that each file of Java code defines a "class," which is something that encapsulates data and methods. Here, we just define a class called "Task1" and it's only going to have one method (and no data). We said we'd keep it simple! You also probably already know that each class that is executed by the Java run-time system must have a "main" method that is defined like this one. You may not know what words "static" and "void" do yet or the "(String args[])" part, but none of that is important for this lab.

Let's examine the Turtle specific code in Task1.java.

        Turtle.forward(2000);
        Turtle.turn(1000);
        Turtle.forward(2000);
        Turtle.turn(1000);
        Turtle.forward(2000);
 
        Turtle.stop();

The forward method tells the Turtle to move forward, the turn method to turn, and the stop method to stop. forward needs to know how long to go forward, and the number is time in milliseconds. Here, we say 2000 milliseconds so that the turtle will go forward for about two seconds. Similarly, the turn method needs to know how many milliseconds to turn. The stop method doesn't need any additional information.

Details: We say that the forward method of Turtle is "invoked" or "called", getting the number 2000 "passed to it". When forward is finished, control returns to main, which then goes on to its next line of code, and so forth. It's important to remember that code executes step by step like this, one thing at a time; it'll help you understand what's going on and predict what programs will do.

Task Two

Create a new class called Task2 (using the Task1 code as a model) that commands the turtle to move in a approximately a "box" (a square) either clockwise or counter-clockwise. Notice how hard it is to get the robot to turn exactly 90 degrees!

Task Three

Now create a class ("Task3") that moves the turtle in an spiral like the one shown.

Some Optional Problems


If you have some time left, how about some optional tasks!

Imagine that the Turtle is dragging a pen around with it. Write programs to draw the following figures, using the Turtle. Can any figure be drawn without re-drawing any segments?

Conclusion

In this lab, you learned the following:

  • How to write code for the Turtle, download it, and run it.
  • How to invoke an object's methods.
  • The following Turtle methods: forward, backward, turn and stop.
  • How to use sequential control to control the actions of the Turtle.