![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Callum and Adel's changes on 12/02/19
Dependencies: Crypto
main.cpp@56:0e7f794d2676, 2019-05-07 (annotated)
- Committer:
- adehadd
- Date:
- Tue May 07 13:34:58 2019 +0000
- Revision:
- 56:0e7f794d2676
- Parent:
- 55:e8b15b4b875f
fixed some errors that prevented compilation (!)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
estott | 0:de4320f74764 | 1 | //Mapping from sequential drive states to motor phase outputs |
estott | 0:de4320f74764 | 2 | /* |
estott | 0:de4320f74764 | 3 | State L1 L2 L3 |
estott | 0:de4320f74764 | 4 | 0 H - L |
estott | 0:de4320f74764 | 5 | 1 - H L |
estott | 0:de4320f74764 | 6 | 2 L H - |
estott | 0:de4320f74764 | 7 | 3 L - H |
estott | 0:de4320f74764 | 8 | 4 - L H |
estott | 0:de4320f74764 | 9 | 5 H L - |
estott | 0:de4320f74764 | 10 | 6 - - - |
estott | 0:de4320f74764 | 11 | 7 - - - |
estott | 0:de4320f74764 | 12 | */ |
CallumAlder | 42:121148278dae | 13 | |
CallumAlder | 42:121148278dae | 14 | //Header Files |
CallumAlder | 42:121148278dae | 15 | #include "SHA256.h" |
CallumAlder | 42:121148278dae | 16 | #include "mbed.h" |
CallumAlder | 42:121148278dae | 17 | |
CallumAlder | 42:121148278dae | 18 | //Photointerrupter Input Pins |
CallumAlder | 42:121148278dae | 19 | #define I1pin D3 |
CallumAlder | 42:121148278dae | 20 | #define I2pin D6 |
CallumAlder | 42:121148278dae | 21 | #define I3pin D5 |
CallumAlder | 42:121148278dae | 22 | |
CallumAlder | 42:121148278dae | 23 | //Incremental Encoder Input Pins |
CallumAlder | 42:121148278dae | 24 | #define CHApin D12 |
CallumAlder | 42:121148278dae | 25 | #define CHBpin D11 |
CallumAlder | 42:121148278dae | 26 | |
CallumAlder | 42:121148278dae | 27 | //Motor Drive High Pins //Mask in output byte |
CallumAlder | 42:121148278dae | 28 | #define L1Hpin A3 //0x02 |
CallumAlder | 42:121148278dae | 29 | #define L2Hpin A6 //0x08 |
CallumAlder | 42:121148278dae | 30 | #define L3Hpin D2 //0x20 |
CallumAlder | 42:121148278dae | 31 | |
CallumAlder | 42:121148278dae | 32 | //Motor Drive Low Pins |
CallumAlder | 42:121148278dae | 33 | #define L1Lpin D1 //0x01 |
CallumAlder | 42:121148278dae | 34 | #define L2Lpin D0 //0x04 |
CallumAlder | 42:121148278dae | 35 | #define L3Lpin D10 //0x10 |
CallumAlder | 42:121148278dae | 36 | |
CallumAlder | 42:121148278dae | 37 | //Motor Pulse Width Modulation (PWM) Pin |
CallumAlder | 42:121148278dae | 38 | #define PWMpin D9 |
CallumAlder | 42:121148278dae | 39 | |
CallumAlder | 42:121148278dae | 40 | //Motor current sense |
CallumAlder | 42:121148278dae | 41 | #define MCSPpin A1 |
CallumAlder | 42:121148278dae | 42 | #define MCSNpin A0 |
CallumAlder | 42:121148278dae | 43 | |
CallumAlder | 42:121148278dae | 44 | // "Lacros" for utility |
CallumAlder | 47:21bf4096faa1 | 45 | #define max(x,y) ( (x)>=(y) ? (x):(y) ) |
CallumAlder | 47:21bf4096faa1 | 46 | #define min(x,y) ( (x)>=(y) ? (y):(x) ) |
CallumAlder | 47:21bf4096faa1 | 47 | #define sgn(x) ( (x)>= 0 ? 1 :-1 ) |
CallumAlder | 42:121148278dae | 48 | |
CallumAlder | 42:121148278dae | 49 | //Status LED |
CallumAlder | 42:121148278dae | 50 | DigitalOut led1(LED1); |
CallumAlder | 42:121148278dae | 51 | |
CallumAlder | 42:121148278dae | 52 | //Photointerrupter Inputs |
CallumAlder | 42:121148278dae | 53 | InterruptIn I1(I1pin); |
CallumAlder | 42:121148278dae | 54 | InterruptIn I2(I2pin); |
CallumAlder | 42:121148278dae | 55 | InterruptIn I3(I3pin); |
CallumAlder | 42:121148278dae | 56 | |
CallumAlder | 42:121148278dae | 57 | //Motor Drive High Outputs |
CallumAlder | 42:121148278dae | 58 | DigitalOut L1H(L1Hpin); |
CallumAlder | 42:121148278dae | 59 | DigitalOut L2H(L2Hpin); |
CallumAlder | 42:121148278dae | 60 | DigitalOut L3H(L3Hpin); |
CallumAlder | 42:121148278dae | 61 | |
CallumAlder | 42:121148278dae | 62 | //Motor Drive Low Outputs |
CallumAlder | 42:121148278dae | 63 | DigitalOut L1L(L1Lpin); |
CallumAlder | 42:121148278dae | 64 | DigitalOut L2L(L2Lpin); |
CallumAlder | 42:121148278dae | 65 | DigitalOut L3L(L3Lpin); |
CallumAlder | 42:121148278dae | 66 | |
CallumAlder | 42:121148278dae | 67 | PwmOut pwmCtrl(PWMpin); |
CallumAlder | 42:121148278dae | 68 | |
adehadd | 51:c03f63c6f930 | 69 | |
adehadd | 51:c03f63c6f930 | 70 | //Encoder inputs |
adehadd | 51:c03f63c6f930 | 71 | InterruptIn CHA(CHApin); |
adehadd | 51:c03f63c6f930 | 72 | InterruptIn CHB(CHBpin); |
adehadd | 51:c03f63c6f930 | 73 | |
adehadd | 51:c03f63c6f930 | 74 | |
adehadd | 27:ce05fed3c1ea | 75 | //Drive state to output table |
estott | 0:de4320f74764 | 76 | const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00}; |
estott | 2:4e88faab6988 | 77 | |
adehadd | 27:ce05fed3c1ea | 78 | //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid |
CallumAlder | 47:21bf4096faa1 | 79 | const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07}; |
CallumAlder | 47:21bf4096faa1 | 80 | //const int8_t stateMap[] = {0x07,0x01,0x03,0x02,0x05,0x00,0x04,0x07}; //Alternative if phase order of input or drive is reversed |
CallumAlder | 47:21bf4096faa1 | 81 | |
adehadd | 49:ae8dedfe2d0f | 82 | #ifndef MAXCMDLENGTH |
adehadd | 49:ae8dedfe2d0f | 83 | #define MAXCMDLENGTH 18 |
adehadd | 49:ae8dedfe2d0f | 84 | #endif |
adehadd | 49:ae8dedfe2d0f | 85 | |
adehadd | 49:ae8dedfe2d0f | 86 | #ifndef MAXCMDLENGTH_HALF |
adehadd | 49:ae8dedfe2d0f | 87 | #define MAXCMDLENGTH_HALF 9 |
adehadd | 49:ae8dedfe2d0f | 88 | #endif |
adehadd | 49:ae8dedfe2d0f | 89 | |
CallumAlder | 42:121148278dae | 90 | class Comm{ |
CallumAlder | 42:121148278dae | 91 | |
CallumAlder | 42:121148278dae | 92 | public: |
estott | 0:de4320f74764 | 93 | |
CallumAlder | 43:a6d20109b2f2 | 94 | volatile bool _outMining; |
CallumAlder | 43:a6d20109b2f2 | 95 | volatile float _targetVel, _targetRot; |
adehadd | 49:ae8dedfe2d0f | 96 | volatile char _notes[MAXCMDLENGTH_HALF]; // Array of actual _notes |
estott | 0:de4320f74764 | 97 | |
CallumAlder | 47:21bf4096faa1 | 98 | volatile int8_t _modeBitField; // 0,0,0,... <=> Melody,Torque,Rotation,Velocity |
CallumAlder | 47:21bf4096faa1 | 99 | const uint8_t _MAXCMDLENGTH; // |
adehadd | 50:d1b983a0dd6f | 100 | volatile uint8_t _inCharIndex, _cmdIndex, _noteRep, |
adehadd | 49:ae8dedfe2d0f | 101 | _noteDur[MAXCMDLENGTH_HALF],_noteLen; // Array of note durations |
CallumAlder | 47:21bf4096faa1 | 102 | volatile uint32_t _motorTorque; // Motor Toque |
CallumAlder | 47:21bf4096faa1 | 103 | volatile uint64_t _newKey; // hash key |
adehadd | 49:ae8dedfe2d0f | 104 | |
adehadd | 51:c03f63c6f930 | 105 | volatile int32_t _motor_pos; |
adehadd | 51:c03f63c6f930 | 106 | |
adehadd | 49:ae8dedfe2d0f | 107 | char _inCharQ[MAXCMDLENGTH], |
adehadd | 49:ae8dedfe2d0f | 108 | _newCmd[MAXCMDLENGTH]; |
adehadd | 49:ae8dedfe2d0f | 109 | |
CallumAlder | 47:21bf4096faa1 | 110 | RawSerial _pc; |
adehadd | 49:ae8dedfe2d0f | 111 | Thread _tCommOut, _tCommIn; |
adehadd | 49:ae8dedfe2d0f | 112 | Mutex _newKeyMutex; // Restrict access to prevent deadlock. |
CallumAlder | 47:21bf4096faa1 | 113 | bool _RUN; |
CallumAlder | 42:121148278dae | 114 | |
CallumAlder | 48:b2afe48ced0d | 115 | enum msgType { mot_orState, posIn, velIn, posOut, velOut, |
CallumAlder | 42:121148278dae | 116 | hashRate, keyAdded, nonceMatch, |
CallumAlder | 43:a6d20109b2f2 | 117 | torque, rotations, melody, |
CallumAlder | 42:121148278dae | 118 | error}; |
adehadd | 27:ce05fed3c1ea | 119 | |
CallumAlder | 47:21bf4096faa1 | 120 | typedef struct { msgType type; |
adehadd | 49:ae8dedfe2d0f | 121 | uint32_t message;} msg; // TODO: Maybe add a thing that stores the newCmd message as well |
adehadd | 27:ce05fed3c1ea | 122 | |
CallumAlder | 47:21bf4096faa1 | 123 | Mail<msg, 32> _msgStack; |
adehadd | 49:ae8dedfe2d0f | 124 | Mail<bool,32> _msgReceived; |
CallumAlder | 47:21bf4096faa1 | 125 | |
iachinweze1 | 23:ab1cb51527d1 | 126 | |
CallumAlder | 48:b2afe48ced0d | 127 | //-- Default Constructor With Inheritance From RawSerial Constructor ------------------------------------------------// |
adehadd | 49:ae8dedfe2d0f | 128 | Comm(): _pc(SERIAL_TX, SERIAL_RX), _tCommOut(osPriorityNormal, 1024), _tCommIn(osPriorityAboveNormal, 1024), _MAXCMDLENGTH(MAXCMDLENGTH){ |
adehadd | 27:ce05fed3c1ea | 129 | |
CallumAlder | 47:21bf4096faa1 | 130 | _cmdIndex = 0; |
CallumAlder | 47:21bf4096faa1 | 131 | _inCharIndex = 0; |
adehadd | 45:402a8a9423b9 | 132 | |
CallumAlder | 47:21bf4096faa1 | 133 | _outMining = false; |
CallumAlder | 47:21bf4096faa1 | 134 | _motorTorque = 300; |
CallumAlder | 47:21bf4096faa1 | 135 | _targetRot = 459.0; |
CallumAlder | 47:21bf4096faa1 | 136 | _targetVel = 45.0; |
iachinweze1 | 23:ab1cb51527d1 | 137 | |
adehadd | 51:c03f63c6f930 | 138 | _motor_pos = 0; |
adehadd | 51:c03f63c6f930 | 139 | |
CallumAlder | 47:21bf4096faa1 | 140 | _modeBitField = 0x01; // Default velocity mode |
adehadd | 45:402a8a9423b9 | 141 | |
adehadd | 49:ae8dedfe2d0f | 142 | _pc.printf("\n\r%s %d\n\r>", "Welcome", _MAXCMDLENGTH ); // Welcome |
CallumAlder | 47:21bf4096faa1 | 143 | for (int i = 0; i < _MAXCMDLENGTH; ++i) // Reset buffer |
CallumAlder | 48:b2afe48ced0d | 144 | _inCharQ[i] = (char)'.'; // If a null terminator is printed Mbed prints 'Embedded Systems are fun and do awesome things!' |
CallumAlder | 42:121148278dae | 145 | |
CallumAlder | 47:21bf4096faa1 | 146 | _inCharQ[_MAXCMDLENGTH] = (char)'\0'; |
CallumAlder | 48:b2afe48ced0d | 147 | sprintf(_inCharQ, "%s", _inCharQ); // Handling of the correct string |
CallumAlder | 47:21bf4096faa1 | 148 | strncpy(_newCmd, _inCharQ, _MAXCMDLENGTH); |
CallumAlder | 19:805c87360b55 | 149 | |
adehadd | 49:ae8dedfe2d0f | 150 | _pc.printf("%s\n\r<", _inCharQ); |
adehadd | 49:ae8dedfe2d0f | 151 | |
CallumAlder | 42:121148278dae | 152 | } |
iachinweze1 | 23:ab1cb51527d1 | 153 | |
adehadd | 49:ae8dedfe2d0f | 154 | void commInFn() { |
adehadd | 49:ae8dedfe2d0f | 155 | _pc.attach(callback(this, &Comm::serialISR)); |
adehadd | 49:ae8dedfe2d0f | 156 | char newChar; |
CallumAlder | 42:121148278dae | 157 | |
adehadd | 49:ae8dedfe2d0f | 158 | while (_RUN) { |
adehadd | 49:ae8dedfe2d0f | 159 | osEvent newEvent = _msgReceived.get(); // Waits forever until it receives a thing |
adehadd | 49:ae8dedfe2d0f | 160 | _msgReceived.free((bool *)newEvent.value.p); |
adehadd | 49:ae8dedfe2d0f | 161 | |
CallumAlder | 43:a6d20109b2f2 | 162 | if (_inCharIndex == (_MAXCMDLENGTH)) { |
CallumAlder | 47:21bf4096faa1 | 163 | _inCharQ[_MAXCMDLENGTH] = '\0'; // Force the string to have an end character |
CallumAlder | 42:121148278dae | 164 | putMessage(error, 1); |
CallumAlder | 47:21bf4096faa1 | 165 | _inCharIndex = 0; // Reset buffer index |
adehadd | 27:ce05fed3c1ea | 166 | } |
adehadd | 27:ce05fed3c1ea | 167 | else{ |
adehadd | 49:ae8dedfe2d0f | 168 | newChar = _inCharQ[_inCharIndex]; |
adehadd | 49:ae8dedfe2d0f | 169 | |
adehadd | 49:ae8dedfe2d0f | 170 | if(newChar != '\r'){ // While the command is not over, |
CallumAlder | 47:21bf4096faa1 | 171 | _inCharIndex++; // Advance index |
CallumAlder | 43:a6d20109b2f2 | 172 | _pc.putc(newChar); |
CallumAlder | 42:121148278dae | 173 | } |
CallumAlder | 42:121148278dae | 174 | else{ |
CallumAlder | 47:21bf4096faa1 | 175 | _inCharQ[_inCharIndex] = '\0'; // When the command is finally over, |
CallumAlder | 47:21bf4096faa1 | 176 | strncpy(_newCmd, _inCharQ, _MAXCMDLENGTH); // Will copy 18 characters from _inCharQ to _newCmd |
CallumAlder | 42:121148278dae | 177 | |
CallumAlder | 47:21bf4096faa1 | 178 | for (int i = 0; i < _MAXCMDLENGTH; ++i) // Reset buffer |
CallumAlder | 47:21bf4096faa1 | 179 | _inCharQ[i] = ' '; |
adehadd | 49:ae8dedfe2d0f | 180 | |
CallumAlder | 47:21bf4096faa1 | 181 | _inCharIndex = 0; // Reset index |
adehadd | 49:ae8dedfe2d0f | 182 | cmdParser(); |
CallumAlder | 42:121148278dae | 183 | } |
adehadd | 27:ce05fed3c1ea | 184 | } |
adehadd | 27:ce05fed3c1ea | 185 | } |
adehadd | 27:ce05fed3c1ea | 186 | } |
CallumAlder | 19:805c87360b55 | 187 | |
adehadd | 49:ae8dedfe2d0f | 188 | //-- Interrupt Service Routine for Serial Port and Character Queue Handling -----------------------------------------// |
adehadd | 49:ae8dedfe2d0f | 189 | void serialISR() { |
adehadd | 49:ae8dedfe2d0f | 190 | if (_pc.readable()) { |
adehadd | 49:ae8dedfe2d0f | 191 | char newChar = _pc.getc(); |
adehadd | 49:ae8dedfe2d0f | 192 | _inCharQ[_inCharIndex] = newChar; // Save input character |
adehadd | 49:ae8dedfe2d0f | 193 | |
adehadd | 49:ae8dedfe2d0f | 194 | bool *new_msg = _msgReceived.alloc(); |
adehadd | 49:ae8dedfe2d0f | 195 | *new_msg = true; |
adehadd | 49:ae8dedfe2d0f | 196 | _msgReceived.put(new_msg); |
adehadd | 49:ae8dedfe2d0f | 197 | } |
adehadd | 49:ae8dedfe2d0f | 198 | } |
adehadd | 49:ae8dedfe2d0f | 199 | |
CallumAlder | 48:b2afe48ced0d | 200 | //-- Reset Cursor Position ------------------------------------------------------------------------------------------// |
CallumAlder | 42:121148278dae | 201 | void returnCursor() { |
CallumAlder | 43:a6d20109b2f2 | 202 | _pc.putc('>'); |
CallumAlder | 43:a6d20109b2f2 | 203 | for (int i = 0; i < _inCharIndex; ++i) |
CallumAlder | 47:21bf4096faa1 | 204 | _pc.putc(_inCharQ[i]); |
CallumAlder | 42:121148278dae | 205 | } |
CallumAlder | 47:21bf4096faa1 | 206 | |
CallumAlder | 48:b2afe48ced0d | 207 | //-- Parse Incoming Data From Serial Port ---------------------------------------------------------------------------// |
CallumAlder | 48:b2afe48ced0d | 208 | void cmdParser() { |
CallumAlder | 47:21bf4096faa1 | 209 | switch(_newCmd[0]) { |
CallumAlder | 47:21bf4096faa1 | 210 | case 'K': // keyAdded |
CallumAlder | 47:21bf4096faa1 | 211 | _newKeyMutex.lock(); // Ensure there is no deadlock |
CallumAlder | 47:21bf4096faa1 | 212 | sscanf(_newCmd, "K%x", &_newKey); // Find desired the Key code |
CallumAlder | 47:21bf4096faa1 | 213 | putMessage(keyAdded, _newKey); // Print it out |
CallumAlder | 43:a6d20109b2f2 | 214 | _newKeyMutex.unlock(); |
CallumAlder | 43:a6d20109b2f2 | 215 | break; |
CallumAlder | 43:a6d20109b2f2 | 216 | |
CallumAlder | 47:21bf4096faa1 | 217 | case 'V': // velIn |
CallumAlder | 47:21bf4096faa1 | 218 | sscanf(_newCmd, "V%f", &_targetVel); // Find desired the target velocity |
CallumAlder | 47:21bf4096faa1 | 219 | _modeBitField = 0x01; // Adjust bitfield pos 1 |
adehadd | 51:c03f63c6f930 | 220 | _motor_pos = 0; |
adehadd | 56:0e7f794d2676 | 221 | if (&_targetVel < (void *)0) { |
iachinweze1 | 53:89d16b398615 | 222 | _targetRot = -100; |
iachinweze1 | 53:89d16b398615 | 223 | } else { |
iachinweze1 | 53:89d16b398615 | 224 | _targetRot = 100; |
iachinweze1 | 53:89d16b398615 | 225 | } |
CallumAlder | 47:21bf4096faa1 | 226 | putMessage(velIn, _targetVel); // Print it out |
CallumAlder | 43:a6d20109b2f2 | 227 | break; |
CallumAlder | 43:a6d20109b2f2 | 228 | |
CallumAlder | 47:21bf4096faa1 | 229 | case 'R': // posIn |
CallumAlder | 47:21bf4096faa1 | 230 | sscanf(_newCmd, "R%f", &_targetRot); // Find desired target rotation |
CallumAlder | 47:21bf4096faa1 | 231 | _modeBitField = 0x02; // Adjust bitfield pos 2 |
adehadd | 51:c03f63c6f930 | 232 | _targetVel = 2e3; |
adehadd | 51:c03f63c6f930 | 233 | _motor_pos = 0; |
CallumAlder | 47:21bf4096faa1 | 234 | putMessage(posIn, _targetRot); // Print it out |
CallumAlder | 42:121148278dae | 235 | break; |
iachinweze1 | 23:ab1cb51527d1 | 236 | |
CallumAlder | 47:21bf4096faa1 | 237 | case 'x': // Torque |
CallumAlder | 47:21bf4096faa1 | 238 | sscanf(_newCmd, "x%u", &_motorTorque); // Find desired target torque |
CallumAlder | 47:21bf4096faa1 | 239 | _modeBitField = 0x04; // Adjust bitfield pos 3 |
CallumAlder | 47:21bf4096faa1 | 240 | putMessage(torque, _motorTorque); // Print it out |
adehadd | 27:ce05fed3c1ea | 241 | break; |
CallumAlder | 42:121148278dae | 242 | |
CallumAlder | 47:21bf4096faa1 | 243 | case 'M': // Mining display toggle |
CallumAlder | 47:21bf4096faa1 | 244 | int8_t miningTest; |
CallumAlder | 47:21bf4096faa1 | 245 | sscanf(_newCmd, "M%d", &miningTest); // Display if input is 1 |
CallumAlder | 47:21bf4096faa1 | 246 | miningTest == 1 ? _outMining = true : _outMining = false; |
CallumAlder | 47:21bf4096faa1 | 247 | break; |
CallumAlder | 47:21bf4096faa1 | 248 | |
CallumAlder | 47:21bf4096faa1 | 249 | case 'T': // Play tune |
CallumAlder | 47:21bf4096faa1 | 250 | regexTune() ? putMessage(melody, 1) : putMessage(error, 2); |
CallumAlder | 47:21bf4096faa1 | 251 | break; // Break from case 'T' |
CallumAlder | 47:21bf4096faa1 | 252 | |
CallumAlder | 47:21bf4096faa1 | 253 | default: // Break from switch |
adehadd | 27:ce05fed3c1ea | 254 | break; |
adehadd | 27:ce05fed3c1ea | 255 | } |
adehadd | 27:ce05fed3c1ea | 256 | } |
adehadd | 27:ce05fed3c1ea | 257 | |
CallumAlder | 48:b2afe48ced0d | 258 | //-- Read In Note Data From Serial Port and Parse Into Class Variables ----------------------------------------------// |
CallumAlder | 47:21bf4096faa1 | 259 | bool regexTune() { |
CallumAlder | 47:21bf4096faa1 | 260 | |
CallumAlder | 47:21bf4096faa1 | 261 | uint8_t len = 0; |
CallumAlder | 47:21bf4096faa1 | 262 | |
CallumAlder | 47:21bf4096faa1 | 263 | for (int i = 1; i < _MAXCMDLENGTH; ++i) // Find first # |
CallumAlder | 47:21bf4096faa1 | 264 | if (_newCmd[i] == '#') { |
CallumAlder | 47:21bf4096faa1 | 265 | len = i; |
CallumAlder | 47:21bf4096faa1 | 266 | break; // Stop at first # found |
CallumAlder | 47:21bf4096faa1 | 267 | } |
CallumAlder | 47:21bf4096faa1 | 268 | |
CallumAlder | 47:21bf4096faa1 | 269 | if (len>0) { // Parse the input only if # found |
adehadd | 50:d1b983a0dd6f | 270 | uint8_t specLen = 2*(len+1) +1; // Add extra character for number of repeats, and +1 for the letter T |
adehadd | 50:d1b983a0dd6f | 271 | bool isChar = true; // After 'T' first is character note |
adehadd | 50:d1b983a0dd6f | 272 | char formatSpec[specLen]; |
CallumAlder | 47:21bf4096faa1 | 273 | formatSpec[0]='T'; |
adehadd | 50:d1b983a0dd6f | 274 | for (int i = 1; i < specLen; i=i+2) { // Create a format spec based on length of input |
CallumAlder | 47:21bf4096faa1 | 275 | formatSpec[i] = '%'; |
CallumAlder | 47:21bf4096faa1 | 276 | isChar ? formatSpec[i+1] = 'c' : \ |
CallumAlder | 47:21bf4096faa1 | 277 | formatSpec[i+1] = 'u' ; |
CallumAlder | 47:21bf4096faa1 | 278 | isChar = !isChar; |
CallumAlder | 47:21bf4096faa1 | 279 | } |
CallumAlder | 47:21bf4096faa1 | 280 | |
adehadd | 50:d1b983a0dd6f | 281 | formatSpec[specLen] = '\0'; |
CallumAlder | 47:21bf4096faa1 | 282 | sprintf(formatSpec, "%s", formatSpec); // Set string format correctly |
CallumAlder | 47:21bf4096faa1 | 283 | // _pc.printf("%s\n", formatSpec ); |
CallumAlder | 47:21bf4096faa1 | 284 | sscanf(_newCmd, formatSpec, &_notes[0], &_noteDur[0], |
adehadd | 50:d1b983a0dd6f | 285 | &_notes[1], &_noteDur[1], |
adehadd | 50:d1b983a0dd6f | 286 | &_notes[2], &_noteDur[2], |
adehadd | 50:d1b983a0dd6f | 287 | &_notes[3], &_noteDur[3], |
adehadd | 50:d1b983a0dd6f | 288 | &_notes[4], &_noteDur[4], |
adehadd | 50:d1b983a0dd6f | 289 | &_notes[5], &_noteDur[5], |
adehadd | 50:d1b983a0dd6f | 290 | &_notes[6], &_noteDur[6], |
adehadd | 50:d1b983a0dd6f | 291 | &_notes[7], &_noteDur[7], |
adehadd | 50:d1b983a0dd6f | 292 | &_notes[8], &_noteDur[8]); |
CallumAlder | 47:21bf4096faa1 | 293 | |
CallumAlder | 47:21bf4096faa1 | 294 | |
CallumAlder | 47:21bf4096faa1 | 295 | // Update _newCmd for putMessage print |
CallumAlder | 48:b2afe48ced0d | 296 | sprintf(_newCmd,formatSpec, _notes[0], _noteDur[0],\ |
CallumAlder | 47:21bf4096faa1 | 297 | _notes[1], _noteDur[1],\ |
CallumAlder | 47:21bf4096faa1 | 298 | _notes[2], _noteDur[2],\ |
CallumAlder | 47:21bf4096faa1 | 299 | _notes[3], _noteDur[3],\ |
CallumAlder | 47:21bf4096faa1 | 300 | _notes[4], _noteDur[4],\ |
CallumAlder | 47:21bf4096faa1 | 301 | _notes[5], _noteDur[5],\ |
CallumAlder | 47:21bf4096faa1 | 302 | _notes[6], _noteDur[6],\ |
CallumAlder | 47:21bf4096faa1 | 303 | _notes[7], _noteDur[7],\ |
CallumAlder | 47:21bf4096faa1 | 304 | _notes[8], _noteDur[8]); |
adehadd | 50:d1b983a0dd6f | 305 | _noteLen = (len-1)/2; |
adehadd | 50:d1b983a0dd6f | 306 | _modeBitField = 0x08; |
adehadd | 50:d1b983a0dd6f | 307 | _noteRep = _noteDur[(len-1)/2]; |
CallumAlder | 47:21bf4096faa1 | 308 | return true; |
CallumAlder | 47:21bf4096faa1 | 309 | } |
CallumAlder | 47:21bf4096faa1 | 310 | else { |
CallumAlder | 47:21bf4096faa1 | 311 | return false; |
CallumAlder | 47:21bf4096faa1 | 312 | } |
CallumAlder | 47:21bf4096faa1 | 313 | } |
CallumAlder | 47:21bf4096faa1 | 314 | |
CallumAlder | 48:b2afe48ced0d | 315 | //-- Decode Messages to Print on Serial Port ------------------------------------------------------------------------// |
CallumAlder | 42:121148278dae | 316 | void commOutFn() { |
CallumAlder | 42:121148278dae | 317 | while (_RUN) { |
CallumAlder | 48:b2afe48ced0d | 318 | |
CallumAlder | 47:21bf4096faa1 | 319 | osEvent newEvent = _msgStack.get(); |
CallumAlder | 42:121148278dae | 320 | msg *pMessage = (msg *) newEvent.value.p; |
adehadd | 27:ce05fed3c1ea | 321 | |
CallumAlder | 47:21bf4096faa1 | 322 | //Case switch to choose serial output based on incoming message enum |
CallumAlder | 42:121148278dae | 323 | switch (pMessage->type) { |
CallumAlder | 48:b2afe48ced0d | 324 | case mot_orState: |
CallumAlder | 47:21bf4096faa1 | 325 | _pc.printf("\r>%s< The motor is currently in state %x\n\r", _inCharQ, pMessage->message); |
CallumAlder | 42:121148278dae | 326 | break; |
CallumAlder | 42:121148278dae | 327 | case hashRate: |
CallumAlder | 43:a6d20109b2f2 | 328 | if (_outMining) { |
CallumAlder | 47:21bf4096faa1 | 329 | _pc.printf("\r>%s< Mining: %.4u Hash/s\r", _inCharQ, (uint32_t) pMessage->message); |
CallumAlder | 42:121148278dae | 330 | returnCursor(); |
CallumAlder | 43:a6d20109b2f2 | 331 | _outMining = false; |
CallumAlder | 42:121148278dae | 332 | } |
CallumAlder | 42:121148278dae | 333 | break; |
CallumAlder | 42:121148278dae | 334 | case nonceMatch: |
CallumAlder | 47:21bf4096faa1 | 335 | _pc.printf("\r>%s< Nonce found: %x\n\r", _inCharQ, pMessage->message); |
CallumAlder | 42:121148278dae | 336 | returnCursor(); |
CallumAlder | 42:121148278dae | 337 | break; |
CallumAlder | 42:121148278dae | 338 | case keyAdded: |
CallumAlder | 47:21bf4096faa1 | 339 | _pc.printf("\r>%s< New Key Added:\t0x%016x\n\r", _inCharQ, pMessage->message); |
CallumAlder | 42:121148278dae | 340 | break; |
CallumAlder | 42:121148278dae | 341 | case torque: |
CallumAlder | 47:21bf4096faa1 | 342 | _pc.printf("\r>%s< Motor Torque set to:\t%d\n\r", _inCharQ, (int32_t) pMessage->message); |
CallumAlder | 42:121148278dae | 343 | break; |
CallumAlder | 42:121148278dae | 344 | case velIn: |
CallumAlder | 47:21bf4096faa1 | 345 | _pc.printf("\r>%s< Target Velocity set to:\t%.2f\n\r", _inCharQ, _targetVel); |
CallumAlder | 42:121148278dae | 346 | break; |
CallumAlder | 42:121148278dae | 347 | case velOut: |
CallumAlder | 47:21bf4096faa1 | 348 | _pc.printf("\r>%s< Current Velocity:\t%.2f States/sec\n\r", _inCharQ, (float) ((int32_t) pMessage->message)); |
CallumAlder | 42:121148278dae | 349 | break; |
CallumAlder | 42:121148278dae | 350 | case posIn: |
CallumAlder | 47:21bf4096faa1 | 351 | _pc.printf("\r>%s< Target # Rotations:\t%.2f\n\r", _inCharQ, (float) ((int32_t) pMessage->message)); |
CallumAlder | 42:121148278dae | 352 | break; |
CallumAlder | 42:121148278dae | 353 | case posOut: |
CallumAlder | 47:21bf4096faa1 | 354 | _pc.printf("\r>%s< Current Position:\t%.2f\n\r", _inCharQ, (float) ((int32_t) pMessage->message)); |
CallumAlder | 42:121148278dae | 355 | break; |
CallumAlder | 47:21bf4096faa1 | 356 | case melody: |
CallumAlder | 47:21bf4096faa1 | 357 | _pc.printf("\r>%s< New Tune:\t%s\n\r", _inCharQ, _newCmd); |
CallumAlder | 47:21bf4096faa1 | 358 | break; |
CallumAlder | 42:121148278dae | 359 | case error: |
CallumAlder | 47:21bf4096faa1 | 360 | switch (pMessage->message) { |
CallumAlder | 47:21bf4096faa1 | 361 | case 1: |
CallumAlder | 47:21bf4096faa1 | 362 | _pc.printf("\r>%s< Error:%s\n\r", _inCharQ, "Overfull Buffer Reset" ); |
CallumAlder | 47:21bf4096faa1 | 363 | break; |
CallumAlder | 47:21bf4096faa1 | 364 | case 2: |
CallumAlder | 47:21bf4096faa1 | 365 | _pc.printf("\r>%s< Error:%s\n\r", _inCharQ, "Invalid Melody" ); |
CallumAlder | 47:21bf4096faa1 | 366 | default: |
CallumAlder | 47:21bf4096faa1 | 367 | break; |
CallumAlder | 47:21bf4096faa1 | 368 | } |
CallumAlder | 47:21bf4096faa1 | 369 | for (int i = 0; i < _MAXCMDLENGTH; ++i) // reset buffer |
CallumAlder | 47:21bf4096faa1 | 370 | _inCharQ[i] = ' '; |
CallumAlder | 47:21bf4096faa1 | 371 | |
CallumAlder | 47:21bf4096faa1 | 372 | _inCharIndex = 0; |
CallumAlder | 42:121148278dae | 373 | break; |
CallumAlder | 42:121148278dae | 374 | default: |
CallumAlder | 47:21bf4096faa1 | 375 | _pc.printf("\r>%s< Unknown Error. Message: %x\n\r", _inCharQ, pMessage->message); |
CallumAlder | 42:121148278dae | 376 | break; |
CallumAlder | 42:121148278dae | 377 | } |
CallumAlder | 42:121148278dae | 378 | |
CallumAlder | 47:21bf4096faa1 | 379 | _msgStack.free(pMessage); |
CallumAlder | 42:121148278dae | 380 | } |
CallumAlder | 42:121148278dae | 381 | } |
CallumAlder | 42:121148278dae | 382 | |
CallumAlder | 48:b2afe48ced0d | 383 | //-- Put a Message On the Outgoing Message Stack --------------------------------------------------------------------// |
CallumAlder | 42:121148278dae | 384 | void putMessage(msgType type, uint32_t message){ |
CallumAlder | 47:21bf4096faa1 | 385 | msg *p_msg = _msgStack.alloc(); |
CallumAlder | 42:121148278dae | 386 | p_msg->type = type; |
CallumAlder | 42:121148278dae | 387 | p_msg->message = message; |
CallumAlder | 47:21bf4096faa1 | 388 | _msgStack.put(p_msg); |
CallumAlder | 42:121148278dae | 389 | } |
CallumAlder | 42:121148278dae | 390 | |
CallumAlder | 48:b2afe48ced0d | 391 | //-- Attach CommOut Thread to the Outgoing Communication Function ---------------------------------------------------// |
CallumAlder | 42:121148278dae | 392 | void start_comm(){ |
CallumAlder | 42:121148278dae | 393 | _RUN = true; |
CallumAlder | 47:21bf4096faa1 | 394 | _tCommOut.start(callback(this, &Comm::commOutFn)); |
adehadd | 49:ae8dedfe2d0f | 395 | _tCommIn.start(callback(this, &Comm::commInFn)); |
CallumAlder | 42:121148278dae | 396 | } |
iachinweze1 | 23:ab1cb51527d1 | 397 | |
adehadd | 49:ae8dedfe2d0f | 398 | |
CallumAlder | 42:121148278dae | 399 | }; |
adehadd | 46:b9081aa50bda | 400 | |
CallumAlder | 42:121148278dae | 401 | class Motor { |
adehadd | 27:ce05fed3c1ea | 402 | |
CallumAlder | 42:121148278dae | 403 | protected: |
CallumAlder | 48:b2afe48ced0d | 404 | volatile int8_t _orState, // Rotor offset at motor state 0, motor specific |
CallumAlder | 48:b2afe48ced0d | 405 | _currentState, // Current Rotor State |
CallumAlder | 48:b2afe48ced0d | 406 | _stateList[6], // All possible rotor states stored |
CallumAlder | 48:b2afe48ced0d | 407 | _lead; // Phase _lead to make motor spin |
CallumAlder | 42:121148278dae | 408 | |
iachinweze1 | 54:bd5b586aa2e1 | 409 | int8_t old_rotor_state; |
iachinweze1 | 54:bd5b586aa2e1 | 410 | |
CallumAlder | 48:b2afe48ced0d | 411 | uint8_t _theStates[3], // The Key states |
iachinweze1 | 54:bd5b586aa2e1 | 412 | _stateCount[3], // State Counter |
iachinweze1 | 54:bd5b586aa2e1 | 413 | encState; |
iachinweze1 | 54:bd5b586aa2e1 | 414 | |
adehadd | 51:c03f63c6f930 | 415 | uint32_t _mtrPeriod, // Motor period |
adehadd | 56:0e7f794d2676 | 416 | _MAXPWM_PRD, |
iachinweze1 | 54:bd5b586aa2e1 | 417 | quadratureStates, |
iachinweze1 | 54:bd5b586aa2e1 | 418 | MINPWM_PRD, |
iachinweze1 | 54:bd5b586aa2e1 | 419 | maxEncCount, |
iachinweze1 | 54:bd5b586aa2e1 | 420 | encCount, |
iachinweze1 | 54:bd5b586aa2e1 | 421 | encTotal, |
iachinweze1 | 54:bd5b586aa2e1 | 422 | badEdges; |
iachinweze1 | 54:bd5b586aa2e1 | 423 | |
CallumAlder | 48:b2afe48ced0d | 424 | float _dutyC; // 1 = 100% |
CallumAlder | 47:21bf4096faa1 | 425 | bool _RUN; |
adehadd | 27:ce05fed3c1ea | 426 | |
CallumAlder | 48:b2afe48ced0d | 427 | Comm* _pComm; |
CallumAlder | 48:b2afe48ced0d | 428 | Thread _tMotorCtrl; // Thread for motor Control |
CallumAlder | 42:121148278dae | 429 | |
adehadd | 51:c03f63c6f930 | 430 | |
CallumAlder | 42:121148278dae | 431 | public: |
CallumAlder | 48:b2afe48ced0d | 432 | //-- Default Constructor With Thread Object Constructor -------------------------------------------------------------// |
adehadd | 51:c03f63c6f930 | 433 | Motor() : _tMotorCtrl(osPriorityAboveNormal2, 2048){ |
CallumAlder | 47:21bf4096faa1 | 434 | |
CallumAlder | 48:b2afe48ced0d | 435 | _dutyC = 1.0f; // Set Power to maximum to drive motorHome() |
adehadd | 51:c03f63c6f930 | 436 | _mtrPeriod = 2e3; // Motor period |
adehadd | 51:c03f63c6f930 | 437 | pwmCtrl.period_us(_mtrPeriod); // Initialise PWM |
adehadd | 51:c03f63c6f930 | 438 | pwmCtrl.pulsewidth_us(_mtrPeriod); |
CallumAlder | 42:121148278dae | 439 | |
CallumAlder | 48:b2afe48ced0d | 440 | _orState = motorHome(); // Rotor offset at motor state 0 |
adehadd | 49:ae8dedfe2d0f | 441 | _currentState = readRotorState(); // Current Rotor State |
CallumAlder | 48:b2afe48ced0d | 442 | _lead = 2; // 2 for forwards, -2 for backwards |
adehadd | 51:c03f63c6f930 | 443 | old_rotor_state = _orState; // Set old_rotor_state to the origin to begin with |
adehadd | 27:ce05fed3c1ea | 444 | |
CallumAlder | 48:b2afe48ced0d | 445 | _theStates[0] = _orState +1; // Initialise repeatable states, for us this is state 1 |
CallumAlder | 48:b2afe48ced0d | 446 | _theStates[1] = (_orState + _lead) % 6 +1; // state 3 |
CallumAlder | 48:b2afe48ced0d | 447 | _theStates[2] = (_orState + (_lead*2)) % 6 +1; // state 5 |
CallumAlder | 42:121148278dae | 448 | |
CallumAlder | 48:b2afe48ced0d | 449 | _stateCount[0] = 0; // Initialise |
CallumAlder | 48:b2afe48ced0d | 450 | _stateCount[1] = 0; |
CallumAlder | 48:b2afe48ced0d | 451 | _stateCount[2] = 0; |
CallumAlder | 42:121148278dae | 452 | |
CallumAlder | 48:b2afe48ced0d | 453 | _pComm = NULL; // Initialise as NULL |
CallumAlder | 48:b2afe48ced0d | 454 | _RUN = false; |
CallumAlder | 42:121148278dae | 455 | |
CallumAlder | 47:21bf4096faa1 | 456 | _MAXPWM_PRD = 2e3; |
CallumAlder | 42:121148278dae | 457 | |
adehadd | 51:c03f63c6f930 | 458 | maxEncCount = 0; |
adehadd | 51:c03f63c6f930 | 459 | encCount = 0; |
adehadd | 51:c03f63c6f930 | 460 | encTotal = 0; |
adehadd | 51:c03f63c6f930 | 461 | badEdges = 0; |
adehadd | 51:c03f63c6f930 | 462 | |
adehadd | 51:c03f63c6f930 | 463 | quadratureStates = 1112; |
adehadd | 51:c03f63c6f930 | 464 | |
CallumAlder | 42:121148278dae | 465 | } |
adehadd | 27:ce05fed3c1ea | 466 | |
adehadd | 51:c03f63c6f930 | 467 | int findMinTorque() { |
adehadd | 51:c03f63c6f930 | 468 | int8_t prevState = readRotorState(); |
adehadd | 51:c03f63c6f930 | 469 | uint32_t _mtPeriod = 700; |
adehadd | 51:c03f63c6f930 | 470 | Timer testTimer; |
adehadd | 51:c03f63c6f930 | 471 | testTimer.start(); |
adehadd | 51:c03f63c6f930 | 472 | |
adehadd | 51:c03f63c6f930 | 473 | _pComm->_pc.printf("PState:%i, CState:%i\n", prevState, readRotorState()); |
adehadd | 51:c03f63c6f930 | 474 | |
adehadd | 51:c03f63c6f930 | 475 | // stateUpdate(); |
adehadd | 51:c03f63c6f930 | 476 | |
adehadd | 51:c03f63c6f930 | 477 | while (readRotorState() == prevState) { |
adehadd | 51:c03f63c6f930 | 478 | testTimer.reset(); |
adehadd | 51:c03f63c6f930 | 479 | // pwmCtrl.period_us(2e3); |
adehadd | 51:c03f63c6f930 | 480 | pwmCtrl.pulsewidth_us(_mtPeriod); |
adehadd | 51:c03f63c6f930 | 481 | stateUpdate(); |
adehadd | 51:c03f63c6f930 | 482 | |
adehadd | 51:c03f63c6f930 | 483 | while (testTimer.read_ms() < 500) {} |
adehadd | 51:c03f63c6f930 | 484 | // prevState = readRotorState(); |
adehadd | 51:c03f63c6f930 | 485 | _mtPeriod += 10; |
adehadd | 51:c03f63c6f930 | 486 | _mtPeriod = _mtPeriod >= 1000 ? 700 : _mtPeriod; |
adehadd | 51:c03f63c6f930 | 487 | } |
adehadd | 51:c03f63c6f930 | 488 | |
adehadd | 51:c03f63c6f930 | 489 | _pComm->_pc.printf("Min Torque:%i\n", _mtPeriod); |
adehadd | 51:c03f63c6f930 | 490 | return _mtPeriod; |
adehadd | 51:c03f63c6f930 | 491 | } |
adehadd | 51:c03f63c6f930 | 492 | |
adehadd | 51:c03f63c6f930 | 493 | |
CallumAlder | 48:b2afe48ced0d | 494 | //-- Start Motor and Attach State Update Function to Rise/Fall Interrupts -------------------------------------------// |
CallumAlder | 42:121148278dae | 495 | void motorStart(Comm *comm) { |
CallumAlder | 42:121148278dae | 496 | |
CallumAlder | 48:b2afe48ced0d | 497 | I1.fall(callback(this, &Motor::stateUpdate)); // Establish Photo Interrupter Service Routines (auto choose next state) |
CallumAlder | 42:121148278dae | 498 | I2.fall(callback(this, &Motor::stateUpdate)); |
CallumAlder | 42:121148278dae | 499 | I3.fall(callback(this, &Motor::stateUpdate)); |
CallumAlder | 42:121148278dae | 500 | I1.rise(callback(this, &Motor::stateUpdate)); |
CallumAlder | 42:121148278dae | 501 | I2.rise(callback(this, &Motor::stateUpdate)); |
CallumAlder | 42:121148278dae | 502 | I3.rise(callback(this, &Motor::stateUpdate)); |
CallumAlder | 42:121148278dae | 503 | |
CallumAlder | 48:b2afe48ced0d | 504 | motorOut((_currentState-_orState+_lead+6)%6); // Push digitally so static motor will start moving |
CallumAlder | 42:121148278dae | 505 | |
CallumAlder | 48:b2afe48ced0d | 506 | |
CallumAlder | 48:b2afe48ced0d | 507 | _dutyC = 0.8; // Default a lower duty cycle |
adehadd | 51:c03f63c6f930 | 508 | pwmCtrl.period_us((uint32_t)_mtrPeriod); |
adehadd | 51:c03f63c6f930 | 509 | pwmCtrl.pulsewidth_us((uint32_t)(_mtrPeriod*_dutyC)); |
CallumAlder | 42:121148278dae | 510 | |
CallumAlder | 48:b2afe48ced0d | 511 | _pComm = comm; |
CallumAlder | 42:121148278dae | 512 | _RUN = true; |
iachinweze1 | 53:89d16b398615 | 513 | MINPWM_PRD = 920; |
adehadd | 51:c03f63c6f930 | 514 | |
CallumAlder | 42:121148278dae | 515 | |
CallumAlder | 48:b2afe48ced0d | 516 | _tMotorCtrl.start(callback(this, &Motor::motorCtrlFn)); // Start motor control thread |
CallumAlder | 48:b2afe48ced0d | 517 | _pComm->_pc.printf("origin=%i, _theStates=[%i,%i,%i]\n\r", _orState, _theStates[0], _theStates[1], _theStates[2]); // Print information to terminal |
CallumAlder | 42:121148278dae | 518 | |
CallumAlder | 42:121148278dae | 519 | } |
CallumAlder | 42:121148278dae | 520 | |
CallumAlder | 48:b2afe48ced0d | 521 | //-- Set a Predetermined Drive State -------------------------------------------------------------------------------// |
CallumAlder | 42:121148278dae | 522 | void motorOut(int8_t driveState) { |
iachinweze1 | 23:ab1cb51527d1 | 523 | |
CallumAlder | 48:b2afe48ced0d | 524 | int8_t driveOut = driveTable[driveState & 0x07]; //Lookup the output byte from the drive state |
CallumAlder | 42:121148278dae | 525 | |
CallumAlder | 48:b2afe48ced0d | 526 | if (~driveOut & 0x01) L1L = 0; //Turn off first |
CallumAlder | 42:121148278dae | 527 | if (~driveOut & 0x02) L1H = 1; |
CallumAlder | 42:121148278dae | 528 | if (~driveOut & 0x04) L2L = 0; |
CallumAlder | 42:121148278dae | 529 | if (~driveOut & 0x08) L2H = 1; |
CallumAlder | 42:121148278dae | 530 | if (~driveOut & 0x10) L3L = 0; |
CallumAlder | 42:121148278dae | 531 | if (~driveOut & 0x20) L3H = 1; |
CallumAlder | 42:121148278dae | 532 | |
CallumAlder | 48:b2afe48ced0d | 533 | if (driveOut & 0x01) L1L = 1; //Then turn on |
CallumAlder | 48:b2afe48ced0d | 534 | if (driveOut & 0x02) L1H = 0; |
CallumAlder | 48:b2afe48ced0d | 535 | if (driveOut & 0x04) L2L = 1; |
CallumAlder | 48:b2afe48ced0d | 536 | if (driveOut & 0x08) L2H = 0; |
CallumAlder | 48:b2afe48ced0d | 537 | if (driveOut & 0x10) L3L = 1; |
CallumAlder | 48:b2afe48ced0d | 538 | if (driveOut & 0x20) L3H = 0; |
CallumAlder | 42:121148278dae | 539 | } |
CallumAlder | 42:121148278dae | 540 | |
CallumAlder | 48:b2afe48ced0d | 541 | //-- Inline Conversion of Photointerrupts to a Rotor State ----------------------------------------------------------// |
adehadd | 49:ae8dedfe2d0f | 542 | inline int8_t readRotorState() { |
CallumAlder | 42:121148278dae | 543 | return stateMap[I1 + 2*I2 + 4*I3]; |
CallumAlder | 42:121148278dae | 544 | } |
CallumAlder | 42:121148278dae | 545 | |
CallumAlder | 48:b2afe48ced0d | 546 | //-- Basic Motor Stabilisation and Synchronisation ------------------------------------------------------------------// |
CallumAlder | 42:121148278dae | 547 | int8_t motorHome() { |
CallumAlder | 48:b2afe48ced0d | 548 | |
CallumAlder | 48:b2afe48ced0d | 549 | motorOut(0); //Put the motor in drive state 0 and wait for it to stabilise |
CallumAlder | 42:121148278dae | 550 | wait(3.0); |
CallumAlder | 42:121148278dae | 551 | |
adehadd | 49:ae8dedfe2d0f | 552 | return readRotorState(); //Get the rotor state |
CallumAlder | 42:121148278dae | 553 | } |
iachinweze1 | 23:ab1cb51527d1 | 554 | |
CallumAlder | 48:b2afe48ced0d | 555 | //-- Motor State Log, Circumvents Issues From Occasionally Skipping Certain States ----------------------------------// |
CallumAlder | 42:121148278dae | 556 | void stateUpdate() { // () { // **params |
adehadd | 49:ae8dedfe2d0f | 557 | _currentState = readRotorState(); // Get current state |
adehadd | 27:ce05fed3c1ea | 558 | |
CallumAlder | 48:b2afe48ced0d | 559 | motorOut((_currentState - _orState + _lead + 6) % 6); // Send the next state to the motor |
iachinweze1 | 23:ab1cb51527d1 | 560 | |
adehadd | 51:c03f63c6f930 | 561 | if (_currentState == 1) { |
adehadd | 51:c03f63c6f930 | 562 | //if (encCount > maxEncCount) maxEncCount = encCount; |
adehadd | 51:c03f63c6f930 | 563 | //if (encCount < minEncCount) minEncCount = encCount; |
adehadd | 51:c03f63c6f930 | 564 | //if (badEdges > maxBadEdges) maxBadEdges = badEdges; |
adehadd | 51:c03f63c6f930 | 565 | |
adehadd | 51:c03f63c6f930 | 566 | //revCount++; |
adehadd | 51:c03f63c6f930 | 567 | encTotal += encCount; |
adehadd | 51:c03f63c6f930 | 568 | encCount = 0; |
adehadd | 51:c03f63c6f930 | 569 | badEdges = 0; |
adehadd | 51:c03f63c6f930 | 570 | } |
adehadd | 51:c03f63c6f930 | 571 | |
adehadd | 51:c03f63c6f930 | 572 | if (_currentState - old_rotor_state == 5) { |
adehadd | 51:c03f63c6f930 | 573 | _pComm->_motor_pos--; |
adehadd | 51:c03f63c6f930 | 574 | } else if (_currentState - old_rotor_state == -5) { |
adehadd | 51:c03f63c6f930 | 575 | _pComm->_motor_pos++; |
adehadd | 51:c03f63c6f930 | 576 | } else { |
adehadd | 51:c03f63c6f930 | 577 | _pComm->_motor_pos += (_currentState - old_rotor_state); |
adehadd | 51:c03f63c6f930 | 578 | } |
adehadd | 51:c03f63c6f930 | 579 | |
adehadd | 51:c03f63c6f930 | 580 | old_rotor_state = _currentState; |
adehadd | 51:c03f63c6f930 | 581 | } |
adehadd | 51:c03f63c6f930 | 582 | |
adehadd | 51:c03f63c6f930 | 583 | // A Rise |
adehadd | 51:c03f63c6f930 | 584 | void encISR0() { |
adehadd | 51:c03f63c6f930 | 585 | if (encState == 3) {encCount++;} |
adehadd | 51:c03f63c6f930 | 586 | else badEdges++; |
adehadd | 51:c03f63c6f930 | 587 | encState = 0; |
adehadd | 51:c03f63c6f930 | 588 | } |
adehadd | 51:c03f63c6f930 | 589 | |
adehadd | 51:c03f63c6f930 | 590 | // B Rise |
adehadd | 51:c03f63c6f930 | 591 | void encISR1() { |
adehadd | 51:c03f63c6f930 | 592 | if (encState == 0) {encCount++;} |
adehadd | 51:c03f63c6f930 | 593 | else badEdges++; |
adehadd | 51:c03f63c6f930 | 594 | encState = 1; |
adehadd | 51:c03f63c6f930 | 595 | } |
adehadd | 51:c03f63c6f930 | 596 | |
adehadd | 51:c03f63c6f930 | 597 | // A Fall |
adehadd | 51:c03f63c6f930 | 598 | void encISR2() { |
adehadd | 51:c03f63c6f930 | 599 | if (encState == 1) {encCount++;} |
adehadd | 51:c03f63c6f930 | 600 | else badEdges++; |
adehadd | 51:c03f63c6f930 | 601 | encState = 2; |
adehadd | 51:c03f63c6f930 | 602 | } |
adehadd | 51:c03f63c6f930 | 603 | |
adehadd | 51:c03f63c6f930 | 604 | // B Fall |
adehadd | 51:c03f63c6f930 | 605 | void encISR3() { |
adehadd | 51:c03f63c6f930 | 606 | if (encState == 2) {encCount++;} |
adehadd | 51:c03f63c6f930 | 607 | else badEdges++; |
adehadd | 51:c03f63c6f930 | 608 | encState = 3; |
CallumAlder | 42:121148278dae | 609 | } |
CallumAlder | 19:805c87360b55 | 610 | |
CallumAlder | 48:b2afe48ced0d | 611 | //-- Motor PID Control ---------------------------------------------------------------------------------------------// |
CallumAlder | 42:121148278dae | 612 | void motorCtrlFn() { |
CallumAlder | 42:121148278dae | 613 | Ticker motorCtrlTicker; |
CallumAlder | 42:121148278dae | 614 | Timer m_timer; |
CallumAlder | 42:121148278dae | 615 | motorCtrlTicker.attach_us(callback(this,&Motor::motorCtrlTick), 1e5); |
iachinweze1 | 23:ab1cb51527d1 | 616 | |
CallumAlder | 42:121148278dae | 617 | // Init some things |
CallumAlder | 48:b2afe48ced0d | 618 | uint8_t cpyStateCount[3]; |
CallumAlder | 48:b2afe48ced0d | 619 | uint8_t cpyCurrentState; |
CallumAlder | 48:b2afe48ced0d | 620 | int8_t cpyModeBitfield; |
CallumAlder | 42:121148278dae | 621 | |
CallumAlder | 48:b2afe48ced0d | 622 | int32_t ting[2] = {6,1}; // 360,60 (for degrees), 5,1 (for states) |
CallumAlder | 48:b2afe48ced0d | 623 | uint8_t iterElementMax; |
CallumAlder | 48:b2afe48ced0d | 624 | int32_t totalDegrees; |
CallumAlder | 48:b2afe48ced0d | 625 | int32_t stateDiff; |
adehadd | 27:ce05fed3c1ea | 626 | |
adehadd | 51:c03f63c6f930 | 627 | // int32_t cur_speed; // Variable for local velocity calculation |
adehadd | 51:c03f63c6f930 | 628 | // int32_t locMotorPos; // Local copy of motor position |
CallumAlder | 47:21bf4096faa1 | 629 | volatile int32_t torque; // Local variable to set motor torque |
CallumAlder | 48:b2afe48ced0d | 630 | static int32_t oldTorque =0; |
CallumAlder | 48:b2afe48ced0d | 631 | float sError, // Velocity error between target and reality |
adehadd | 51:c03f63c6f930 | 632 | rError, |
adehadd | 51:c03f63c6f930 | 633 | eError = 0; // Rotation error between target and reality |
CallumAlder | 48:b2afe48ced0d | 634 | static float rErrorOld; // Old rotation error used for calculation |
adehadd | 51:c03f63c6f930 | 635 | float encRemain = 0; |
adehadd | 51:c03f63c6f930 | 636 | bool attached = true; |
adehadd | 51:c03f63c6f930 | 637 | bool declared = false; |
CallumAlder | 42:121148278dae | 638 | |
CallumAlder | 42:121148278dae | 639 | //~~~Controller constants~~~~ |
iachinweze1 | 53:89d16b398615 | 640 | int32_t Kp1=88; //22, 27 //Proportional controller constants |
iachinweze1 | 53:89d16b398615 | 641 | int32_t Kp2=26; //12 //Calculated by trial and error to give optimal accuracy |
iachinweze1 | 53:89d16b398615 | 642 | float Kd =34.5; // 40, 14.75 |
adehadd | 51:c03f63c6f930 | 643 | float Kis = 10.0f; // 50 |
adehadd | 51:c03f63c6f930 | 644 | int32_t Kir = 0.0f; |
adehadd | 51:c03f63c6f930 | 645 | float sIntegral = 0.0f; |
adehadd | 51:c03f63c6f930 | 646 | float rIntegral = 0.0f; |
CallumAlder | 42:121148278dae | 647 | |
CallumAlder | 48:b2afe48ced0d | 648 | int32_t Ts; // Initialise controller output Ts (s=speed) |
CallumAlder | 48:b2afe48ced0d | 649 | int32_t Tr; // Initialise controller output Tr (r=rotations) |
CallumAlder | 42:121148278dae | 650 | |
adehadd | 51:c03f63c6f930 | 651 | int32_t old_pos = 0, |
iachinweze1 | 53:89d16b398615 | 652 | cur_pos = 0, |
iachinweze1 | 53:89d16b398615 | 653 | sign = 1; |
adehadd | 27:ce05fed3c1ea | 654 | |
adehadd | 51:c03f63c6f930 | 655 | float cur_err = 0.0f, |
adehadd | 51:c03f63c6f930 | 656 | old_err = 0.0f, |
adehadd | 51:c03f63c6f930 | 657 | err_diff, |
adehadd | 51:c03f63c6f930 | 658 | time_diff, |
adehadd | 51:c03f63c6f930 | 659 | hold_pos = 0, |
adehadd | 51:c03f63c6f930 | 660 | cur_time = 0.0f, |
adehadd | 51:c03f63c6f930 | 661 | old_time = 0.0f, |
adehadd | 51:c03f63c6f930 | 662 | cur_speed = 0.0f; |
CallumAlder | 42:121148278dae | 663 | |
CallumAlder | 42:121148278dae | 664 | |
adehadd | 50:d1b983a0dd6f | 665 | |
adehadd | 50:d1b983a0dd6f | 666 | enum { // To nearest Hz |
adehadd | 50:d1b983a0dd6f | 667 | NOTE_C = 261, // C4 |
adehadd | 50:d1b983a0dd6f | 668 | NOTE_D = 293, // D4 |
adehadd | 50:d1b983a0dd6f | 669 | NOTE_E = 330, // E4 |
adehadd | 50:d1b983a0dd6f | 670 | NOTE_F = 349, // F4 |
adehadd | 50:d1b983a0dd6f | 671 | NOTE_G = 392, // G4 |
adehadd | 50:d1b983a0dd6f | 672 | NOTE_A = 440, // A4 |
adehadd | 50:d1b983a0dd6f | 673 | NOTE_B = 494 // B4 |
adehadd | 50:d1b983a0dd6f | 674 | }; |
adehadd | 50:d1b983a0dd6f | 675 | |
CallumAlder | 42:121148278dae | 676 | m_timer.start(); |
CallumAlder | 42:121148278dae | 677 | |
CallumAlder | 42:121148278dae | 678 | while (_RUN) { |
CallumAlder | 48:b2afe48ced0d | 679 | _tMotorCtrl.signal_wait((int32_t)0x1); |
iachinweze1 | 23:ab1cb51527d1 | 680 | |
CallumAlder | 48:b2afe48ced0d | 681 | core_util_critical_section_enter(); // Access shared variables here |
iachinweze1 | 53:89d16b398615 | 682 | cpyModeBitfield = _pComm->_modeBitField; |
CallumAlder | 42:121148278dae | 683 | core_util_critical_section_exit(); |
adehadd | 20:c60f4785b556 | 684 | |
iachinweze1 | 53:89d16b398615 | 685 | if ((cpyModeBitfield & 0x01) | (cpyModeBitfield & 0x02)) {// Speed, torque control and PID |
adehadd | 51:c03f63c6f930 | 686 | if (abs(cur_speed) <= 120 && (!attached)) { |
adehadd | 51:c03f63c6f930 | 687 | eError = 1.0f; |
adehadd | 51:c03f63c6f930 | 688 | attached = true; |
adehadd | 51:c03f63c6f930 | 689 | |
adehadd | 51:c03f63c6f930 | 690 | CHA.rise(callback(this, &Motor::encISR0)); |
adehadd | 51:c03f63c6f930 | 691 | CHB.fall(callback(this, &Motor::encISR3)); |
adehadd | 51:c03f63c6f930 | 692 | CHB.rise(callback(this, &Motor::encISR1)); |
adehadd | 51:c03f63c6f930 | 693 | CHA.fall(callback(this, &Motor::encISR2)); |
adehadd | 51:c03f63c6f930 | 694 | |
adehadd | 51:c03f63c6f930 | 695 | encCount = 0; |
adehadd | 51:c03f63c6f930 | 696 | encTotal = 0; |
adehadd | 51:c03f63c6f930 | 697 | encRemain = old_err * quadratureStates; // Number of encs remaining |
adehadd | 51:c03f63c6f930 | 698 | } |
adehadd | 51:c03f63c6f930 | 699 | |
adehadd | 51:c03f63c6f930 | 700 | else if (abs(cur_speed) > 120 && (attached)) { |
adehadd | 51:c03f63c6f930 | 701 | eError = 0.0f; |
adehadd | 51:c03f63c6f930 | 702 | attached = false; |
adehadd | 51:c03f63c6f930 | 703 | |
adehadd | 51:c03f63c6f930 | 704 | CHA.rise(NULL); |
adehadd | 51:c03f63c6f930 | 705 | CHB.fall(NULL); |
adehadd | 51:c03f63c6f930 | 706 | CHB.rise(NULL); |
adehadd | 51:c03f63c6f930 | 707 | CHA.fall(NULL); |
adehadd | 51:c03f63c6f930 | 708 | |
adehadd | 51:c03f63c6f930 | 709 | encCount = 0; |
adehadd | 51:c03f63c6f930 | 710 | encTotal = 0; |
adehadd | 51:c03f63c6f930 | 711 | eError = 0; |
adehadd | 51:c03f63c6f930 | 712 | |
adehadd | 51:c03f63c6f930 | 713 | // Store the number of rotations that have been done up to the point where the channels are activated |
adehadd | 51:c03f63c6f930 | 714 | hold_pos = (cur_pos / 6.0f); |
adehadd | 51:c03f63c6f930 | 715 | _pComm->_pc.printf("HPos:%f\n", hold_pos); |
adehadd | 51:c03f63c6f930 | 716 | |
adehadd | 51:c03f63c6f930 | 717 | } |
CallumAlder | 42:121148278dae | 718 | |
adehadd | 51:c03f63c6f930 | 719 | // read state & timestamp |
adehadd | 51:c03f63c6f930 | 720 | if (cur_pos < 2) { |
adehadd | 51:c03f63c6f930 | 721 | rIntegral = 0.0f; |
iachinweze1 | 53:89d16b398615 | 722 | sIntegral = 0.0f; |
iachinweze1 | 53:89d16b398615 | 723 | sign = sgn(_pComm->_targetRot); |
adehadd | 51:c03f63c6f930 | 724 | } |
iachinweze1 | 53:89d16b398615 | 725 | cur_pos = (_pComm->_motor_pos)*sign; // USE |
adehadd | 51:c03f63c6f930 | 726 | cur_time = m_timer.read(); |
adehadd | 51:c03f63c6f930 | 727 | |
adehadd | 51:c03f63c6f930 | 728 | // compute speed |
adehadd | 51:c03f63c6f930 | 729 | time_diff = cur_time - old_time; |
adehadd | 51:c03f63c6f930 | 730 | cur_speed = (cur_pos - old_pos) / time_diff; |
adehadd | 51:c03f63c6f930 | 731 | |
adehadd | 51:c03f63c6f930 | 732 | // prep values for next time through loop |
adehadd | 51:c03f63c6f930 | 733 | old_time = cur_time; |
adehadd | 51:c03f63c6f930 | 734 | old_pos = cur_pos; |
adehadd | 51:c03f63c6f930 | 735 | |
adehadd | 51:c03f63c6f930 | 736 | // Position error |
adehadd | 51:c03f63c6f930 | 737 | // if (!attached) { |
adehadd | 51:c03f63c6f930 | 738 | rError = (_pComm->_targetRot) - (cur_pos/6.0f); |
adehadd | 51:c03f63c6f930 | 739 | // } |
adehadd | 51:c03f63c6f930 | 740 | // else{ |
adehadd | 51:c03f63c6f930 | 741 | // rErrorOld = RError; |
adehadd | 51:c03f63c6f930 | 742 | // rError = (_pComm->_targetRot) - (hold_pos + ((encRemain - encTotal)/quadratureStates)); |
adehadd | 51:c03f63c6f930 | 743 | // } |
adehadd | 51:c03f63c6f930 | 744 | |
adehadd | 51:c03f63c6f930 | 745 | |
adehadd | 51:c03f63c6f930 | 746 | if ((cur_speed != 0)) { |
adehadd | 51:c03f63c6f930 | 747 | rIntegral += rError * time_diff; |
adehadd | 51:c03f63c6f930 | 748 | } |
adehadd | 51:c03f63c6f930 | 749 | err_diff = rError - old_err; |
adehadd | 51:c03f63c6f930 | 750 | old_err = rError; |
adehadd | 51:c03f63c6f930 | 751 | |
adehadd | 51:c03f63c6f930 | 752 | // Speed error - Convert curr_speed from states per time to rotations per time |
adehadd | 51:c03f63c6f930 | 753 | // TODO: Check the direction that CPos goes in when _targetVel < 0 |
adehadd | 51:c03f63c6f930 | 754 | sError = (_pComm->_targetVel) - (abs(cur_speed/6.0f)); //Read global variable _targetVel updated by interrupt and calculate error between target and reality |
adehadd | 51:c03f63c6f930 | 755 | if ((cur_speed != 0) && (torque != _MAXPWM_PRD)) { |
adehadd | 51:c03f63c6f930 | 756 | sIntegral += sError * time_diff; |
adehadd | 51:c03f63c6f930 | 757 | if (abs(sIntegral * Kis) >= 2000) { |
adehadd | 51:c03f63c6f930 | 758 | sIntegral -= sError * time_diff; |
adehadd | 51:c03f63c6f930 | 759 | } |
adehadd | 51:c03f63c6f930 | 760 | } |
adehadd | 51:c03f63c6f930 | 761 | |
iachinweze1 | 53:89d16b398615 | 762 | Ts = (int32_t)(((Kp1 * sError * sign) + (Kis * sIntegral * sign))); // * sgn(cur_pos)); |
iachinweze1 | 53:89d16b398615 | 763 | Tr = (int32_t)((Kp2*rError*sign) + (Kd/time_diff) * err_diff * sign); |
CallumAlder | 42:121148278dae | 764 | |
adehadd | 51:c03f63c6f930 | 765 | if (cpyModeBitfield & 0x01) { |
adehadd | 51:c03f63c6f930 | 766 | // Speed control |
adehadd | 51:c03f63c6f930 | 767 | if (_pComm->_targetVel == 0) { //Check if user entered V0, |
adehadd | 51:c03f63c6f930 | 768 | torque = _MAXPWM_PRD; //and set the output to maximum as specified |
adehadd | 51:c03f63c6f930 | 769 | } |
adehadd | 51:c03f63c6f930 | 770 | |
adehadd | 51:c03f63c6f930 | 771 | else { |
adehadd | 51:c03f63c6f930 | 772 | // select minimum absolute value torque |
iachinweze1 | 53:89d16b398615 | 773 | // if (cur_speed < 0) { |
iachinweze1 | 53:89d16b398615 | 774 | // torque = max(Ts, Tr); |
iachinweze1 | 53:89d16b398615 | 775 | // } |
adehadd | 51:c03f63c6f930 | 776 | |
iachinweze1 | 53:89d16b398615 | 777 | // else { |
iachinweze1 | 53:89d16b398615 | 778 | // torque = min(Ts, Tr); |
iachinweze1 | 53:89d16b398615 | 779 | // } |
adehadd | 51:c03f63c6f930 | 780 | |
adehadd | 51:c03f63c6f930 | 781 | torque = Ts; |
adehadd | 51:c03f63c6f930 | 782 | if (abs(sError) > 0.6) { |
adehadd | 51:c03f63c6f930 | 783 | // torque = abs(Tr) < 1000 ? 1000*sgn(Tr) : Tr; |
adehadd | 51:c03f63c6f930 | 784 | torque = abs(torque) < MINPWM_PRD ? MINPWM_PRD*sgn(torque) : torque; |
iachinweze1 | 53:89d16b398615 | 785 | // _pComm->_pc.printf("Speed:%f\r\n", (cur_speed/6.0f)); |
adehadd | 51:c03f63c6f930 | 786 | } else { |
adehadd | 51:c03f63c6f930 | 787 | torque = 0; |
adehadd | 51:c03f63c6f930 | 788 | } |
adehadd | 51:c03f63c6f930 | 789 | |
iachinweze1 | 53:89d16b398615 | 790 | torque = abs(torque) < MINPWM_PRD ? MINPWM_PRD*sgn(torque) : torque; |
adehadd | 51:c03f63c6f930 | 791 | } |
adehadd | 51:c03f63c6f930 | 792 | } |
adehadd | 51:c03f63c6f930 | 793 | |
adehadd | 51:c03f63c6f930 | 794 | else if (cpyModeBitfield & 0x02) { |
adehadd | 51:c03f63c6f930 | 795 | // select minimum absolute value torque |
adehadd | 51:c03f63c6f930 | 796 | if (_pComm->_targetRot == 0) { |
adehadd | 51:c03f63c6f930 | 797 | // Not spinning at max speed |
adehadd | 51:c03f63c6f930 | 798 | torque = 0.7 * _MAXPWM_PRD; |
adehadd | 51:c03f63c6f930 | 799 | } |
CallumAlder | 42:121148278dae | 800 | |
adehadd | 51:c03f63c6f930 | 801 | else { |
iachinweze1 | 53:89d16b398615 | 802 | // select minimum absolute value torque |
iachinweze1 | 53:89d16b398615 | 803 | // if (cur_speed < 0) { |
iachinweze1 | 53:89d16b398615 | 804 | // torque = max(Ts, Tr); |
iachinweze1 | 53:89d16b398615 | 805 | // } else { |
iachinweze1 | 53:89d16b398615 | 806 | // torque = min(Ts, Tr); |
iachinweze1 | 53:89d16b398615 | 807 | // } |
adehadd | 51:c03f63c6f930 | 808 | |
iachinweze1 | 53:89d16b398615 | 809 | torque = Tr; |
iachinweze1 | 53:89d16b398615 | 810 | if (abs(rError) > 0.3) { |
adehadd | 51:c03f63c6f930 | 811 | torque = abs(torque) < MINPWM_PRD ? MINPWM_PRD*sgn(torque) : torque; |
adehadd | 51:c03f63c6f930 | 812 | } else { |
adehadd | 51:c03f63c6f930 | 813 | torque = 0; |
adehadd | 51:c03f63c6f930 | 814 | if (!declared) { |
adehadd | 51:c03f63c6f930 | 815 | _pComm->_pc.printf("Remaining Turns:%f\n", rError); |
adehadd | 51:c03f63c6f930 | 816 | declared = true; |
adehadd | 51:c03f63c6f930 | 817 | } |
adehadd | 51:c03f63c6f930 | 818 | //attached = false; |
adehadd | 51:c03f63c6f930 | 819 | } |
adehadd | 51:c03f63c6f930 | 820 | |
adehadd | 51:c03f63c6f930 | 821 | } |
adehadd | 51:c03f63c6f930 | 822 | } |
adehadd | 51:c03f63c6f930 | 823 | |
adehadd | 51:c03f63c6f930 | 824 | if (torque < 0) { |
adehadd | 51:c03f63c6f930 | 825 | torque = -torque; |
adehadd | 51:c03f63c6f930 | 826 | _lead = -2; |
adehadd | 51:c03f63c6f930 | 827 | } else { |
adehadd | 51:c03f63c6f930 | 828 | _lead = 2; |
adehadd | 51:c03f63c6f930 | 829 | } |
adehadd | 51:c03f63c6f930 | 830 | |
adehadd | 51:c03f63c6f930 | 831 | torque = torque > _MAXPWM_PRD ? _MAXPWM_PRD : torque; // Set a cap on torque |
adehadd | 51:c03f63c6f930 | 832 | _pComm->_motorTorque = torque; |
adehadd | 51:c03f63c6f930 | 833 | pwmCtrl.pulsewidth_us(torque); |
adehadd | 51:c03f63c6f930 | 834 | // _pComm->_pc.printf("EError:%.4f, tot:%d, encRemain:%.4f || RError:%.4f || SError:%.4f, CSpeed:%f, Torque:%i\r\n", eError, encTotal, encRemain, rError, sError, (cur_speed/6.0f), torque); |
adehadd | 51:c03f63c6f930 | 835 | //_pComm->_pc.printf("EError:%.4f, remain:%.4f, tot%d || RError:%.4f || Torque:%i \r\n", eError, encRemain, encTotal, rError, torque); |
iachinweze1 | 54:bd5b586aa2e1 | 836 | // _pComm->_pc.printf("RError:%.4f\r\n", rError); |
adehadd | 51:c03f63c6f930 | 837 | |
adehadd | 51:c03f63c6f930 | 838 | |
adehadd | 51:c03f63c6f930 | 839 | // Give the motor a kick |
adehadd | 51:c03f63c6f930 | 840 | stateUpdate(); |
CallumAlder | 47:21bf4096faa1 | 841 | |
CallumAlder | 47:21bf4096faa1 | 842 | } |
CallumAlder | 47:21bf4096faa1 | 843 | else if (cpyModeBitfield & 0x04) { // If it is in torque mode, do no PID math, just set pulsewidth |
CallumAlder | 48:b2afe48ced0d | 844 | torque = (int32_t)_pComm->_motorTorque; |
CallumAlder | 47:21bf4096faa1 | 845 | if (oldTorque != torque) { |
CallumAlder | 48:b2afe48ced0d | 846 | _pComm->putMessage((Comm::msgType)8, torque); |
CallumAlder | 47:21bf4096faa1 | 847 | oldTorque = torque; |
CallumAlder | 42:121148278dae | 848 | } |
CallumAlder | 47:21bf4096faa1 | 849 | } |
adehadd | 50:d1b983a0dd6f | 850 | else if (cpyModeBitfield & 0x08) { // If it is in Melody mode |
adehadd | 51:c03f63c6f930 | 851 | uint32_t oldMtrPeriod = _mtrPeriod; // Backup of current motor period |
adehadd | 50:d1b983a0dd6f | 852 | float oldDutyC = _dutyC; // Backup of current duty cycle |
adehadd | 50:d1b983a0dd6f | 853 | uint32_t noteFreq, newDur, newPeriod =0, |
adehadd | 51:c03f63c6f930 | 854 | oldPeriod = _mtrPeriod; |
adehadd | 50:d1b983a0dd6f | 855 | |
adehadd | 50:d1b983a0dd6f | 856 | Timer testTimer; |
adehadd | 50:d1b983a0dd6f | 857 | testTimer.start(); |
adehadd | 50:d1b983a0dd6f | 858 | _pComm->_pc.printf("rep:%i, len:%i \n\r", _pComm->_noteRep, _pComm->_noteLen); |
adehadd | 50:d1b983a0dd6f | 859 | for (int k = 0; k < _pComm->_noteRep; ++k) { |
adehadd | 50:d1b983a0dd6f | 860 | |
adehadd | 50:d1b983a0dd6f | 861 | for (int i = 0; i < _pComm->_noteLen; ++i) { |
adehadd | 50:d1b983a0dd6f | 862 | |
adehadd | 50:d1b983a0dd6f | 863 | noteFreq = 0; |
adehadd | 50:d1b983a0dd6f | 864 | newDur = (uint32_t)_pComm->_noteDur[i]*1e3; // ms |
adehadd | 50:d1b983a0dd6f | 865 | |
adehadd | 50:d1b983a0dd6f | 866 | switch (_pComm->_notes[i]) { |
adehadd | 50:d1b983a0dd6f | 867 | case 'C': |
adehadd | 50:d1b983a0dd6f | 868 | noteFreq = NOTE_C; |
adehadd | 50:d1b983a0dd6f | 869 | break; |
adehadd | 50:d1b983a0dd6f | 870 | case 'D': |
adehadd | 50:d1b983a0dd6f | 871 | noteFreq = NOTE_D; |
adehadd | 50:d1b983a0dd6f | 872 | break; |
adehadd | 50:d1b983a0dd6f | 873 | case 'E': |
adehadd | 50:d1b983a0dd6f | 874 | noteFreq = NOTE_E; |
adehadd | 50:d1b983a0dd6f | 875 | break; |
adehadd | 50:d1b983a0dd6f | 876 | case 'F': |
adehadd | 50:d1b983a0dd6f | 877 | noteFreq = NOTE_F; |
adehadd | 50:d1b983a0dd6f | 878 | break; |
adehadd | 50:d1b983a0dd6f | 879 | case 'G': |
adehadd | 50:d1b983a0dd6f | 880 | noteFreq = NOTE_G; |
adehadd | 50:d1b983a0dd6f | 881 | break; |
adehadd | 50:d1b983a0dd6f | 882 | case 'A': |
adehadd | 50:d1b983a0dd6f | 883 | noteFreq = NOTE_A; |
adehadd | 50:d1b983a0dd6f | 884 | break; |
adehadd | 50:d1b983a0dd6f | 885 | case 'B': |
adehadd | 50:d1b983a0dd6f | 886 | noteFreq = NOTE_B; |
adehadd | 50:d1b983a0dd6f | 887 | break; |
adehadd | 50:d1b983a0dd6f | 888 | default: |
adehadd | 50:d1b983a0dd6f | 889 | break; |
adehadd | 50:d1b983a0dd6f | 890 | } |
adehadd | 50:d1b983a0dd6f | 891 | |
adehadd | 51:c03f63c6f930 | 892 | if (noteFreq == 0) {break;} // leave the loop if we get a wrong character |
adehadd | 51:c03f63c6f930 | 893 | |
adehadd | 50:d1b983a0dd6f | 894 | newPeriod = (uint32_t)(1e6/(uint32_t)noteFreq); // us |
adehadd | 50:d1b983a0dd6f | 895 | |
adehadd | 50:d1b983a0dd6f | 896 | if (newPeriod>oldPeriod) { // Change Period First |
iachinweze1 | 55:e8b15b4b875f | 897 | // _pComm->_pc.printf("Period:changed \n" ); |
adehadd | 50:d1b983a0dd6f | 898 | pwmCtrl.period_us(newPeriod); //set frequency of PWM |
adehadd | 50:d1b983a0dd6f | 899 | pwmCtrl.pulsewidth_us((uint32_t)(newPeriod*0.8)); |
adehadd | 50:d1b983a0dd6f | 900 | } else { // Change Pulse WidthFirst |
adehadd | 50:d1b983a0dd6f | 901 | pwmCtrl.pulsewidth_us((uint32_t)(newPeriod*0.8)); |
adehadd | 50:d1b983a0dd6f | 902 | pwmCtrl.period_us(newPeriod); //set frequency of PWM |
adehadd | 50:d1b983a0dd6f | 903 | } |
adehadd | 50:d1b983a0dd6f | 904 | // stateUpdate(); |
adehadd | 50:d1b983a0dd6f | 905 | oldPeriod = newPeriod; |
adehadd | 50:d1b983a0dd6f | 906 | testTimer.reset(); |
adehadd | 50:d1b983a0dd6f | 907 | while (testTimer.read_ms() < newDur ) {} // Do nothing |
iachinweze1 | 55:e8b15b4b875f | 908 | // _pComm->_pc.printf("Period:%d Dur:%d \n",newPeriod, newDur ); |
adehadd | 50:d1b983a0dd6f | 909 | } |
adehadd | 50:d1b983a0dd6f | 910 | |
adehadd | 50:d1b983a0dd6f | 911 | _pComm->_modeBitField = 0x04; // stop melody mode |
adehadd | 50:d1b983a0dd6f | 912 | |
adehadd | 50:d1b983a0dd6f | 913 | } |
adehadd | 50:d1b983a0dd6f | 914 | // After playing notes, go back to the old speed |
adehadd | 50:d1b983a0dd6f | 915 | torque = (int32_t)_pComm->_motorTorque; |
adehadd | 50:d1b983a0dd6f | 916 | pwmCtrl.pulsewidth_us((uint32_t)(torque*0.8)); |
adehadd | 50:d1b983a0dd6f | 917 | // And reset the motor period and duty cycle |
adehadd | 50:d1b983a0dd6f | 918 | |
adehadd | 50:d1b983a0dd6f | 919 | } |
CallumAlder | 47:21bf4096faa1 | 920 | else{ |
CallumAlder | 47:21bf4096faa1 | 921 | torque = _MAXPWM_PRD * 0.5; // Run at 50% duty cycle if argument not properly defined |
adehadd | 27:ce05fed3c1ea | 922 | |
CallumAlder | 42:121148278dae | 923 | } |
CallumAlder | 42:121148278dae | 924 | } |
CallumAlder | 42:121148278dae | 925 | } |
CallumAlder | 42:121148278dae | 926 | |
CallumAlder | 42:121148278dae | 927 | void motorCtrlTick(){ |
CallumAlder | 48:b2afe48ced0d | 928 | _tMotorCtrl.signal_set(0x1); |
CallumAlder | 42:121148278dae | 929 | } |
CallumAlder | 42:121148278dae | 930 | }; |
CallumAlder | 42:121148278dae | 931 | |
CallumAlder | 42:121148278dae | 932 | |
adehadd | 27:ce05fed3c1ea | 933 | int main() { |
adehadd | 26:fb6151e5907d | 934 | |
CallumAlder | 42:121148278dae | 935 | // Declare Objects |
CallumAlder | 42:121148278dae | 936 | Comm comm_port; |
CallumAlder | 42:121148278dae | 937 | SHA256 miner; |
CallumAlder | 42:121148278dae | 938 | Motor motor; |
adehadd | 27:ce05fed3c1ea | 939 | |
CallumAlder | 42:121148278dae | 940 | // Start Motor and Comm Port |
CallumAlder | 42:121148278dae | 941 | motor.motorStart(&comm_port); |
CallumAlder | 42:121148278dae | 942 | comm_port.start_comm(); |
adehadd | 27:ce05fed3c1ea | 943 | |
CallumAlder | 42:121148278dae | 944 | // Declare Hash Variables |
CallumAlder | 47:21bf4096faa1 | 945 | uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64, |
CallumAlder | 47:21bf4096faa1 | 946 | 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73, |
CallumAlder | 47:21bf4096faa1 | 947 | 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E, |
CallumAlder | 47:21bf4096faa1 | 948 | 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20, |
CallumAlder | 47:21bf4096faa1 | 949 | 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20, |
CallumAlder | 47:21bf4096faa1 | 950 | 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20, |
CallumAlder | 47:21bf4096faa1 | 951 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
CallumAlder | 47:21bf4096faa1 | 952 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; |
CallumAlder | 47:21bf4096faa1 | 953 | uint8_t hash[32]; |
CallumAlder | 47:21bf4096faa1 | 954 | uint32_t length64 = 64; |
CallumAlder | 47:21bf4096faa1 | 955 | uint32_t hashCounter = 0; |
CallumAlder | 47:21bf4096faa1 | 956 | uint64_t* nonce = (uint64_t*)((int)sequence + 56); |
adehadd | 27:ce05fed3c1ea | 957 | uint64_t* key = (uint64_t*)((int)sequence + 48); |
iachinweze1 | 23:ab1cb51527d1 | 958 | |
CallumAlder | 42:121148278dae | 959 | // Begin Main Timer |
CallumAlder | 42:121148278dae | 960 | Timer timer; |
CallumAlder | 42:121148278dae | 961 | timer.start(); |
adehadd | 27:ce05fed3c1ea | 962 | |
CallumAlder | 47:21bf4096faa1 | 963 | // Loop Program |
CallumAlder | 42:121148278dae | 964 | while (1) { |
adehadd | 26:fb6151e5907d | 965 | |
CallumAlder | 47:21bf4096faa1 | 966 | //try{ |
CallumAlder | 47:21bf4096faa1 | 967 | |
CallumAlder | 47:21bf4096faa1 | 968 | // Mutex For Access Control |
adehadd | 49:ae8dedfe2d0f | 969 | comm_port._newKeyMutex.lock(); |
CallumAlder | 47:21bf4096faa1 | 970 | *key = comm_port._newKey; |
adehadd | 49:ae8dedfe2d0f | 971 | comm_port._newKeyMutex.unlock(); |
adehadd | 20:c60f4785b556 | 972 | |
CallumAlder | 47:21bf4096faa1 | 973 | // Compute Hash and Counter |
CallumAlder | 47:21bf4096faa1 | 974 | miner.computeHash(hash, sequence, length64); |
CallumAlder | 47:21bf4096faa1 | 975 | hashCounter++; |
CallumAlder | 47:21bf4096faa1 | 976 | |
CallumAlder | 47:21bf4096faa1 | 977 | // Enum Casting and Condition |
CallumAlder | 47:21bf4096faa1 | 978 | if (hash[0]==0 && hash[1]==0) |
CallumAlder | 47:21bf4096faa1 | 979 | comm_port.putMessage((Comm::msgType)7, *nonce); |
adehadd | 20:c60f4785b556 | 980 | |
CallumAlder | 47:21bf4096faa1 | 981 | // Try Nonce |
CallumAlder | 47:21bf4096faa1 | 982 | (*nonce)++; |
adehadd | 26:fb6151e5907d | 983 | |
CallumAlder | 47:21bf4096faa1 | 984 | // Display via Comm Port |
CallumAlder | 47:21bf4096faa1 | 985 | if (timer.read() >= 1){ |
CallumAlder | 47:21bf4096faa1 | 986 | comm_port.putMessage((Comm::msgType)5, hashCounter); |
CallumAlder | 47:21bf4096faa1 | 987 | hashCounter=0; |
CallumAlder | 47:21bf4096faa1 | 988 | timer.reset(); |
CallumAlder | 47:21bf4096faa1 | 989 | } |
CallumAlder | 47:21bf4096faa1 | 990 | //} |
adehadd | 26:fb6151e5907d | 991 | |
CallumAlder | 47:21bf4096faa1 | 992 | //catch(...){ |
CallumAlder | 47:21bf4096faa1 | 993 | // break; |
CallumAlder | 47:21bf4096faa1 | 994 | //} |
CallumAlder | 47:21bf4096faa1 | 995 | |
CallumAlder | 15:2f95f2fb68e3 | 996 | } |
CallumAlder | 42:121148278dae | 997 | |
CallumAlder | 42:121148278dae | 998 | return 0; |
adehadd | 27:ce05fed3c1ea | 999 | } |