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.
Fork of m3pi_ng by
m3pi_ng.h@11:7bfb33432791, 2013-05-17 (annotated)
- Committer:
- ngoldin
- Date:
- Fri May 17 14:04:12 2013 +0000
- Revision:
- 11:7bfb33432791
- Parent:
- 10:f89d2a3a9ed2
slowstop
Who changed what in which revision?
User | Revision | Line number | New 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 | 10:f89d2a3a9ed2 | 108 | * needs a length 6 char array to write into |
ngoldin | 9:deb9547909c3 | 109 | */ |
ngoldin | 10:f89d2a3a9ed2 | 110 | void signature (char *); |
ngoldin | 9:deb9547909c3 | 111 | |
ngoldin | 9:deb9547909c3 | 112 | /** Get the raw sensor values |
ngoldin | 10:f89d2a3a9ed2 | 113 | * needs a int[5] array to write into |
ngoldin | 10:f89d2a3a9ed2 | 114 | */ |
ngoldin | 10:f89d2a3a9ed2 | 115 | void raw_sensor (int *); |
ngoldin | 9:deb9547909c3 | 116 | |
ngoldin | 9:deb9547909c3 | 117 | /** Get the calibrated sensor values |
ngoldin | 10:f89d2a3a9ed2 | 118 | * needs a int[5] array to write into |
ngoldin | 10:f89d2a3a9ed2 | 119 | */ |
ngoldin | 10:f89d2a3a9ed2 | 120 | void calibrated_sensor (int *); |
ngoldin | 9:deb9547909c3 | 121 | |
ngoldin | 11:7bfb33432791 | 122 | void slowstop (float speed, float waittime, int steps); |
ngoldin | 11:7bfb33432791 | 123 | |
ngoldin | 9:deb9547909c3 | 124 | /** Play music using the buzzer |
ngoldin | 9:deb9547909c3 | 125 | */ |
ngoldin | 9:deb9547909c3 | 126 | void playtune (char* text, int length); |
ngoldin | 9:deb9547909c3 | 127 | |
ngoldin | 9:deb9547909c3 | 128 | /** Force a hardware reset of the 3pi |
ngoldin | 9:deb9547909c3 | 129 | */ |
ngoldin | 9:deb9547909c3 | 130 | void reset (void); |
ngoldin | 9:deb9547909c3 | 131 | |
ngoldin | 9:deb9547909c3 | 132 | /** Directly control the speed and direction of the left motor |
ngoldin | 9:deb9547909c3 | 133 | * |
ngoldin | 9:deb9547909c3 | 134 | * @param speed A normalised number -1.0 - 1.0 represents the full range. |
ngoldin | 9:deb9547909c3 | 135 | */ |
ngoldin | 9:deb9547909c3 | 136 | void left_motor (float speed); |
ngoldin | 9:deb9547909c3 | 137 | |
ngoldin | 9:deb9547909c3 | 138 | /** Directly control the speed and direction of the right motor |
ngoldin | 9:deb9547909c3 | 139 | * |
ngoldin | 9:deb9547909c3 | 140 | * @param speed A normalised number -1.0 - 1.0 represents the full range. |
ngoldin | 9:deb9547909c3 | 141 | */ |
ngoldin | 9:deb9547909c3 | 142 | void right_motor (float speed); |
ngoldin | 9:deb9547909c3 | 143 | |
ngoldin | 9:deb9547909c3 | 144 | /** Drive both motors forward as the same speed |
ngoldin | 9:deb9547909c3 | 145 | * |
ngoldin | 9:deb9547909c3 | 146 | * @param speed A normalised number 0 - 1.0 represents the full range. |
ngoldin | 9:deb9547909c3 | 147 | */ |
ngoldin | 9:deb9547909c3 | 148 | void forward (float speed); |
ngoldin | 9:deb9547909c3 | 149 | |
ngoldin | 9:deb9547909c3 | 150 | /** Drive both motors backward as the same speed |
ngoldin | 9:deb9547909c3 | 151 | * |
ngoldin | 9:deb9547909c3 | 152 | * @param speed A normalised number 0 - 1.0 represents the full range. |
ngoldin | 9:deb9547909c3 | 153 | */ |
ngoldin | 9:deb9547909c3 | 154 | void backward (float speed); |
ngoldin | 9:deb9547909c3 | 155 | |
ngoldin | 9:deb9547909c3 | 156 | /** Drive left motor backwards and right motor forwards at the same speed to turn on the spot |
ngoldin | 9:deb9547909c3 | 157 | * |
ngoldin | 9:deb9547909c3 | 158 | * @param speed A normalised number 0 - 1.0 represents the full range. |
ngoldin | 9:deb9547909c3 | 159 | */ |
ngoldin | 9:deb9547909c3 | 160 | void left (float speed); |
ngoldin | 9:deb9547909c3 | 161 | |
ngoldin | 9:deb9547909c3 | 162 | /** Drive left motor forward and right motor backwards at the same speed to turn on the spot |
ngoldin | 9:deb9547909c3 | 163 | * @param speed A normalised number 0 - 1.0 represents the full range. |
ngoldin | 9:deb9547909c3 | 164 | */ |
ngoldin | 9:deb9547909c3 | 165 | void right (float speed); |
ngoldin | 9:deb9547909c3 | 166 | |
ngoldin | 9:deb9547909c3 | 167 | /** Stop both motors |
ngoldin | 9:deb9547909c3 | 168 | * |
ngoldin | 9:deb9547909c3 | 169 | */ |
ngoldin | 9:deb9547909c3 | 170 | void stop (void); |
ngoldin | 9:deb9547909c3 | 171 | |
ngoldin | 9:deb9547909c3 | 172 | /** Read the voltage of the potentiometer on the 3pi |
ngoldin | 9:deb9547909c3 | 173 | * @returns voltage as a float |
ngoldin | 9:deb9547909c3 | 174 | * |
ngoldin | 9:deb9547909c3 | 175 | */ |
ngoldin | 9:deb9547909c3 | 176 | float pot_voltage(void); |
ngoldin | 9:deb9547909c3 | 177 | |
ngoldin | 9:deb9547909c3 | 178 | /** Read the battery voltage on the 3pi |
ngoldin | 9:deb9547909c3 | 179 | * @returns battery voltage as a float |
ngoldin | 9:deb9547909c3 | 180 | */ |
ngoldin | 9:deb9547909c3 | 181 | float battery(void); |
ngoldin | 9:deb9547909c3 | 182 | |
ngoldin | 9:deb9547909c3 | 183 | /** Read the position of the detected line |
ngoldin | 9:deb9547909c3 | 184 | * @returns position as A normalised number -1.0 - 1.0 represents the full range. |
ngoldin | 9:deb9547909c3 | 185 | * -1.0 means line is on the left, or the line has been lost |
ngoldin | 9:deb9547909c3 | 186 | * 0.0 means the line is in the middle |
ngoldin | 9:deb9547909c3 | 187 | * 1.0 means the line is on the right |
ngoldin | 9:deb9547909c3 | 188 | */ |
ngoldin | 9:deb9547909c3 | 189 | float line_position (void); |
ngoldin | 9:deb9547909c3 | 190 | |
ngoldin | 9:deb9547909c3 | 191 | |
ngoldin | 9:deb9547909c3 | 192 | /** Calibrate the sensors. This turns the robot left then right, looking for a line |
ngoldin | 9:deb9547909c3 | 193 | * |
ngoldin | 9:deb9547909c3 | 194 | */ |
ngoldin | 9:deb9547909c3 | 195 | char sensor_auto_calibrate (void); |
ngoldin | 9:deb9547909c3 | 196 | |
ngoldin | 9:deb9547909c3 | 197 | /** Set calibration manually to the current settings. |
ngoldin | 9:deb9547909c3 | 198 | * |
ngoldin | 9:deb9547909c3 | 199 | */ |
ngoldin | 9:deb9547909c3 | 200 | void calibrate(void); |
ngoldin | 9:deb9547909c3 | 201 | |
ngoldin | 9:deb9547909c3 | 202 | /** Clear the current calibration settings |
ngoldin | 9:deb9547909c3 | 203 | * |
ngoldin | 9:deb9547909c3 | 204 | */ |
ngoldin | 9:deb9547909c3 | 205 | void reset_calibration (void); |
ngoldin | 9:deb9547909c3 | 206 | |
ngoldin | 9:deb9547909c3 | 207 | void PID_start(int max_speed, int a, int b, int c, int d); |
ngoldin | 9:deb9547909c3 | 208 | |
ngoldin | 9:deb9547909c3 | 209 | void PID_stop(); |
ngoldin | 9:deb9547909c3 | 210 | |
ngoldin | 9:deb9547909c3 | 211 | /** Write to the 8 LEDs |
ngoldin | 9:deb9547909c3 | 212 | * |
ngoldin | 9:deb9547909c3 | 213 | * @param leds An 8 bit value to put on the LEDs |
ngoldin | 9:deb9547909c3 | 214 | */ |
ngoldin | 9:deb9547909c3 | 215 | void leds(int val); |
ngoldin | 9:deb9547909c3 | 216 | |
ngoldin | 9:deb9547909c3 | 217 | /** Locate the cursor on the 8x2 LCD |
ngoldin | 9:deb9547909c3 | 218 | * |
ngoldin | 9:deb9547909c3 | 219 | * @param x The horizontal position, from 0 to 7 |
ngoldin | 9:deb9547909c3 | 220 | * @param y The vertical position, from 0 to 1 |
ngoldin | 9:deb9547909c3 | 221 | */ |
ngoldin | 9:deb9547909c3 | 222 | void locate(int x, int y); |
ngoldin | 9:deb9547909c3 | 223 | |
ngoldin | 9:deb9547909c3 | 224 | /** Clear the LCD |
ngoldin | 9:deb9547909c3 | 225 | * |
ngoldin | 9:deb9547909c3 | 226 | */ |
ngoldin | 9:deb9547909c3 | 227 | void cls(void); |
ngoldin | 9:deb9547909c3 | 228 | |
ngoldin | 9:deb9547909c3 | 229 | /** Send a character directly to the 3pi serial interface |
ngoldin | 9:deb9547909c3 | 230 | * @param c The character to send to the 3pi |
ngoldin | 9:deb9547909c3 | 231 | */ |
ngoldin | 9:deb9547909c3 | 232 | int putc(int c); |
ngoldin | 9:deb9547909c3 | 233 | |
ngoldin | 9:deb9547909c3 | 234 | /** Receive a character directly to the 3pi serial interface |
ngoldin | 9:deb9547909c3 | 235 | * @returns c The character received from the 3pi |
ngoldin | 9:deb9547909c3 | 236 | */ |
ngoldin | 9:deb9547909c3 | 237 | int getc(); |
ngoldin | 9:deb9547909c3 | 238 | |
ngoldin | 9:deb9547909c3 | 239 | /** Send a string buffer to the 3pi serial interface |
ngoldin | 9:deb9547909c3 | 240 | * @param text A pointer to a char array |
ngoldin | 9:deb9547909c3 | 241 | * @param int The character to send to the 3pi |
ngoldin | 9:deb9547909c3 | 242 | */ |
ngoldin | 9:deb9547909c3 | 243 | int print(char* text, int length); |
ngoldin | 9:deb9547909c3 | 244 | |
ngoldin | 9:deb9547909c3 | 245 | #ifdef MBED_RPC |
ngoldin | 9:deb9547909c3 | 246 | virtual const struct rpc_method *get_rpc_methods(); |
ngoldin | 9:deb9547909c3 | 247 | #endif |
ngoldin | 9:deb9547909c3 | 248 | |
ngoldin | 9:deb9547909c3 | 249 | private : |
ngoldin | 9:deb9547909c3 | 250 | |
ngoldin | 9:deb9547909c3 | 251 | DigitalOut _nrst; |
ngoldin | 9:deb9547909c3 | 252 | Serial _ser; |
ngoldin | 9:deb9547909c3 | 253 | |
ngoldin | 9:deb9547909c3 | 254 | void motor (int motor, float speed); |
ngoldin | 9:deb9547909c3 | 255 | virtual int _putc(int c); |
ngoldin | 9:deb9547909c3 | 256 | virtual int _getc(); |
ngoldin | 9:deb9547909c3 | 257 | |
ngoldin | 9:deb9547909c3 | 258 | }; |
ngoldin | 9:deb9547909c3 | 259 | |
ngoldin | 9:deb9547909c3 | 260 | #endif |