Line follower and object avoider

Dependencies:   mbed SHARPIR

Committer:
nepol77
Date:
Thu Apr 12 14:37:54 2012 +0000
Revision:
0:df7d60d7a6ee
Hi guys, i will put the pictures on very soon!! Thanks for looking

Who changed what in which revision?

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