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

Dependents:   m3pi_MazeSolver m3pi_MazeSolverLVC mbeddedNets hiworld

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 /* m3pi Library
jonmarsh 0:e1dac380452c 2 *
jonmarsh 0:e1dac380452c 3 * Copyright (c) 2007-2010 cstyles
jonmarsh 0:e1dac380452c 4 *
jonmarsh 0:e1dac380452c 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
jonmarsh 0:e1dac380452c 6 * of this software and associated documentation files (the "Software"), to deal
jonmarsh 0:e1dac380452c 7 * in the Software without restriction, including without limitation the rights
jonmarsh 0:e1dac380452c 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jonmarsh 0:e1dac380452c 9 * copies of the Software, and to permit persons to whom the Software is
jonmarsh 0:e1dac380452c 10 * furnished to do so, subject to the following conditions:
jonmarsh 0:e1dac380452c 11 *
jonmarsh 0:e1dac380452c 12 * The above copyright notice and this permission notice shall be included in
jonmarsh 0:e1dac380452c 13 * all copies or substantial portions of the Software.
jonmarsh 0:e1dac380452c 14 *
jonmarsh 0:e1dac380452c 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jonmarsh 0:e1dac380452c 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jonmarsh 0:e1dac380452c 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jonmarsh 0:e1dac380452c 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jonmarsh 0:e1dac380452c 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jonmarsh 0:e1dac380452c 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jonmarsh 0:e1dac380452c 21 * THE SOFTWARE.
jonmarsh 0:e1dac380452c 22 */
jonmarsh 0:e1dac380452c 23
jonmarsh 0:e1dac380452c 24 #include "mbed.h"
jonmarsh 0:e1dac380452c 25 #include "m3pimaze.h"
jonmarsh 0:e1dac380452c 26
jonmarsh 0:e1dac380452c 27 m3pi::m3pi(PinName nrst, PinName tx, PinName rx) : Stream("m3pi"), _nrst(nrst), _ser(tx, rx), _leds(p20,p19,p18,p17,p16,p15,p14,p13) {
jonmarsh 0:e1dac380452c 28 _leds = 0;
jonmarsh 0:e1dac380452c 29 _ser.baud(115200);
jonmarsh 0:e1dac380452c 30 reset();
jonmarsh 0:e1dac380452c 31 }
jonmarsh 0:e1dac380452c 32
jonmarsh 0:e1dac380452c 33 void m3pi::reset () {
jonmarsh 0:e1dac380452c 34 _nrst = 0;
jonmarsh 0:e1dac380452c 35 wait (0.01);
jonmarsh 0:e1dac380452c 36 _nrst = 1;
jonmarsh 0:e1dac380452c 37 wait (0.1);
jonmarsh 0:e1dac380452c 38 }
jonmarsh 0:e1dac380452c 39
jonmarsh 0:e1dac380452c 40 void m3pi::left_motor (float speed) {
jonmarsh 0:e1dac380452c 41 motor(0,speed);
jonmarsh 0:e1dac380452c 42 }
jonmarsh 0:e1dac380452c 43
jonmarsh 0:e1dac380452c 44 void m3pi::right_motor (float speed) {
jonmarsh 0:e1dac380452c 45 motor(1,speed);
jonmarsh 0:e1dac380452c 46 }
jonmarsh 0:e1dac380452c 47
jonmarsh 0:e1dac380452c 48 void m3pi::forward (float speed) {
jonmarsh 0:e1dac380452c 49 motor(0,speed);
jonmarsh 0:e1dac380452c 50 motor(1,speed);
jonmarsh 0:e1dac380452c 51 }
jonmarsh 0:e1dac380452c 52
jonmarsh 0:e1dac380452c 53 void m3pi::backward (float speed) {
jonmarsh 0:e1dac380452c 54 motor(0,-1.0*speed);
jonmarsh 0:e1dac380452c 55 motor(1,-1.0*speed);
jonmarsh 0:e1dac380452c 56 }
jonmarsh 0:e1dac380452c 57
jonmarsh 0:e1dac380452c 58 void m3pi::left (float speed) {
jonmarsh 0:e1dac380452c 59 motor(0,speed);
jonmarsh 0:e1dac380452c 60 motor(1,-1.0*speed);
jonmarsh 0:e1dac380452c 61 }
jonmarsh 0:e1dac380452c 62
jonmarsh 0:e1dac380452c 63 void m3pi::right (float speed) {
jonmarsh 0:e1dac380452c 64 motor(0,-1.0*speed);
jonmarsh 0:e1dac380452c 65 motor(1,speed);
jonmarsh 0:e1dac380452c 66 }
jonmarsh 0:e1dac380452c 67
jonmarsh 0:e1dac380452c 68 void m3pi::stop (void) {
jonmarsh 0:e1dac380452c 69 motor(0,0.0);
jonmarsh 0:e1dac380452c 70 motor(1,0.0);
jonmarsh 0:e1dac380452c 71 }
jonmarsh 0:e1dac380452c 72
jonmarsh 0:e1dac380452c 73 void m3pi::motor (int motor, float speed) {
jonmarsh 0:e1dac380452c 74 char opcode = 0x0;
jonmarsh 0:e1dac380452c 75 if (speed > 0.0) {
jonmarsh 0:e1dac380452c 76 if (motor==1)
jonmarsh 0:e1dac380452c 77 opcode = M1_FORWARD;
jonmarsh 0:e1dac380452c 78 else
jonmarsh 0:e1dac380452c 79 opcode = M2_FORWARD;
jonmarsh 0:e1dac380452c 80 } else {
jonmarsh 0:e1dac380452c 81 if (motor==1)
jonmarsh 0:e1dac380452c 82 opcode = M1_BACKWARD;
jonmarsh 0:e1dac380452c 83 else
jonmarsh 0:e1dac380452c 84 opcode = M2_BACKWARD;
jonmarsh 0:e1dac380452c 85 }
jonmarsh 0:e1dac380452c 86 unsigned char arg = 0x7f * abs(speed);
jonmarsh 0:e1dac380452c 87
jonmarsh 0:e1dac380452c 88 _ser.putc(opcode);
jonmarsh 0:e1dac380452c 89 _ser.putc(arg);
jonmarsh 0:e1dac380452c 90 }
jonmarsh 0:e1dac380452c 91
jonmarsh 0:e1dac380452c 92 float m3pi::battery() {
jonmarsh 0:e1dac380452c 93 _ser.putc(SEND_BATTERY_MILLIVOLTS);
jonmarsh 0:e1dac380452c 94 char lowbyte = _ser.getc();
jonmarsh 0:e1dac380452c 95 char hibyte = _ser.getc();
jonmarsh 0:e1dac380452c 96 float v = ((lowbyte + (hibyte << 8))/1000.0);
jonmarsh 0:e1dac380452c 97 return(v);
jonmarsh 0:e1dac380452c 98 }
jonmarsh 0:e1dac380452c 99
jonmarsh 0:e1dac380452c 100 float m3pi::line_position() {
jonmarsh 0:e1dac380452c 101 int pos = 0;
jonmarsh 0:e1dac380452c 102 _ser.putc(SEND_LINE_POSITION);
jonmarsh 0:e1dac380452c 103 pos = _ser.getc();
jonmarsh 0:e1dac380452c 104 pos += _ser.getc() << 8;
jonmarsh 0:e1dac380452c 105
jonmarsh 0:e1dac380452c 106 float fpos = ((float)pos - 2048.0)/2048.0;
jonmarsh 0:e1dac380452c 107 return(fpos);
jonmarsh 0:e1dac380452c 108 }
jonmarsh 0:e1dac380452c 109
jonmarsh 0:e1dac380452c 110 char m3pi::sensor_auto_calibrate() {
jonmarsh 0:e1dac380452c 111 _ser.putc(AUTO_CALIBRATE);
jonmarsh 0:e1dac380452c 112 return(_ser.getc());
jonmarsh 0:e1dac380452c 113 }
jonmarsh 0:e1dac380452c 114
jonmarsh 0:e1dac380452c 115
jonmarsh 0:e1dac380452c 116 void m3pi::calibrate(void) {
jonmarsh 0:e1dac380452c 117 _ser.putc(PI_CALIBRATE);
jonmarsh 0:e1dac380452c 118 }
jonmarsh 0:e1dac380452c 119
jonmarsh 0:e1dac380452c 120 void m3pi::reset_calibration() {
jonmarsh 0:e1dac380452c 121 _ser.putc(LINE_SENSORS_RESET_CALIBRATION);
jonmarsh 0:e1dac380452c 122 }
jonmarsh 0:e1dac380452c 123
jonmarsh 0:e1dac380452c 124 void m3pi::PID_start(int max_speed, int a, int b, int c, int d) {
jonmarsh 0:e1dac380452c 125 _ser.putc(max_speed);
jonmarsh 0:e1dac380452c 126 _ser.putc(a);
jonmarsh 0:e1dac380452c 127 _ser.putc(b);
jonmarsh 0:e1dac380452c 128 _ser.putc(c);
jonmarsh 0:e1dac380452c 129 _ser.putc(d);
jonmarsh 0:e1dac380452c 130 }
jonmarsh 0:e1dac380452c 131
jonmarsh 0:e1dac380452c 132 void m3pi::PID_stop() {
jonmarsh 0:e1dac380452c 133 _ser.putc(STOP_PID);
jonmarsh 0:e1dac380452c 134 }
jonmarsh 0:e1dac380452c 135
jonmarsh 0:e1dac380452c 136 float m3pi::pot_voltage(void) {
jonmarsh 0:e1dac380452c 137 int volt = 0;
jonmarsh 0:e1dac380452c 138 _ser.putc(SEND_TRIMPOT);
jonmarsh 0:e1dac380452c 139 volt = _ser.getc();
jonmarsh 0:e1dac380452c 140 volt += _ser.getc() << 8;
jonmarsh 0:e1dac380452c 141 return(volt);
jonmarsh 0:e1dac380452c 142 }
jonmarsh 0:e1dac380452c 143
jonmarsh 0:e1dac380452c 144
jonmarsh 0:e1dac380452c 145 void m3pi::leds(int val) {
jonmarsh 0:e1dac380452c 146 _leds = val;
jonmarsh 0:e1dac380452c 147 }
jonmarsh 0:e1dac380452c 148
jonmarsh 0:e1dac380452c 149
jonmarsh 0:e1dac380452c 150 void m3pi::locate(int x, int y) {
jonmarsh 0:e1dac380452c 151 _ser.putc(DO_LCD_GOTO_XY);
jonmarsh 0:e1dac380452c 152 _ser.putc(x);
jonmarsh 0:e1dac380452c 153 _ser.putc(y);
jonmarsh 0:e1dac380452c 154 }
jonmarsh 0:e1dac380452c 155
jonmarsh 0:e1dac380452c 156 void m3pi::cls(void) {
jonmarsh 0:e1dac380452c 157 _ser.putc(DO_CLEAR);
jonmarsh 0:e1dac380452c 158 }
jonmarsh 0:e1dac380452c 159
jonmarsh 0:e1dac380452c 160 int m3pi::print (char* text, int length) {
jonmarsh 0:e1dac380452c 161 _ser.putc(DO_PRINT);
jonmarsh 0:e1dac380452c 162 _ser.putc(length);
jonmarsh 0:e1dac380452c 163 for (int i = 0 ; i < length ; i++) {
jonmarsh 0:e1dac380452c 164 _ser.putc(text[i]);
jonmarsh 0:e1dac380452c 165 }
jonmarsh 0:e1dac380452c 166 return(0);
jonmarsh 0:e1dac380452c 167 }
jonmarsh 0:e1dac380452c 168
jonmarsh 0:e1dac380452c 169 int m3pi::playtune (char* text, int length) {
jonmarsh 0:e1dac380452c 170 _ser.putc(DO_PLAY);
jonmarsh 0:e1dac380452c 171 _ser.putc(length);
jonmarsh 0:e1dac380452c 172 for (int i = 0 ; i < length ; i++) {
jonmarsh 0:e1dac380452c 173 _ser.putc(text[i]);
jonmarsh 0:e1dac380452c 174 }
jonmarsh 0:e1dac380452c 175 return(0);
jonmarsh 0:e1dac380452c 176 }
jonmarsh 0:e1dac380452c 177 int m3pi::_putc (int c) {
jonmarsh 0:e1dac380452c 178 _ser.putc(DO_PRINT);
jonmarsh 0:e1dac380452c 179 _ser.putc(0x1);
jonmarsh 0:e1dac380452c 180 _ser.putc(c);
jonmarsh 0:e1dac380452c 181 wait (0.001);
jonmarsh 0:e1dac380452c 182 return(c);
jonmarsh 0:e1dac380452c 183 }
jonmarsh 0:e1dac380452c 184
jonmarsh 0:e1dac380452c 185 int m3pi::_getc (void) {
jonmarsh 0:e1dac380452c 186 char r = 0;
jonmarsh 0:e1dac380452c 187 return(r);
jonmarsh 0:e1dac380452c 188 }
jonmarsh 0:e1dac380452c 189
jonmarsh 0:e1dac380452c 190 int m3pi::putc (int c) {
jonmarsh 0:e1dac380452c 191 return(_ser.putc(c));
jonmarsh 0:e1dac380452c 192 }
jonmarsh 0:e1dac380452c 193
jonmarsh 0:e1dac380452c 194 int m3pi::getc (void) {
jonmarsh 0:e1dac380452c 195 return(_ser.getc());
jonmarsh 0:e1dac380452c 196 }
jonmarsh 0:e1dac380452c 197
jonmarsh 0:e1dac380452c 198 void m3pi::readsensor (int *sensor){
jonmarsh 0:e1dac380452c 199
jonmarsh 0:e1dac380452c 200 _ser.putc(SEND_CALIBRATED_SENSOR_VALUES);
jonmarsh 0:e1dac380452c 201 sensor[0] = _ser.getc();
jonmarsh 0:e1dac380452c 202 sensor[0] += _ser.getc() << 8;
jonmarsh 0:e1dac380452c 203 sensor[1] = _ser.getc();
jonmarsh 0:e1dac380452c 204 sensor[1] += _ser.getc() << 8;
jonmarsh 0:e1dac380452c 205 sensor[2] = _ser.getc();
jonmarsh 0:e1dac380452c 206 sensor[2] += _ser.getc() << 8;
jonmarsh 0:e1dac380452c 207 sensor[3] = _ser.getc();
jonmarsh 0:e1dac380452c 208 sensor[3] += _ser.getc() << 8;
jonmarsh 0:e1dac380452c 209 sensor[4] = _ser.getc();
jonmarsh 0:e1dac380452c 210 sensor[4] += _ser.getc() << 8;
jonmarsh 0:e1dac380452c 211
jonmarsh 0:e1dac380452c 212 return;
jonmarsh 0:e1dac380452c 213 }
jonmarsh 0:e1dac380452c 214 #ifdef MBED_RPC
jonmarsh 0:e1dac380452c 215 const rpc_method *m3pi::get_rpc_methods() {
jonmarsh 0:e1dac380452c 216 static const rpc_method rpc_methods[] = {{ "forward", rpc_method_caller<m3pi, float, &m3pi::forward> },
jonmarsh 0:e1dac380452c 217 { "backward", rpc_method_caller<m3pi, float, &m3pi::backward> },
jonmarsh 0:e1dac380452c 218 { "left", rpc_method_caller<m3pi, float, &m3pi::left> },
jonmarsh 0:e1dac380452c 219 { "right", rpc_method_caller<m3pi, float, &m3pi::right> },
jonmarsh 0:e1dac380452c 220 { "stop", rpc_method_caller<m3pi, &m3pi::stop> },
jonmarsh 0:e1dac380452c 221 { "left_motor", rpc_method_caller<m3pi, float, &m3pi::left_motor> },
jonmarsh 0:e1dac380452c 222 { "right_motor", rpc_method_caller<m3pi, float, &m3pi::right_motor> },
jonmarsh 0:e1dac380452c 223 { "battery", rpc_method_caller<float, m3pi, &m3pi::battery> },
jonmarsh 0:e1dac380452c 224 { "line_position", rpc_method_caller<float, m3pi, &m3pi::line_position> },
jonmarsh 0:e1dac380452c 225 { "sensor_auto_calibrate", rpc_method_caller<char, m3pi, &m3pi::sensor_auto_calibrate> },
jonmarsh 0:e1dac380452c 226
jonmarsh 0:e1dac380452c 227
jonmarsh 0:e1dac380452c 228 RPC_METHOD_SUPER(Base)
jonmarsh 0:e1dac380452c 229 };
jonmarsh 0:e1dac380452c 230 return rpc_methods;
jonmarsh 0:e1dac380452c 231 }
jonmarsh 0:e1dac380452c 232 #endif