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 10: Navigation

small logo

Navigation

In this lab we will design a navigation class that will allow the Turtle travel around in a "grid world." A grid world is just a grid of locations, with each location a square of a certain size. Below is a picture of an five by eight grid world.

The Turtle will move around in this world in the "Manhattan" way: it can move up or down, right or left, but can't angle across grid locations (just as in a grid of streets and buildings you have to follow the streets and can't cut through buildings). So to move from grid location <4,1> to location <2,5> you could go from <4,1> to <4,5> and then to <2,5>. Or you could go <4,1> to <2,1> and then to <2,5>. But you can't go directly from <4,1> to <2,5>.

Prerequisite Knowledge

Knowledge of how to control the Turtle, how to define and use classes, and how to use two dimensional arrays.

Materials

Basic Turtle with bump sensors and a gridded area for testing.

Task One

Task one is the core of this lab, and is the hard part. Your task is to create a program that has two classes in it. One will be the standard driver class which contains the main method. The other class should be called Naver. It should have a constructor which receives and stores the size of the grid (the number of rows and columns):

   public Naver(int x, int y)

and methods which (1) set the current location and orientation of the Turtle and (2) ask the Turtle to move from its current location to a new location:

   public void setLoc(int x, int y, int direction)
   //sets the current location in the grid with direction


   public void goTo(int x, int y)
   //moves the robot to the new location specified by x and y

Check in each of these methods that the location received is legal, e.g., inside the grid world. If it is not, have the Turtle beep and ignore the command. The goTo command should be able to handle any legal location, so it needs to be able to make two part journeys as described in the lab's introductory section.

You will need instance variables to store the location and orientation of the Turtle (i.e., NORTH, SOUTH, EAST, or WEST). As well you will need to figure out how long it takes the Turtle to move from one grid location to the next. Be sure your Turtle is well calibrated!

Test your code by having your main method

  1. create a new Naver object
  2. set the current location to <1,2,EAST>
  3. go to <4,3>
  4. go to <2,0>

Task Two

In your main class use a two dimensional array to store a series of locations for the Turtle to visit. The rows represent the various locations. There should be two columns in the array, which hold the vertical and horizontal coordinates of each of the locations.

Test your program by initializing your array with a series of points and then have your Turtle visit them by issuing a series of goto commands.

Task Three

Now redesign Task Two so that you can input the coordinates of each location in order using the bump sensors, storing them in the array as they are entered. After all the values are entered the Turtle should move to each location stored in the array.

Optional Problem

Re-implement the navigating program you have to allow for angled movements as opposed to moving Manhattan. That way the Turtle should move directly from one location to the other.

Conclusion

In this lab, you worked with:

  • Two dimensional arrays.
  • Classes
  • Navigating in a grid world.