Source code – Pololu 3pi Robot User Manual
Page 50

0xBA
autocalibrate
0
1
Turns the robot left and right while calibrating. For use when
the robot it positioned over a line. Returns the character ‘c’
when complete.
0xBB
start PID
5
0
Sets up PID parameters and begins line following. The first
data byte sets the maximum motor speed. The next four
bytes, a, b, c, and d, represent the PID parameters.
Specifically, the difference in the motor speeds will be set to
(L-2000)×a/b + D×c/d, where L is the position of the line as
described above, and D is the derivative of L. The integral
term is not implemented in this program. See
for
more information on PID line following.
0xBC
stop PID
0
0
Stops PID line following, setting motor speeds to 0.
0xC1
M1 forward
1
0
Sets motor M1 turning forward with a speed of 0 (off) up to
127 (full speed).
0xC2
M1 backward
1
0
Sets motor M1 turning backward with a speed of 0 (off) up
to 127 (full reverse).
0xC5
M2 forward
1
0
Sets motor M2 turning forward with a speed of 0 (off) up to
127 (full speed).
0xC6
M2 backward
1
0
Sets motor M2 turning backward with a speed of 0 (off) up
to 127 (full reverse).
Source code
#include
/*
* 3pi-serial-slave - An example serial slave program for the Pololu
* 3pi Robot. See the following pages for more information:
*
* http://www.pololu.com/docs/0J21
* http://www.pololu.com/docs/0J20
* http://www.poolu.com/
*
*/
// PID constants
unsigned int pid_enabled = 0;
unsigned char max_speed = 255;
unsigned char p_num = 0;
unsigned char p_den = 0;
unsigned char d_num = 0;
unsigned char d_den = 0;
unsigned int last_proportional = 0;
unsigned int sensors[5];
// This routine will be called repeatedly to keep the PID algorithm running
void pid_check()
{
if(!pid_enabled)
return;
// Do nothing if the denominator of any constant is zero.
if(p_den == 0 || d_den == 0)
{
set_motors(0,0);
return;
}
// Read the line position, with serial interrupts running in the background.
Pololu 3pi Robot User's Guide
© 2001–2014 Pololu Corporation
10. Expansion Information
Page 50 of 63