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