Dependents:   LPC1768_m3pi_LineFollower_PID

Committer:
donde
Date:
Tue Nov 15 02:14:45 2011 +0000
Revision:
0:4d063fa5d347

        

Who changed what in which revision?

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