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