Pololu 3pi Robot User Manual

Page 26

Advertising
background image

if(counter < 20 || counter >= 60)

set_motors(40,-40);

else

set_motors(-40,40);

// This function records a set of sensor readings and keeps

// track of the minimum and maximum values encountered. The

// IR_EMITTERS_ON argument means that the IR LEDs will be

// turned on during the reading, which is usually what you

// want.

calibrate_line_sensors(IR_EMITTERS_ON);

// Since our counter runs to 80, the total delay will be

// 80*20 = 1600 ms.

delay_ms(20);

}

set_motors(0,0);

// Display calibrated values as a bar graph.

while(!button_is_pressed(BUTTON_B))

{

// Read the sensor values and get the position measurement.

unsigned int position = read_line(sensors,IR_EMITTERS_ON);

// Display the position measurement, which will go from 0

// (when the leftmost sensor is over the line) to 4000 (when

// the rightmost sensor is over the line) on the 3pi, along

// with a bar graph of the sensor readings. This allows you

// to make sure the robot is ready to go.

clear();

print_long(position);

lcd_goto_xy(0,1);

display_readings(sensors);

delay_ms(100);

}

wait_for_button_release(BUTTON_B);

clear();

print("Go!");

// Play music and wait for it to finish before we start driving.

play_from_program_space(go);

while(is_playing());

}

// This is the main function, where the code starts. All C programs

// must have a main() function defined somewhere.

int main()

{

unsigned int sensors[5]; // an array to hold sensor values

// set up the 3pi

initialize();

// This is the "main loop" - it will run forever.

while(1)

{

// Get the position of the line. Note that we *must* provide

// the "sensors" argument to read_line() here, even though we

// are not interested in the individual sensor readings.

unsigned int position = read_line(sensors,IR_EMITTERS_ON);

if(position < 1000)

{

// We are far to the right of the line: turn left.

// Set the right motor to 100 and the left motor to zero,

// to do a sharp turn to the left. Note that the maximum

// value of either motor speed is 255, so we are driving

// it at just about 40% of the max.

set_motors(0,100);

// Just for fun, indicate the direction we are turning on

// the LEDs.

left_led(1);

Pololu 3pi Robot User's Guide

© 2001–2014 Pololu Corporation

7. Example Project #1: Line Following

Page 26 of 63

Advertising