Callum and Adel's changes on 12/02/19

Dependencies:   Crypto

Committer:
adehadd
Date:
Fri Mar 22 16:50:54 2019 +0000
Revision:
49:ae8dedfe2d0f
Parent:
48:b2afe48ced0d
Child:
50:d1b983a0dd6f
New communication thread system that works with mutex

Who changed what in which revision?

UserRevisionLine numberNew contents of line
adehadd 27:ce05fed3c1ea 1 /*TODO:
CallumAlder 42:121148278dae 2 Change:
CallumAlder 42:121148278dae 3 Indx
CallumAlder 47:21bf4096faa1 4 _newCmd
CallumAlder 43:a6d20109b2f2 5 _MAXCMDLENGTH
CallumAlder 42:121148278dae 6 move the global variables to a class because we arent paeasents - Mission Failed
CallumAlder 42:121148278dae 7 use jack's motor motor position
CallumAlder 42:121148278dae 8 fix class variable naming
CallumAlder 42:121148278dae 9 dont make everything public becuase thats fucling dumb and defeats the whole point of a class
CallumAlder 47:21bf4096faa1 10
CallumAlder 47:21bf4096faa1 11 Move things out of public and into a protected part of the class
CallumAlder 47:21bf4096faa1 12
CallumAlder 47:21bf4096faa1 13 Move char Comm::_inCharQ[] = {'.','.','... into the class by making it a vector
CallumAlder 47:21bf4096faa1 14
CallumAlder 47:21bf4096faa1 15 Change abs lacro to
CallumAlder 47:21bf4096faa1 16 NOT V0 but R0 (to go on forever)
CallumAlder 47:21bf4096faa1 17
CallumAlder 47:21bf4096faa1 18 Actually make the code robust lol
adehadd 27:ce05fed3c1ea 19 */
estott 0:de4320f74764 20
estott 0:de4320f74764 21 //Mapping from sequential drive states to motor phase outputs
estott 0:de4320f74764 22 /*
estott 0:de4320f74764 23 State L1 L2 L3
estott 0:de4320f74764 24 0 H - L
estott 0:de4320f74764 25 1 - H L
estott 0:de4320f74764 26 2 L H -
estott 0:de4320f74764 27 3 L - H
estott 0:de4320f74764 28 4 - L H
estott 0:de4320f74764 29 5 H L -
estott 0:de4320f74764 30 6 - - -
estott 0:de4320f74764 31 7 - - -
estott 0:de4320f74764 32 */
CallumAlder 42:121148278dae 33
CallumAlder 42:121148278dae 34 //Header Files
CallumAlder 42:121148278dae 35 #include "SHA256.h"
CallumAlder 42:121148278dae 36 #include "mbed.h"
CallumAlder 42:121148278dae 37
CallumAlder 42:121148278dae 38 //Photointerrupter Input Pins
CallumAlder 42:121148278dae 39 #define I1pin D3
CallumAlder 42:121148278dae 40 #define I2pin D6
CallumAlder 42:121148278dae 41 #define I3pin D5
CallumAlder 42:121148278dae 42
CallumAlder 42:121148278dae 43 //Incremental Encoder Input Pins
CallumAlder 42:121148278dae 44 #define CHApin D12
CallumAlder 42:121148278dae 45 #define CHBpin D11
CallumAlder 42:121148278dae 46
CallumAlder 42:121148278dae 47 //Motor Drive High Pins //Mask in output byte
CallumAlder 42:121148278dae 48 #define L1Hpin A3 //0x02
CallumAlder 42:121148278dae 49 #define L2Hpin A6 //0x08
CallumAlder 42:121148278dae 50 #define L3Hpin D2 //0x20
CallumAlder 42:121148278dae 51
CallumAlder 42:121148278dae 52 //Motor Drive Low Pins
CallumAlder 42:121148278dae 53 #define L1Lpin D1 //0x01
CallumAlder 42:121148278dae 54 #define L2Lpin D0 //0x04
CallumAlder 42:121148278dae 55 #define L3Lpin D10 //0x10
CallumAlder 42:121148278dae 56
CallumAlder 42:121148278dae 57 //Motor Pulse Width Modulation (PWM) Pin
CallumAlder 42:121148278dae 58 #define PWMpin D9
CallumAlder 42:121148278dae 59
CallumAlder 42:121148278dae 60 //Motor current sense
CallumAlder 42:121148278dae 61 #define MCSPpin A1
CallumAlder 42:121148278dae 62 #define MCSNpin A0
CallumAlder 42:121148278dae 63
CallumAlder 42:121148278dae 64 // "Lacros" for utility
CallumAlder 47:21bf4096faa1 65 #define max(x,y) ( (x)>=(y) ? (x):(y) )
CallumAlder 47:21bf4096faa1 66 #define min(x,y) ( (x)>=(y) ? (y):(x) )
CallumAlder 47:21bf4096faa1 67 #define sgn(x) ( (x)>= 0 ? 1 :-1 )
CallumAlder 42:121148278dae 68
CallumAlder 42:121148278dae 69 //Status LED
CallumAlder 42:121148278dae 70 DigitalOut led1(LED1);
CallumAlder 42:121148278dae 71
CallumAlder 42:121148278dae 72 //Photointerrupter Inputs
CallumAlder 42:121148278dae 73 InterruptIn I1(I1pin);
CallumAlder 42:121148278dae 74 InterruptIn I2(I2pin);
CallumAlder 42:121148278dae 75 InterruptIn I3(I3pin);
CallumAlder 42:121148278dae 76
CallumAlder 42:121148278dae 77 //Motor Drive High Outputs
CallumAlder 42:121148278dae 78 DigitalOut L1H(L1Hpin);
CallumAlder 42:121148278dae 79 DigitalOut L2H(L2Hpin);
CallumAlder 42:121148278dae 80 DigitalOut L3H(L3Hpin);
CallumAlder 42:121148278dae 81
CallumAlder 42:121148278dae 82 //Motor Drive Low Outputs
CallumAlder 42:121148278dae 83 DigitalOut L1L(L1Lpin);
CallumAlder 42:121148278dae 84 DigitalOut L2L(L2Lpin);
CallumAlder 42:121148278dae 85 DigitalOut L3L(L3Lpin);
CallumAlder 42:121148278dae 86
CallumAlder 42:121148278dae 87 PwmOut pwmCtrl(PWMpin);
CallumAlder 42:121148278dae 88
adehadd 27:ce05fed3c1ea 89 //Drive state to output table
estott 0:de4320f74764 90 const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00};
estott 2:4e88faab6988 91
adehadd 27:ce05fed3c1ea 92 //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid
CallumAlder 47:21bf4096faa1 93 const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07};
CallumAlder 47:21bf4096faa1 94 //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 95
adehadd 49:ae8dedfe2d0f 96 #ifndef MAXCMDLENGTH
adehadd 49:ae8dedfe2d0f 97 #define MAXCMDLENGTH 18
adehadd 49:ae8dedfe2d0f 98 #endif
adehadd 49:ae8dedfe2d0f 99
adehadd 49:ae8dedfe2d0f 100 #ifndef MAXCMDLENGTH_HALF
adehadd 49:ae8dedfe2d0f 101 #define MAXCMDLENGTH_HALF 9
adehadd 49:ae8dedfe2d0f 102 #endif
adehadd 49:ae8dedfe2d0f 103
CallumAlder 42:121148278dae 104 class Comm{
CallumAlder 42:121148278dae 105
CallumAlder 42:121148278dae 106 public:
estott 0:de4320f74764 107
CallumAlder 43:a6d20109b2f2 108 volatile bool _outMining;
CallumAlder 43:a6d20109b2f2 109 volatile float _targetVel, _targetRot;
adehadd 49:ae8dedfe2d0f 110 volatile char _notes[MAXCMDLENGTH_HALF]; // Array of actual _notes
estott 0:de4320f74764 111
CallumAlder 47:21bf4096faa1 112 volatile int8_t _modeBitField; // 0,0,0,... <=> Melody,Torque,Rotation,Velocity
CallumAlder 47:21bf4096faa1 113 const uint8_t _MAXCMDLENGTH; //
CallumAlder 47:21bf4096faa1 114 volatile uint8_t _inCharIndex, _cmdIndex,
adehadd 49:ae8dedfe2d0f 115 _noteDur[MAXCMDLENGTH_HALF],_noteLen; // Array of note durations
CallumAlder 47:21bf4096faa1 116 volatile uint32_t _motorTorque; // Motor Toque
CallumAlder 47:21bf4096faa1 117 volatile uint64_t _newKey; // hash key
adehadd 49:ae8dedfe2d0f 118
CallumAlder 48:b2afe48ced0d 119
adehadd 49:ae8dedfe2d0f 120 char _inCharQ[MAXCMDLENGTH],
adehadd 49:ae8dedfe2d0f 121 _newCmd[MAXCMDLENGTH];
adehadd 49:ae8dedfe2d0f 122
CallumAlder 47:21bf4096faa1 123 RawSerial _pc;
adehadd 49:ae8dedfe2d0f 124 Thread _tCommOut, _tCommIn;
adehadd 49:ae8dedfe2d0f 125 Mutex _newKeyMutex; // Restrict access to prevent deadlock.
CallumAlder 47:21bf4096faa1 126 bool _RUN;
CallumAlder 42:121148278dae 127
CallumAlder 48:b2afe48ced0d 128 enum msgType { mot_orState, posIn, velIn, posOut, velOut,
CallumAlder 42:121148278dae 129 hashRate, keyAdded, nonceMatch,
CallumAlder 43:a6d20109b2f2 130 torque, rotations, melody,
CallumAlder 42:121148278dae 131 error};
adehadd 27:ce05fed3c1ea 132
CallumAlder 47:21bf4096faa1 133 typedef struct { msgType type;
adehadd 49:ae8dedfe2d0f 134 uint32_t message;} msg; // TODO: Maybe add a thing that stores the newCmd message as well
adehadd 27:ce05fed3c1ea 135
CallumAlder 47:21bf4096faa1 136 Mail<msg, 32> _msgStack;
adehadd 49:ae8dedfe2d0f 137 Mail<bool,32> _msgReceived;
CallumAlder 47:21bf4096faa1 138
iachinweze1 23:ab1cb51527d1 139
CallumAlder 48:b2afe48ced0d 140 //-- Default Constructor With Inheritance From RawSerial Constructor ------------------------------------------------//
adehadd 49:ae8dedfe2d0f 141 Comm(): _pc(SERIAL_TX, SERIAL_RX), _tCommOut(osPriorityNormal, 1024), _tCommIn(osPriorityAboveNormal, 1024), _MAXCMDLENGTH(MAXCMDLENGTH){
adehadd 27:ce05fed3c1ea 142
CallumAlder 47:21bf4096faa1 143 _cmdIndex = 0;
CallumAlder 47:21bf4096faa1 144 _inCharIndex = 0;
adehadd 45:402a8a9423b9 145
CallumAlder 47:21bf4096faa1 146 _outMining = false;
CallumAlder 47:21bf4096faa1 147 _motorTorque = 300;
CallumAlder 47:21bf4096faa1 148 _targetRot = 459.0;
CallumAlder 47:21bf4096faa1 149 _targetVel = 45.0;
iachinweze1 23:ab1cb51527d1 150
CallumAlder 47:21bf4096faa1 151 _modeBitField = 0x01; // Default velocity mode
adehadd 45:402a8a9423b9 152
adehadd 49:ae8dedfe2d0f 153 _pc.printf("\n\r%s %d\n\r>", "Welcome", _MAXCMDLENGTH ); // Welcome
CallumAlder 47:21bf4096faa1 154 for (int i = 0; i < _MAXCMDLENGTH; ++i) // Reset buffer
CallumAlder 48:b2afe48ced0d 155 _inCharQ[i] = (char)'.'; // If a null terminator is printed Mbed prints 'Embedded Systems are fun and do awesome things!'
CallumAlder 42:121148278dae 156
CallumAlder 47:21bf4096faa1 157 _inCharQ[_MAXCMDLENGTH] = (char)'\0';
CallumAlder 48:b2afe48ced0d 158 sprintf(_inCharQ, "%s", _inCharQ); // Handling of the correct string
CallumAlder 47:21bf4096faa1 159 strncpy(_newCmd, _inCharQ, _MAXCMDLENGTH);
CallumAlder 19:805c87360b55 160
adehadd 49:ae8dedfe2d0f 161 _pc.printf("%s\n\r<", _inCharQ);
adehadd 49:ae8dedfe2d0f 162
CallumAlder 42:121148278dae 163 }
iachinweze1 23:ab1cb51527d1 164
adehadd 49:ae8dedfe2d0f 165 void commInFn() {
adehadd 49:ae8dedfe2d0f 166 _pc.attach(callback(this, &Comm::serialISR));
adehadd 49:ae8dedfe2d0f 167 char newChar;
CallumAlder 42:121148278dae 168
adehadd 49:ae8dedfe2d0f 169 while (_RUN) {
adehadd 49:ae8dedfe2d0f 170 osEvent newEvent = _msgReceived.get(); // Waits forever until it receives a thing
adehadd 49:ae8dedfe2d0f 171 _msgReceived.free((bool *)newEvent.value.p);
adehadd 49:ae8dedfe2d0f 172
CallumAlder 43:a6d20109b2f2 173 if (_inCharIndex == (_MAXCMDLENGTH)) {
CallumAlder 47:21bf4096faa1 174 _inCharQ[_MAXCMDLENGTH] = '\0'; // Force the string to have an end character
CallumAlder 42:121148278dae 175 putMessage(error, 1);
CallumAlder 47:21bf4096faa1 176 _inCharIndex = 0; // Reset buffer index
adehadd 27:ce05fed3c1ea 177 }
adehadd 27:ce05fed3c1ea 178 else{
adehadd 49:ae8dedfe2d0f 179 newChar = _inCharQ[_inCharIndex];
adehadd 49:ae8dedfe2d0f 180
adehadd 49:ae8dedfe2d0f 181 if(newChar != '\r'){ // While the command is not over,
CallumAlder 47:21bf4096faa1 182 _inCharIndex++; // Advance index
CallumAlder 43:a6d20109b2f2 183 _pc.putc(newChar);
CallumAlder 42:121148278dae 184 }
CallumAlder 42:121148278dae 185 else{
CallumAlder 47:21bf4096faa1 186 _inCharQ[_inCharIndex] = '\0'; // When the command is finally over,
CallumAlder 47:21bf4096faa1 187 strncpy(_newCmd, _inCharQ, _MAXCMDLENGTH); // Will copy 18 characters from _inCharQ to _newCmd
CallumAlder 42:121148278dae 188
CallumAlder 47:21bf4096faa1 189 for (int i = 0; i < _MAXCMDLENGTH; ++i) // Reset buffer
CallumAlder 47:21bf4096faa1 190 _inCharQ[i] = ' ';
adehadd 49:ae8dedfe2d0f 191
CallumAlder 47:21bf4096faa1 192 _inCharIndex = 0; // Reset index
adehadd 49:ae8dedfe2d0f 193 cmdParser();
adehadd 49:ae8dedfe2d0f 194
adehadd 49:ae8dedfe2d0f 195 // _tCommIn.signal_wait(0x01);
CallumAlder 42:121148278dae 196 }
adehadd 27:ce05fed3c1ea 197 }
adehadd 27:ce05fed3c1ea 198 }
adehadd 27:ce05fed3c1ea 199 }
CallumAlder 19:805c87360b55 200
adehadd 49:ae8dedfe2d0f 201 //-- Interrupt Service Routine for Serial Port and Character Queue Handling -----------------------------------------//
adehadd 49:ae8dedfe2d0f 202 void serialISR() {
adehadd 49:ae8dedfe2d0f 203 if (_pc.readable()) {
adehadd 49:ae8dedfe2d0f 204 char newChar = _pc.getc();
adehadd 49:ae8dedfe2d0f 205 _inCharQ[_inCharIndex] = newChar; // Save input character
adehadd 49:ae8dedfe2d0f 206
adehadd 49:ae8dedfe2d0f 207 bool *new_msg = _msgReceived.alloc();
adehadd 49:ae8dedfe2d0f 208 *new_msg = true;
adehadd 49:ae8dedfe2d0f 209 _msgReceived.put(new_msg);
adehadd 49:ae8dedfe2d0f 210 }
adehadd 49:ae8dedfe2d0f 211 }
adehadd 49:ae8dedfe2d0f 212
CallumAlder 48:b2afe48ced0d 213 //-- Reset Cursor Position ------------------------------------------------------------------------------------------//
CallumAlder 42:121148278dae 214 void returnCursor() {
CallumAlder 43:a6d20109b2f2 215 _pc.putc('>');
CallumAlder 43:a6d20109b2f2 216 for (int i = 0; i < _inCharIndex; ++i)
CallumAlder 47:21bf4096faa1 217 _pc.putc(_inCharQ[i]);
CallumAlder 42:121148278dae 218 }
CallumAlder 47:21bf4096faa1 219
CallumAlder 48:b2afe48ced0d 220 //-- Parse Incoming Data From Serial Port ---------------------------------------------------------------------------//
CallumAlder 48:b2afe48ced0d 221 void cmdParser() {
CallumAlder 47:21bf4096faa1 222 switch(_newCmd[0]) {
CallumAlder 47:21bf4096faa1 223 case 'K': // keyAdded
CallumAlder 47:21bf4096faa1 224 _newKeyMutex.lock(); // Ensure there is no deadlock
CallumAlder 47:21bf4096faa1 225 sscanf(_newCmd, "K%x", &_newKey); // Find desired the Key code
CallumAlder 47:21bf4096faa1 226 putMessage(keyAdded, _newKey); // Print it out
CallumAlder 43:a6d20109b2f2 227 _newKeyMutex.unlock();
CallumAlder 43:a6d20109b2f2 228 break;
CallumAlder 43:a6d20109b2f2 229
CallumAlder 47:21bf4096faa1 230 case 'V': // velIn
CallumAlder 47:21bf4096faa1 231 sscanf(_newCmd, "V%f", &_targetVel); // Find desired the target velocity
CallumAlder 47:21bf4096faa1 232 _modeBitField = 0x01; // Adjust bitfield pos 1
CallumAlder 47:21bf4096faa1 233 putMessage(velIn, _targetVel); // Print it out
CallumAlder 43:a6d20109b2f2 234 break;
CallumAlder 43:a6d20109b2f2 235
CallumAlder 47:21bf4096faa1 236 case 'R': // posIn
CallumAlder 47:21bf4096faa1 237 sscanf(_newCmd, "R%f", &_targetRot); // Find desired target rotation
CallumAlder 47:21bf4096faa1 238 _modeBitField = 0x02; // Adjust bitfield pos 2
CallumAlder 47:21bf4096faa1 239 putMessage(posIn, _targetRot); // Print it out
CallumAlder 42:121148278dae 240 break;
iachinweze1 23:ab1cb51527d1 241
CallumAlder 47:21bf4096faa1 242 case 'x': // Torque
CallumAlder 47:21bf4096faa1 243 sscanf(_newCmd, "x%u", &_motorTorque); // Find desired target torque
CallumAlder 47:21bf4096faa1 244 _modeBitField = 0x04; // Adjust bitfield pos 3
CallumAlder 47:21bf4096faa1 245 putMessage(torque, _motorTorque); // Print it out
adehadd 27:ce05fed3c1ea 246 break;
CallumAlder 42:121148278dae 247
CallumAlder 47:21bf4096faa1 248 case 'M': // Mining display toggle
CallumAlder 47:21bf4096faa1 249 int8_t miningTest;
CallumAlder 47:21bf4096faa1 250 sscanf(_newCmd, "M%d", &miningTest); // Display if input is 1
CallumAlder 47:21bf4096faa1 251 miningTest == 1 ? _outMining = true : _outMining = false;
CallumAlder 47:21bf4096faa1 252 break;
CallumAlder 47:21bf4096faa1 253
CallumAlder 47:21bf4096faa1 254 case 'T': // Play tune
CallumAlder 47:21bf4096faa1 255 regexTune() ? putMessage(melody, 1) : putMessage(error, 2);
CallumAlder 47:21bf4096faa1 256 break; // Break from case 'T'
CallumAlder 47:21bf4096faa1 257
CallumAlder 47:21bf4096faa1 258 default: // Break from switch
adehadd 27:ce05fed3c1ea 259 break;
adehadd 27:ce05fed3c1ea 260 }
adehadd 27:ce05fed3c1ea 261 }
adehadd 27:ce05fed3c1ea 262
CallumAlder 48:b2afe48ced0d 263 //-- Read In Note Data From Serial Port and Parse Into Class Variables ----------------------------------------------//
CallumAlder 47:21bf4096faa1 264 bool regexTune() {
CallumAlder 47:21bf4096faa1 265
CallumAlder 47:21bf4096faa1 266 uint8_t len = 0;
CallumAlder 47:21bf4096faa1 267
CallumAlder 47:21bf4096faa1 268 for (int i = 1; i < _MAXCMDLENGTH; ++i) // Find first #
CallumAlder 47:21bf4096faa1 269 if (_newCmd[i] == '#') {
CallumAlder 47:21bf4096faa1 270 len = i;
CallumAlder 47:21bf4096faa1 271 break; // Stop at first # found
CallumAlder 47:21bf4096faa1 272 }
CallumAlder 47:21bf4096faa1 273
CallumAlder 47:21bf4096faa1 274 if (len>0) { // Parse the input only if # found
CallumAlder 47:21bf4096faa1 275 uint8_t newLen = 2*(len+1)+1;
CallumAlder 47:21bf4096faa1 276 bool isChar = true;
CallumAlder 47:21bf4096faa1 277 char formatSpec[newLen];
CallumAlder 47:21bf4096faa1 278 formatSpec[0]='T';
CallumAlder 47:21bf4096faa1 279 for (int i = 1; i < newLen; i=i+2) { // Create a format spec based on length of input
CallumAlder 47:21bf4096faa1 280 formatSpec[i] = '%';
CallumAlder 47:21bf4096faa1 281 isChar ? formatSpec[i+1] = 'c' : \
CallumAlder 47:21bf4096faa1 282 formatSpec[i+1] = 'u' ;
CallumAlder 47:21bf4096faa1 283 isChar = !isChar;
CallumAlder 47:21bf4096faa1 284 }
CallumAlder 47:21bf4096faa1 285
CallumAlder 47:21bf4096faa1 286 formatSpec[newLen] = '\0';
CallumAlder 47:21bf4096faa1 287 sprintf(formatSpec, "%s", formatSpec); // Set string format correctly
CallumAlder 47:21bf4096faa1 288 // _pc.printf("%s\n", formatSpec );
CallumAlder 47:21bf4096faa1 289 sscanf(_newCmd, formatSpec, &_notes[0], &_noteDur[0],
CallumAlder 47:21bf4096faa1 290 &_notes[1], &_noteDur[1],
CallumAlder 47:21bf4096faa1 291 &_notes[2], &_noteDur[2],
CallumAlder 47:21bf4096faa1 292 &_notes[3], &_noteDur[3],
CallumAlder 47:21bf4096faa1 293 &_notes[4], &_noteDur[4],
CallumAlder 47:21bf4096faa1 294 &_notes[5], &_noteDur[5],
CallumAlder 47:21bf4096faa1 295 &_notes[6], &_noteDur[6],
CallumAlder 47:21bf4096faa1 296 &_notes[7], &_noteDur[7],
CallumAlder 47:21bf4096faa1 297 &_notes[8], &_noteDur[8]);
CallumAlder 47:21bf4096faa1 298
CallumAlder 47:21bf4096faa1 299
CallumAlder 47:21bf4096faa1 300 // Update _newCmd for putMessage print
CallumAlder 48:b2afe48ced0d 301 sprintf(_newCmd,formatSpec, _notes[0], _noteDur[0],\
CallumAlder 47:21bf4096faa1 302 _notes[1], _noteDur[1],\
CallumAlder 47:21bf4096faa1 303 _notes[2], _noteDur[2],\
CallumAlder 47:21bf4096faa1 304 _notes[3], _noteDur[3],\
CallumAlder 47:21bf4096faa1 305 _notes[4], _noteDur[4],\
CallumAlder 47:21bf4096faa1 306 _notes[5], _noteDur[5],\
CallumAlder 47:21bf4096faa1 307 _notes[6], _noteDur[6],\
CallumAlder 47:21bf4096faa1 308 _notes[7], _noteDur[7],\
CallumAlder 47:21bf4096faa1 309 _notes[8], _noteDur[8]);
CallumAlder 47:21bf4096faa1 310 _noteLen = len;
CallumAlder 47:21bf4096faa1 311 return true;
CallumAlder 47:21bf4096faa1 312 }
CallumAlder 47:21bf4096faa1 313 else {
CallumAlder 47:21bf4096faa1 314 return false;
CallumAlder 47:21bf4096faa1 315 }
CallumAlder 47:21bf4096faa1 316 }
CallumAlder 47:21bf4096faa1 317
CallumAlder 48:b2afe48ced0d 318 //-- Decode Messages to Print on Serial Port ------------------------------------------------------------------------//
CallumAlder 42:121148278dae 319 void commOutFn() {
CallumAlder 42:121148278dae 320 while (_RUN) {
CallumAlder 48:b2afe48ced0d 321
CallumAlder 47:21bf4096faa1 322 osEvent newEvent = _msgStack.get();
CallumAlder 42:121148278dae 323 msg *pMessage = (msg *) newEvent.value.p;
adehadd 27:ce05fed3c1ea 324
CallumAlder 47:21bf4096faa1 325 //Case switch to choose serial output based on incoming message enum
CallumAlder 42:121148278dae 326 switch (pMessage->type) {
CallumAlder 48:b2afe48ced0d 327 case mot_orState:
CallumAlder 47:21bf4096faa1 328 _pc.printf("\r>%s< The motor is currently in state %x\n\r", _inCharQ, pMessage->message);
CallumAlder 42:121148278dae 329 break;
CallumAlder 42:121148278dae 330 case hashRate:
CallumAlder 43:a6d20109b2f2 331 if (_outMining) {
CallumAlder 47:21bf4096faa1 332 _pc.printf("\r>%s< Mining: %.4u Hash/s\r", _inCharQ, (uint32_t) pMessage->message);
CallumAlder 42:121148278dae 333 returnCursor();
CallumAlder 43:a6d20109b2f2 334 _outMining = false;
CallumAlder 42:121148278dae 335 }
CallumAlder 42:121148278dae 336 break;
CallumAlder 42:121148278dae 337 case nonceMatch:
CallumAlder 47:21bf4096faa1 338 _pc.printf("\r>%s< Nonce found: %x\n\r", _inCharQ, pMessage->message);
CallumAlder 42:121148278dae 339 returnCursor();
CallumAlder 42:121148278dae 340 break;
CallumAlder 42:121148278dae 341 case keyAdded:
CallumAlder 47:21bf4096faa1 342 _pc.printf("\r>%s< New Key Added:\t0x%016x\n\r", _inCharQ, pMessage->message);
CallumAlder 42:121148278dae 343 break;
CallumAlder 42:121148278dae 344 case torque:
CallumAlder 47:21bf4096faa1 345 _pc.printf("\r>%s< Motor Torque set to:\t%d\n\r", _inCharQ, (int32_t) pMessage->message);
CallumAlder 42:121148278dae 346 break;
CallumAlder 42:121148278dae 347 case velIn:
CallumAlder 47:21bf4096faa1 348 _pc.printf("\r>%s< Target Velocity set to:\t%.2f\n\r", _inCharQ, _targetVel);
CallumAlder 42:121148278dae 349 break;
CallumAlder 42:121148278dae 350 case velOut:
CallumAlder 47:21bf4096faa1 351 _pc.printf("\r>%s< Current Velocity:\t%.2f States/sec\n\r", _inCharQ, (float) ((int32_t) pMessage->message));
CallumAlder 42:121148278dae 352 break;
CallumAlder 42:121148278dae 353 case posIn:
CallumAlder 47:21bf4096faa1 354 _pc.printf("\r>%s< Target # Rotations:\t%.2f\n\r", _inCharQ, (float) ((int32_t) pMessage->message));
CallumAlder 42:121148278dae 355 break;
CallumAlder 42:121148278dae 356 case posOut:
CallumAlder 47:21bf4096faa1 357 _pc.printf("\r>%s< Current Position:\t%.2f\n\r", _inCharQ, (float) ((int32_t) pMessage->message));
CallumAlder 42:121148278dae 358 break;
CallumAlder 47:21bf4096faa1 359 case melody:
CallumAlder 47:21bf4096faa1 360 _pc.printf("\r>%s< New Tune:\t%s\n\r", _inCharQ, _newCmd);
CallumAlder 47:21bf4096faa1 361 break;
CallumAlder 42:121148278dae 362 case error:
CallumAlder 47:21bf4096faa1 363 switch (pMessage->message) {
CallumAlder 47:21bf4096faa1 364 case 1:
CallumAlder 47:21bf4096faa1 365 _pc.printf("\r>%s< Error:%s\n\r", _inCharQ, "Overfull Buffer Reset" );
CallumAlder 47:21bf4096faa1 366 break;
CallumAlder 47:21bf4096faa1 367 case 2:
CallumAlder 47:21bf4096faa1 368 _pc.printf("\r>%s< Error:%s\n\r", _inCharQ, "Invalid Melody" );
CallumAlder 47:21bf4096faa1 369 default:
CallumAlder 47:21bf4096faa1 370 break;
CallumAlder 47:21bf4096faa1 371 }
CallumAlder 47:21bf4096faa1 372 for (int i = 0; i < _MAXCMDLENGTH; ++i) // reset buffer
CallumAlder 47:21bf4096faa1 373 _inCharQ[i] = ' ';
CallumAlder 47:21bf4096faa1 374
CallumAlder 47:21bf4096faa1 375 _inCharIndex = 0;
CallumAlder 42:121148278dae 376 break;
CallumAlder 42:121148278dae 377 default:
CallumAlder 47:21bf4096faa1 378 _pc.printf("\r>%s< Unknown Error. Message: %x\n\r", _inCharQ, pMessage->message);
CallumAlder 42:121148278dae 379 break;
CallumAlder 42:121148278dae 380 }
CallumAlder 42:121148278dae 381
CallumAlder 47:21bf4096faa1 382 _msgStack.free(pMessage);
CallumAlder 42:121148278dae 383 }
CallumAlder 42:121148278dae 384 }
CallumAlder 42:121148278dae 385
CallumAlder 48:b2afe48ced0d 386 //-- Put a Message On the Outgoing Message Stack --------------------------------------------------------------------//
CallumAlder 42:121148278dae 387 void putMessage(msgType type, uint32_t message){
CallumAlder 47:21bf4096faa1 388 msg *p_msg = _msgStack.alloc();
CallumAlder 42:121148278dae 389 p_msg->type = type;
CallumAlder 42:121148278dae 390 p_msg->message = message;
CallumAlder 47:21bf4096faa1 391 _msgStack.put(p_msg);
CallumAlder 42:121148278dae 392 }
CallumAlder 42:121148278dae 393
CallumAlder 48:b2afe48ced0d 394 //-- Attach CommOut Thread to the Outgoing Communication Function ---------------------------------------------------//
CallumAlder 42:121148278dae 395 void start_comm(){
CallumAlder 42:121148278dae 396 _RUN = true;
CallumAlder 47:21bf4096faa1 397 _tCommOut.start(callback(this, &Comm::commOutFn));
adehadd 49:ae8dedfe2d0f 398 _tCommIn.start(callback(this, &Comm::commInFn));
CallumAlder 42:121148278dae 399 }
iachinweze1 23:ab1cb51527d1 400
adehadd 49:ae8dedfe2d0f 401
CallumAlder 42:121148278dae 402 };
adehadd 49:ae8dedfe2d0f 403 // char Comm::_inCharQ[] = {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','\0'}; // Static member must be defined outside class
CallumAlder 48:b2afe48ced0d 404 //Mutex Comm::_newKeyMutex;
adehadd 46:b9081aa50bda 405
CallumAlder 42:121148278dae 406 class Motor {
adehadd 27:ce05fed3c1ea 407
CallumAlder 42:121148278dae 408 protected:
CallumAlder 48:b2afe48ced0d 409 volatile int8_t _orState, // Rotor offset at motor state 0, motor specific
CallumAlder 48:b2afe48ced0d 410 _currentState, // Current Rotor State
CallumAlder 48:b2afe48ced0d 411 _stateList[6], // All possible rotor states stored
CallumAlder 48:b2afe48ced0d 412 _lead; // Phase _lead to make motor spin
CallumAlder 42:121148278dae 413
CallumAlder 48:b2afe48ced0d 414 uint8_t _theStates[3], // The Key states
CallumAlder 48:b2afe48ced0d 415 _stateCount[3]; // State Counter
CallumAlder 47:21bf4096faa1 416 uint32_t mtrPeriod, // Motor period
CallumAlder 47:21bf4096faa1 417 _MAXPWM_PRD;
CallumAlder 48:b2afe48ced0d 418 float _dutyC; // 1 = 100%
CallumAlder 47:21bf4096faa1 419 bool _RUN;
adehadd 27:ce05fed3c1ea 420
CallumAlder 48:b2afe48ced0d 421 Comm* _pComm;
CallumAlder 48:b2afe48ced0d 422 Thread _tMotorCtrl; // Thread for motor Control
CallumAlder 42:121148278dae 423
CallumAlder 42:121148278dae 424 public:
CallumAlder 48:b2afe48ced0d 425 //-- Default Constructor With Thread Object Constructor -------------------------------------------------------------//
CallumAlder 48:b2afe48ced0d 426 Motor() : _tMotorCtrl(osPriorityAboveNormal2, 1024){
CallumAlder 47:21bf4096faa1 427
CallumAlder 48:b2afe48ced0d 428 _dutyC = 1.0f; // Set Power to maximum to drive motorHome()
CallumAlder 48:b2afe48ced0d 429 mtrPeriod = 2e3; // Motor period
CallumAlder 48:b2afe48ced0d 430 pwmCtrl.period_us(mtrPeriod); // Initialise PWM
CallumAlder 42:121148278dae 431 pwmCtrl.pulsewidth_us(mtrPeriod);
CallumAlder 42:121148278dae 432
CallumAlder 48:b2afe48ced0d 433 _orState = motorHome(); // Rotor offset at motor state 0
adehadd 49:ae8dedfe2d0f 434 _currentState = readRotorState(); // Current Rotor State
CallumAlder 48:b2afe48ced0d 435 _lead = 2; // 2 for forwards, -2 for backwards
adehadd 27:ce05fed3c1ea 436
CallumAlder 48:b2afe48ced0d 437 _theStates[0] = _orState +1; // Initialise repeatable states, for us this is state 1
CallumAlder 48:b2afe48ced0d 438 _theStates[1] = (_orState + _lead) % 6 +1; // state 3
CallumAlder 48:b2afe48ced0d 439 _theStates[2] = (_orState + (_lead*2)) % 6 +1; // state 5
CallumAlder 42:121148278dae 440
CallumAlder 48:b2afe48ced0d 441 _stateCount[0] = 0; // Initialise
CallumAlder 48:b2afe48ced0d 442 _stateCount[1] = 0;
CallumAlder 48:b2afe48ced0d 443 _stateCount[2] = 0;
CallumAlder 42:121148278dae 444
CallumAlder 48:b2afe48ced0d 445 _pComm = NULL; // Initialise as NULL
CallumAlder 48:b2afe48ced0d 446 _RUN = false;
CallumAlder 42:121148278dae 447
CallumAlder 47:21bf4096faa1 448 _MAXPWM_PRD = 2e3;
CallumAlder 42:121148278dae 449
CallumAlder 42:121148278dae 450 }
adehadd 27:ce05fed3c1ea 451
CallumAlder 48:b2afe48ced0d 452 //-- Start Motor and Attach State Update Function to Rise/Fall Interrupts -------------------------------------------//
CallumAlder 42:121148278dae 453 void motorStart(Comm *comm) {
CallumAlder 42:121148278dae 454
CallumAlder 48:b2afe48ced0d 455 I1.fall(callback(this, &Motor::stateUpdate)); // Establish Photo Interrupter Service Routines (auto choose next state)
CallumAlder 42:121148278dae 456 I2.fall(callback(this, &Motor::stateUpdate));
CallumAlder 42:121148278dae 457 I3.fall(callback(this, &Motor::stateUpdate));
CallumAlder 42:121148278dae 458 I1.rise(callback(this, &Motor::stateUpdate));
CallumAlder 42:121148278dae 459 I2.rise(callback(this, &Motor::stateUpdate));
CallumAlder 42:121148278dae 460 I3.rise(callback(this, &Motor::stateUpdate));
CallumAlder 42:121148278dae 461
CallumAlder 48:b2afe48ced0d 462 motorOut((_currentState-_orState+_lead+6)%6); // Push digitally so static motor will start moving
CallumAlder 42:121148278dae 463
CallumAlder 48:b2afe48ced0d 464
CallumAlder 48:b2afe48ced0d 465 _dutyC = 0.8; // Default a lower duty cycle
CallumAlder 42:121148278dae 466 pwmCtrl.period_us((uint32_t)mtrPeriod);
CallumAlder 48:b2afe48ced0d 467 pwmCtrl.pulsewidth_us((uint32_t)mtrPeriod*_dutyC);
CallumAlder 42:121148278dae 468
CallumAlder 48:b2afe48ced0d 469 _pComm = comm;
CallumAlder 42:121148278dae 470 _RUN = true;
CallumAlder 42:121148278dae 471
CallumAlder 48:b2afe48ced0d 472 _tMotorCtrl.start(callback(this, &Motor::motorCtrlFn)); // Start motor control thread
CallumAlder 42:121148278dae 473
CallumAlder 48:b2afe48ced0d 474 _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 475
CallumAlder 42:121148278dae 476 }
CallumAlder 42:121148278dae 477
CallumAlder 48:b2afe48ced0d 478 //-- Set a Predetermined Drive State -------------------------------------------------------------------------------//
CallumAlder 42:121148278dae 479 void motorOut(int8_t driveState) {
iachinweze1 23:ab1cb51527d1 480
CallumAlder 48:b2afe48ced0d 481 int8_t driveOut = driveTable[driveState & 0x07]; //Lookup the output byte from the drive state
CallumAlder 42:121148278dae 482
CallumAlder 48:b2afe48ced0d 483 if (~driveOut & 0x01) L1L = 0; //Turn off first
CallumAlder 42:121148278dae 484 if (~driveOut & 0x02) L1H = 1;
CallumAlder 42:121148278dae 485 if (~driveOut & 0x04) L2L = 0;
CallumAlder 42:121148278dae 486 if (~driveOut & 0x08) L2H = 1;
CallumAlder 42:121148278dae 487 if (~driveOut & 0x10) L3L = 0;
CallumAlder 42:121148278dae 488 if (~driveOut & 0x20) L3H = 1;
CallumAlder 42:121148278dae 489
CallumAlder 48:b2afe48ced0d 490 if (driveOut & 0x01) L1L = 1; //Then turn on
CallumAlder 48:b2afe48ced0d 491 if (driveOut & 0x02) L1H = 0;
CallumAlder 48:b2afe48ced0d 492 if (driveOut & 0x04) L2L = 1;
CallumAlder 48:b2afe48ced0d 493 if (driveOut & 0x08) L2H = 0;
CallumAlder 48:b2afe48ced0d 494 if (driveOut & 0x10) L3L = 1;
CallumAlder 48:b2afe48ced0d 495 if (driveOut & 0x20) L3H = 0;
CallumAlder 42:121148278dae 496 }
CallumAlder 42:121148278dae 497
CallumAlder 48:b2afe48ced0d 498 //-- Inline Conversion of Photointerrupts to a Rotor State ----------------------------------------------------------//
adehadd 49:ae8dedfe2d0f 499 inline int8_t readRotorState() {
CallumAlder 42:121148278dae 500 return stateMap[I1 + 2*I2 + 4*I3];
CallumAlder 42:121148278dae 501 }
CallumAlder 42:121148278dae 502
CallumAlder 48:b2afe48ced0d 503 //-- Basic Motor Stabilisation and Synchronisation ------------------------------------------------------------------//
CallumAlder 42:121148278dae 504 int8_t motorHome() {
CallumAlder 48:b2afe48ced0d 505
CallumAlder 48:b2afe48ced0d 506 motorOut(0); //Put the motor in drive state 0 and wait for it to stabilise
CallumAlder 42:121148278dae 507 wait(3.0);
CallumAlder 42:121148278dae 508
adehadd 49:ae8dedfe2d0f 509 return readRotorState(); //Get the rotor state
CallumAlder 42:121148278dae 510 }
iachinweze1 23:ab1cb51527d1 511
CallumAlder 48:b2afe48ced0d 512 //-- Motor State Log, Circumvents Issues From Occasionally Skipping Certain States ----------------------------------//
CallumAlder 42:121148278dae 513 void stateUpdate() { // () { // **params
adehadd 49:ae8dedfe2d0f 514 _currentState = readRotorState(); // Get current state
adehadd 27:ce05fed3c1ea 515
CallumAlder 48:b2afe48ced0d 516 if (_currentState == _theStates[0]) // Cumulative state counter (1 for our group's motor)
CallumAlder 48:b2afe48ced0d 517 _stateCount[0]++;
CallumAlder 48:b2afe48ced0d 518 else if (_currentState == _theStates[1]) // Cumulative state counter (3 for our group's motor)
CallumAlder 48:b2afe48ced0d 519 _stateCount[1]++;
CallumAlder 48:b2afe48ced0d 520 else if (_currentState == _theStates[2]) // Cumulative state counter (5 for our group's motor)
CallumAlder 48:b2afe48ced0d 521 _stateCount[2]++;
adehadd 27:ce05fed3c1ea 522
CallumAlder 48:b2afe48ced0d 523 motorOut((_currentState - _orState + _lead + 6) % 6); // Send the next state to the motor
iachinweze1 23:ab1cb51527d1 524
CallumAlder 42:121148278dae 525 }
CallumAlder 19:805c87360b55 526
CallumAlder 48:b2afe48ced0d 527 //-- Motor PID Control ---------------------------------------------------------------------------------------------//
CallumAlder 42:121148278dae 528 void motorCtrlFn() {
CallumAlder 42:121148278dae 529 Ticker motorCtrlTicker;
CallumAlder 42:121148278dae 530 Timer m_timer;
CallumAlder 42:121148278dae 531 motorCtrlTicker.attach_us(callback(this,&Motor::motorCtrlTick), 1e5);
iachinweze1 23:ab1cb51527d1 532
CallumAlder 42:121148278dae 533 // Init some things
CallumAlder 48:b2afe48ced0d 534 uint8_t cpyStateCount[3];
CallumAlder 48:b2afe48ced0d 535 uint8_t cpyCurrentState;
CallumAlder 48:b2afe48ced0d 536 int8_t cpyModeBitfield;
CallumAlder 42:121148278dae 537
CallumAlder 48:b2afe48ced0d 538 int32_t ting[2] = {6,1}; // 360,60 (for degrees), 5,1 (for states)
CallumAlder 48:b2afe48ced0d 539 uint8_t iterElementMax;
CallumAlder 48:b2afe48ced0d 540 int32_t totalDegrees;
CallumAlder 48:b2afe48ced0d 541 int32_t stateDiff;
adehadd 27:ce05fed3c1ea 542
CallumAlder 48:b2afe48ced0d 543 int32_t cur_speed; // Variable for local velocity calculation
CallumAlder 48:b2afe48ced0d 544 int32_t locMotorPos; // Local copy of motor position
CallumAlder 47:21bf4096faa1 545 volatile int32_t torque; // Local variable to set motor torque
CallumAlder 48:b2afe48ced0d 546 static int32_t oldTorque =0;
CallumAlder 48:b2afe48ced0d 547 float sError, // Velocity error between target and reality
CallumAlder 48:b2afe48ced0d 548 rError; // Rotation error between target and reality
CallumAlder 48:b2afe48ced0d 549 static float rErrorOld; // Old rotation error used for calculation
CallumAlder 42:121148278dae 550
CallumAlder 42:121148278dae 551 //~~~Controller constants~~~~
CallumAlder 48:b2afe48ced0d 552 int32_t Kp1=22; // Proportional controller constants
CallumAlder 48:b2afe48ced0d 553 int32_t Kp2=22; // Calculated by trial and error to give optimal accuracy
CallumAlder 48:b2afe48ced0d 554 int32_t Ki = 12;
CallumAlder 48:b2afe48ced0d 555 float Kd=15.5;
CallumAlder 42:121148278dae 556
CallumAlder 42:121148278dae 557
CallumAlder 48:b2afe48ced0d 558 int32_t Ts; // Initialise controller output Ts (s=speed)
CallumAlder 48:b2afe48ced0d 559 int32_t Tr; // Initialise controller output Tr (r=rotations)
CallumAlder 42:121148278dae 560
CallumAlder 48:b2afe48ced0d 561 int32_t old_pos = 0;
adehadd 27:ce05fed3c1ea 562
CallumAlder 48:b2afe48ced0d 563 uint32_t cur_time = 0,
CallumAlder 48:b2afe48ced0d 564 old_time = 0,
CallumAlder 48:b2afe48ced0d 565 time_diff;
CallumAlder 42:121148278dae 566
CallumAlder 48:b2afe48ced0d 567 float cur_err = 0.0f,
CallumAlder 48:b2afe48ced0d 568 old_err = 0.0f,
CallumAlder 48:b2afe48ced0d 569 err_diff;
CallumAlder 42:121148278dae 570
CallumAlder 42:121148278dae 571 m_timer.start();
CallumAlder 42:121148278dae 572
CallumAlder 42:121148278dae 573 while (_RUN) {
CallumAlder 48:b2afe48ced0d 574 _tMotorCtrl.signal_wait((int32_t)0x1);
adehadd 49:ae8dedfe2d0f 575 // _pComm->_tCommIn.signal_set(0x01);
iachinweze1 23:ab1cb51527d1 576
CallumAlder 48:b2afe48ced0d 577 core_util_critical_section_enter(); // Access shared variables here
CallumAlder 48:b2afe48ced0d 578 cpyModeBitfield = _pComm->_modeBitField;
CallumAlder 48:b2afe48ced0d 579 std::copy(_stateCount, _stateCount+3, cpyStateCount);
CallumAlder 48:b2afe48ced0d 580 cpyCurrentState = _currentState;
CallumAlder 47:21bf4096faa1 581 for (int i = 0; i < 3; ++i) {
CallumAlder 48:b2afe48ced0d 582 _stateCount[i] = 0;
CallumAlder 47:21bf4096faa1 583 }
CallumAlder 42:121148278dae 584 core_util_critical_section_exit();
adehadd 20:c60f4785b556 585
CallumAlder 48:b2afe48ced0d 586
CallumAlder 48:b2afe48ced0d 587 cur_time = m_timer.read(); // Read state & timestamp
CallumAlder 42:121148278dae 588
CallumAlder 42:121148278dae 589 time_diff = cur_time - old_time;
CallumAlder 42:121148278dae 590 // cur_speed = (cur_pos - old_pos) / time_diff;
CallumAlder 48:b2afe48ced0d 591
CallumAlder 48:b2afe48ced0d 592 old_time = cur_time; // prep values for next time through loop
CallumAlder 42:121148278dae 593 old_pos = cpyCurrentState;
CallumAlder 42:121148278dae 594
CallumAlder 47:21bf4096faa1 595 // Hence we make the value positive,// and instead set the direction to the opposite one
CallumAlder 42:121148278dae 596
CallumAlder 42:121148278dae 597 iterElementMax = std::max_element(cpyStateCount, cpyStateCount+3) - cpyStateCount;
adehadd 20:c60f4785b556 598
CallumAlder 42:121148278dae 599 totalDegrees = ting[0] * cpyStateCount[iterElementMax];
CallumAlder 48:b2afe48ced0d 600 stateDiff = _theStates[iterElementMax]-cpyCurrentState;
CallumAlder 47:21bf4096faa1 601 stateDiff >= 0 ? totalDegrees = totalDegrees + (ting[1]* stateDiff) : \
CallumAlder 47:21bf4096faa1 602 totalDegrees = totalDegrees + (ting[1]* stateDiff *-1);
CallumAlder 42:121148278dae 603
CallumAlder 47:21bf4096faa1 604 if ((cpyModeBitfield & 0x01)|(cpyModeBitfield & 0x02)) {// Speed, torque control and PID
CallumAlder 42:121148278dae 605 cur_speed = totalDegrees / time_diff;
CallumAlder 48:b2afe48ced0d 606 sError = (_pComm->_targetVel * 6) - abs(cur_speed); // Read global variable _targetVel updated by interrupt and calculate error between target and reality
CallumAlder 42:121148278dae 607
CallumAlder 48:b2afe48ced0d 608 // Ts = Kp * (s -|v|) where, // SPEED CONTROLLER
CallumAlder 48:b2afe48ced0d 609 // Ts = controller output, Kp = prop controller constant, s = target velocity and v is the measured velocity
CallumAlder 42:121148278dae 610
CallumAlder 47:21bf4096faa1 611 // Check if user entered V0 and set the output to maximum as specified
adehadd 49:ae8dedfe2d0f 612 sError == -abs(cur_speed) ? Ts = _MAXPWM_PRD : \
CallumAlder 48:b2afe48ced0d 613 Ts = (Kp1 * sError); // If the user didn't enter V0 implement controller transfer function:
CallumAlder 47:21bf4096faa1 614
CallumAlder 47:21bf4096faa1 615
CallumAlder 48:b2afe48ced0d 616 // Tr= Kp*Er + Kd* (dEr/dt) where, // ROTATION CONTROLLER
CallumAlder 48:b2afe48ced0d 617 // Tr = controller output, Kp = prop controller constant, Er = error in number of rotations
CallumAlder 48:b2afe48ced0d 618 rError = (_pComm->_targetRot)*6 - totalDegrees; // Read global variable _targetRot updated by interrupt and calculate the rotation error.
CallumAlder 48:b2afe48ced0d 619 Tr = Kp2*rError + Kd*(rError - rErrorOld); // Implement controller transfer function
CallumAlder 47:21bf4096faa1 620 rErrorOld = rError; // Update rotation error
CallumAlder 47:21bf4096faa1 621
CallumAlder 48:b2afe48ced0d 622 Ts = (int32_t)( Ts * sgn(rError) ); // Use the sign of the error to set controller wrt direction of rotation
CallumAlder 42:121148278dae 623
CallumAlder 48:b2afe48ced0d 624 cur_speed < 0 ? torque = max(Ts, Tr): torque = min(Ts, Tr);
CallumAlder 47:21bf4096faa1 625
CallumAlder 47:21bf4096faa1 626 }
CallumAlder 47:21bf4096faa1 627 else if (cpyModeBitfield & 0x04) { // If it is in torque mode, do no PID math, just set pulsewidth
CallumAlder 48:b2afe48ced0d 628 torque = (int32_t)_pComm->_motorTorque;
CallumAlder 47:21bf4096faa1 629 if (oldTorque != torque) {
CallumAlder 48:b2afe48ced0d 630 _pComm->putMessage((Comm::msgType)8, torque);
CallumAlder 47:21bf4096faa1 631 oldTorque = torque;
CallumAlder 42:121148278dae 632 }
CallumAlder 47:21bf4096faa1 633 }
CallumAlder 47:21bf4096faa1 634 else{
CallumAlder 47:21bf4096faa1 635 torque = _MAXPWM_PRD * 0.5; // Run at 50% duty cycle if argument not properly defined
adehadd 27:ce05fed3c1ea 636
CallumAlder 42:121148278dae 637 }
adehadd 27:ce05fed3c1ea 638
CallumAlder 48:b2afe48ced0d 639 torque < 0 ? _lead = -2 : _lead = +2;
CallumAlder 47:21bf4096faa1 640 torque = abs(torque);
adehadd 26:fb6151e5907d 641
CallumAlder 47:21bf4096faa1 642 if(torque > _MAXPWM_PRD) torque = _MAXPWM_PRD; // In case the calculated PWM is higher than our maximum 50% allowance,
CallumAlder 47:21bf4096faa1 643 // Set it to our max.
CallumAlder 48:b2afe48ced0d 644 _pComm->_motorTorque = torque;
CallumAlder 47:21bf4096faa1 645 pwmCtrl.pulsewidth_us(torque);
CallumAlder 47:21bf4096faa1 646
CallumAlder 42:121148278dae 647 }
CallumAlder 42:121148278dae 648 }
CallumAlder 42:121148278dae 649
CallumAlder 42:121148278dae 650 void motorCtrlTick(){
CallumAlder 48:b2afe48ced0d 651 _tMotorCtrl.signal_set(0x1);
CallumAlder 42:121148278dae 652 }
CallumAlder 42:121148278dae 653 };
CallumAlder 42:121148278dae 654
CallumAlder 42:121148278dae 655
adehadd 27:ce05fed3c1ea 656 int main() {
adehadd 26:fb6151e5907d 657
CallumAlder 42:121148278dae 658 // Declare Objects
CallumAlder 42:121148278dae 659 Comm comm_port;
CallumAlder 42:121148278dae 660 SHA256 miner;
CallumAlder 42:121148278dae 661 Motor motor;
adehadd 27:ce05fed3c1ea 662
CallumAlder 42:121148278dae 663 // Start Motor and Comm Port
CallumAlder 42:121148278dae 664 motor.motorStart(&comm_port);
CallumAlder 42:121148278dae 665 comm_port.start_comm();
adehadd 27:ce05fed3c1ea 666
CallumAlder 42:121148278dae 667 // Declare Hash Variables
CallumAlder 47:21bf4096faa1 668 uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,
CallumAlder 47:21bf4096faa1 669 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73,
CallumAlder 47:21bf4096faa1 670 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E,
CallumAlder 47:21bf4096faa1 671 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20,
CallumAlder 47:21bf4096faa1 672 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20,
CallumAlder 47:21bf4096faa1 673 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20,
CallumAlder 47:21bf4096faa1 674 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
CallumAlder 47:21bf4096faa1 675 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
CallumAlder 47:21bf4096faa1 676 uint8_t hash[32];
CallumAlder 47:21bf4096faa1 677 uint32_t length64 = 64;
CallumAlder 47:21bf4096faa1 678 uint32_t hashCounter = 0;
CallumAlder 47:21bf4096faa1 679 uint64_t* nonce = (uint64_t*)((int)sequence + 56);
adehadd 27:ce05fed3c1ea 680 uint64_t* key = (uint64_t*)((int)sequence + 48);
iachinweze1 23:ab1cb51527d1 681
CallumAlder 42:121148278dae 682 // Begin Main Timer
CallumAlder 42:121148278dae 683 Timer timer;
CallumAlder 42:121148278dae 684 timer.start();
adehadd 27:ce05fed3c1ea 685
CallumAlder 47:21bf4096faa1 686 // Loop Program
CallumAlder 42:121148278dae 687 while (1) {
adehadd 26:fb6151e5907d 688
CallumAlder 47:21bf4096faa1 689 //try{
CallumAlder 47:21bf4096faa1 690
CallumAlder 47:21bf4096faa1 691 // Mutex For Access Control
adehadd 49:ae8dedfe2d0f 692 comm_port._newKeyMutex.lock();
CallumAlder 47:21bf4096faa1 693 *key = comm_port._newKey;
adehadd 49:ae8dedfe2d0f 694 comm_port._newKeyMutex.unlock();
adehadd 20:c60f4785b556 695
CallumAlder 47:21bf4096faa1 696 // Compute Hash and Counter
CallumAlder 47:21bf4096faa1 697 miner.computeHash(hash, sequence, length64);
CallumAlder 47:21bf4096faa1 698 hashCounter++;
CallumAlder 47:21bf4096faa1 699
CallumAlder 47:21bf4096faa1 700 // Enum Casting and Condition
CallumAlder 47:21bf4096faa1 701 if (hash[0]==0 && hash[1]==0)
CallumAlder 47:21bf4096faa1 702 comm_port.putMessage((Comm::msgType)7, *nonce);
adehadd 20:c60f4785b556 703
CallumAlder 47:21bf4096faa1 704 // Try Nonce
CallumAlder 47:21bf4096faa1 705 (*nonce)++;
adehadd 26:fb6151e5907d 706
CallumAlder 47:21bf4096faa1 707 // Display via Comm Port
CallumAlder 47:21bf4096faa1 708 if (timer.read() >= 1){
CallumAlder 47:21bf4096faa1 709 comm_port.putMessage((Comm::msgType)5, hashCounter);
CallumAlder 47:21bf4096faa1 710 hashCounter=0;
CallumAlder 47:21bf4096faa1 711 timer.reset();
CallumAlder 47:21bf4096faa1 712 }
CallumAlder 47:21bf4096faa1 713 //}
adehadd 26:fb6151e5907d 714
CallumAlder 47:21bf4096faa1 715 //catch(...){
CallumAlder 47:21bf4096faa1 716 // break;
CallumAlder 47:21bf4096faa1 717 //}
CallumAlder 47:21bf4096faa1 718
CallumAlder 15:2f95f2fb68e3 719 }
CallumAlder 42:121148278dae 720
CallumAlder 42:121148278dae 721 return 0;
adehadd 27:ce05fed3c1ea 722 }