Allows the M3Pi to be used as a Sumo robot, using the sharp 100 distance sensors on the front. Run away strategy

Dependencies:   mbed

Committer:
jonmarsh
Date:
Mon Jun 18 09:38:17 2012 +0000
Revision:
0:11d0f3e0d1ad
Not tested

Who changed what in which revision?

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