a
Dependencies: Roboter m3pi_js mbed
Fork of m3pi_LineFollower by
main.cpp@7:218e861ea777, 2011-05-12 (annotated)
- Committer:
- chris
- Date:
- Thu May 12 11:34:15 2011 +0000
- Revision:
- 7:218e861ea777
- Parent:
- 6:8f46c6ac55ca
- Child:
- 8:2cca24684c6d
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 | 6:8f46c6ac55ca | 4 | m3pi m3pi; |
chris | 0:eb1ece444e15 | 5 | |
chris | 0:eb1ece444e15 | 6 | int main() { |
chris | 0:eb1ece444e15 | 7 | |
chris | 7:218e861ea777 | 8 | // Parameters that affect the performance |
chris | 7:218e861ea777 | 9 | float speed = 0.2; |
chris | 7:218e861ea777 | 10 | float correction = 0.1; |
chris | 7:218e861ea777 | 11 | float threshold = 0.5; |
chris | 7:218e861ea777 | 12 | |
chris | 2:f3f0843285e2 | 13 | m3pi.locate(0,1); |
chris | 2:f3f0843285e2 | 14 | m3pi.printf("Line Flw"); |
chris | 0:eb1ece444e15 | 15 | |
chris | 5:c705b7e7e1a8 | 16 | wait(2.0); |
chris | 6:8f46c6ac55ca | 17 | |
chris | 2:f3f0843285e2 | 18 | m3pi.sensor_auto_calibrate(); |
chris | 6:8f46c6ac55ca | 19 | |
chris | 0:eb1ece444e15 | 20 | while (1) { |
chris | 1:5ddd3faed06d | 21 | |
chris | 6:8f46c6ac55ca | 22 | // -1.0 is far left, 1.0 is far right, 0.0 in the middle |
chris | 6:8f46c6ac55ca | 23 | float position_of_line = m3pi.line_position(); |
chris | 1:5ddd3faed06d | 24 | |
chris | 6:8f46c6ac55ca | 25 | // Line is more than the threshold to the right, slow the left motor |
chris | 6:8f46c6ac55ca | 26 | if (position_of_line > threshold) { |
chris | 6:8f46c6ac55ca | 27 | m3pi.right_motor(speed); |
chris | 6:8f46c6ac55ca | 28 | m3pi.left_motor(speed-correction); |
chris | 6:8f46c6ac55ca | 29 | } |
chris | 6:8f46c6ac55ca | 30 | |
chris | 6:8f46c6ac55ca | 31 | // Line is more than 50% to the left, slow the right motor |
chris | 6:8f46c6ac55ca | 32 | else if (position_of_line < -threshold) { |
chris | 2:f3f0843285e2 | 33 | m3pi.left_motor(speed); |
chris | 6:8f46c6ac55ca | 34 | m3pi.right_motor(speed-correction); |
chris | 1:5ddd3faed06d | 35 | } |
chris | 6:8f46c6ac55ca | 36 | |
chris | 5:c705b7e7e1a8 | 37 | // Line is in the middle |
chris | 0:eb1ece444e15 | 38 | else { |
chris | 2:f3f0843285e2 | 39 | m3pi.forward(speed); |
chris | 0:eb1ece444e15 | 40 | } |
chris | 0:eb1ece444e15 | 41 | } |
chris | 0:eb1ece444e15 | 42 | } |