Alex Chadwick
/
ggc2012-racing
Example program for mbed racing at GGC 2012
main.cpp@0:b69a66f03c16, 2012-10-16 (annotated)
- Committer:
- alexchadwick
- Date:
- Tue Oct 16 09:58:18 2012 +0000
- Revision:
- 0:b69a66f03c16
Example mbed racing program for GGC2012
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alexchadwick | 0:b69a66f03c16 | 1 | #include "mbed.h" |
alexchadwick | 0:b69a66f03c16 | 2 | #include "m3pi.h" |
alexchadwick | 0:b69a66f03c16 | 3 | |
alexchadwick | 0:b69a66f03c16 | 4 | #define BASE_SPEED 0.25 |
alexchadwick | 0:b69a66f03c16 | 5 | #define K_P 0.25 |
alexchadwick | 0:b69a66f03c16 | 6 | |
alexchadwick | 0:b69a66f03c16 | 7 | // The pi object lets us access the robot sensors, motors and LCD. |
alexchadwick | 0:b69a66f03c16 | 8 | // See the user guide at: http://mbed.org/cookbook/m3pi and |
alexchadwick | 0:b69a66f03c16 | 9 | // http://mbed.org/users/chris/code/m3pi/docs/4b7d6ea9b35b/classm3pi.html |
alexchadwick | 0:b69a66f03c16 | 10 | m3pi pi; |
alexchadwick | 0:b69a66f03c16 | 11 | |
alexchadwick | 0:b69a66f03c16 | 12 | int main() { |
alexchadwick | 0:b69a66f03c16 | 13 | float left_speed, right_speed, line_position; |
alexchadwick | 0:b69a66f03c16 | 14 | |
alexchadwick | 0:b69a66f03c16 | 15 | // Display "GGC 2012" so users know they have the correct program |
alexchadwick | 0:b69a66f03c16 | 16 | pi.locate(0,0); |
alexchadwick | 0:b69a66f03c16 | 17 | pi.printf("GGC 2012"); |
alexchadwick | 0:b69a66f03c16 | 18 | |
alexchadwick | 0:b69a66f03c16 | 19 | wait(0.5); |
alexchadwick | 0:b69a66f03c16 | 20 | |
alexchadwick | 0:b69a66f03c16 | 21 | // Auto-align the sensors on the bottom of the robot |
alexchadwick | 0:b69a66f03c16 | 22 | pi.sensor_auto_calibrate(); |
alexchadwick | 0:b69a66f03c16 | 23 | |
alexchadwick | 0:b69a66f03c16 | 24 | while(1) |
alexchadwick | 0:b69a66f03c16 | 25 | { // This implements proportional feedback for driving the motors |
alexchadwick | 0:b69a66f03c16 | 26 | |
alexchadwick | 0:b69a66f03c16 | 27 | // Get the position of the line: -1.0 is off to the left; |
alexchadwick | 0:b69a66f03c16 | 28 | // +1.0 is off to the right. |
alexchadwick | 0:b69a66f03c16 | 29 | line_position = pi.line_position(); |
alexchadwick | 0:b69a66f03c16 | 30 | |
alexchadwick | 0:b69a66f03c16 | 31 | // Speed for each motor can be -1.0 to +1.0. With the default values |
alexchadwick | 0:b69a66f03c16 | 32 | // it is limited to between 0 and 0.5. |
alexchadwick | 0:b69a66f03c16 | 33 | left_speed = BASE_SPEED - line_position*K_P; |
alexchadwick | 0:b69a66f03c16 | 34 | right_speed = BASE_SPEED + line_position*K_P; |
alexchadwick | 0:b69a66f03c16 | 35 | |
alexchadwick | 0:b69a66f03c16 | 36 | // Send new speeds to the motor controller |
alexchadwick | 0:b69a66f03c16 | 37 | pi.left_motor(left_speed); |
alexchadwick | 0:b69a66f03c16 | 38 | pi.right_motor(right_speed); |
alexchadwick | 0:b69a66f03c16 | 39 | } |
alexchadwick | 0:b69a66f03c16 | 40 | |
alexchadwick | 0:b69a66f03c16 | 41 | pi.stop(); |
alexchadwick | 0:b69a66f03c16 | 42 | } |