m3pi library with fixes for left and right motor specific functions and additional functionality like play

Committer:
ryantm
Date:
Thu Jun 02 19:14:14 2011 +0000
Revision:
0:a6a3ac2e729d
Child:
2:68c904e3f9d0
adds a play method to m3pi class

Who changed what in which revision?

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