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

Dependencies:   Crypto

Committer:
iachinweze1
Date:
Wed Mar 20 08:30:45 2019 +0000
Revision:
38:a3713a09c828
Parent:
37:71adabab284a
Child:
39:05a021718517
19/03

Who changed what in which revision?

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