First Commit

Dependencies:   mbed Crypto_light mbed-rtos

Spin it 2 win it

Committer:
andrebharath
Date:
Tue Mar 20 18:59:22 2018 +0000
Revision:
25:bf5fff055aef
Parent:
24:0a0e22d4d8d3
Child:
26:1a7a154e375b
Child:
27:d50f1914f23a
Printing torque. Changed torque from uint32_t to int32_t

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TrebleStick 0:88c3d6c8a4eb 1 #include "mbed.h"
andrebharath 3:2e32d7974962 2 #include "Crypto_light/hash/SHA256.h"
andrebharath 3:2e32d7974962 3 #include "mbed-rtos/rtos/rtos.h"
TrebleStick 0:88c3d6c8a4eb 4
TrebleStick 0:88c3d6c8a4eb 5 //Photointerrupter input pins
TrebleStick 0:88c3d6c8a4eb 6 #define I1pin D2
TrebleStick 0:88c3d6c8a4eb 7 #define I2pin D11
TrebleStick 0:88c3d6c8a4eb 8 #define I3pin D12
andrebharath 9:ecef1e8cbe3d 9
TrebleStick 0:88c3d6c8a4eb 10 //Incremental encoder input pins
TrebleStick 0:88c3d6c8a4eb 11 #define CHA D7
TrebleStick 12:1b2e2540e4e1 12 #define CHB D8
andrebharath 9:ecef1e8cbe3d 13
TrebleStick 0:88c3d6c8a4eb 14 //Motor Drive output pins //Mask in output byte
TrebleStick 0:88c3d6c8a4eb 15 #define L1Lpin D4 //0x01
TrebleStick 0:88c3d6c8a4eb 16 #define L1Hpin D5 //0x02
TrebleStick 0:88c3d6c8a4eb 17 #define L2Lpin D3 //0x04
TrebleStick 0:88c3d6c8a4eb 18 #define L2Hpin D6 //0x08
TrebleStick 0:88c3d6c8a4eb 19 #define L3Lpin D9 //0x10
TrebleStick 0:88c3d6c8a4eb 20 #define L3Hpin D10 //0x20
TrebleStick 0:88c3d6c8a4eb 21
andrebharath 9:ecef1e8cbe3d 22 #define CHAR_ARR_SIZE 18 //Max length of input codes
andrebharath 17:80159ace5ddf 23 #define MAX_PWM_PERIOD 2000
andrebharath 18:05e5d280a082 24 #define MAX_TORQUE 1000
andrebharath 23:58de87ec3997 25 #define KP 25
andrebharath 3:2e32d7974962 26
TrebleStick 0:88c3d6c8a4eb 27 //Mapping from sequential drive states to motor phase outputs
TrebleStick 0:88c3d6c8a4eb 28 /*
TrebleStick 0:88c3d6c8a4eb 29 State L1 L2 L3
TrebleStick 0:88c3d6c8a4eb 30 0 H - L
TrebleStick 0:88c3d6c8a4eb 31 1 - H L
TrebleStick 0:88c3d6c8a4eb 32 2 L H -
TrebleStick 0:88c3d6c8a4eb 33 3 L - H
TrebleStick 0:88c3d6c8a4eb 34 4 - L H
TrebleStick 0:88c3d6c8a4eb 35 5 H L -
TrebleStick 0:88c3d6c8a4eb 36 6 - - -
TrebleStick 0:88c3d6c8a4eb 37 7 - - -
TrebleStick 0:88c3d6c8a4eb 38 */
TrebleStick 0:88c3d6c8a4eb 39 //Drive state to output table
TrebleStick 0:88c3d6c8a4eb 40 const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00};
andrebharath 9:ecef1e8cbe3d 41
TrebleStick 0:88c3d6c8a4eb 42 //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid
TrebleStick 12:1b2e2540e4e1 43 const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07};
TrebleStick 0:88c3d6c8a4eb 44 //const int8_t stateMap[] = {0x07,0x01,0x03,0x02,0x05,0x00,0x04,0x07}; //Alternative if phase order of input or drive is reversed
andrebharath 9:ecef1e8cbe3d 45
TrebleStick 0:88c3d6c8a4eb 46 //Phase lead to make motor spin
TrebleStick 0:88c3d6c8a4eb 47 const int8_t lead = 2; //2 for forwards, -2 for backwards
andrebharath 9:ecef1e8cbe3d 48
andrebharath 9:ecef1e8cbe3d 49 //Rotor offset at motor state 0
andrebharath 18:05e5d280a082 50 volatile int8_t orState = 0;
andrebharath 9:ecef1e8cbe3d 51
andrebharath 17:80159ace5ddf 52 //Set initial torque of 1000
andrebharath 25:bf5fff055aef 53 volatile int32_t torque = 1000;
andrebharath 17:80159ace5ddf 54
andrebharath 9:ecef1e8cbe3d 55
TrebleStick 12:1b2e2540e4e1 56 enum MSG {MSG_RESET, MSG_HASHCOUNT, MSG_NONCE_OK,
TrebleStick 20:a435105305fe 57 MSG_OVERFLOW, MSG_ROT_PEN, MSG_MAX_SPD, MSG_NEW_KEY, MSG_INP_ERR, MSG_TORQUE,
TrebleStick 20:a435105305fe 58 MSG_TEST, MSG_CUR_SPD};
andrebharath 9:ecef1e8cbe3d 59
andrebharath 3:2e32d7974962 60 //Instantiate the serial port
andrebharath 9:ecef1e8cbe3d 61 RawSerial pc(SERIAL_TX, SERIAL_RX);
andrebharath 9:ecef1e8cbe3d 62
andrebharath 9:ecef1e8cbe3d 63 //Status LED
andrebharath 9:ecef1e8cbe3d 64 DigitalOut led1(LED1);
andrebharath 9:ecef1e8cbe3d 65
andrebharath 9:ecef1e8cbe3d 66 //Photointerrupter inputs
andrebharath 9:ecef1e8cbe3d 67 InterruptIn I1(I1pin);
andrebharath 9:ecef1e8cbe3d 68 InterruptIn I2(I2pin);
andrebharath 9:ecef1e8cbe3d 69 InterruptIn I3(I3pin);
andrebharath 9:ecef1e8cbe3d 70
andrebharath 9:ecef1e8cbe3d 71 //Motor Drive outputs
andrebharath 17:80159ace5ddf 72 PwmOut L1L(L1Lpin);
andrebharath 9:ecef1e8cbe3d 73 DigitalOut L1H(L1Hpin);
andrebharath 17:80159ace5ddf 74 PwmOut L2L(L2Lpin);
andrebharath 9:ecef1e8cbe3d 75 DigitalOut L2H(L2Hpin);
andrebharath 17:80159ace5ddf 76 PwmOut L3L(L3Lpin);
andrebharath 9:ecef1e8cbe3d 77 DigitalOut L3H(L3Hpin);
andrebharath 9:ecef1e8cbe3d 78
TrebleStick 0:88c3d6c8a4eb 79
andrebharath 3:2e32d7974962 80
TrebleStick 20:a435105305fe 81
andrebharath 3:2e32d7974962 82 typedef struct {
TrebleStick 20:a435105305fe 83 uint8_t code;
TrebleStick 20:a435105305fe 84 int32_t data;
andrebharath 3:2e32d7974962 85 } message_t ;
andrebharath 3:2e32d7974962 86
andrebharath 3:2e32d7974962 87 Mail<message_t,16> outMessages;
andrebharath 3:2e32d7974962 88
TrebleStick 12:1b2e2540e4e1 89
TrebleStick 15:bd303ab8a21f 90 void putMessage(uint8_t code, int32_t data) {//uint64_t
TrebleStick 12:1b2e2540e4e1 91 message_t *pMessage = outMessages.alloc();
TrebleStick 12:1b2e2540e4e1 92 pMessage->code = code;
TrebleStick 12:1b2e2540e4e1 93 pMessage->data = data;
TrebleStick 12:1b2e2540e4e1 94 outMessages.put(pMessage);
TrebleStick 12:1b2e2540e4e1 95 }
TrebleStick 12:1b2e2540e4e1 96
TrebleStick 21:0d1025dc6433 97 Thread commOutT (osPriorityNormal, 1024);
andrebharath 3:2e32d7974962 98
TrebleStick 12:1b2e2540e4e1 99 void commOutFn() {
andrebharath 3:2e32d7974962 100 while(1) {
andrebharath 3:2e32d7974962 101 osEvent newEvent = outMessages.get();
andrebharath 3:2e32d7974962 102 message_t *pMessage = (message_t*)newEvent.value.p;
TrebleStick 15:bd303ab8a21f 103 pc.printf("Message: [%d], data: 0x%08x\r\n",
andrebharath 3:2e32d7974962 104 pMessage->code,pMessage->data);
andrebharath 3:2e32d7974962 105 outMessages.free(pMessage);
andrebharath 3:2e32d7974962 106 }
andrebharath 3:2e32d7974962 107 }
andrebharath 3:2e32d7974962 108
andrebharath 9:ecef1e8cbe3d 109
andrebharath 9:ecef1e8cbe3d 110
andrebharath 24:0a0e22d4d8d3 111 //Global varible for the Bitcoin Key,targetSpeed and rotations_pending
TrebleStick 12:1b2e2540e4e1 112 volatile uint64_t newKey = 0; //check initialise value? ****
andrebharath 24:0a0e22d4d8d3 113 volatile float targetSpeed = 0, rotations_pending = 0;
TrebleStick 14:66746291017c 114 //mutex variables
TrebleStick 14:66746291017c 115 Mutex newKey_mutex;
andrebharath 24:0a0e22d4d8d3 116 Mutex targetSpeed_mutex;
TrebleStick 14:66746291017c 117 Mutex rotations_pending_mutex;
andrebharath 9:ecef1e8cbe3d 118
andrebharath 9:ecef1e8cbe3d 119 //Instantiate a Queue to buffer incoming characters
andrebharath 9:ecef1e8cbe3d 120 Queue<void, 8> inCharQ;
andrebharath 9:ecef1e8cbe3d 121 //serial port ISR to receive each incoming byte and place into queue
andrebharath 9:ecef1e8cbe3d 122 void serialISR() {
andrebharath 9:ecef1e8cbe3d 123 uint8_t newChar = pc.getc();
andrebharath 9:ecef1e8cbe3d 124 inCharQ.put((void*)newChar);
andrebharath 9:ecef1e8cbe3d 125 }
TrebleStick 20:a435105305fe 126 Thread motorCtrlT (osPriorityNormal, 2048);
TrebleStick 20:a435105305fe 127
TrebleStick 20:a435105305fe 128 int8_t readRotorState();
TrebleStick 20:a435105305fe 129 void motorOut(int8_t,uint32_t);
TrebleStick 20:a435105305fe 130 volatile int32_t motorPosition;
TrebleStick 20:a435105305fe 131
TrebleStick 20:a435105305fe 132 void photointerrupter_isr() {
TrebleStick 20:a435105305fe 133 static int8_t oldRotorState = 0;
TrebleStick 20:a435105305fe 134 int8_t rotorState = readRotorState();
TrebleStick 20:a435105305fe 135 motorOut((rotorState-orState+lead+6)%6, torque); //+6 to make sure the remainder is positive
TrebleStick 20:a435105305fe 136 if (rotorState - oldRotorState == 5) motorPosition--;
TrebleStick 20:a435105305fe 137 else if (rotorState - oldRotorState == -5) motorPosition++;
TrebleStick 20:a435105305fe 138 else motorPosition += (rotorState - oldRotorState);
TrebleStick 20:a435105305fe 139 oldRotorState = rotorState;
TrebleStick 20:a435105305fe 140 }
TrebleStick 20:a435105305fe 141
TrebleStick 20:a435105305fe 142 //TODO Implement timer for greater accuracy
TrebleStick 20:a435105305fe 143 void motorCtrlTick(){
TrebleStick 20:a435105305fe 144 motorCtrlT.signal_set(0x1);
TrebleStick 20:a435105305fe 145 }
TrebleStick 20:a435105305fe 146
andrebharath 23:58de87ec3997 147 volatile float curSpeed = 0;
andrebharath 9:ecef1e8cbe3d 148
andrebharath 9:ecef1e8cbe3d 149
TrebleStick 20:a435105305fe 150 void motorCtrlFn(){
andrebharath 23:58de87ec3997 151 static int32_t curPos = 0, oldPos = 0;
TrebleStick 20:a435105305fe 152 Ticker motorCtrlTicker;
TrebleStick 20:a435105305fe 153 motorCtrlTicker.attach_us(&motorCtrlTick,100000);
andrebharath 23:58de87ec3997 154 uint8_t count = 0;
andrebharath 23:58de87ec3997 155 Timer timer;
andrebharath 23:58de87ec3997 156 timer.start();
andrebharath 23:58de87ec3997 157 uint32_t curTime = 0, oldTime = 0, timeDiff;
TrebleStick 20:a435105305fe 158
TrebleStick 20:a435105305fe 159 while(1){
TrebleStick 20:a435105305fe 160 motorCtrlT.signal_wait(0x1);
andrebharath 23:58de87ec3997 161 curTime = timer.read();
andrebharath 23:58de87ec3997 162 timeDiff = curTime - oldTime;
andrebharath 23:58de87ec3997 163 oldTime = curTime;
andrebharath 23:58de87ec3997 164 curPos = motorPosition;
andrebharath 23:58de87ec3997 165 curSpeed = (curPos - oldPos) / timeDiff;
andrebharath 23:58de87ec3997 166 oldPos = curPos;
andrebharath 23:58de87ec3997 167 count = (count+1)%10;
andrebharath 23:58de87ec3997 168 if (!count) {
andrebharath 25:bf5fff055aef 169 putMessage(MSG_CUR_SPD, torque);
TrebleStick 20:a435105305fe 170 }
andrebharath 25:bf5fff055aef 171 torque = (int32_t)(KP*(targetSpeed-abs(curSpeed)));
TrebleStick 20:a435105305fe 172 }
TrebleStick 19:526fd700e1b3 173 }
andrebharath 9:ecef1e8cbe3d 174
TrebleStick 20:a435105305fe 175
andrebharath 9:ecef1e8cbe3d 176
TrebleStick 14:66746291017c 177 void setNewCmd(char s[CHAR_ARR_SIZE])
TrebleStick 14:66746291017c 178 {
TrebleStick 14:66746291017c 179 uint64_t newKey_;
andrebharath 25:bf5fff055aef 180 int32_t torque_;
andrebharath 24:0a0e22d4d8d3 181 float targetSpeed_, rotations_pending_;
TrebleStick 14:66746291017c 182 //R
TrebleStick 14:66746291017c 183 if (sscanf(s, "R%f", &rotations_pending_)) {
andrebharath 17:80159ace5ddf 184 rotations_pending_mutex.lock();
andrebharath 17:80159ace5ddf 185 rotations_pending = rotations_pending_;
andrebharath 17:80159ace5ddf 186 rotations_pending_mutex.unlock();
andrebharath 17:80159ace5ddf 187 putMessage(MSG_ROT_PEN, rotations_pending);
TrebleStick 14:66746291017c 188 //V
TrebleStick 20:a435105305fe 189 }
andrebharath 24:0a0e22d4d8d3 190 else if (sscanf(s, "V%f", &targetSpeed_)) {
andrebharath 24:0a0e22d4d8d3 191 targetSpeed_mutex.lock();
andrebharath 24:0a0e22d4d8d3 192 targetSpeed = targetSpeed_;
andrebharath 24:0a0e22d4d8d3 193 targetSpeed_mutex.unlock();
andrebharath 24:0a0e22d4d8d3 194 putMessage(MSG_MAX_SPD, targetSpeed);
TrebleStick 14:66746291017c 195 //K
TrebleStick 20:a435105305fe 196 }
TrebleStick 20:a435105305fe 197 else if (sscanf(s, "K%llx", &newKey_)) {
andrebharath 17:80159ace5ddf 198 newKey_mutex.lock();
andrebharath 17:80159ace5ddf 199 newKey = newKey_;
andrebharath 17:80159ace5ddf 200 newKey_mutex.unlock();
andrebharath 17:80159ace5ddf 201 putMessage(MSG_NEW_KEY, newKey);
TrebleStick 20:a435105305fe 202 }
TrebleStick 20:a435105305fe 203 else if (sscanf(s, "T%u", &torque_)) {
andrebharath 17:80159ace5ddf 204 torque = torque_;
TrebleStick 19:526fd700e1b3 205 photointerrupter_isr(); //Give it a kick
andrebharath 17:80159ace5ddf 206 putMessage(MSG_TORQUE, torque);
TrebleStick 14:66746291017c 207 //ERROR
TrebleStick 20:a435105305fe 208 }
TrebleStick 20:a435105305fe 209 else{
TrebleStick 20:a435105305fe 210 putMessage(MSG_INP_ERR, 0x404);
TrebleStick 20:a435105305fe 211 }
TrebleStick 14:66746291017c 212 }
TrebleStick 12:1b2e2540e4e1 213
TrebleStick 20:a435105305fe 214 Thread decodeT (osPriorityNormal, 512);
andrebharath 9:ecef1e8cbe3d 215
andrebharath 9:ecef1e8cbe3d 216 void decodeFn() {
andrebharath 9:ecef1e8cbe3d 217 pc.attach(&serialISR);
andrebharath 9:ecef1e8cbe3d 218 char charSeq[CHAR_ARR_SIZE] = "";
andrebharath 9:ecef1e8cbe3d 219 uint8_t bufPos = 0;
andrebharath 9:ecef1e8cbe3d 220 while(1) {
TrebleStick 12:1b2e2540e4e1 221
andrebharath 9:ecef1e8cbe3d 222 if(bufPos >= CHAR_ARR_SIZE) {
andrebharath 9:ecef1e8cbe3d 223 putMessage(MSG_OVERFLOW, bufPos);
TrebleStick 12:1b2e2540e4e1 224 bufPos = 0;
andrebharath 9:ecef1e8cbe3d 225 }
TrebleStick 13:ecccfc611025 226 else{
TrebleStick 12:1b2e2540e4e1 227
TrebleStick 13:ecccfc611025 228 osEvent newEvent = inCharQ.get();
TrebleStick 13:ecccfc611025 229 uint8_t newChar = (uint8_t)newEvent.value.p;
TrebleStick 12:1b2e2540e4e1 230
TrebleStick 13:ecccfc611025 231 if(newChar == '\r' || newChar == '\n') {
TrebleStick 13:ecccfc611025 232 charSeq[bufPos] = '\0';
TrebleStick 13:ecccfc611025 233 bufPos = 0;
TrebleStick 13:ecccfc611025 234 setNewCmd(charSeq);
TrebleStick 13:ecccfc611025 235 }
TrebleStick 13:ecccfc611025 236 else {
TrebleStick 13:ecccfc611025 237 charSeq[bufPos] = newChar;
TrebleStick 13:ecccfc611025 238 bufPos++;
TrebleStick 13:ecccfc611025 239 }
TrebleStick 12:1b2e2540e4e1 240 }
andrebharath 9:ecef1e8cbe3d 241 }
andrebharath 9:ecef1e8cbe3d 242 }
andrebharath 9:ecef1e8cbe3d 243
andrebharath 3:2e32d7974962 244 volatile uint16_t hashcount = 0;
TrebleStick 0:88c3d6c8a4eb 245
andrebharath 9:ecef1e8cbe3d 246 void do_hashcount() {
TrebleStick 15:bd303ab8a21f 247 //putMessage(MSG_HASHCOUNT, hashcount);
andrebharath 3:2e32d7974962 248 hashcount = 0;
andrebharath 3:2e32d7974962 249 }
andrebharath 9:ecef1e8cbe3d 250
andrebharath 9:ecef1e8cbe3d 251
TrebleStick 0:88c3d6c8a4eb 252 //Set a given drive state
andrebharath 17:80159ace5ddf 253 void motorOut(int8_t driveState, uint32_t t){
TrebleStick 12:1b2e2540e4e1 254
TrebleStick 0:88c3d6c8a4eb 255 //Lookup the output byte from the drive state.
TrebleStick 0:88c3d6c8a4eb 256 int8_t driveOut = driveTable[driveState & 0x07];
TrebleStick 12:1b2e2540e4e1 257
TrebleStick 0:88c3d6c8a4eb 258 //Turn off first
andrebharath 17:80159ace5ddf 259 if (~driveOut & 0x01) L1L.pulsewidth_us(0);
TrebleStick 0:88c3d6c8a4eb 260 if (~driveOut & 0x02) L1H = 1;
andrebharath 17:80159ace5ddf 261 if (~driveOut & 0x04) L2L.pulsewidth_us(0);
TrebleStick 0:88c3d6c8a4eb 262 if (~driveOut & 0x08) L2H = 1;
andrebharath 17:80159ace5ddf 263 if (~driveOut & 0x10) L3L.pulsewidth_us(0);
TrebleStick 0:88c3d6c8a4eb 264 if (~driveOut & 0x20) L3H = 1;
TrebleStick 12:1b2e2540e4e1 265
TrebleStick 0:88c3d6c8a4eb 266 //Then turn on
andrebharath 17:80159ace5ddf 267 if (driveOut & 0x01) L1L.pulsewidth_us(t);
TrebleStick 0:88c3d6c8a4eb 268 if (driveOut & 0x02) L1H = 0;
andrebharath 17:80159ace5ddf 269 if (driveOut & 0x04) L2L.pulsewidth_us(t);
TrebleStick 0:88c3d6c8a4eb 270 if (driveOut & 0x08) L2H = 0;
andrebharath 17:80159ace5ddf 271 if (driveOut & 0x10) L3L.pulsewidth_us(t);
TrebleStick 0:88c3d6c8a4eb 272 if (driveOut & 0x20) L3H = 0;
andrebharath 17:80159ace5ddf 273 }
TrebleStick 12:1b2e2540e4e1 274
andrebharath 9:ecef1e8cbe3d 275 //Convert photointerrupter inputs to a rotor state
andrebharath 9:ecef1e8cbe3d 276 inline int8_t readRotorState(){
TrebleStick 0:88c3d6c8a4eb 277 return stateMap[I1 + 2*I2 + 4*I3];
andrebharath 17:80159ace5ddf 278 }
andrebharath 9:ecef1e8cbe3d 279
TrebleStick 12:1b2e2540e4e1 280 //Basic synchronisation routine
andrebharath 9:ecef1e8cbe3d 281 int8_t motorHome() {
TrebleStick 0:88c3d6c8a4eb 282 //Put the motor in drive state 0 and wait for it to stabilise
andrebharath 18:05e5d280a082 283 motorOut(0, MAX_TORQUE);
andrebharath 3:2e32d7974962 284 wait(2.0);
TrebleStick 12:1b2e2540e4e1 285
TrebleStick 0:88c3d6c8a4eb 286 //Get the rotor state
TrebleStick 0:88c3d6c8a4eb 287 return readRotorState();
TrebleStick 0:88c3d6c8a4eb 288 }
andrebharath 3:2e32d7974962 289
andrebharath 9:ecef1e8cbe3d 290
TrebleStick 12:1b2e2540e4e1 291
TrebleStick 0:88c3d6c8a4eb 292 //Main
andrebharath 9:ecef1e8cbe3d 293 int main() {
TrebleStick 12:1b2e2540e4e1 294
andrebharath 9:ecef1e8cbe3d 295 putMessage(MSG_RESET, 0);
andrebharath 9:ecef1e8cbe3d 296
TrebleStick 20:a435105305fe 297 commOutT.start(&commOutFn);
TrebleStick 12:1b2e2540e4e1 298
TrebleStick 20:a435105305fe 299 motorCtrlT.start(&motorCtrlFn);
TrebleStick 12:1b2e2540e4e1 300
TrebleStick 12:1b2e2540e4e1 301 decodeT.start(&decodeFn);
TrebleStick 12:1b2e2540e4e1 302
andrebharath 9:ecef1e8cbe3d 303 //Run the motor synchronisation
andrebharath 9:ecef1e8cbe3d 304 orState = motorHome();
TrebleStick 20:a435105305fe 305
andrebharath 9:ecef1e8cbe3d 306 //orState is subtracted from future rotor state inputs to align rotor and motor states
TrebleStick 12:1b2e2540e4e1 307
andrebharath 3:2e32d7974962 308 I1.rise(&photointerrupter_isr);
andrebharath 3:2e32d7974962 309 I2.rise(&photointerrupter_isr);
andrebharath 3:2e32d7974962 310 I3.rise(&photointerrupter_isr);
TrebleStick 12:1b2e2540e4e1 311
andrebharath 3:2e32d7974962 312 I1.fall(&photointerrupter_isr);
andrebharath 3:2e32d7974962 313 I2.fall(&photointerrupter_isr);
andrebharath 3:2e32d7974962 314 I3.fall(&photointerrupter_isr);
TrebleStick 12:1b2e2540e4e1 315
andrebharath 18:05e5d280a082 316
andrebharath 18:05e5d280a082 317 L1L.period_us(MAX_PWM_PERIOD);
andrebharath 18:05e5d280a082 318 L2L.period_us(MAX_PWM_PERIOD);
andrebharath 18:05e5d280a082 319 L3L.period_us(MAX_PWM_PERIOD);
andrebharath 18:05e5d280a082 320
andrebharath 9:ecef1e8cbe3d 321 //Calling the ISR once starts the motor movement
andrebharath 9:ecef1e8cbe3d 322 photointerrupter_isr();
TrebleStick 19:526fd700e1b3 323
TrebleStick 19:526fd700e1b3 324
TrebleStick 12:1b2e2540e4e1 325
andrebharath 9:ecef1e8cbe3d 326 SHA256 sha256;
TrebleStick 12:1b2e2540e4e1 327
andrebharath 3:2e32d7974962 328 uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,
andrebharath 3:2e32d7974962 329 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73,
andrebharath 3:2e32d7974962 330 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E,
andrebharath 3:2e32d7974962 331 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20,
andrebharath 3:2e32d7974962 332 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20,
andrebharath 3:2e32d7974962 333 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20,
andrebharath 3:2e32d7974962 334 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
andrebharath 3:2e32d7974962 335 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
andrebharath 3:2e32d7974962 336 uint64_t* key = (uint64_t*)((int)sequence + 48);
andrebharath 3:2e32d7974962 337 uint64_t* nonce = (uint64_t*)((int)sequence + 56);
andrebharath 3:2e32d7974962 338 uint8_t hash[32];
TrebleStick 12:1b2e2540e4e1 339
andrebharath 9:ecef1e8cbe3d 340 Ticker hashcounter;
andrebharath 9:ecef1e8cbe3d 341 hashcounter.attach(&do_hashcount, 1.0);
TrebleStick 12:1b2e2540e4e1 342
TrebleStick 0:88c3d6c8a4eb 343 //Poll the rotor state and set the motor outputs accordingly to spin the motor
TrebleStick 0:88c3d6c8a4eb 344 while (1) {
TrebleStick 12:1b2e2540e4e1 345
andrebharath 9:ecef1e8cbe3d 346 *key = newKey;
TrebleStick 12:1b2e2540e4e1 347
andrebharath 9:ecef1e8cbe3d 348 sha256.computeHash(hash, sequence, 64);
TrebleStick 12:1b2e2540e4e1 349
andrebharath 3:2e32d7974962 350 if (hash[0] == 0 && hash[1] == 0) {
TrebleStick 15:bd303ab8a21f 351 //putMessage(MSG_NONCE_OK, *nonce);
andrebharath 3:2e32d7974962 352 }
andrebharath 3:2e32d7974962 353
andrebharath 3:2e32d7974962 354 (*nonce)++;
andrebharath 3:2e32d7974962 355 hashcount++;
TrebleStick 0:88c3d6c8a4eb 356 }
TrebleStick 0:88c3d6c8a4eb 357 }