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

Dependencies:   Crypto

Committer:
CallumAlder
Date:
Wed Mar 20 16:41:44 2019 +0000
Revision:
42:121148278dae
Parent:
27:ce05fed3c1ea
Child:
43:a6d20109b2f2
Begin Tidying and Junk

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