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