a
Dependencies: Roboter m3pi_js mbed
Fork of m3pi_LineFollower by
main.cpp@0:eb1ece444e15, 2010-11-01 (annotated)
- Committer:
- chris
- Date:
- Mon Nov 01 23:16:57 2010 +0000
- Revision:
- 0:eb1ece444e15
- Child:
- 1:5ddd3faed06d
Added some LEDs to show how far off the line the sensors are
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 | 0:eb1ece444e15 | 5 | m3pi pi (p27,p9,p10); //nRST, TX, RX |
chris | 0:eb1ece444e15 | 6 | |
chris | 0:eb1ece444e15 | 7 | int main() { |
chris | 0:eb1ece444e15 | 8 | |
chris | 0:eb1ece444e15 | 9 | pi.locate(0,1); |
chris | 0:eb1ece444e15 | 10 | pi.printf("Line Flw"); |
chris | 0:eb1ece444e15 | 11 | |
chris | 0:eb1ece444e15 | 12 | float position_of_line = 0.0; |
chris | 0:eb1ece444e15 | 13 | |
chris | 0:eb1ece444e15 | 14 | pi.sensor_auto_calibrate(); |
chris | 0:eb1ece444e15 | 15 | float speed = 0.4; |
chris | 0:eb1ece444e15 | 16 | |
chris | 0:eb1ece444e15 | 17 | while (1) { |
chris | 0:eb1ece444e15 | 18 | |
chris | 0:eb1ece444e15 | 19 | // -1.0 is far left, 1.0 is far right |
chris | 0:eb1ece444e15 | 20 | position_of_line = pi.line_position(); |
chris | 0:eb1ece444e15 | 21 | |
chris | 0:eb1ece444e15 | 22 | // Line is more than 75% to the left |
chris | 0:eb1ece444e15 | 23 | if (position_of_line < -0.25) { |
chris | 0:eb1ece444e15 | 24 | pi.left_motor(speed + 0.3); |
chris | 0:eb1ece444e15 | 25 | pi.right_motor(speed - 0.3); |
chris | 0:eb1ece444e15 | 26 | leds = 0xc; |
chris | 0:eb1ece444e15 | 27 | } |
chris | 0:eb1ece444e15 | 28 | // Line is more than 25% to the left |
chris | 0:eb1ece444e15 | 29 | else if (position_of_line < -0.10) { |
chris | 0:eb1ece444e15 | 30 | pi.left_motor(speed); |
chris | 0:eb1ece444e15 | 31 | pi.right_motor(speed - 0.3); |
chris | 0:eb1ece444e15 | 32 | leds = 0x4; |
chris | 0:eb1ece444e15 | 33 | } |
chris | 0:eb1ece444e15 | 34 | // Line is more than 75% to the right |
chris | 0:eb1ece444e15 | 35 | else if (position_of_line > 0.25) { |
chris | 0:eb1ece444e15 | 36 | pi.right_motor(speed + 0.3); |
chris | 0:eb1ece444e15 | 37 | pi.left_motor(speed - 0.3); |
chris | 0:eb1ece444e15 | 38 | leds = 0x3; |
chris | 0:eb1ece444e15 | 39 | } |
chris | 0:eb1ece444e15 | 40 | // Line is more than 75% to the right |
chris | 0:eb1ece444e15 | 41 | else if (position_of_line > 0.10) { |
chris | 0:eb1ece444e15 | 42 | pi.right_motor(speed); |
chris | 0:eb1ece444e15 | 43 | pi.left_motor(speed - 0.3); |
chris | 0:eb1ece444e15 | 44 | leds = 0x2; |
chris | 0:eb1ece444e15 | 45 | } |
chris | 0:eb1ece444e15 | 46 | // Line is inthe middle |
chris | 0:eb1ece444e15 | 47 | else { |
chris | 0:eb1ece444e15 | 48 | pi.forward(speed); |
chris | 0:eb1ece444e15 | 49 | leds = 0x0; |
chris | 0:eb1ece444e15 | 50 | } |
chris | 0:eb1ece444e15 | 51 | |
chris | 0:eb1ece444e15 | 52 | } |
chris | 0:eb1ece444e15 | 53 | |
chris | 0:eb1ece444e15 | 54 | } |