Pololu 3pi Robot User Manual

Page 30

Advertising
background image

switch(dir)

{

case 'L':

// Turn left.

set_motors(-80,80);

delay_ms(200);

break;

case 'R':

// Turn right.

set_motors(80,-80);

delay_ms(200);

break;

case 'B':

// Turn around.

set_motors(80,-80);

delay_ms(400);

break;

case 'S':

// Don't do anything!

break;

}

}

The first line of the file, like any C file that you will be writing for the 3pi, contains an include command that gives
you access to the functions in the Pololu AVR Library. Within turn(), we then use the library functions delay_ms() and
set_motors() to perform left turns, right turns, and U-turns. Straight “turns” are also handled by this function, though
they don’t require us to take any action. The motor speeds and the timings for the turns are parameters that needed to
be adjusted for the 3pi; as you work on making your maze solver faster, these are some of the numbers that you might
need to adjust.

To access this function from other C files, we need a “header file”, which is called

turn.h

. The header file just

contains a single line:

void turn(char dir);

This line declares the turn() function without actually including a copy of its code. To access the declaration, each C
file that needs to call turn() adds the following line:

#include "turn.h"

Note the double-quotes being used instead of angle brackets. This signifies to the C compiler that the header file is
in the project directory, rather than being a system header file like

3pi.h

. Always remember to put the code for your

functions in the C file instead of the header file! If you do it the other way, you will be making a separate copy of the
code in each file that includes the header.

The file

follow-segment.c

also contains a single function, follow_segment(), which will drive 3pi straight along a

line segment until it reaches an intersection or the end of the line. This is almost the same as the line following code
discussed in

Section 7

, but with extra checks for intersections and the ends of lines. Here is the function:

void follow_segment()

{

int last_proportional = 0;

long integral=0;

while(1)

{

// Normally, we will be following a line. The code below is

// similar to the 3pi-linefollower-pid example, but the maximum

// speed is turned down to 60 for reliability.

// Get the position of the line.

unsigned int sensors[5];

unsigned int position = read_line(sensors,IR_EMITTERS_ON);

Pololu 3pi Robot User's Guide

© 2001–2014 Pololu Corporation

8. Example Project #2: Maze Solving

Page 30 of 63

Advertising