Lab 7: Line Following - Robot with states

You may work with a Partner

If you have a Partner put both names in the comments of your files

Topics:

The main topics you may need in this lab are:

Your goal is to write a function

followLine()
That has the scribbler it gets passed follow a line. Be sure to test your robot on a closed loop (draw a closed track).

In this Lab you will write a program that allows your robot to follow a line. You will use the line sensors on the bottom of the robot, so inspect them now.

On the bottom of the robot, where "Parallax BASIC Stamp..." is printed, there are two line sensors side by side, each composed of an infra red emitter and a receiver. The sensors read either 0, if IR is reflected, or 1, for no reflection. This means the sensors will read 0 when they encounter white and 1 when they encounter black. Since the sensors are on the "scribbler forward" side of the robot, at the beginning of your program, be sure to switch the forwardness of your robot, then switch it back at the end.

If you don't recall how to do this from lab 5 the code is listed on the lab 5 page

Don't forget to set the forwardness of your robot back to normal before you disconnect.

Use the Scribbler's get(sensor) function, which returns a void pointer to get data from the line sesor. Void pointers must be cast to the type you need. Cast the void pointer to a vector of integers pointer. The code will look like this (assuming your robot is a pointer to your scribbler) :

vector<int>* lineSensors = (vector<int>*)robot.get("line");
In this line of code, the get() function is called and returns the line sensor information in the form of a void pointer. This void pointer is cast to a vector of integers pointer by the expression in parentheses. Finally, the pointer is assigned to another vector of integers pointer called 'lineSensor', so that we can hang on to it. When dealing with the vector, be sure to manage memory. Use the delete operator when you're done using the variable, if you forget to do this your program will have a "memory leak" and will eventually use all the memory in the computer and crash.

delete lineSensors;

The left sensor information is stored in index 0 of the vector, right is stored in index 1. Use vector functions to access this data.

Say for example you wanted your program to store the result of the left line sesnor in a variable "left"

int left = lineSensors->at(0);

Essentially, you want your robot to go forward when it is on the line, and when it deviates from the line, you want it to find the line again.

The Line Following Algorithm:

A good way to think about this problem is to consider the robot to be in certain state, where different states represent the robot's position relative to the line.

The robot's state is updated each time it moves.
How many possible states are there? 
What are the possible combinations of the two sensor readings?
Which real-world states do these correspond to? - How would the sensors read if the robot was off the line?
If the program is written so that it "remembers" which side of the line it is on?
What should the robot do if it gets lost and can't determine which side of the line it is on?
Under what circumstances will the robot begin a "searching" behavior (what state signifies a "lost" robot)?

Use the possible states in decision loops. Decision loops is a fancy way of saying if, else, while, and other statements that are nested to control how and when the robot behaves certain ways.

Think about all these possible questions as you design your robot's states, it may be helpful to write out a plan before you code.

It would simplify this lab greatly to make use of your existing turn and travel functions, though it isn't required

You may find it helpful to use these tracks.

They also work better if you use them on the tile floor.

Do This: Write the function followLine() in driver.cpp file, and have the function tell the robot to follow the line until completion.