Library for the m3pi robot. This works with a Pololu 3pi robot with the Serial Slave firmware, and exposes and API. This is a fork of cstyles m3pi library.

Dependents:   newlib PID Robot WarehouseBot1 ... more

Fork of m3pi_ng by Nikolas Goldin

Committer:
ngoldin
Date:
Mon May 13 10:54:18 2013 +0000
Revision:
9:deb9547909c3
Child:
10:f89d2a3a9ed2
added read out calibrated sensors function

Who changed what in which revision?

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