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 2: Variables and Expressions

small logo

Making the Turtle Move with More Precision

This lab is devoted to learning how to control the Turtle more easily and with more precision. During it you will be introduced to fundamental programming topics such as constants, variables, mathematical operations and assignments.

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 requires basic knowledge of the Turtle and its operations covered in Lab 1.

Materials

The standard Turtle robot used in Lab 1 with the addition of double bump sensors.

More Turtle Methods

In this lab we introduce the following turtle methods which allow the programming to specify the amount the turtle should turn in degrees:

left(d)         : Turn left d degrees

right(d)        : Turn right d degrees

The left and right methods work by turning for a fixed amount of time base on a calibration of how many milliseconds it takes to turn a degree. Since motors vary in speed, batteries vary in charge, and surfaces provide different amount of friction, we need a way to this this calibration. To do so we use the following method.

calibrateTurn(int d, int degreesPerMillisecond)
                : Sets the degreesPerMillisecond value based on
                  the amount of time it takes to turn d degrees.

The calibrateTurn method takes two arguments: an angle and an estimated time in milliseconds to turn that angle. For example:

Turtle.calibrateTurn(90,1170);

When the method runs, it first displays the time in the LCD screen on the robot. If you're happy with that time, press the green RUN button, and the method is finished. If you want to reject it, press the black VIEW button and the robot will start turning. When it has turned to the desired angle, press one of the bumpers. It will stop and report the time in the LCD, whereupon you can make the same decision as before: green to accept and black to re-calibrate. What angle should you use? The sort of angle your program uses. (Why?) Once you have it calibrated correctly, use that time value as your "guess" in the method call. That way you won't have to actually run the calibration each time you run the program.

Now that we know how to calibrate the Turtle, we are ready to ask it to turn right and left.

Task One: Calibration

Create a new project in BlueJ called Lab2. Then create a new class in that project named Task1 which causes the Turtle to move in a box shape using the calibrateTurn and left methods. Be sure to also add in the Turtle class from lab 1 by using the "Add Class from File" in BlueJ's edit menu.

Effector Error: You will probably notice that it's hard to get the turtle to move exactly as you would like. There are many errors that can creep in from your mathematically perfect code to the physical turtle moving in the world: timing may be inexact, wheels and gears can slip, voltage can be less than ideal, and many other problems. The result is often called effector error, because the motors are effectors - they do things that have an effect in the world. Some may just be random, so that the Turtle doesn't act precisely the same way each time, and others may be systematic (the left motor might be slightly more powerful than the right, resulting in a tendency to pull to the right).

Certainly you should feel free to make adjustments to your program to improve the behavior. You may even find that you have to continue to make adjustments, after the program seems perfect. This is because of the battery level: batteries get weaker (lower voltage) over time, and weaker batteries will turn the robots more slowly, so you might have to tell it to turn for longer and longer in order for the turn the right amount.

Task Two: Using Constants

Now create a copy of Task 1 called Task 2. Insert the following lines of code as the first statements in the main method:

final int distToTravel = 1000;
final int degreesToTurn = 90;

These two lines create two constant values that can be used over and over again without using the literal numbers again. Use them in your code like so:

Turtle.forward(distToTravel);
Turtle.left(degreesToTurn);

Re-implement your code to incorporate constants into your code. Using constants are useful for at least two reasons:

  • They allow the programmer to define appropriate names for values (degreesToTurn instead of 90).
  • They allow the program to change a value once and have it reflected throughout the program by simply changing the constant declaration.

Task Three: Using Variables and Expressions

A variable is a unit of storage that can be used and manipulated by the program as it runs. To use variables they must first be declared and to do this in Java you use the code like the following:

int x = 1;

In this example int is the type of variable being declared. This can be many different types which your instructor will cover with you as needed. The x in our example is an identifier, the name the programmer gives to that variable. Lastly, the value 1 an example of a value that can be given to initialize that variable.

A handy use of variables it to apply arithmetic operations to the variable such as +,-,*,/,%, etc. to modify it as the program runs. These can be used as follows:

x = x + 5;

This expression takes the int variable x and adds 5 to it. A simpler way of writing this same expression is:

x += 5;

Now now to the task. Create a new class, Task3, and in it re-implement your spiral program from Lab 1 using both calibration and variables to make a more exact spiral, with each forward motion being less than the one before. The idea is to use a variable to represent the length of time the Turtle move forward, and to change the value of this variable as needed. Can you think of a way that a new constant would be useful here?

Task Four:

Modify your code from Task 3 to create Task 4 so that the turtle spirals inward then turns around and retraces its spiral back out.

Task Five

Adapt your Task4 program to be Task5 so that the Turtle moves in a pattern similar to the "stairstep" design at right. Each "step" should be 2/3 the size of the previous one.

An Optional Problem

Create a program that will cause the turtle to create three squares that decrease in size by 1/3 each time and start from the same location. See image.

Conclusion

In this lab, you learned the following:

  • How to implement the right, left, calibrateDegreesPerMillisecond functions.
  • How to declare and manipulate/use variables.
  • How to use the basic mathematical operations.