Adds ability to play a tune and to directly read sensor values

Dependents:   MusicTest Hail_Music

Fork of m3pimaze by Jon Marsh

Committer:
jonmarsh
Date:
Thu Mar 03 10:07:21 2011 +0000
Revision:
0:e1dac380452c

        

Who changed what in which revision?

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