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