beautypg.com

Pololu 3pi Robot User Manual

Page 23

background image

function used later in the code also depends on having calibrated values. For more information, see

Section 19

of the

command reference

[http://www.pololu.com/docs/0J18]

.

4. Displaying the calibrated line sensor values in a bar graph. This demonstrates the use of the
lcd_load_custom_character() function together with print_character() to make it easy to see whether the line
sensors are working properly before starting the robot. For more information on this and other LCD commands,
see

Section 5

of the

command reference

[http://www.pololu.com/docs/0J18]

.

5. Waiting for the user to press a button. It’s very important for your robot not to start driving until you want
it to start, or it could unexpectedly drive off of a table or out of your hands when you are trying to program
it. We use the button_is_pressed() function to wait for you to press the B button while displaying the battery
voltage or sensor readings. For more information on button commands, see

Section 9

of the

command reference

[http://www.pololu.com/docs/0J18]

.

In the second phase of the program, your 3pi will take a sensor reading and set the motor speed appropriately based
on the reading. The general idea is that if the robot is off on either side, it should turn to get back on, but if it’s on the
line, it should try to drive straight ahead. The following steps occur inside of a

while(1)

loop, which will continue

repeating over and over until the robot is turned off or reset.

1. The function read_line() is called. This takes a sensor reading and returns an estimate of the robot’s position
with respect to the line, as a number between 0 and 4000. A value of 0 means that the line is to the left of sensor
0, value of 1000 means that the line is directly under sensor 1, 2000 means that the line is directly under sensor
2, and so on.

2. The value returned by read_line() is divided into three possible cases:

0–1000: the robot is far to the right of the line. In this case, to turn sharply left, we set the right motor
speed to 100 and the left motor speed to 0. Note that the maximum speed of the motors is 255, so we are
driving the right motor at only about 40% power here.

1000–3000: the robot is approximately centered on the line. In this case, we set both motors to speed
100, to drive straight ahead.

3000–4000: the robot is far to the left of the line. In this case, we turn sharply to the right by setting the
right motor speed to 0 and the left motor speed to 100.

3. Depending on which motors are activated, the corresponding LEDs are turned on for a more interesting
display. This can also help with debugging.

To open the program in Atmel Studio, you may go to

examples\atmegaxx8\3pi-linefollower

and simply double-

click on

3pi-linefollower.cproj

. Compile the program, load it onto your 3pi, and try it out. You should find that

your robot is able to follow the curves of your line course without ever completely losing the line. However, its
motors are moving at a speed of at most 100 out of the maximum possible of 255, and the algorithm causes a lot
of unnecessary shaking on the curves. At this point, you might want to work on trying to adjust and improve this
algorithm, before moving on to the next section. Some ideas for improvement are:

• Increase the maximum possible speed.

• Add more intermediate cases, with intermediate speed settings, to make the motion less jerky.

• Give your robot a memory: have its maximum speed increase after it has been on the line consistently for a
few cycles.

You might also want to:

• Measure the speed of your loop, using timing functions from

Section 17

of the

command reference

[http://www.pololu.com/docs/0J18]

to time a few thousand cycles or by blinking the LEDs on and off every 1000

cycles.

Pololu 3pi Robot User's Guide

© 2001–2014 Pololu Corporation

7. Example Project #1: Line Following

Page 23 of 63