![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Callum and Adel's changes on 12/02/19
Dependencies: Crypto
main.cpp@24:be5fef3dace1, 2019-03-16 (annotated)
- Committer:
- iachinweze1
- Date:
- Sat Mar 16 16:43:47 2019 +0000
- Revision:
- 24:be5fef3dace1
- Parent:
- 23:ab1cb51527d1
- Child:
- 25:995865498aee
- Child:
- 28:4f02ac845e5d
commitment frightens me
Who changed what in which revision?
User | Revision | Line number | New 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 | 19:805c87360b55 | 11 | */ |
estott | 0:de4320f74764 | 12 | |
estott | 0:de4320f74764 | 13 | //Photointerrupter input pins |
estott | 10:a4b5723b6c9d | 14 | #define I1pin D3 |
estott | 10:a4b5723b6c9d | 15 | #define I2pin D6 |
estott | 10:a4b5723b6c9d | 16 | #define I3pin D5 |
estott | 2:4e88faab6988 | 17 | |
estott | 2:4e88faab6988 | 18 | //Incremental encoder input pins |
estott | 10:a4b5723b6c9d | 19 | #define CHApin D12 |
estott | 10:a4b5723b6c9d | 20 | #define CHBpin D11 |
estott | 0:de4320f74764 | 21 | |
estott | 0:de4320f74764 | 22 | //Motor Drive output pins //Mask in output byte |
estott | 10:a4b5723b6c9d | 23 | #define L1Lpin D1 //0x01 |
estott | 10:a4b5723b6c9d | 24 | #define L1Hpin A3 //0x02 |
estott | 10:a4b5723b6c9d | 25 | #define L2Lpin D0 //0x04 |
estott | 10:a4b5723b6c9d | 26 | #define L2Hpin A6 //0x08 |
estott | 10:a4b5723b6c9d | 27 | #define L3Lpin D10 //0x10 |
estott | 10:a4b5723b6c9d | 28 | #define L3Hpin D2 //0x20 |
estott | 10:a4b5723b6c9d | 29 | |
estott | 10:a4b5723b6c9d | 30 | #define PWMpin D9 |
estott | 5:08f338b5e4d9 | 31 | |
estott | 5:08f338b5e4d9 | 32 | //Motor current sense |
estott | 5:08f338b5e4d9 | 33 | #define MCSPpin A1 |
estott | 5:08f338b5e4d9 | 34 | #define MCSNpin A0 |
estott | 0:de4320f74764 | 35 | |
estott | 0:de4320f74764 | 36 | //Mapping from sequential drive states to motor phase outputs |
estott | 0:de4320f74764 | 37 | /* |
estott | 0:de4320f74764 | 38 | State L1 L2 L3 |
estott | 0:de4320f74764 | 39 | 0 H - L |
estott | 0:de4320f74764 | 40 | 1 - H L |
estott | 0:de4320f74764 | 41 | 2 L H - |
estott | 0:de4320f74764 | 42 | 3 L - H |
estott | 0:de4320f74764 | 43 | 4 - L H |
estott | 0:de4320f74764 | 44 | 5 H L - |
estott | 0:de4320f74764 | 45 | 6 - - - |
estott | 0:de4320f74764 | 46 | 7 - - - |
estott | 0:de4320f74764 | 47 | */ |
estott | 0:de4320f74764 | 48 | //Drive state to output table |
estott | 0:de4320f74764 | 49 | const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00}; |
estott | 2:4e88faab6988 | 50 | |
estott | 0:de4320f74764 | 51 | //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid |
iachinweze1 | 23:ab1cb51527d1 | 52 | const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07}; |
estott | 2:4e88faab6988 | 53 | //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 | 54 | |
estott | 2:4e88faab6988 | 55 | //Phase lead to make motor spin |
estott | 3:569b35e2a602 | 56 | const int8_t lead = 2; //2 for forwards, -2 for backwards |
estott | 0:de4320f74764 | 57 | |
estott | 0:de4320f74764 | 58 | //Status LED |
estott | 0:de4320f74764 | 59 | DigitalOut led1(LED1); |
estott | 0:de4320f74764 | 60 | |
estott | 0:de4320f74764 | 61 | //Photointerrupter inputs |
iachinweze1 | 12:41b3112021a3 | 62 | InterruptIn I1(I1pin); |
iachinweze1 | 12:41b3112021a3 | 63 | InterruptIn I2(I2pin); |
iachinweze1 | 12:41b3112021a3 | 64 | InterruptIn I3(I3pin); |
estott | 0:de4320f74764 | 65 | |
estott | 0:de4320f74764 | 66 | //Motor Drive outputs |
adehadd | 20:c60f4785b556 | 67 | DigitalOut L1L(L1Lpin); |
estott | 0:de4320f74764 | 68 | DigitalOut L1H(L1Hpin); |
adehadd | 20:c60f4785b556 | 69 | DigitalOut L2L(L2Lpin); |
estott | 0:de4320f74764 | 70 | DigitalOut L2H(L2Hpin); |
adehadd | 20:c60f4785b556 | 71 | DigitalOut L3L(L3Lpin); |
estott | 0:de4320f74764 | 72 | DigitalOut L3H(L3Hpin); |
estott | 0:de4320f74764 | 73 | |
adehadd | 21:b296db05483d | 74 | PwmOut pwmCtrl(PWMpin); |
iachinweze1 | 23:ab1cb51527d1 | 75 | |
iachinweze1 | 24:be5fef3dace1 | 76 | uint8_t stateCount[3]; |
iachinweze1 | 24:be5fef3dace1 | 77 | uint8_t theStates[3]; |
CallumAlder | 19:805c87360b55 | 78 | |
adehadd | 20:c60f4785b556 | 79 | class Comm /*: public T_*/{ |
iachinweze1 | 23:ab1cb51527d1 | 80 | |
iachinweze1 | 23:ab1cb51527d1 | 81 | public: |
iachinweze1 | 23:ab1cb51527d1 | 82 | |
iachinweze1 | 23:ab1cb51527d1 | 83 | Thread t_comm_out; |
iachinweze1 | 23:ab1cb51527d1 | 84 | Thread t_motor_ctrl; |
iachinweze1 | 23:ab1cb51527d1 | 85 | // Thread *p_motor_ctrl; |
CallumAlder | 19:805c87360b55 | 86 | |
iachinweze1 | 23:ab1cb51527d1 | 87 | bool _RUN; |
iachinweze1 | 23:ab1cb51527d1 | 88 | |
iachinweze1 | 23:ab1cb51527d1 | 89 | RawSerial pc; |
iachinweze1 | 23:ab1cb51527d1 | 90 | // Queue<void, 8> inCharQ; // Input Character Queue |
iachinweze1 | 23:ab1cb51527d1 | 91 | |
iachinweze1 | 23:ab1cb51527d1 | 92 | |
iachinweze1 | 23:ab1cb51527d1 | 93 | static const char MsgChar[11]; |
CallumAlder | 19:805c87360b55 | 94 | |
iachinweze1 | 23:ab1cb51527d1 | 95 | uint8_t MAXCMDLENGTH; |
iachinweze1 | 23:ab1cb51527d1 | 96 | |
iachinweze1 | 23:ab1cb51527d1 | 97 | volatile uint8_t cmdIndx; |
iachinweze1 | 23:ab1cb51527d1 | 98 | volatile uint8_t inCharQIdx; |
CallumAlder | 19:805c87360b55 | 99 | |
iachinweze1 | 23:ab1cb51527d1 | 100 | volatile uint32_t motorPower; // motor toque |
iachinweze1 | 23:ab1cb51527d1 | 101 | volatile float targetVel; |
iachinweze1 | 23:ab1cb51527d1 | 102 | volatile float targetRot; |
CallumAlder | 19:805c87360b55 | 103 | |
iachinweze1 | 23:ab1cb51527d1 | 104 | enum msgType {motorState, posIn, velIn, posOut, velOut, |
iachinweze1 | 23:ab1cb51527d1 | 105 | |
iachinweze1 | 23:ab1cb51527d1 | 106 | hashRate, keyAdded, nonceMatch, |
iachinweze1 | 23:ab1cb51527d1 | 107 | |
iachinweze1 | 23:ab1cb51527d1 | 108 | torque, rotations, |
adehadd | 16:db7ef0a4aa23 | 109 | |
iachinweze1 | 23:ab1cb51527d1 | 110 | error}; |
iachinweze1 | 23:ab1cb51527d1 | 111 | |
iachinweze1 | 23:ab1cb51527d1 | 112 | typedef struct { |
iachinweze1 | 23:ab1cb51527d1 | 113 | msgType type; |
iachinweze1 | 23:ab1cb51527d1 | 114 | uint32_t message; |
iachinweze1 | 23:ab1cb51527d1 | 115 | } msg; |
iachinweze1 | 23:ab1cb51527d1 | 116 | |
iachinweze1 | 23:ab1cb51527d1 | 117 | Mail<msg, 32> mailStack; |
iachinweze1 | 23:ab1cb51527d1 | 118 | |
iachinweze1 | 23:ab1cb51527d1 | 119 | void serialISR(){ |
iachinweze1 | 23:ab1cb51527d1 | 120 | if (pc.readable()) { |
iachinweze1 | 23:ab1cb51527d1 | 121 | char newChar = pc.getc(); |
iachinweze1 | 23:ab1cb51527d1 | 122 | // inCharQ.put((void*)newChar); // void* = pointer to an unknown type that cannot be dereferenced |
CallumAlder | 19:805c87360b55 | 123 | |
iachinweze1 | 23:ab1cb51527d1 | 124 | if (inCharQIdx == (MAXCMDLENGTH)) { |
iachinweze1 | 23:ab1cb51527d1 | 125 | inCharQ[MAXCMDLENGTH] = '\0'; // force the string to have an end character |
iachinweze1 | 23:ab1cb51527d1 | 126 | putMessage(error, 1); |
iachinweze1 | 23:ab1cb51527d1 | 127 | inCharQIdx = 0; // reset buffer index |
iachinweze1 | 23:ab1cb51527d1 | 128 | // pc.putc('\r'); // carriage return moves to the start of the line |
iachinweze1 | 23:ab1cb51527d1 | 129 | // for (int i = 0; i < MAXCMDLENGTH; ++i) |
iachinweze1 | 23:ab1cb51527d1 | 130 | // { |
iachinweze1 | 23:ab1cb51527d1 | 131 | // inCharQ[i] = ' '; |
iachinweze1 | 23:ab1cb51527d1 | 132 | // pc.putc(' '); |
iachinweze1 | 23:ab1cb51527d1 | 133 | // } |
iachinweze1 | 23:ab1cb51527d1 | 134 | |
iachinweze1 | 23:ab1cb51527d1 | 135 | // pc.putc('\r'); // carriage return moves to the start of the line |
iachinweze1 | 23:ab1cb51527d1 | 136 | } |
iachinweze1 | 23:ab1cb51527d1 | 137 | else{ |
iachinweze1 | 23:ab1cb51527d1 | 138 | if(newChar != '\r'){ //While the command is not over, |
iachinweze1 | 23:ab1cb51527d1 | 139 | inCharQ[inCharQIdx] = newChar; //save input character and |
iachinweze1 | 23:ab1cb51527d1 | 140 | inCharQIdx++; //advance index |
iachinweze1 | 23:ab1cb51527d1 | 141 | pc.putc(newChar); |
adehadd | 20:c60f4785b556 | 142 | } |
adehadd | 20:c60f4785b556 | 143 | else{ |
iachinweze1 | 23:ab1cb51527d1 | 144 | inCharQ[inCharQIdx] = '\0'; //When the command is finally over, |
iachinweze1 | 23:ab1cb51527d1 | 145 | strncpy(newCmd, inCharQ, MAXCMDLENGTH); // Will copy 18 characters from inCharQ to newCmd |
iachinweze1 | 23:ab1cb51527d1 | 146 | cmdParser(); //parse the command for decoding. |
iachinweze1 | 23:ab1cb51527d1 | 147 | for (int i = 0; i < MAXCMDLENGTH; ++i) // reset buffer |
iachinweze1 | 23:ab1cb51527d1 | 148 | inCharQ[i] = ' '; |
iachinweze1 | 23:ab1cb51527d1 | 149 | inCharQIdx = 0; // reset index |
CallumAlder | 19:805c87360b55 | 150 | } |
CallumAlder | 19:805c87360b55 | 151 | } |
CallumAlder | 19:805c87360b55 | 152 | } |
CallumAlder | 19:805c87360b55 | 153 | |
iachinweze1 | 23:ab1cb51527d1 | 154 | |
iachinweze1 | 23:ab1cb51527d1 | 155 | } |
iachinweze1 | 23:ab1cb51527d1 | 156 | |
iachinweze1 | 23:ab1cb51527d1 | 157 | /*void commInFn() { |
iachinweze1 | 23:ab1cb51527d1 | 158 | // if (_RUN) |
iachinweze1 | 23:ab1cb51527d1 | 159 | |
iachinweze1 | 23:ab1cb51527d1 | 160 | while (_RUN) { |
iachinweze1 | 23:ab1cb51527d1 | 161 | osEvent newEvent = inCharQ.get(); |
iachinweze1 | 23:ab1cb51527d1 | 162 | uint8_t newChar = (uint8_t)(newEvent.value.p); // size_t to type cast the 64bit pointer properly |
iachinweze1 | 23:ab1cb51527d1 | 163 | pc.putc(newChar); |
iachinweze1 | 23:ab1cb51527d1 | 164 | if(cmdIndx >= MAXCMDLENGTH){ //Make sure there is no overflow in comand. |
iachinweze1 | 23:ab1cb51527d1 | 165 | cmdIndx = 0; |
iachinweze1 | 23:ab1cb51527d1 | 166 | putMessage(error, 1); |
iachinweze1 | 23:ab1cb51527d1 | 167 | } |
iachinweze1 | 23:ab1cb51527d1 | 168 | else{ |
iachinweze1 | 23:ab1cb51527d1 | 169 | if(newChar != '\r'){ //While the command is not over, |
iachinweze1 | 23:ab1cb51527d1 | 170 | newCmd[cmdIndx] = newChar; //save input character and |
iachinweze1 | 23:ab1cb51527d1 | 171 | cmdIndx++; //advance index |
iachinweze1 | 23:ab1cb51527d1 | 172 | } |
iachinweze1 | 23:ab1cb51527d1 | 173 | else{ |
iachinweze1 | 23:ab1cb51527d1 | 174 | newCmd[cmdIndx] = '\0'; //When the command is finally over, |
iachinweze1 | 23:ab1cb51527d1 | 175 | cmdIndx = 0; //reset index and |
iachinweze1 | 23:ab1cb51527d1 | 176 | cmdParser(); //parse the command for decoding. |
iachinweze1 | 23:ab1cb51527d1 | 177 | } |
iachinweze1 | 23:ab1cb51527d1 | 178 | } |
iachinweze1 | 23:ab1cb51527d1 | 179 | } |
iachinweze1 | 23:ab1cb51527d1 | 180 | }*/ |
iachinweze1 | 23:ab1cb51527d1 | 181 | |
iachinweze1 | 23:ab1cb51527d1 | 182 | void returnCursor() { |
iachinweze1 | 23:ab1cb51527d1 | 183 | pc.putc('>'); |
iachinweze1 | 23:ab1cb51527d1 | 184 | for (int i = 0; i < inCharQIdx; ++i) // reset cursor position |
iachinweze1 | 23:ab1cb51527d1 | 185 | pc.putc(inCharQ[i]); |
iachinweze1 | 23:ab1cb51527d1 | 186 | // for (int i = inCharQIdx; i < MAXCMDLENGTH; ++i) // fill remaining with blanks |
iachinweze1 | 23:ab1cb51527d1 | 187 | // pc.putc(' '); |
iachinweze1 | 23:ab1cb51527d1 | 188 | // pc.putc('<'); |
iachinweze1 | 23:ab1cb51527d1 | 189 | } |
iachinweze1 | 23:ab1cb51527d1 | 190 | |
iachinweze1 | 23:ab1cb51527d1 | 191 | void cmdParser(){ |
iachinweze1 | 23:ab1cb51527d1 | 192 | switch(newCmd[0]) { |
iachinweze1 | 23:ab1cb51527d1 | 193 | case 'K': //(MsgChar[keyAdded]):// |
iachinweze1 | 23:ab1cb51527d1 | 194 | newKey_mutex.lock(); //Ensure there is no deadlock |
iachinweze1 | 23:ab1cb51527d1 | 195 | sscanf(newCmd, "K%x", &newKey); //Find desired the Key code |
iachinweze1 | 23:ab1cb51527d1 | 196 | putMessage(keyAdded, newKey); //Print it out |
iachinweze1 | 23:ab1cb51527d1 | 197 | newKey_mutex.unlock(); |
iachinweze1 | 23:ab1cb51527d1 | 198 | break; |
iachinweze1 | 23:ab1cb51527d1 | 199 | case 'V': //(MsgChar[velIn]):// |
iachinweze1 | 23:ab1cb51527d1 | 200 | sscanf(newCmd, "V%f", &targetVel); //Find desired the target velocity |
iachinweze1 | 23:ab1cb51527d1 | 201 | putMessage(velIn, targetVel); //Print it out |
iachinweze1 | 23:ab1cb51527d1 | 202 | break; |
iachinweze1 | 23:ab1cb51527d1 | 203 | case 'R': //(MsgChar[posIn]):// |
iachinweze1 | 23:ab1cb51527d1 | 204 | sscanf(newCmd, "R%f", &targetRot); //Find desired target rotation |
iachinweze1 | 23:ab1cb51527d1 | 205 | putMessage(posIn, targetRot); //Print it out |
iachinweze1 | 23:ab1cb51527d1 | 206 | break; |
iachinweze1 | 23:ab1cb51527d1 | 207 | case 'T': //(MsgChar[torque]):// |
iachinweze1 | 23:ab1cb51527d1 | 208 | sscanf(newCmd, "T%d", &motorPower); //Find desired target torque |
iachinweze1 | 23:ab1cb51527d1 | 209 | putMessage(torque, motorPower); //Print it out |
iachinweze1 | 23:ab1cb51527d1 | 210 | break; |
iachinweze1 | 23:ab1cb51527d1 | 211 | default: break; |
iachinweze1 | 23:ab1cb51527d1 | 212 | } |
iachinweze1 | 23:ab1cb51527d1 | 213 | } |
CallumAlder | 19:805c87360b55 | 214 | |
iachinweze1 | 23:ab1cb51527d1 | 215 | //~~~~~Decode messages to print on serial port~~~~~ |
iachinweze1 | 23:ab1cb51527d1 | 216 | void commOutFn() { |
iachinweze1 | 23:ab1cb51527d1 | 217 | while (_RUN) { |
iachinweze1 | 23:ab1cb51527d1 | 218 | osEvent newEvent = mailStack.get(); |
iachinweze1 | 23:ab1cb51527d1 | 219 | msg *pMessage = (msg *) newEvent.value.p; |
iachinweze1 | 23:ab1cb51527d1 | 220 | |
iachinweze1 | 23:ab1cb51527d1 | 221 | //Case switch to choose serial output based on incoming message |
iachinweze1 | 23:ab1cb51527d1 | 222 | switch (pMessage->type) { |
iachinweze1 | 23:ab1cb51527d1 | 223 | case motorState: |
iachinweze1 | 23:ab1cb51527d1 | 224 | pc.printf("The motor is currently in state %x\n\r", pMessage->message); |
iachinweze1 | 23:ab1cb51527d1 | 225 | break; |
iachinweze1 | 23:ab1cb51527d1 | 226 | case hashRate: |
iachinweze1 | 23:ab1cb51527d1 | 227 | pc.printf("\r>%s< Mining: %.4u Hash/s\r", inCharQ, (uint32_t) pMessage->message); |
iachinweze1 | 23:ab1cb51527d1 | 228 | returnCursor(); |
iachinweze1 | 23:ab1cb51527d1 | 229 | break; |
iachinweze1 | 23:ab1cb51527d1 | 230 | case nonceMatch: |
iachinweze1 | 23:ab1cb51527d1 | 231 | pc.printf("\r>%s< Nonce found: %x\r", inCharQ, pMessage->message); |
iachinweze1 | 23:ab1cb51527d1 | 232 | returnCursor(); |
iachinweze1 | 23:ab1cb51527d1 | 233 | break; |
iachinweze1 | 23:ab1cb51527d1 | 234 | case keyAdded: |
iachinweze1 | 23:ab1cb51527d1 | 235 | pc.printf("New Key Added:\t0x%016x\n\r", pMessage->message); |
iachinweze1 | 23:ab1cb51527d1 | 236 | break; |
iachinweze1 | 23:ab1cb51527d1 | 237 | case torque: |
iachinweze1 | 23:ab1cb51527d1 | 238 | pc.printf("Motor Torque set to:\t%d\n\r", pMessage->message); |
iachinweze1 | 23:ab1cb51527d1 | 239 | break; |
iachinweze1 | 23:ab1cb51527d1 | 240 | case velIn: |
iachinweze1 | 23:ab1cb51527d1 | 241 | pc.printf("Target Velocity set to:\t%.2f\n\r", targetVel); |
iachinweze1 | 23:ab1cb51527d1 | 242 | break; |
iachinweze1 | 23:ab1cb51527d1 | 243 | case velOut: |
iachinweze1 | 23:ab1cb51527d1 | 244 | pc.printf("Current Velocity:\t%.2f\n\r", \ |
iachinweze1 | 23:ab1cb51527d1 | 245 | (float) ((int32_t) pMessage->message / 6)); |
iachinweze1 | 23:ab1cb51527d1 | 246 | break; |
iachinweze1 | 23:ab1cb51527d1 | 247 | case posIn: |
iachinweze1 | 23:ab1cb51527d1 | 248 | pc.printf("Target Rotation set to:\t%.2f\n\r", \ |
iachinweze1 | 23:ab1cb51527d1 | 249 | (float) ((int32_t) pMessage->message / 6)); |
iachinweze1 | 23:ab1cb51527d1 | 250 | break; |
iachinweze1 | 23:ab1cb51527d1 | 251 | case posOut: |
iachinweze1 | 23:ab1cb51527d1 | 252 | pc.printf("Current Position:\t%.2f\n\r", \ |
iachinweze1 | 23:ab1cb51527d1 | 253 | (float) ((int32_t) pMessage->message / 6)); |
iachinweze1 | 23:ab1cb51527d1 | 254 | break; |
iachinweze1 | 23:ab1cb51527d1 | 255 | case error: |
iachinweze1 | 23:ab1cb51527d1 | 256 | pc.printf("\r>%s< Debugging position:%x\n\r", inCharQ, pMessage->message); |
iachinweze1 | 23:ab1cb51527d1 | 257 | for (int i = 0; i < MAXCMDLENGTH; ++i) // reset buffer |
iachinweze1 | 23:ab1cb51527d1 | 258 | inCharQ[i] = ' '; |
iachinweze1 | 23:ab1cb51527d1 | 259 | break; |
iachinweze1 | 23:ab1cb51527d1 | 260 | default: |
iachinweze1 | 23:ab1cb51527d1 | 261 | pc.printf("Unknown Error. Message: %x\n\r", pMessage->message); |
iachinweze1 | 23:ab1cb51527d1 | 262 | break; |
CallumAlder | 19:805c87360b55 | 263 | } |
iachinweze1 | 23:ab1cb51527d1 | 264 | mailStack.free(pMessage); |
iachinweze1 | 23:ab1cb51527d1 | 265 | } |
iachinweze1 | 23:ab1cb51527d1 | 266 | } |
adehadd | 20:c60f4785b556 | 267 | |
iachinweze1 | 23:ab1cb51527d1 | 268 | // attach_us -> runs funtion every 100ms |
iachinweze1 | 23:ab1cb51527d1 | 269 | void motorCtrlFn() { |
iachinweze1 | 23:ab1cb51527d1 | 270 | Ticker motorCtrlTicker; |
iachinweze1 | 23:ab1cb51527d1 | 271 | motorCtrlTicker.attach_us(callback(this,&Comm::motorCtrlTick), 1e5); |
iachinweze1 | 24:be5fef3dace1 | 272 | uint8_t cpyStateCount[3]; |
iachinweze1 | 24:be5fef3dace1 | 273 | uint8_t cpyCurrentState; |
iachinweze1 | 24:be5fef3dace1 | 274 | while (_RUN) { |
iachinweze1 | 23:ab1cb51527d1 | 275 | t_motor_ctrl.signal_wait((int32_t)0x1); |
iachinweze1 | 24:be5fef3dace1 | 276 | core_util_critical_section_enter(); |
iachinweze1 | 24:be5fef3dace1 | 277 | //Access shared variables here |
iachinweze1 | 24:be5fef3dace1 | 278 | std::copy(stateCount, stateCount+3, cpyStateCount); |
iachinweze1 | 24:be5fef3dace1 | 279 | // TODO: A thing |
iachinweze1 | 24:be5fef3dace1 | 280 | cpyCurrentState = 0; |
iachinweze1 | 24:be5fef3dace1 | 281 | for (int i = 0; i < 3; ++i) { |
iachinweze1 | 24:be5fef3dace1 | 282 | stateCount[i] = 0; |
iachinweze1 | 24:be5fef3dace1 | 283 | } |
iachinweze1 | 24:be5fef3dace1 | 284 | core_util_critical_section_exit(); |
iachinweze1 | 24:be5fef3dace1 | 285 | |
iachinweze1 | 24:be5fef3dace1 | 286 | uint8_t iterElementMax = std::max_element(cpyStateCount, cpyStateCount+3) - cpyStateCount; |
iachinweze1 | 24:be5fef3dace1 | 287 | |
iachinweze1 | 24:be5fef3dace1 | 288 | int16_t totalDegrees = 360 * cpyStateCount[iterElementMax]; |
iachinweze1 | 24:be5fef3dace1 | 289 | int16_t stateDiff = theStates[iterElementMax]-cpyCurrentState; |
iachinweze1 | 24:be5fef3dace1 | 290 | if (stateDiff >= 0) { |
iachinweze1 | 24:be5fef3dace1 | 291 | totalDegrees = totalDegrees + (60* stateDiff); |
iachinweze1 | 24:be5fef3dace1 | 292 | } else { |
iachinweze1 | 24:be5fef3dace1 | 293 | totalDegrees = totalDegrees + (60*stateDiff*-1); |
iachinweze1 | 24:be5fef3dace1 | 294 | } |
iachinweze1 | 24:be5fef3dace1 | 295 | pc.printf("%u,%u,%u,%u. %.6i \r", iterElementMax, cpyStateCount[0],cpyStateCount[1],cpyStateCount[2], (totalDegrees*10)); |
iachinweze1 | 23:ab1cb51527d1 | 296 | } |
iachinweze1 | 23:ab1cb51527d1 | 297 | } |
adehadd | 20:c60f4785b556 | 298 | |
iachinweze1 | 23:ab1cb51527d1 | 299 | void motorCtrlTick(){ |
iachinweze1 | 23:ab1cb51527d1 | 300 | t_motor_ctrl.signal_set(0x1); |
iachinweze1 | 23:ab1cb51527d1 | 301 | } |
adehadd | 20:c60f4785b556 | 302 | |
adehadd | 20:c60f4785b556 | 303 | |
iachinweze1 | 23:ab1cb51527d1 | 304 | //TODO: stop function, maybe use parent de-constructor |
iachinweze1 | 23:ab1cb51527d1 | 305 | //void stop_comm{} |
iachinweze1 | 23:ab1cb51527d1 | 306 | |
iachinweze1 | 23:ab1cb51527d1 | 307 | // public: |
iachinweze1 | 23:ab1cb51527d1 | 308 | |
iachinweze1 | 23:ab1cb51527d1 | 309 | volatile uint64_t newKey; // hash key |
iachinweze1 | 23:ab1cb51527d1 | 310 | Mutex newKey_mutex; // Restrict access to prevent deadlock. |
adehadd | 20:c60f4785b556 | 311 | |
iachinweze1 | 23:ab1cb51527d1 | 312 | Comm() : pc(SERIAL_TX, SERIAL_RX), |
iachinweze1 | 23:ab1cb51527d1 | 313 | t_comm_out(osPriorityAboveNormal, 1024), |
iachinweze1 | 24:be5fef3dace1 | 314 | t_motor_ctrl(osPriorityAboveNormal2, 1024) |
iachinweze1 | 23:ab1cb51527d1 | 315 | { // inherit from the RawSerial constructor |
adehadd | 20:c60f4785b556 | 316 | |
iachinweze1 | 23:ab1cb51527d1 | 317 | pc.printf("%s\n\r", "Welcome" ); |
iachinweze1 | 23:ab1cb51527d1 | 318 | MAXCMDLENGTH = 18; |
adehadd | 20:c60f4785b556 | 319 | |
iachinweze1 | 23:ab1cb51527d1 | 320 | // reset buffer |
iachinweze1 | 23:ab1cb51527d1 | 321 | // MbedOS prints 'Embedded Systems are fun and do awesome things!' |
iachinweze1 | 23:ab1cb51527d1 | 322 | // if you print a null terminator |
iachinweze1 | 23:ab1cb51527d1 | 323 | pc.putc('>'); |
iachinweze1 | 23:ab1cb51527d1 | 324 | for (int i = 0; i < MAXCMDLENGTH; ++i) { |
iachinweze1 | 23:ab1cb51527d1 | 325 | inCharQ[i] = '.'; |
iachinweze1 | 23:ab1cb51527d1 | 326 | pc.putc('.'); |
CallumAlder | 19:805c87360b55 | 327 | } |
iachinweze1 | 23:ab1cb51527d1 | 328 | pc.putc('<'); |
iachinweze1 | 23:ab1cb51527d1 | 329 | pc.putc('\r'); |
iachinweze1 | 23:ab1cb51527d1 | 330 | |
iachinweze1 | 23:ab1cb51527d1 | 331 | inCharQ[MAXCMDLENGTH] = '\0'; |
iachinweze1 | 23:ab1cb51527d1 | 332 | strncpy(newCmd, inCharQ, MAXCMDLENGTH); |
iachinweze1 | 23:ab1cb51527d1 | 333 | |
iachinweze1 | 23:ab1cb51527d1 | 334 | cmdIndx = 0; |
iachinweze1 | 23:ab1cb51527d1 | 335 | |
iachinweze1 | 23:ab1cb51527d1 | 336 | inCharQIdx = 0; |
iachinweze1 | 23:ab1cb51527d1 | 337 | // inCharQIdx = MAXCMDLENGTH-1; |
iachinweze1 | 23:ab1cb51527d1 | 338 | |
iachinweze1 | 23:ab1cb51527d1 | 339 | |
iachinweze1 | 23:ab1cb51527d1 | 340 | |
iachinweze1 | 23:ab1cb51527d1 | 341 | pc.attach(callback(this, &Comm::serialISR)); |
iachinweze1 | 23:ab1cb51527d1 | 342 | |
iachinweze1 | 23:ab1cb51527d1 | 343 | // Thread t_comm_in(osPriorityAboveNormal, 1024); |
iachinweze1 | 23:ab1cb51527d1 | 344 | // Thread t_comm_out(osPriorityAboveNormal, 1024); |
iachinweze1 | 23:ab1cb51527d1 | 345 | // Thread t_motor_ctrl(osPriorityAboveNormal, 1024); |
iachinweze1 | 23:ab1cb51527d1 | 346 | |
iachinweze1 | 23:ab1cb51527d1 | 347 | motorPower = 300; |
iachinweze1 | 23:ab1cb51527d1 | 348 | targetVel = 45.0; |
iachinweze1 | 23:ab1cb51527d1 | 349 | targetRot = 459.0; |
iachinweze1 | 23:ab1cb51527d1 | 350 | |
adehadd | 20:c60f4785b556 | 351 | |
CallumAlder | 19:805c87360b55 | 352 | |
iachinweze1 | 23:ab1cb51527d1 | 353 | /*MsgChar = {'m', 'R', 'V', 'r', 'v', |
iachinweze1 | 23:ab1cb51527d1 | 354 | |
iachinweze1 | 23:ab1cb51527d1 | 355 | 'h', 'K', 'n', |
iachinweze1 | 23:ab1cb51527d1 | 356 | |
iachinweze1 | 23:ab1cb51527d1 | 357 | 'T', 'r', |
iachinweze1 | 23:ab1cb51527d1 | 358 | |
iachinweze1 | 23:ab1cb51527d1 | 359 | 'e'};*/ |
iachinweze1 | 23:ab1cb51527d1 | 360 | } |
iachinweze1 | 23:ab1cb51527d1 | 361 | |
iachinweze1 | 23:ab1cb51527d1 | 362 | |
iachinweze1 | 23:ab1cb51527d1 | 363 | void putMessage(msgType type, uint32_t message){ |
iachinweze1 | 23:ab1cb51527d1 | 364 | msg *p_msg = mailStack.alloc(); |
iachinweze1 | 23:ab1cb51527d1 | 365 | p_msg->type = type; |
iachinweze1 | 23:ab1cb51527d1 | 366 | p_msg->message = message; |
iachinweze1 | 23:ab1cb51527d1 | 367 | mailStack.put(p_msg); |
iachinweze1 | 23:ab1cb51527d1 | 368 | } |
iachinweze1 | 23:ab1cb51527d1 | 369 | |
iachinweze1 | 23:ab1cb51527d1 | 370 | void start_comm(){ |
iachinweze1 | 23:ab1cb51527d1 | 371 | _RUN = true; |
iachinweze1 | 23:ab1cb51527d1 | 372 | |
adehadd | 20:c60f4785b556 | 373 | |
iachinweze1 | 23:ab1cb51527d1 | 374 | // reset buffer |
iachinweze1 | 23:ab1cb51527d1 | 375 | // MbedOS prints 'Embedded Systems are fun and do awesome things!' |
iachinweze1 | 23:ab1cb51527d1 | 376 | // if you print a null terminator |
iachinweze1 | 23:ab1cb51527d1 | 377 | pc.putc('>'); |
iachinweze1 | 23:ab1cb51527d1 | 378 | for (int i = 0; i < MAXCMDLENGTH; ++i) { |
iachinweze1 | 23:ab1cb51527d1 | 379 | inCharQ[i] = '.'; |
iachinweze1 | 23:ab1cb51527d1 | 380 | pc.putc('.'); |
iachinweze1 | 23:ab1cb51527d1 | 381 | } |
iachinweze1 | 23:ab1cb51527d1 | 382 | pc.putc('<'); |
iachinweze1 | 23:ab1cb51527d1 | 383 | pc.putc('\r'); |
iachinweze1 | 23:ab1cb51527d1 | 384 | |
iachinweze1 | 23:ab1cb51527d1 | 385 | inCharQ[MAXCMDLENGTH] = '\0'; |
iachinweze1 | 23:ab1cb51527d1 | 386 | strncpy(newCmd, inCharQ, MAXCMDLENGTH); |
adehadd | 20:c60f4785b556 | 387 | |
iachinweze1 | 23:ab1cb51527d1 | 388 | // returnCursor(); |
adehadd | 20:c60f4785b556 | 389 | |
iachinweze1 | 23:ab1cb51527d1 | 390 | // t_comm_in.start(callback(this, &Comm::commInFn)); |
iachinweze1 | 23:ab1cb51527d1 | 391 | // this::thread::wait() |
iachinweze1 | 23:ab1cb51527d1 | 392 | // wait(1.0); |
iachinweze1 | 23:ab1cb51527d1 | 393 | t_comm_out.start(callback(this, &Comm::commOutFn)); |
iachinweze1 | 23:ab1cb51527d1 | 394 | t_motor_ctrl.start(callback(this, &Comm::motorCtrlFn)); |
iachinweze1 | 23:ab1cb51527d1 | 395 | |
iachinweze1 | 23:ab1cb51527d1 | 396 | |
iachinweze1 | 23:ab1cb51527d1 | 397 | } |
iachinweze1 | 23:ab1cb51527d1 | 398 | |
iachinweze1 | 23:ab1cb51527d1 | 399 | char newCmd[]; // because unallocated must be defined at the bottom of the class |
iachinweze1 | 23:ab1cb51527d1 | 400 | char inCharQ[]; |
CallumAlder | 19:805c87360b55 | 401 | }; |
CallumAlder | 19:805c87360b55 | 402 | |
iachinweze1 | 12:41b3112021a3 | 403 | |
adehadd | 20:c60f4785b556 | 404 | |
adehadd | 20:c60f4785b556 | 405 | //Set a given drive state |
adehadd | 20:c60f4785b556 | 406 | void motorOut(int8_t driveState){ |
iachinweze1 | 23:ab1cb51527d1 | 407 | |
adehadd | 20:c60f4785b556 | 408 | //Lookup the output byte from the drive state. |
adehadd | 20:c60f4785b556 | 409 | int8_t driveOut = driveTable[driveState & 0x07]; |
iachinweze1 | 23:ab1cb51527d1 | 410 | |
adehadd | 20:c60f4785b556 | 411 | //Turn off first |
adehadd | 20:c60f4785b556 | 412 | if (~driveOut & 0x01) L1L = 0; |
adehadd | 20:c60f4785b556 | 413 | if (~driveOut & 0x02) L1H = 1; |
adehadd | 20:c60f4785b556 | 414 | if (~driveOut & 0x04) L2L = 0; |
adehadd | 20:c60f4785b556 | 415 | if (~driveOut & 0x08) L2H = 1; |
adehadd | 20:c60f4785b556 | 416 | if (~driveOut & 0x10) L3L = 0; |
adehadd | 20:c60f4785b556 | 417 | if (~driveOut & 0x20) L3H = 1; |
iachinweze1 | 23:ab1cb51527d1 | 418 | |
adehadd | 20:c60f4785b556 | 419 | //Then turn on |
adehadd | 20:c60f4785b556 | 420 | if (driveOut & 0x01) L1L = 1; |
adehadd | 20:c60f4785b556 | 421 | if (driveOut & 0x02) L1H = 0; |
adehadd | 20:c60f4785b556 | 422 | if (driveOut & 0x04) L2L = 1; |
adehadd | 20:c60f4785b556 | 423 | if (driveOut & 0x08) L2H = 0; |
adehadd | 20:c60f4785b556 | 424 | if (driveOut & 0x10) L3L = 1; |
adehadd | 20:c60f4785b556 | 425 | if (driveOut & 0x20) L3H = 0; |
iachinweze1 | 23:ab1cb51527d1 | 426 | } |
iachinweze1 | 23:ab1cb51527d1 | 427 | |
iachinweze1 | 23:ab1cb51527d1 | 428 | //Convert photointerrupter inputs to a rotor state |
adehadd | 20:c60f4785b556 | 429 | inline int8_t readRotorState(){ |
adehadd | 20:c60f4785b556 | 430 | return stateMap[I1 + 2*I2 + 4*I3]; |
iachinweze1 | 23:ab1cb51527d1 | 431 | } |
adehadd | 20:c60f4785b556 | 432 | |
iachinweze1 | 23:ab1cb51527d1 | 433 | //Basic synchronisation routine |
adehadd | 20:c60f4785b556 | 434 | int8_t motorHome() { |
adehadd | 20:c60f4785b556 | 435 | //Put the motor in drive state 0 and wait for it to stabilise |
adehadd | 20:c60f4785b556 | 436 | motorOut(0); |
adehadd | 20:c60f4785b556 | 437 | wait(2.0); |
iachinweze1 | 23:ab1cb51527d1 | 438 | |
adehadd | 20:c60f4785b556 | 439 | //Get the rotor state |
adehadd | 20:c60f4785b556 | 440 | return readRotorState(); |
adehadd | 20:c60f4785b556 | 441 | } |
adehadd | 20:c60f4785b556 | 442 | |
adehadd | 20:c60f4785b556 | 443 | |
adehadd | 20:c60f4785b556 | 444 | void stateUpdate(int8_t *params[]) { // () { // **params |
iachinweze1 | 23:ab1cb51527d1 | 445 | *params[0] = readRotorState(); |
adehadd | 20:c60f4785b556 | 446 | int8_t currentState = *params[0]; |
adehadd | 20:c60f4785b556 | 447 | int8_t offset = *params[1]; |
iachinweze1 | 23:ab1cb51527d1 | 448 | |
iachinweze1 | 24:be5fef3dace1 | 449 | switch (currentState) { |
iachinweze1 | 24:be5fef3dace1 | 450 | case 1: |
iachinweze1 | 24:be5fef3dace1 | 451 | stateCount[0]++; |
iachinweze1 | 24:be5fef3dace1 | 452 | break; |
iachinweze1 | 24:be5fef3dace1 | 453 | case (1 + lead): |
iachinweze1 | 24:be5fef3dace1 | 454 | stateCount[1]++; |
iachinweze1 | 24:be5fef3dace1 | 455 | break; |
iachinweze1 | 24:be5fef3dace1 | 456 | case (1 + (lead*2)): |
iachinweze1 | 24:be5fef3dace1 | 457 | stateCount[2]++; |
iachinweze1 | 24:be5fef3dace1 | 458 | break; |
iachinweze1 | 24:be5fef3dace1 | 459 | } |
iachinweze1 | 24:be5fef3dace1 | 460 | |
adehadd | 20:c60f4785b556 | 461 | motorOut((currentState - offset + lead + 6) % 6); |
adehadd | 20:c60f4785b556 | 462 | } |
adehadd | 20:c60f4785b556 | 463 | |
estott | 0:de4320f74764 | 464 | //Main |
estott | 0:de4320f74764 | 465 | int main() { |
CallumAlder | 19:805c87360b55 | 466 | |
iachinweze1 | 23:ab1cb51527d1 | 467 | // std::ios::sync_with_stdio(false); |
iachinweze1 | 23:ab1cb51527d1 | 468 | Comm comm_plz; |
adehadd | 20:c60f4785b556 | 469 | |
adehadd | 20:c60f4785b556 | 470 | // comm_plz.pc.printf("%s\n", "do i work bruh" ); // using printf of class is calm |
adehadd | 20:c60f4785b556 | 471 | SHA256 Miner; |
iachinweze1 | 23:ab1cb51527d1 | 472 | |
CallumAlder | 14:4e312fb83330 | 473 | uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64, |
CallumAlder | 14:4e312fb83330 | 474 | 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73, |
CallumAlder | 14:4e312fb83330 | 475 | 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E, |
CallumAlder | 14:4e312fb83330 | 476 | 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20, |
CallumAlder | 14:4e312fb83330 | 477 | 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20, |
CallumAlder | 14:4e312fb83330 | 478 | 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20, |
CallumAlder | 14:4e312fb83330 | 479 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
CallumAlder | 14:4e312fb83330 | 480 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; |
CallumAlder | 14:4e312fb83330 | 481 | uint64_t* key = (uint64_t*)((int)sequence + 48); |
CallumAlder | 14:4e312fb83330 | 482 | uint64_t* nonce = (uint64_t*)((int)sequence + 56); |
CallumAlder | 14:4e312fb83330 | 483 | uint8_t hash[32]; |
CallumAlder | 14:4e312fb83330 | 484 | uint32_t length64 = 64; |
CallumAlder | 15:2f95f2fb68e3 | 485 | uint32_t hashCounter = 0; |
iachinweze1 | 23:ab1cb51527d1 | 486 | Timer timer; |
iachinweze1 | 23:ab1cb51527d1 | 487 | |
adehadd | 21:b296db05483d | 488 | float dutyC = 1; // 100% |
adehadd | 21:b296db05483d | 489 | float mtrPeriod = 2e-3; // motor period |
adehadd | 21:b296db05483d | 490 | |
adehadd | 21:b296db05483d | 491 | pwmCtrl.period(mtrPeriod); |
adehadd | 21:b296db05483d | 492 | pwmCtrl.pulsewidth(mtrPeriod*dutyC); |
adehadd | 21:b296db05483d | 493 | |
adehadd | 20:c60f4785b556 | 494 | comm_plz.start_comm(); |
adehadd | 20:c60f4785b556 | 495 | |
adehadd | 16:db7ef0a4aa23 | 496 | // Motor States |
adehadd | 16:db7ef0a4aa23 | 497 | int8_t orState = 0; //Rotot offset at motor state 0 |
adehadd | 16:db7ef0a4aa23 | 498 | int8_t currentState = 0; //Rotot offset at motor state 0 |
adehadd | 18:7ee632098fd4 | 499 | int8_t stateList[6]; //Rotot offset at motor state 0 |
estott | 0:de4320f74764 | 500 | //Run the motor synchronisation |
adehadd | 20:c60f4785b556 | 501 | orState = motorHome(); |
iachinweze1 | 23:ab1cb51527d1 | 502 | |
iachinweze1 | 24:be5fef3dace1 | 503 | theStates[0] = orState; |
iachinweze1 | 24:be5fef3dace1 | 504 | theStates[1] = (orState + lead) % 6; |
iachinweze1 | 24:be5fef3dace1 | 505 | theStates[2] = (orState + (lead*2)) % 6; |
iachinweze1 | 24:be5fef3dace1 | 506 | |
CallumAlder | 15:2f95f2fb68e3 | 507 | // Add callbacks |
iachinweze1 | 23:ab1cb51527d1 | 508 | // I1.fall(&stateUpdate); |
iachinweze1 | 23:ab1cb51527d1 | 509 | // I2.fall(&stateUpdate); |
iachinweze1 | 23:ab1cb51527d1 | 510 | // I3.fall(&stateUpdate); |
adehadd | 16:db7ef0a4aa23 | 511 | int8_t* params[2]; |
adehadd | 16:db7ef0a4aa23 | 512 | params[0] = ¤tState; |
adehadd | 16:db7ef0a4aa23 | 513 | params[1] = &orState; |
iachinweze1 | 23:ab1cb51527d1 | 514 | |
adehadd | 20:c60f4785b556 | 515 | I1.fall(callback(&stateUpdate,params)); |
adehadd | 16:db7ef0a4aa23 | 516 | I2.fall(callback(&stateUpdate,params)); |
adehadd | 16:db7ef0a4aa23 | 517 | I3.fall(callback(&stateUpdate,params)); |
iachinweze1 | 23:ab1cb51527d1 | 518 | |
adehadd | 18:7ee632098fd4 | 519 | I1.rise(callback(&stateUpdate,params)); |
adehadd | 18:7ee632098fd4 | 520 | I2.rise(callback(&stateUpdate,params)); |
adehadd | 18:7ee632098fd4 | 521 | I3.rise(callback(&stateUpdate,params)); |
iachinweze1 | 23:ab1cb51527d1 | 522 | |
CallumAlder | 15:2f95f2fb68e3 | 523 | // Push motor to move |
iachinweze1 | 12:41b3112021a3 | 524 | currentState = readRotorState(); |
iachinweze1 | 12:41b3112021a3 | 525 | motorOut((currentState-orState+lead+6)%6); // We push it digitally |
iachinweze1 | 23:ab1cb51527d1 | 526 | |
adehadd | 20:c60f4785b556 | 527 | // pc.printf("Rotor origin: %x\n\r",orState); |
adehadd | 20:c60f4785b556 | 528 | // orState is subtracted from future rotor state inputs to align rotor and motor states |
adehadd | 20:c60f4785b556 | 529 | // intState = readRotorState(); |
adehadd | 20:c60f4785b556 | 530 | //if (intState != intStateOld) { |
iachinweze1 | 23:ab1cb51527d1 | 531 | // pc.printf("old:%d \t new:%d \t next:%d \n\r",intStateOld, intState, (intState-orState+lead+6)%6); |
iachinweze1 | 23:ab1cb51527d1 | 532 | // intStateOld = intState; |
iachinweze1 | 23:ab1cb51527d1 | 533 | // motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive |
iachinweze1 | 23:ab1cb51527d1 | 534 | // } |
iachinweze1 | 23:ab1cb51527d1 | 535 | |
adehadd | 21:b296db05483d | 536 | dutyC = 0.8; |
adehadd | 21:b296db05483d | 537 | pwmCtrl.pulsewidth(mtrPeriod*dutyC); |
iachinweze1 | 12:41b3112021a3 | 538 | |
CallumAlder | 15:2f95f2fb68e3 | 539 | |
iachinweze1 | 12:41b3112021a3 | 540 | // Keep the program running indefinitely |
CallumAlder | 15:2f95f2fb68e3 | 541 | timer.start(); // start timer |
adehadd | 18:7ee632098fd4 | 542 | int stateCount = 0; |
CallumAlder | 15:2f95f2fb68e3 | 543 | while (1) { |
iachinweze1 | 23:ab1cb51527d1 | 544 | // pc.printf("Current:%d \t Next:%d \n\r", currentState, (currentState-orState+lead+6)%6); |
iachinweze1 | 23:ab1cb51527d1 | 545 | comm_plz.newKey_mutex.lock(); |
adehadd | 20:c60f4785b556 | 546 | *key = comm_plz.newKey; |
iachinweze1 | 23:ab1cb51527d1 | 547 | comm_plz.newKey_mutex.unlock(); |
CallumAlder | 15:2f95f2fb68e3 | 548 | Miner.computeHash(hash, sequence, length64); |
CallumAlder | 15:2f95f2fb68e3 | 549 | hashCounter++; |
CallumAlder | 15:2f95f2fb68e3 | 550 | if ((hash[0]==0) && (hash[1]==0)){ |
adehadd | 20:c60f4785b556 | 551 | comm_plz.putMessage((Comm::msgType)7, *nonce); |
CallumAlder | 15:2f95f2fb68e3 | 552 | } |
CallumAlder | 15:2f95f2fb68e3 | 553 | |
adehadd | 20:c60f4785b556 | 554 | // Try a new nonce |
CallumAlder | 15:2f95f2fb68e3 | 555 | (*nonce)++; |
CallumAlder | 15:2f95f2fb68e3 | 556 | |
adehadd | 18:7ee632098fd4 | 557 | if (stateCount<6){ |
adehadd | 18:7ee632098fd4 | 558 | stateList[stateCount] = currentState; |
adehadd | 18:7ee632098fd4 | 559 | stateCount++; |
adehadd | 18:7ee632098fd4 | 560 | } |
adehadd | 18:7ee632098fd4 | 561 | else { |
CallumAlder | 19:805c87360b55 | 562 | //pc.printf("states"); |
CallumAlder | 19:805c87360b55 | 563 | //for(int i = 0; i < 6; ++i) |
iachinweze1 | 23:ab1cb51527d1 | 564 | //pc.printf("%02i,", stateList[i]); |
iachinweze1 | 23:ab1cb51527d1 | 565 | //pc.printf("\n\r"); |
iachinweze1 | 23:ab1cb51527d1 | 566 | stateCount = 0; |
adehadd | 18:7ee632098fd4 | 567 | } |
adehadd | 18:7ee632098fd4 | 568 | |
adehadd | 20:c60f4785b556 | 569 | // Per Second i.e. when greater or equal to 1 |
CallumAlder | 15:2f95f2fb68e3 | 570 | if (timer.read() >= 1){ |
adehadd | 20:c60f4785b556 | 571 | comm_plz.putMessage((Comm::msgType)5, hashCounter); |
CallumAlder | 19:805c87360b55 | 572 | //pc.printf("HashRate = %02u \n\r",hashCounter); |
CallumAlder | 15:2f95f2fb68e3 | 573 | hashCounter=0; |
CallumAlder | 15:2f95f2fb68e3 | 574 | timer.reset(); |
CallumAlder | 15:2f95f2fb68e3 | 575 | } |
CallumAlder | 15:2f95f2fb68e3 | 576 | } |
CallumAlder | 19:805c87360b55 | 577 | } |