C. left hand on the wall – Pololu 3pi Robot User Manual
Page 31
// The "proportional" term should be 0 when we are on the line.
int proportional = ((int)position) - 2000;
// Compute the derivative (change) and integral (sum) of the
// position.
int derivative = proportional - last_proportional;
integral += proportional;
// Remember the last position.
last_proportional = proportional;
// Compute the difference between the two motor power settings,
// m1 - m2. If this is a positive number the robot will turn
// to the left. If it is a negative number, the robot will
// turn to the right, and the magnitude of the number determines
// the sharpness of the turn.
int power_difference = proportional/20 + integral/10000 + derivative*3/2;
// Compute the actual motor settings. We never set either motor
// to a negative value.
const int max = 60; // the maximum speed
if(power_difference > max)
power_difference = max;
if(power_difference < -max)
power_difference = -max;
if(power_difference < 0)
set_motors(max+power_difference,max);
else
set_motors(max,max-power_difference);
// We use the inner three sensors (1, 2, and 3) for
// determining whether there is a line straight ahead, and the
// sensors 0 and 4 for detecting lines going to the left and
// right.
if(sensors[1] < 100 && sensors[2] < 100 && sensors[3] < 100)
{
// There is no line visible ahead, and we didn't see any
// intersection. Must be a dead end.
return;
}
else if(sensors[0] > 200 || sensors[4] > 200)
{
// Found an intersection.
return;
}
}
}
Between the PID code and the intersection detection, there are now about six more parameters that could be adjusted.
We’ve picked values here that allow 3pi to solve the maze at a safe, controlled speed; try increasing the speed and
you will quickly run in to lots of problems that you’ll have to handle with more complicated code.
Putting the C files and header files into your project is easy with Atmel Studio. On the right side of your screen, in
the “Solution Explorer” pane, you should see a list of files in your project. Right click on the name of your project
and you will have the option to add files to the list. When you build your project, Atmel Studio will automatically
compile all C files in the project together to produce a single hex file.
8.c. Left Hand on the Wall
The basic strategy for solving a non-looped maze is called “left hand on the wall”. Imagine walking through a real
labyrinth – a human-sized maze built with stone walls – while keeping your left hand on the wall at all times. You’ll
turn left whenever possible and only turn right at an intersection if there is no other exit. Sometimes, when you reach
a dead end, you’ll turn 180 degrees to the right and start walking back the way you came. Eventually, as long as there
are no loops, your hand will travel along each length of wall in the entire labyrinth exactly once, and you’ll find your
way back to the entrance. If there is a room somewhere in the labyrinth with a monster or some treasure, you’ll find
Pololu 3pi Robot User's Guide
© 2001–2014 Pololu Corporation
8. Example Project #2: Maze Solving
Page 31 of 63