Allows the M3Pi to be used as a Sumo robot, using the sharp 100 distance sensors on the front. Hunter strategy
m3pi/m3pi.h@0:a29bcf098632, 2012-06-18 (annotated)
- Committer:
- jonmarsh
- Date:
- Mon Jun 18 09:27:10 2012 +0000
- Revision:
- 0:a29bcf098632
1st pass - not tested
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jonmarsh | 0:a29bcf098632 | 1 | /* mbed m3pi Library |
jonmarsh | 0:a29bcf098632 | 2 | * Copyright (c) 2007-2010 cstyles |
jonmarsh | 0:a29bcf098632 | 3 | * |
jonmarsh | 0:a29bcf098632 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
jonmarsh | 0:a29bcf098632 | 5 | * of this software and associated documentation files (the "Software"), to deal |
jonmarsh | 0:a29bcf098632 | 6 | * in the Software without restriction, including without limitation the rights |
jonmarsh | 0:a29bcf098632 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
jonmarsh | 0:a29bcf098632 | 8 | * copies of the Software, and to permit persons to whom the Software is |
jonmarsh | 0:a29bcf098632 | 9 | * furnished to do so, subject to the following conditions: |
jonmarsh | 0:a29bcf098632 | 10 | * |
jonmarsh | 0:a29bcf098632 | 11 | * The above copyright notice and this permission notice shall be included in |
jonmarsh | 0:a29bcf098632 | 12 | * all copies or substantial portions of the Software. |
jonmarsh | 0:a29bcf098632 | 13 | * |
jonmarsh | 0:a29bcf098632 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
jonmarsh | 0:a29bcf098632 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
jonmarsh | 0:a29bcf098632 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
jonmarsh | 0:a29bcf098632 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
jonmarsh | 0:a29bcf098632 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
jonmarsh | 0:a29bcf098632 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
jonmarsh | 0:a29bcf098632 | 20 | * THE SOFTWARE. |
jonmarsh | 0:a29bcf098632 | 21 | */ |
jonmarsh | 0:a29bcf098632 | 22 | |
jonmarsh | 0:a29bcf098632 | 23 | #ifndef M3PI_H |
jonmarsh | 0:a29bcf098632 | 24 | #define M3PI_H |
jonmarsh | 0:a29bcf098632 | 25 | |
jonmarsh | 0:a29bcf098632 | 26 | #include "mbed.h" |
jonmarsh | 0:a29bcf098632 | 27 | #include "platform.h" |
jonmarsh | 0:a29bcf098632 | 28 | |
jonmarsh | 0:a29bcf098632 | 29 | #ifdef MBED_RPC |
jonmarsh | 0:a29bcf098632 | 30 | #include "rpc.h" |
jonmarsh | 0:a29bcf098632 | 31 | #endif |
jonmarsh | 0:a29bcf098632 | 32 | |
jonmarsh | 0:a29bcf098632 | 33 | #define SEND_SIGNATURE 0x81 |
jonmarsh | 0:a29bcf098632 | 34 | #define SEND_RAW_SENSOR_VALUES 0x86 |
jonmarsh | 0:a29bcf098632 | 35 | #define SEND_CALIBRATED_SENSOR_VALUES 0x87 |
jonmarsh | 0:a29bcf098632 | 36 | #define SEND_TRIMPOT 0xB0 |
jonmarsh | 0:a29bcf098632 | 37 | #define SEND_BATTERY_MILLIVOLTS 0xB1 |
jonmarsh | 0:a29bcf098632 | 38 | #define DO_PLAY 0xB3 |
jonmarsh | 0:a29bcf098632 | 39 | #define PI_CALIBRATE 0xB4 |
jonmarsh | 0:a29bcf098632 | 40 | #define DO_CLEAR 0xB7 |
jonmarsh | 0:a29bcf098632 | 41 | #define DO_PRINT 0xB8 |
jonmarsh | 0:a29bcf098632 | 42 | #define DO_LCD_GOTO_XY 0xB9 |
jonmarsh | 0:a29bcf098632 | 43 | #define LINE_SENSORS_RESET_CALIBRATION 0xB5 |
jonmarsh | 0:a29bcf098632 | 44 | #define SEND_LINE_POSITION 0xB6 |
jonmarsh | 0:a29bcf098632 | 45 | #define AUTO_CALIBRATE 0xBA |
jonmarsh | 0:a29bcf098632 | 46 | #define SET_PID 0xBB |
jonmarsh | 0:a29bcf098632 | 47 | #define STOP_PID 0xBC |
jonmarsh | 0:a29bcf098632 | 48 | #define M1_FORWARD 0xC1 |
jonmarsh | 0:a29bcf098632 | 49 | #define M1_BACKWARD 0xC2 |
jonmarsh | 0:a29bcf098632 | 50 | #define M2_FORWARD 0xC5 |
jonmarsh | 0:a29bcf098632 | 51 | #define M2_BACKWARD 0xC6 |
jonmarsh | 0:a29bcf098632 | 52 | |
jonmarsh | 0:a29bcf098632 | 53 | |
jonmarsh | 0:a29bcf098632 | 54 | #define LINE_THRESHOLD 100 |
jonmarsh | 0:a29bcf098632 | 55 | |
jonmarsh | 0:a29bcf098632 | 56 | |
jonmarsh | 0:a29bcf098632 | 57 | /** m3pi control class |
jonmarsh | 0:a29bcf098632 | 58 | * |
jonmarsh | 0:a29bcf098632 | 59 | * Example: |
jonmarsh | 0:a29bcf098632 | 60 | * @code |
jonmarsh | 0:a29bcf098632 | 61 | * // Drive the m3pi forward, turn left, back, turn right, at half speed for half a second |
jonmarsh | 0:a29bcf098632 | 62 | |
jonmarsh | 0:a29bcf098632 | 63 | #include "mbed.h" |
jonmarsh | 0:a29bcf098632 | 64 | #include "m3pi.h" |
jonmarsh | 0:a29bcf098632 | 65 | |
jonmarsh | 0:a29bcf098632 | 66 | m3pi pi; |
jonmarsh | 0:a29bcf098632 | 67 | |
jonmarsh | 0:a29bcf098632 | 68 | int main() { |
jonmarsh | 0:a29bcf098632 | 69 | |
jonmarsh | 0:a29bcf098632 | 70 | wait(0.5); |
jonmarsh | 0:a29bcf098632 | 71 | |
jonmarsh | 0:a29bcf098632 | 72 | pi.forward(0.5); |
jonmarsh | 0:a29bcf098632 | 73 | wait (0.5); |
jonmarsh | 0:a29bcf098632 | 74 | pi.left(0.5); |
jonmarsh | 0:a29bcf098632 | 75 | wait (0.5); |
jonmarsh | 0:a29bcf098632 | 76 | pi.backward(0.5); |
jonmarsh | 0:a29bcf098632 | 77 | wait (0.5); |
jonmarsh | 0:a29bcf098632 | 78 | pi.right(0.5); |
jonmarsh | 0:a29bcf098632 | 79 | wait (0.5); |
jonmarsh | 0:a29bcf098632 | 80 | |
jonmarsh | 0:a29bcf098632 | 81 | pi.stop(); |
jonmarsh | 0:a29bcf098632 | 82 | |
jonmarsh | 0:a29bcf098632 | 83 | } |
jonmarsh | 0:a29bcf098632 | 84 | * @endcode |
jonmarsh | 0:a29bcf098632 | 85 | */ |
jonmarsh | 0:a29bcf098632 | 86 | class m3pi : public Stream { |
jonmarsh | 0:a29bcf098632 | 87 | |
jonmarsh | 0:a29bcf098632 | 88 | // Public functions |
jonmarsh | 0:a29bcf098632 | 89 | public: |
jonmarsh | 0:a29bcf098632 | 90 | |
jonmarsh | 0:a29bcf098632 | 91 | /** Create the m3pi object connected to the default pins |
jonmarsh | 0:a29bcf098632 | 92 | * |
jonmarsh | 0:a29bcf098632 | 93 | */ |
jonmarsh | 0:a29bcf098632 | 94 | m3pi(); |
jonmarsh | 0:a29bcf098632 | 95 | |
jonmarsh | 0:a29bcf098632 | 96 | |
jonmarsh | 0:a29bcf098632 | 97 | |
jonmarsh | 0:a29bcf098632 | 98 | |
jonmarsh | 0:a29bcf098632 | 99 | /** Create the m3pi object connected to the spesific pins |
jonmarsh | 0:a29bcf098632 | 100 | * |
jonmarsh | 0:a29bcf098632 | 101 | * @param nrst GPIO pin used for reset. Default is p23 |
jonmarsh | 0:a29bcf098632 | 102 | * @param tx Serial transmit pin. Default is p9 |
jonmarsh | 0:a29bcf098632 | 103 | * @param rx Serial receive pin. Default is p10 |
jonmarsh | 0:a29bcf098632 | 104 | */ |
jonmarsh | 0:a29bcf098632 | 105 | m3pi(PinName nrst, PinName tx, PinName rx); |
jonmarsh | 0:a29bcf098632 | 106 | |
jonmarsh | 0:a29bcf098632 | 107 | |
jonmarsh | 0:a29bcf098632 | 108 | |
jonmarsh | 0:a29bcf098632 | 109 | /** Force a hardware reset of the 3pi |
jonmarsh | 0:a29bcf098632 | 110 | */ |
jonmarsh | 0:a29bcf098632 | 111 | void reset (void); |
jonmarsh | 0:a29bcf098632 | 112 | |
jonmarsh | 0:a29bcf098632 | 113 | /** Directly control the speed and direction of the left motor |
jonmarsh | 0:a29bcf098632 | 114 | * |
jonmarsh | 0:a29bcf098632 | 115 | * @param speed A normalised number -1.0 - 1.0 represents the full range. |
jonmarsh | 0:a29bcf098632 | 116 | */ |
jonmarsh | 0:a29bcf098632 | 117 | void left_motor (float speed); |
jonmarsh | 0:a29bcf098632 | 118 | |
jonmarsh | 0:a29bcf098632 | 119 | /** Directly control the speed and direction of the right motor |
jonmarsh | 0:a29bcf098632 | 120 | * |
jonmarsh | 0:a29bcf098632 | 121 | * @param speed A normalised number -1.0 - 1.0 represents the full range. |
jonmarsh | 0:a29bcf098632 | 122 | */ |
jonmarsh | 0:a29bcf098632 | 123 | void right_motor (float speed); |
jonmarsh | 0:a29bcf098632 | 124 | |
jonmarsh | 0:a29bcf098632 | 125 | /** Drive both motors forward as the same speed |
jonmarsh | 0:a29bcf098632 | 126 | * |
jonmarsh | 0:a29bcf098632 | 127 | * @param speed A normalised number 0 - 1.0 represents the full range. |
jonmarsh | 0:a29bcf098632 | 128 | */ |
jonmarsh | 0:a29bcf098632 | 129 | void forward (float speed); |
jonmarsh | 0:a29bcf098632 | 130 | |
jonmarsh | 0:a29bcf098632 | 131 | /** Drive both motors backward as the same speed |
jonmarsh | 0:a29bcf098632 | 132 | * |
jonmarsh | 0:a29bcf098632 | 133 | * @param speed A normalised number 0 - 1.0 represents the full range. |
jonmarsh | 0:a29bcf098632 | 134 | */ |
jonmarsh | 0:a29bcf098632 | 135 | void backward (float speed); |
jonmarsh | 0:a29bcf098632 | 136 | |
jonmarsh | 0:a29bcf098632 | 137 | /** Drive left motor backwards and right motor forwards at the same speed to turn on the spot |
jonmarsh | 0:a29bcf098632 | 138 | * |
jonmarsh | 0:a29bcf098632 | 139 | * @param speed A normalised number 0 - 1.0 represents the full range. |
jonmarsh | 0:a29bcf098632 | 140 | */ |
jonmarsh | 0:a29bcf098632 | 141 | void left (float speed); |
jonmarsh | 0:a29bcf098632 | 142 | |
jonmarsh | 0:a29bcf098632 | 143 | /** Drive left motor forward and right motor backwards at the same speed to turn on the spot |
jonmarsh | 0:a29bcf098632 | 144 | * @param speed A normalised number 0 - 1.0 represents the full range. |
jonmarsh | 0:a29bcf098632 | 145 | */ |
jonmarsh | 0:a29bcf098632 | 146 | void right (float speed); |
jonmarsh | 0:a29bcf098632 | 147 | |
jonmarsh | 0:a29bcf098632 | 148 | /** Stop both motors |
jonmarsh | 0:a29bcf098632 | 149 | * |
jonmarsh | 0:a29bcf098632 | 150 | */ |
jonmarsh | 0:a29bcf098632 | 151 | void stop (void); |
jonmarsh | 0:a29bcf098632 | 152 | |
jonmarsh | 0:a29bcf098632 | 153 | /** Read the voltage of the potentiometer on the 3pi |
jonmarsh | 0:a29bcf098632 | 154 | * @returns voltage as a float |
jonmarsh | 0:a29bcf098632 | 155 | * |
jonmarsh | 0:a29bcf098632 | 156 | */ |
jonmarsh | 0:a29bcf098632 | 157 | float pot_voltage(void); |
jonmarsh | 0:a29bcf098632 | 158 | |
jonmarsh | 0:a29bcf098632 | 159 | /** Read the battery voltage on the 3pi |
jonmarsh | 0:a29bcf098632 | 160 | * @returns battery voltage as a float |
jonmarsh | 0:a29bcf098632 | 161 | */ |
jonmarsh | 0:a29bcf098632 | 162 | float battery(void); |
jonmarsh | 0:a29bcf098632 | 163 | |
jonmarsh | 0:a29bcf098632 | 164 | /** Read the position of the detected line |
jonmarsh | 0:a29bcf098632 | 165 | * @returns position as A normalised number -1.0 - 1.0 represents the full range. |
jonmarsh | 0:a29bcf098632 | 166 | * -1.0 means line is on the left, or the line has been lost |
jonmarsh | 0:a29bcf098632 | 167 | * 0.0 means the line is in the middle |
jonmarsh | 0:a29bcf098632 | 168 | * 1.0 means the line is on the right |
jonmarsh | 0:a29bcf098632 | 169 | */ |
jonmarsh | 0:a29bcf098632 | 170 | float line_position (void); |
jonmarsh | 0:a29bcf098632 | 171 | |
jonmarsh | 0:a29bcf098632 | 172 | /** Get the raw sensor values |
jonmarsh | 0:a29bcf098632 | 173 | * |
jonmarsh | 0:a29bcf098632 | 174 | * @param int* A pointer to a interger array |
jonmarsh | 0:a29bcf098632 | 175 | */ |
jonmarsh | 0:a29bcf098632 | 176 | void get_raw_sensors(int* values); |
jonmarsh | 0:a29bcf098632 | 177 | |
jonmarsh | 0:a29bcf098632 | 178 | /** Get the calibrated sensor values |
jonmarsh | 0:a29bcf098632 | 179 | * |
jonmarsh | 0:a29bcf098632 | 180 | * @param int* A pointer to an float array |
jonmarsh | 0:a29bcf098632 | 181 | */ |
jonmarsh | 0:a29bcf098632 | 182 | void get_calibrated_sensors(float* values); |
jonmarsh | 0:a29bcf098632 | 183 | |
jonmarsh | 0:a29bcf098632 | 184 | /** Records current levels as white, values used to detect a line |
jonmarsh | 0:a29bcf098632 | 185 | * |
jonmarsh | 0:a29bcf098632 | 186 | */ |
jonmarsh | 0:a29bcf098632 | 187 | void get_white_levels(); |
jonmarsh | 0:a29bcf098632 | 188 | |
jonmarsh | 0:a29bcf098632 | 189 | /** Gets the line state |
jonmarsh | 0:a29bcf098632 | 190 | * |
jonmarsh | 0:a29bcf098632 | 191 | * @returns The line status: 0 means no line, -1 means line at back two sensors, 1 means line at front sensors |
jonmarsh | 0:a29bcf098632 | 192 | */ |
jonmarsh | 0:a29bcf098632 | 193 | |
jonmarsh | 0:a29bcf098632 | 194 | int is_line(); |
jonmarsh | 0:a29bcf098632 | 195 | |
jonmarsh | 0:a29bcf098632 | 196 | |
jonmarsh | 0:a29bcf098632 | 197 | /** Calibrate the sensors. This turns the robot left then right, looking for a line |
jonmarsh | 0:a29bcf098632 | 198 | * |
jonmarsh | 0:a29bcf098632 | 199 | */ |
jonmarsh | 0:a29bcf098632 | 200 | char sensor_auto_calibrate (void); |
jonmarsh | 0:a29bcf098632 | 201 | |
jonmarsh | 0:a29bcf098632 | 202 | /** Set calibration manually to the current settings. |
jonmarsh | 0:a29bcf098632 | 203 | * |
jonmarsh | 0:a29bcf098632 | 204 | */ |
jonmarsh | 0:a29bcf098632 | 205 | void calibrate(void); |
jonmarsh | 0:a29bcf098632 | 206 | |
jonmarsh | 0:a29bcf098632 | 207 | /** Clear the current calibration settings |
jonmarsh | 0:a29bcf098632 | 208 | * |
jonmarsh | 0:a29bcf098632 | 209 | */ |
jonmarsh | 0:a29bcf098632 | 210 | void reset_calibration (void); |
jonmarsh | 0:a29bcf098632 | 211 | |
jonmarsh | 0:a29bcf098632 | 212 | void PID_start(int max_speed, int a, int b, int c, int d); |
jonmarsh | 0:a29bcf098632 | 213 | |
jonmarsh | 0:a29bcf098632 | 214 | void PID_stop(); |
jonmarsh | 0:a29bcf098632 | 215 | |
jonmarsh | 0:a29bcf098632 | 216 | /** Write to the 8 LEDs |
jonmarsh | 0:a29bcf098632 | 217 | * |
jonmarsh | 0:a29bcf098632 | 218 | * @param leds An 8 bit value to put on the LEDs |
jonmarsh | 0:a29bcf098632 | 219 | */ |
jonmarsh | 0:a29bcf098632 | 220 | void leds(int val); |
jonmarsh | 0:a29bcf098632 | 221 | |
jonmarsh | 0:a29bcf098632 | 222 | /** Locate the cursor on the 8x2 LCD |
jonmarsh | 0:a29bcf098632 | 223 | * |
jonmarsh | 0:a29bcf098632 | 224 | * @param x The horizontal position, from 0 to 7 |
jonmarsh | 0:a29bcf098632 | 225 | * @param y The vertical position, from 0 to 1 |
jonmarsh | 0:a29bcf098632 | 226 | */ |
jonmarsh | 0:a29bcf098632 | 227 | void locate(int x, int y); |
jonmarsh | 0:a29bcf098632 | 228 | |
jonmarsh | 0:a29bcf098632 | 229 | /** Clear the LCD |
jonmarsh | 0:a29bcf098632 | 230 | * |
jonmarsh | 0:a29bcf098632 | 231 | */ |
jonmarsh | 0:a29bcf098632 | 232 | void cls(void); |
jonmarsh | 0:a29bcf098632 | 233 | |
jonmarsh | 0:a29bcf098632 | 234 | /** Send a character directly to the 3pi serial interface |
jonmarsh | 0:a29bcf098632 | 235 | * @param c The character to send to the 3pi |
jonmarsh | 0:a29bcf098632 | 236 | */ |
jonmarsh | 0:a29bcf098632 | 237 | int putc(int c); |
jonmarsh | 0:a29bcf098632 | 238 | |
jonmarsh | 0:a29bcf098632 | 239 | /** Receive a character directly to the 3pi serial interface |
jonmarsh | 0:a29bcf098632 | 240 | * @returns c The character received from the 3pi |
jonmarsh | 0:a29bcf098632 | 241 | */ |
jonmarsh | 0:a29bcf098632 | 242 | int getc(); |
jonmarsh | 0:a29bcf098632 | 243 | |
jonmarsh | 0:a29bcf098632 | 244 | /** Send a string buffer to the 3pi serial interface |
jonmarsh | 0:a29bcf098632 | 245 | * @param text A pointer to a char array |
jonmarsh | 0:a29bcf098632 | 246 | * @param int The character to send to the 3pi |
jonmarsh | 0:a29bcf098632 | 247 | */ |
jonmarsh | 0:a29bcf098632 | 248 | int print(char* text, int length); |
jonmarsh | 0:a29bcf098632 | 249 | |
jonmarsh | 0:a29bcf098632 | 250 | |
jonmarsh | 0:a29bcf098632 | 251 | |
jonmarsh | 0:a29bcf098632 | 252 | |
jonmarsh | 0:a29bcf098632 | 253 | #ifdef MBED_RPC |
jonmarsh | 0:a29bcf098632 | 254 | virtual const struct rpc_method *get_rpc_methods(); |
jonmarsh | 0:a29bcf098632 | 255 | #endif |
jonmarsh | 0:a29bcf098632 | 256 | |
jonmarsh | 0:a29bcf098632 | 257 | private : |
jonmarsh | 0:a29bcf098632 | 258 | |
jonmarsh | 0:a29bcf098632 | 259 | DigitalOut _nrst; |
jonmarsh | 0:a29bcf098632 | 260 | Serial _ser; |
jonmarsh | 0:a29bcf098632 | 261 | |
jonmarsh | 0:a29bcf098632 | 262 | void motor (int motor, float speed); |
jonmarsh | 0:a29bcf098632 | 263 | virtual int _putc(int c); |
jonmarsh | 0:a29bcf098632 | 264 | virtual int _getc(); |
jonmarsh | 0:a29bcf098632 | 265 | |
jonmarsh | 0:a29bcf098632 | 266 | int raw_white_levels[5]; |
jonmarsh | 0:a29bcf098632 | 267 | |
jonmarsh | 0:a29bcf098632 | 268 | }; |
jonmarsh | 0:a29bcf098632 | 269 | |
jonmarsh | 0:a29bcf098632 | 270 | #endif |