Sille Van Landschoot
/
racing_robots
Racing Robots Session
Fork of racing_robots by
robot_logic.h@3:b5d18690f357, 2015-02-24 (annotated)
- Committer:
- pcordemans
- Date:
- Tue Feb 24 09:01:06 2015 +0000
- Revision:
- 3:b5d18690f357
- Parent:
- 2:356bb8d99326
- Child:
- 4:3743cbfe031b
added mbed and m3pi sublibraries
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dwini | 0:c0ae66a0ec7a | 1 | #include "mbed.h" |
dwini | 0:c0ae66a0ec7a | 2 | #include "m3pi.h" |
dwini | 0:c0ae66a0ec7a | 3 | |
dwini | 0:c0ae66a0ec7a | 4 | #ifndef H_ROBOT_LOGIC |
dwini | 0:c0ae66a0ec7a | 5 | #define H_ROBOT_LOGIC |
dwini | 0:c0ae66a0ec7a | 6 | |
dwini | 2:356bb8d99326 | 7 | typedef enum { |
dwini | 2:356bb8d99326 | 8 | LED_1 = 0, |
dwini | 2:356bb8d99326 | 9 | LED_2 = 1, |
dwini | 2:356bb8d99326 | 10 | LED_3 = 2, |
dwini | 2:356bb8d99326 | 11 | LED_4 = 3, |
dwini | 2:356bb8d99326 | 12 | LED_5 = 4, |
dwini | 2:356bb8d99326 | 13 | LED_6 = 5, |
dwini | 2:356bb8d99326 | 14 | LED_7 = 6, |
dwini | 2:356bb8d99326 | 15 | LED_8 = 7 |
dwini | 2:356bb8d99326 | 16 | } LedIndex; |
dwini | 0:c0ae66a0ec7a | 17 | |
dwini | 0:c0ae66a0ec7a | 18 | typedef enum { |
dwini | 0:c0ae66a0ec7a | 19 | LED_ON = 0, |
dwini | 0:c0ae66a0ec7a | 20 | LED_OFF = 1, |
dwini | 0:c0ae66a0ec7a | 21 | LED_TOGGLE = 2 |
dwini | 0:c0ae66a0ec7a | 22 | } LedState; |
dwini | 0:c0ae66a0ec7a | 23 | |
dwini | 0:c0ae66a0ec7a | 24 | |
dwini | 0:c0ae66a0ec7a | 25 | /* |
dwini | 0:c0ae66a0ec7a | 26 | * Drive the robot forward or backward. |
dwini | 2:356bb8d99326 | 27 | * If the robot was turning it will stop turning and drive in a straight line. |
dwini | 0:c0ae66a0ec7a | 28 | * |
dwini | 2:356bb8d99326 | 29 | * @speed The speed percentage with which to drive forward or backward. |
dwini | 2:356bb8d99326 | 30 | * Can range from -100 (full throttle backward) to +100 (full throttle forward). |
dwini | 0:c0ae66a0ec7a | 31 | */ |
dwini | 0:c0ae66a0ec7a | 32 | void drive(int speed); |
dwini | 0:c0ae66a0ec7a | 33 | |
dwini | 2:356bb8d99326 | 34 | /* |
dwini | 2:356bb8d99326 | 35 | * Turn the robot left or right while driving. |
dwini | 2:356bb8d99326 | 36 | * |
dwini | 2:356bb8d99326 | 37 | * @turnspeed The percentage with which to turn the robot. |
dwini | 2:356bb8d99326 | 38 | * Can range from -100 (full throttle left) to +100 (full throttle right). |
dwini | 2:356bb8d99326 | 39 | */ |
dwini | 0:c0ae66a0ec7a | 40 | void turn(int turnspeed); |
dwini | 0:c0ae66a0ec7a | 41 | |
dwini | 2:356bb8d99326 | 42 | /* |
dwini | 2:356bb8d99326 | 43 | * Stop the robot. |
dwini | 2:356bb8d99326 | 44 | */ |
dwini | 2:356bb8d99326 | 45 | void stop(void); |
dwini | 0:c0ae66a0ec7a | 46 | |
dwini | 2:356bb8d99326 | 47 | /* |
dwini | 2:356bb8d99326 | 48 | * Calibrate the line follow sensors. |
dwini | 2:356bb8d99326 | 49 | * Take note that the pololu should be placed over the line |
dwini | 2:356bb8d99326 | 50 | * before this function is called and that it will rotate to |
dwini | 2:356bb8d99326 | 51 | * both sides. |
dwini | 2:356bb8d99326 | 52 | */ |
dwini | 2:356bb8d99326 | 53 | void sensor_calibrate(void); |
dwini | 0:c0ae66a0ec7a | 54 | |
dwini | 2:356bb8d99326 | 55 | /* |
dwini | 2:356bb8d99326 | 56 | * Read the value from the line sensor. The returned value indicates the |
dwini | 2:356bb8d99326 | 57 | * position of the line. The value ranges from -100 to +100 where -100 is |
dwini | 2:356bb8d99326 | 58 | * fully left, +100 is fully right and 0 means the line is detected in the middle. |
dwini | 2:356bb8d99326 | 59 | * |
dwini | 2:356bb8d99326 | 60 | * @return The position of the line with a range of -100 to +100. |
dwini | 2:356bb8d99326 | 61 | */ |
dwini | 2:356bb8d99326 | 62 | int line_sensor(void); |
dwini | 0:c0ae66a0ec7a | 63 | |
dwini | 2:356bb8d99326 | 64 | /* |
dwini | 2:356bb8d99326 | 65 | * Initialize the PID drive control with |
dwini | 2:356bb8d99326 | 66 | * the P, I and T factors. |
dwini | 2:356bb8d99326 | 67 | * |
dwini | 2:356bb8d99326 | 68 | * @p The P factor |
dwini | 2:356bb8d99326 | 69 | * @i The I factor |
dwini | 2:356bb8d99326 | 70 | * @d The D factor |
dwini | 2:356bb8d99326 | 71 | */ |
dwini | 0:c0ae66a0ec7a | 72 | void pid_init(int p, int i, int d); |
dwini | 0:c0ae66a0ec7a | 73 | |
dwini | 2:356bb8d99326 | 74 | /* |
dwini | 2:356bb8d99326 | 75 | * Determine PID turnspeed with which the pololu should |
dwini | 2:356bb8d99326 | 76 | * turn to follow the line at the given position. |
dwini | 2:356bb8d99326 | 77 | * |
dwini | 2:356bb8d99326 | 78 | * @line_position The position of the line in a range of [-100, +100] |
dwini | 2:356bb8d99326 | 79 | * |
dwini | 2:356bb8d99326 | 80 | * @returns The turnspeed in a range of [-100, +100] |
dwini | 2:356bb8d99326 | 81 | */ |
dwini | 0:c0ae66a0ec7a | 82 | int pid_turn(int line_position); |
dwini | 0:c0ae66a0ec7a | 83 | |
dwini | 0:c0ae66a0ec7a | 84 | // Show drive speed and sensor data |
dwini | 2:356bb8d99326 | 85 | void show_stats(); |
dwini | 0:c0ae66a0ec7a | 86 | |
dwini | 2:356bb8d99326 | 87 | // Show the name of the robot on the display |
dwini | 2:356bb8d99326 | 88 | void show_name(char * name); |
dwini | 2:356bb8d99326 | 89 | |
dwini | 0:c0ae66a0ec7a | 90 | |
dwini | 0:c0ae66a0ec7a | 91 | // Turn a led on or off |
dwini | 2:356bb8d99326 | 92 | void led(LedIndex i, LedState state); |
dwini | 0:c0ae66a0ec7a | 93 | |
dwini | 0:c0ae66a0ec7a | 94 | // Wait for a number of milliseconds |
dwini | 0:c0ae66a0ec7a | 95 | void await(int milliseconds); |
dwini | 0:c0ae66a0ec7a | 96 | |
dwini | 0:c0ae66a0ec7a | 97 | #endif |