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