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 9: Array Structures

small logo

Control of the Turtle Through External Means


Arrays are an essential part of programming. Today, you will use an array to help you communicate with your robot. You are encouraged to incorporate into your program programming techniques you have recently learned.

This lab will be group-based, with teams of two (or three if required). Each group's goal is to create a program which will allow their Turtle to be taught how to traverse an arbitrary course. Two such courses will be provided, and the total time required to traverse each course will be recorded. Here's an example course:

 

 

Here is the main idea: Imagine your robot has asked you directions to the end of the path. You will input the instructions to the Turtle by causing events, for instance pressing its bumpers to tell it to turn right or left, and its RUN button to move forward. As these instructions are entered, they are stored in an array. After the last instruction is entered, the Turtle runs the course.

In short, your Turtle will need to

  • Accept instructions through its sensors.
  • Put these instructions into an array.
  • Realize when the instructions are done.
  • Follow the instructions to get through the path.
  • Stop when it has completed the instructions

In this lab we will approach this problem in three stages.

  1. Write a program that allows the user to guide the Turtle through the course by causing Turtle events.
  2. Write a program that has an array which has been initialized with the instructions required to guide the Turtle through the course, and have it run the course.
  3. Combine aspects of both these programs to complete our goal of first instructing the Turtle how to run the course (with its running the course at the same time) and then having it run the course on its own.

Prerequisite Knowledge

A basic understanding of how to control and manipulate the Turtle's movements, the Turtle event loop, and arrays.

Materials

Standard Turtle robot with bump sensors.

Task One

Implement a program which lets you guide the robot through the course. You will guide it using the left and right bumpers and the green RUN button. When you touch the bumpers, the robot should turn accordingly. When you touch the green RUN button, it should move a short ways forward. The exact amount is up to you: the smaller the step, the more accuracy you have, but the more tedious the navigation. Similarly, you could have the robot turn just a little, instead of 90 degrees, so you can adjust its angle if necessary. These motions will be the building blocks of the course navigation. (Note: It is a good idea to record the movements that are required to move the Turtle through the course).

Task Two

Create a program that does one of the courses completely hands-off, using the information you figured out by doing task one. Do this using an array to record numbers corresponding to the inputs you gave the robot (touching the bumpers and pushing the RUN button). In other words, the robot will get its information from the array instead of from the input events.

It would be a good idea to encapsulate all this in an appropriate method, but that's not necessary to solving the problem; it's merely good practice in modularity and coding style.

Task Three

Create a program that allows you to run the robot through the course once by hand, recording the inputs in an array. Use the black VIEW button to tell the robot that you're done. Then place the Turtle back at the start point and have the Turtle run the course by itself.

Consider how big the array should be. You'll have to pre-allocate enough space to put all the elements. If you make it too small, your program won't work. If you make it too big, you'll be wasting valuable space. (A program that is too big won't fit on the RCX).

You'll probably want to implement some way of telling the robot to run the course by itself, since you don't want it to start right away when you're done instructing it. There are several ways you could handle this. Consider the pros and cons.

As before, it would be a good idea to encapsulate aspects of this solution in appropriate methods.

Conclusion

In this lab, you learned the following:

  • How to communicate commands to the Turtle
  • How to use arrays to store information