Sille Van Landschoot
/
racing_robots
Racing Robots Session
Fork of racing_robots by
robot_logic.h@10:c67825bf5f6b, 2015-06-03 (annotated)
- Committer:
- sillevl
- Date:
- Wed Jun 03 11:28:28 2015 +0000
- Revision:
- 10:c67825bf5f6b
- Parent:
- 9:0385d1bfc38b
initial project
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pcordemans | 4:3743cbfe031b | 1 | #ifndef H_ROBOT_LOGIC |
pcordemans | 4:3743cbfe031b | 2 | #define H_ROBOT_LOGIC |
pcordemans | 4:3743cbfe031b | 3 | |
sillevl | 10:c67825bf5f6b | 4 | #define XBEE |
sillevl | 10:c67825bf5f6b | 5 | |
dwini | 0:c0ae66a0ec7a | 6 | #include "mbed.h" |
dwini | 0:c0ae66a0ec7a | 7 | #include "m3pi.h" |
dwini | 0:c0ae66a0ec7a | 8 | |
dwini | 2:356bb8d99326 | 9 | typedef enum { |
dwini | 2:356bb8d99326 | 10 | LED_1 = 0, |
dwini | 2:356bb8d99326 | 11 | LED_2 = 1, |
dwini | 2:356bb8d99326 | 12 | LED_3 = 2, |
dwini | 2:356bb8d99326 | 13 | LED_4 = 3, |
dwini | 2:356bb8d99326 | 14 | LED_5 = 4, |
dwini | 2:356bb8d99326 | 15 | LED_6 = 5, |
dwini | 2:356bb8d99326 | 16 | LED_7 = 6, |
dwini | 2:356bb8d99326 | 17 | LED_8 = 7 |
dwini | 2:356bb8d99326 | 18 | } LedIndex; |
dwini | 0:c0ae66a0ec7a | 19 | |
dwini | 0:c0ae66a0ec7a | 20 | typedef enum { |
dwini | 0:c0ae66a0ec7a | 21 | LED_ON = 0, |
dwini | 0:c0ae66a0ec7a | 22 | LED_OFF = 1, |
dwini | 0:c0ae66a0ec7a | 23 | LED_TOGGLE = 2 |
dwini | 0:c0ae66a0ec7a | 24 | } LedState; |
dwini | 0:c0ae66a0ec7a | 25 | |
dwini | 0:c0ae66a0ec7a | 26 | |
pcordemans | 6:0dc4e4225881 | 27 | /** |
dwini | 0:c0ae66a0ec7a | 28 | * Drive the robot forward or backward. |
dwini | 2:356bb8d99326 | 29 | * If the robot was turning it will stop turning and drive in a straight line. |
dwini | 0:c0ae66a0ec7a | 30 | * |
pcordemans | 6:0dc4e4225881 | 31 | * @param speed The speed percentage with which to drive forward or backward. |
dwini | 2:356bb8d99326 | 32 | * Can range from -100 (full throttle backward) to +100 (full throttle forward). |
dwini | 0:c0ae66a0ec7a | 33 | */ |
dwini | 0:c0ae66a0ec7a | 34 | void drive(int speed); |
dwini | 0:c0ae66a0ec7a | 35 | |
pcordemans | 6:0dc4e4225881 | 36 | /** |
dwini | 2:356bb8d99326 | 37 | * Turn the robot left or right while driving. |
dwini | 2:356bb8d99326 | 38 | * |
pcordemans | 6:0dc4e4225881 | 39 | * @param turnspeed The percentage with which to turn the robot. |
pcordemans | 6:0dc4e4225881 | 40 | * Can range from -100 (full throttle left) to +100 (full throttle right). |
dwini | 2:356bb8d99326 | 41 | */ |
dwini | 0:c0ae66a0ec7a | 42 | void turn(int turnspeed); |
dwini | 0:c0ae66a0ec7a | 43 | |
pcordemans | 6:0dc4e4225881 | 44 | /** |
dwini | 2:356bb8d99326 | 45 | * Stop the robot. |
dwini | 2:356bb8d99326 | 46 | */ |
dwini | 2:356bb8d99326 | 47 | void stop(void); |
dwini | 0:c0ae66a0ec7a | 48 | |
pcordemans | 6:0dc4e4225881 | 49 | /** |
dwini | 2:356bb8d99326 | 50 | * Calibrate the line follow sensors. |
pcordemans | 7:a72215b1910b | 51 | * Take note that the robot should be placed over the line |
dwini | 2:356bb8d99326 | 52 | * before this function is called and that it will rotate to |
dwini | 2:356bb8d99326 | 53 | * both sides. |
dwini | 2:356bb8d99326 | 54 | */ |
dwini | 2:356bb8d99326 | 55 | void sensor_calibrate(void); |
dwini | 0:c0ae66a0ec7a | 56 | |
pcordemans | 6:0dc4e4225881 | 57 | /** |
dwini | 2:356bb8d99326 | 58 | * Read the value from the line sensor. The returned value indicates the |
dwini | 2:356bb8d99326 | 59 | * position of the line. The value ranges from -100 to +100 where -100 is |
dwini | 2:356bb8d99326 | 60 | * fully left, +100 is fully right and 0 means the line is detected in the middle. |
dwini | 2:356bb8d99326 | 61 | * |
dwini | 2:356bb8d99326 | 62 | * @return The position of the line with a range of -100 to +100. |
dwini | 2:356bb8d99326 | 63 | */ |
dwini | 2:356bb8d99326 | 64 | int line_sensor(void); |
dwini | 0:c0ae66a0ec7a | 65 | |
pcordemans | 6:0dc4e4225881 | 66 | /** |
dwini | 2:356bb8d99326 | 67 | * Initialize the PID drive control with |
pcordemans | 6:0dc4e4225881 | 68 | * the P, I and D factors. |
dwini | 2:356bb8d99326 | 69 | * |
pcordemans | 6:0dc4e4225881 | 70 | * @param p The P factor |
pcordemans | 6:0dc4e4225881 | 71 | * @param i The I factor |
pcordemans | 6:0dc4e4225881 | 72 | * @param d The D factor |
dwini | 2:356bb8d99326 | 73 | */ |
dwini | 0:c0ae66a0ec7a | 74 | void pid_init(int p, int i, int d); |
dwini | 0:c0ae66a0ec7a | 75 | |
pcordemans | 6:0dc4e4225881 | 76 | /** |
pcordemans | 7:a72215b1910b | 77 | * Determine PID turnspeed with which the robot should |
dwini | 2:356bb8d99326 | 78 | * turn to follow the line at the given position. |
dwini | 2:356bb8d99326 | 79 | * |
pcordemans | 6:0dc4e4225881 | 80 | * @param line_position The position of the line in a range of [-100, +100] |
dwini | 2:356bb8d99326 | 81 | * |
pcordemans | 6:0dc4e4225881 | 82 | * @return The turnspeed in a range of [-100, +100] |
dwini | 2:356bb8d99326 | 83 | */ |
dwini | 0:c0ae66a0ec7a | 84 | int pid_turn(int line_position); |
dwini | 0:c0ae66a0ec7a | 85 | |
pcordemans | 6:0dc4e4225881 | 86 | /** |
pcordemans | 6:0dc4e4225881 | 87 | *Show speed, turn and sensor data on the LCD |
pcordemans | 6:0dc4e4225881 | 88 | */ |
dwini | 2:356bb8d99326 | 89 | void show_stats(); |
dwini | 0:c0ae66a0ec7a | 90 | |
pcordemans | 6:0dc4e4225881 | 91 | /** |
pcordemans | 6:0dc4e4225881 | 92 | * Shows the name of the robot on the display. |
pcordemans | 6:0dc4e4225881 | 93 | * |
pcordemans | 6:0dc4e4225881 | 94 | * @param name C character string (null-terminated) with the name of the robot (max 8 chars) |
pcordemans | 6:0dc4e4225881 | 95 | */ |
dwini | 2:356bb8d99326 | 96 | void show_name(char * name); |
dwini | 2:356bb8d99326 | 97 | |
dwini | 0:c0ae66a0ec7a | 98 | |
pcordemans | 6:0dc4e4225881 | 99 | /** |
pcordemans | 6:0dc4e4225881 | 100 | * Turn on, off or toggle a specific LED |
pcordemans | 6:0dc4e4225881 | 101 | * @param i the LED number LED_0 .. LED_7 |
pcordemans | 6:0dc4e4225881 | 102 | * @param state the LED state LED_ON, LED_OFF, LED_TOGGLE |
pcordemans | 7:a72215b1910b | 103 | * @example led(LED_0, LED_ON); turns LED 0 on. |
pcordemans | 6:0dc4e4225881 | 104 | */ |
dwini | 2:356bb8d99326 | 105 | void led(LedIndex i, LedState state); |
dwini | 0:c0ae66a0ec7a | 106 | |
pcordemans | 6:0dc4e4225881 | 107 | /** |
pcordemans | 6:0dc4e4225881 | 108 | * Wait for an approximate number of milliseconds. |
pcordemans | 6:0dc4e4225881 | 109 | * |
pcordemans | 6:0dc4e4225881 | 110 | * @param milliseconds The number of milliseconds to wait. |
pcordemans | 6:0dc4e4225881 | 111 | */ |
dwini | 0:c0ae66a0ec7a | 112 | void await(int milliseconds); |
dwini | 0:c0ae66a0ec7a | 113 | |
sillevl | 8:597ce8a7d34b | 114 | /** |
sillevl | 9:0385d1bfc38b | 115 | * If using Xbee to start/stop the robot, you can set a unique code to prevent starting or stopping other robots. |
sillevl | 9:0385d1bfc38b | 116 | * To start or stop the robot, send the a json string only containing a "start" or "stop" key with the code as a value. |
sillevl | 9:0385d1bfc38b | 117 | * eg: {"start": 1234} will start the robot with code set to 1234, {"stop": 9876} will stop the robot with code 9876. |
sillevl | 8:597ce8a7d34b | 118 | * |
sillevl | 8:597ce8a7d34b | 119 | * @param code A number between 0 and 1000. |
sillevl | 8:597ce8a7d34b | 120 | */ |
sillevl | 8:597ce8a7d34b | 121 | void setCode(int code); |
sillevl | 8:597ce8a7d34b | 122 | |
sillevl | 8:597ce8a7d34b | 123 | /** |
sillevl | 8:597ce8a7d34b | 124 | * Use this method if you need to know if something (eg: Xbee) is preventing the robot from driving |
sillevl | 8:597ce8a7d34b | 125 | * |
sillevl | 8:597ce8a7d34b | 126 | * @return Boolean true if the robot can drive, false if the robot cannot drive |
sillevl | 8:597ce8a7d34b | 127 | */ |
sillevl | 8:597ce8a7d34b | 128 | int canDrive(); |
sillevl | 8:597ce8a7d34b | 129 | |
dwini | 0:c0ae66a0ec7a | 130 | #endif |