Testcode for LVC Robot club

Dependencies:   mbed

Committer:
jonmarsh
Date:
Mon Apr 23 09:32:50 2012 +0000
Revision:
1:fe52aa73cd6a
Parent:
0:f4922a2a0292

        

Who changed what in which revision?

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