m3pi

Committer:
microsat
Date:
Tue Nov 23 06:48:27 2010 +0000
Revision:
0:fda628f82e5f
added play func

Who changed what in which revision?

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