A compilation of code from different sources to provide support for a Playstation 3 controller via bluetooth on the m3pi.

Dependencies:   TextLCD mbed

Fork of mbed_TANK_PS3 by Yasuhiko YAMAMOTO

Committer:
srsmitherman
Date:
Sun Dec 30 05:16:28 2012 +0000
Revision:
1:ae49669c5e92
Bluetooth PS3 interface for m3pi. User must pair bluetooth dongle address to PS3 controller with other program. Works but needs tweaking.

Who changed what in which revision?

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