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

Dependencies:   Crypto

Committer:
adehadd
Date:
Wed Mar 20 10:01:38 2019 +0000
Revision:
39:05a021718517
Parent:
38:a3713a09c828
Child:
40:9fae84f111e6
maybe broke some things - close to getting it to go backwards

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