A simple line following program

Dependencies:   mbed

Committer:
chris
Date:
Thu May 12 11:32:35 2011 +0000
Revision:
6:8f46c6ac55ca
Parent:
5:c705b7e7e1a8
Child:
7:218e861ea777
Updated the program to use variables for the parameters

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chris 0:eb1ece444e15 1 #include "mbed.h"
chris 0:eb1ece444e15 2 #include "m3pi.h"
chris 0:eb1ece444e15 3
chris 6:8f46c6ac55ca 4 m3pi m3pi;
chris 0:eb1ece444e15 5
chris 0:eb1ece444e15 6 int main() {
chris 0:eb1ece444e15 7
chris 2:f3f0843285e2 8 m3pi.locate(0,1);
chris 2:f3f0843285e2 9 m3pi.printf("Line Flw");
chris 0:eb1ece444e15 10
chris 5:c705b7e7e1a8 11 wait(2.0);
chris 6:8f46c6ac55ca 12
chris 2:f3f0843285e2 13 m3pi.sensor_auto_calibrate();
chris 6:8f46c6ac55ca 14
chris 6:8f46c6ac55ca 15 float speed = 0.2;
chris 6:8f46c6ac55ca 16 float correction = 0.1;
chris 6:8f46c6ac55ca 17 float threshold = 0.5;
chris 6:8f46c6ac55ca 18
chris 0:eb1ece444e15 19 while (1) {
chris 1:5ddd3faed06d 20
chris 6:8f46c6ac55ca 21 // -1.0 is far left, 1.0 is far right, 0.0 in the middle
chris 6:8f46c6ac55ca 22 float position_of_line = m3pi.line_position();
chris 1:5ddd3faed06d 23
chris 6:8f46c6ac55ca 24 // Line is more than the threshold to the right, slow the left motor
chris 6:8f46c6ac55ca 25 if (position_of_line > threshold) {
chris 6:8f46c6ac55ca 26 m3pi.right_motor(speed);
chris 6:8f46c6ac55ca 27 m3pi.left_motor(speed-correction);
chris 6:8f46c6ac55ca 28 }
chris 6:8f46c6ac55ca 29
chris 6:8f46c6ac55ca 30 // Line is more than 50% to the left, slow the right motor
chris 6:8f46c6ac55ca 31 else if (position_of_line < -threshold) {
chris 2:f3f0843285e2 32 m3pi.left_motor(speed);
chris 6:8f46c6ac55ca 33 m3pi.right_motor(speed-correction);
chris 1:5ddd3faed06d 34 }
chris 6:8f46c6ac55ca 35
chris 5:c705b7e7e1a8 36 // Line is in the middle
chris 0:eb1ece444e15 37 else {
chris 2:f3f0843285e2 38 m3pi.forward(speed);
chris 0:eb1ece444e15 39 }
chris 0:eb1ece444e15 40 }
chris 0:eb1ece444e15 41 }