First Commit

Dependencies:   mbed Crypto_light mbed-rtos

Spin it 2 win it

Committer:
andrebharath
Date:
Tue Mar 20 17:47:53 2018 +0000
Revision:
22:7c94575d7792
Parent:
21:0d1025dc6433
Child:
23:58de87ec3997
Start of motor velocity PI control

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