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