
A simple line following program
main.cpp@5:c705b7e7e1a8, 2010-11-10 (annotated)
- Committer:
- chris
- Date:
- Wed Nov 10 09:05:50 2010 +0000
- Revision:
- 5:c705b7e7e1a8
- Parent:
- 3:7f077cf1d755
- Child:
- 6:8f46c6ac55ca
Updated to latest m3pi library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chris | 0:eb1ece444e15 | 1 | #include "mbed.h" |
chris | 0:eb1ece444e15 | 2 | #include "m3pi.h" |
chris | 0:eb1ece444e15 | 3 | |
chris | 0:eb1ece444e15 | 4 | BusOut leds(LED1,LED2,LED3,LED4); |
chris | 3:7f077cf1d755 | 5 | m3pi m3pi(p23,p9,p10); |
chris | 0:eb1ece444e15 | 6 | |
chris | 0:eb1ece444e15 | 7 | int main() { |
chris | 0:eb1ece444e15 | 8 | |
chris | 2:f3f0843285e2 | 9 | m3pi.locate(0,1); |
chris | 2:f3f0843285e2 | 10 | m3pi.printf("Line Flw"); |
chris | 0:eb1ece444e15 | 11 | |
chris | 5:c705b7e7e1a8 | 12 | wait(2.0); |
chris | 5:c705b7e7e1a8 | 13 | |
chris | 0:eb1ece444e15 | 14 | float position_of_line = 0.0; |
chris | 2:f3f0843285e2 | 15 | m3pi.sensor_auto_calibrate(); |
chris | 0:eb1ece444e15 | 16 | float speed = 0.4; |
chris | 0:eb1ece444e15 | 17 | |
chris | 0:eb1ece444e15 | 18 | while (1) { |
chris | 1:5ddd3faed06d | 19 | |
chris | 0:eb1ece444e15 | 20 | // -1.0 is far left, 1.0 is far right |
chris | 2:f3f0843285e2 | 21 | position_of_line = m3pi.line_position(); |
chris | 1:5ddd3faed06d | 22 | |
chris | 0:eb1ece444e15 | 23 | // Line is more than 25% to the left |
chris | 3:7f077cf1d755 | 24 | if (position_of_line < -0.50) { |
chris | 2:f3f0843285e2 | 25 | m3pi.left_motor(speed); |
chris | 2:f3f0843285e2 | 26 | m3pi.right_motor(speed - 0.3); |
chris | 0:eb1ece444e15 | 27 | leds = 0x4; |
chris | 1:5ddd3faed06d | 28 | } |
chris | 0:eb1ece444e15 | 29 | // Line is more than 75% to the right |
chris | 3:7f077cf1d755 | 30 | else if (position_of_line > 0.50) { |
chris | 2:f3f0843285e2 | 31 | m3pi.right_motor(speed); |
chris | 2:f3f0843285e2 | 32 | m3pi.left_motor(speed - 0.3); |
chris | 0:eb1ece444e15 | 33 | leds = 0x2; |
chris | 1:5ddd3faed06d | 34 | } |
chris | 5:c705b7e7e1a8 | 35 | // Line is in the middle |
chris | 0:eb1ece444e15 | 36 | else { |
chris | 2:f3f0843285e2 | 37 | m3pi.forward(speed); |
chris | 0:eb1ece444e15 | 38 | leds = 0x0; |
chris | 0:eb1ece444e15 | 39 | } |
chris | 0:eb1ece444e15 | 40 | } |
chris | 0:eb1ece444e15 | 41 | } |