This is probably never gonna get done

Dependencies:   Crypto

Committer:
Olaffo
Date:
Wed Mar 20 16:50:01 2019 +0000
Revision:
22:de980b82d0ce
Parent:
21:34f440ae0227
accepting maxSpeed fixed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
estott 0:de4320f74764 1 #include "mbed.h"
peterith 13:c51d828531d5 2 #include "Crypto.h"
estott 0:de4320f74764 3
estott 0:de4320f74764 4 //Photointerrupter input pins
estott 10:a4b5723b6c9d 5 #define I1pin D3
estott 10:a4b5723b6c9d 6 #define I2pin D6
estott 10:a4b5723b6c9d 7 #define I3pin D5
estott 2:4e88faab6988 8
estott 2:4e88faab6988 9 //Incremental encoder input pins
peterith 13:c51d828531d5 10 #define CHApin D12
peterith 13:c51d828531d5 11 #define CHBpin D11
estott 0:de4320f74764 12
estott 0:de4320f74764 13 //Motor Drive output pins //Mask in output byte
estott 10:a4b5723b6c9d 14 #define L1Lpin D1 //0x01
estott 10:a4b5723b6c9d 15 #define L1Hpin A3 //0x02
estott 10:a4b5723b6c9d 16 #define L2Lpin D0 //0x04
peterith 13:c51d828531d5 17 #define L2Hpin A6 //0x08
peterith 13:c51d828531d5 18 #define L3Lpin D10 //0x10
peterith 13:c51d828531d5 19 #define L3Hpin D2 //0x20
estott 10:a4b5723b6c9d 20
estott 10:a4b5723b6c9d 21 #define PWMpin D9
estott 5:08f338b5e4d9 22
estott 5:08f338b5e4d9 23 //Motor current sense
peterith 13:c51d828531d5 24 #define MCSPpin A1
peterith 13:c51d828531d5 25 #define MCSNpin A0
estott 0:de4320f74764 26
estott 0:de4320f74764 27 //Mapping from sequential drive states to motor phase outputs
estott 0:de4320f74764 28 /*
estott 0:de4320f74764 29 State L1 L2 L3
estott 0:de4320f74764 30 0 H - L
estott 0:de4320f74764 31 1 - H L
estott 0:de4320f74764 32 2 L H -
estott 0:de4320f74764 33 3 L - H
estott 0:de4320f74764 34 4 - L H
estott 0:de4320f74764 35 5 H L -
estott 0:de4320f74764 36 6 - - -
estott 0:de4320f74764 37 7 - - -
estott 0:de4320f74764 38 */
estott 0:de4320f74764 39 //Drive state to output table
estott 0:de4320f74764 40 const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00};
estott 2:4e88faab6988 41
estott 0:de4320f74764 42 //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid
estott 2:4e88faab6988 43 const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07};
estott 2:4e88faab6988 44 //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 45
estott 2:4e88faab6988 46 //Phase lead to make motor spin
tanyuzhuo 20:5bd9f9e406d1 47 const int8_t lead = -2; //2 for forwards, -2 for backwards
estott 0:de4320f74764 48
estott 0:de4320f74764 49 //Status LED
estott 0:de4320f74764 50 DigitalOut led1(LED1);
estott 0:de4320f74764 51
estott 0:de4320f74764 52 //Photointerrupter inputs
tanyuzhuo 12:899cd6bf9844 53 InterruptIn I1(I1pin);
tanyuzhuo 12:899cd6bf9844 54 InterruptIn I2(I2pin);
tanyuzhuo 12:899cd6bf9844 55 InterruptIn I3(I3pin);
tanyuzhuo 12:899cd6bf9844 56
estott 0:de4320f74764 57 //Motor Drive outputs
tanyuzhuo 20:5bd9f9e406d1 58 DigitalOut L1L(L1Lpin);
estott 0:de4320f74764 59 DigitalOut L1H(L1Hpin);
tanyuzhuo 20:5bd9f9e406d1 60 DigitalOut L2L(L2Lpin);
estott 0:de4320f74764 61 DigitalOut L2H(L2Hpin);
tanyuzhuo 20:5bd9f9e406d1 62 DigitalOut L3L(L3Lpin);
estott 0:de4320f74764 63 DigitalOut L3H(L3Hpin);
tanyuzhuo 20:5bd9f9e406d1 64 PwmOut d9 (PWMpin);
estott 0:de4320f74764 65
peterith 13:c51d828531d5 66 int8_t orState = 0;
tanyuzhuo 20:5bd9f9e406d1 67 //int8_t intState = 0;
tanyuzhuo 20:5bd9f9e406d1 68 //int8_t intStateOld = 0;
tanyuzhuo 17:ff5300ba5442 69 int32_t revoCounter = 0; //Counts the number of revolutions
tanyuzhuo 18:e48c0910c71e 70 int32_t motorVelocity;
tanyuzhuo 18:e48c0910c71e 71 //Phase lead to make motor spin
tanyuzhuo 20:5bd9f9e406d1 72 //int8_t lead = -2; //2 for forwards, -2 for backwards
tanyuzhuo 12:899cd6bf9844 73
peterith 14:0481b606d10e 74 typedef struct {
tanyuzhuo 20:5bd9f9e406d1 75 int message;
tanyuzhuo 20:5bd9f9e406d1 76 uint64_t data;
tanyuzhuo 20:5bd9f9e406d1 77 float fdata;
peterith 14:0481b606d10e 78 } mail_t;
tanyuzhuo 20:5bd9f9e406d1 79
peterith 14:0481b606d10e 80 Mail<mail_t, 16> mail_box;
tanyuzhuo 20:5bd9f9e406d1 81 Thread commandProcessorthread(osPriorityNormal,1024);
tanyuzhuo 20:5bd9f9e406d1 82 Thread bitcointhread(osPriorityNormal,1024);
tanyuzhuo 20:5bd9f9e406d1 83 Thread motorCtrlT(osPriorityNormal,1024);
tanyuzhuo 20:5bd9f9e406d1 84 Thread commsOut(osPriorityNormal,1024);
tanyuzhuo 20:5bd9f9e406d1 85
peterith 14:0481b606d10e 86 RawSerial pc(SERIAL_TX, SERIAL_RX);
peterith 14:0481b606d10e 87 Queue<void, 8> inCharQ;
peterith 14:0481b606d10e 88 Mutex newKey_mutex;
peterith 14:0481b606d10e 89 uint64_t newKey = 0;
peterith 14:0481b606d10e 90
tanyuzhuo 18:e48c0910c71e 91 volatile float newRev;
tanyuzhuo 18:e48c0910c71e 92 volatile float maxSpeed = 300;
tanyuzhuo 18:e48c0910c71e 93 uint32_t pulseWidth;
tanyuzhuo 18:e48c0910c71e 94 float motorPosition_command;
tanyuzhuo 18:e48c0910c71e 95 float motorPosition;
tanyuzhuo 18:e48c0910c71e 96
tanyuzhuo 20:5bd9f9e406d1 97 void serialISR() {
tanyuzhuo 20:5bd9f9e406d1 98 uint8_t newChar = pc.getc();
tanyuzhuo 20:5bd9f9e406d1 99 inCharQ.put((void*) newChar);
tanyuzhuo 20:5bd9f9e406d1 100 }
tanyuzhuo 20:5bd9f9e406d1 101 /***************************************************************/
tanyuzhuo 20:5bd9f9e406d1 102 //The following block should be moved to a library, but I don't the time to figure this out atm.
tanyuzhuo 20:5bd9f9e406d1 103
tanyuzhuo 20:5bd9f9e406d1 104 //CommsOut.h
tanyuzhuo 20:5bd9f9e406d1 105
tanyuzhuo 20:5bd9f9e406d1 106 enum message_keys {
tanyuzhuo 20:5bd9f9e406d1 107 KEY_DECLINED = 0,
tanyuzhuo 20:5bd9f9e406d1 108 ROTATION_CMD = 1,
tanyuzhuo 20:5bd9f9e406d1 109 MAX_SPEED_CMD = 2,
tanyuzhuo 20:5bd9f9e406d1 110 KEY_ECHO = 3,
tanyuzhuo 20:5bd9f9e406d1 111 R_ECHO1 = 4,
tanyuzhuo 20:5bd9f9e406d1 112 R_ECHO2 = 5,
tanyuzhuo 20:5bd9f9e406d1 113 MAX_SPEED_ECHO1= 6,
tanyuzhuo 20:5bd9f9e406d1 114 MAX_SPEED_ECHO2= 7,
tanyuzhuo 20:5bd9f9e406d1 115
tanyuzhuo 20:5bd9f9e406d1 116
tanyuzhuo 20:5bd9f9e406d1 117 INVALID_CMD = 10,
tanyuzhuo 20:5bd9f9e406d1 118 MOTOR_POS = 11,
tanyuzhuo 20:5bd9f9e406d1 119 MOTOR_SPEED = 12,
tanyuzhuo 20:5bd9f9e406d1 120
tanyuzhuo 20:5bd9f9e406d1 121
tanyuzhuo 20:5bd9f9e406d1 122 NONCE_DETECT = 14,
tanyuzhuo 20:5bd9f9e406d1 123 ROTOR_ORG = 15,
tanyuzhuo 20:5bd9f9e406d1 124
tanyuzhuo 20:5bd9f9e406d1 125 WELCOME = 20
tanyuzhuo 20:5bd9f9e406d1 126 };
tanyuzhuo 20:5bd9f9e406d1 127
tanyuzhuo 20:5bd9f9e406d1 128 void putMessage(int message, uint64_t data = 0, float fdata=0){
peterith 14:0481b606d10e 129 mail_t *mail = mail_box.alloc();
tanyuzhuo 20:5bd9f9e406d1 130 mail->message = message;
tanyuzhuo 20:5bd9f9e406d1 131 mail->data = data;
tanyuzhuo 20:5bd9f9e406d1 132 mail->fdata = fdata;
peterith 14:0481b606d10e 133 mail_box.put(mail);
peterith 14:0481b606d10e 134 }
peterith 14:0481b606d10e 135
tanyuzhuo 20:5bd9f9e406d1 136 void commsOutFunc() {
tanyuzhuo 20:5bd9f9e406d1 137 while (true) {
tanyuzhuo 20:5bd9f9e406d1 138 osEvent evt = mail_box.get();
tanyuzhuo 20:5bd9f9e406d1 139 if (evt.status == osEventMail) {
tanyuzhuo 20:5bd9f9e406d1 140 mail_t *mail = (mail_t*)evt.value.p;
tanyuzhuo 20:5bd9f9e406d1 141 switch (mail->message) {
tanyuzhuo 20:5bd9f9e406d1 142 case KEY_DECLINED :
tanyuzhuo 20:5bd9f9e406d1 143 pc.printf("Not a valid key!\n\r");
tanyuzhuo 20:5bd9f9e406d1 144 break;
tanyuzhuo 20:5bd9f9e406d1 145 case ROTATION_CMD :
tanyuzhuo 20:5bd9f9e406d1 146 pc.printf("Rotation command\n\r");
tanyuzhuo 20:5bd9f9e406d1 147 break;
tanyuzhuo 20:5bd9f9e406d1 148 case MAX_SPEED_CMD :
Olaffo 22:de980b82d0ce 149 pc.printf("Max speed set to: %2.1f\n\r", mail->fdata);
tanyuzhuo 20:5bd9f9e406d1 150 break;
tanyuzhuo 20:5bd9f9e406d1 151 case R_ECHO1 :
tanyuzhuo 20:5bd9f9e406d1 152 pc.printf("Rotor command:\n\r");
tanyuzhuo 20:5bd9f9e406d1 153 pc.printf("Full rotations: %d\n\r", mail->data);
tanyuzhuo 20:5bd9f9e406d1 154 break;
tanyuzhuo 20:5bd9f9e406d1 155 case R_ECHO2 :
tanyuzhuo 20:5bd9f9e406d1 156 pc.printf("Partial rotation: %d\n\r", mail->data);
tanyuzhuo 20:5bd9f9e406d1 157 break;
tanyuzhuo 20:5bd9f9e406d1 158 case KEY_ECHO :
tanyuzhuo 20:5bd9f9e406d1 159 pc.printf("Received key: %016llx\n\r", mail->data);
tanyuzhuo 20:5bd9f9e406d1 160 break;
tanyuzhuo 20:5bd9f9e406d1 161 case INVALID_CMD :
tanyuzhuo 20:5bd9f9e406d1 162 pc.printf("Invalid command!\r\n");
tanyuzhuo 20:5bd9f9e406d1 163 break;
tanyuzhuo 20:5bd9f9e406d1 164 case MOTOR_POS :
tanyuzhuo 20:5bd9f9e406d1 165 pc.printf("Motor position: %f\r\n", mail->fdata);
tanyuzhuo 20:5bd9f9e406d1 166 //pc.printf("{TIMEPLOT|%.2f|Motor Position", mail->fdata);
tanyuzhuo 20:5bd9f9e406d1 167 break;
tanyuzhuo 20:5bd9f9e406d1 168 case MOTOR_SPEED :
tanyuzhuo 20:5bd9f9e406d1 169 pc.printf("Motor speed: %f\r\n", mail->fdata);
tanyuzhuo 20:5bd9f9e406d1 170 break;
tanyuzhuo 20:5bd9f9e406d1 171 case NONCE_DETECT :
tanyuzhuo 20:5bd9f9e406d1 172 pc.printf("Successful nonce: %016x\n\r", mail->data);
tanyuzhuo 20:5bd9f9e406d1 173 break;
tanyuzhuo 20:5bd9f9e406d1 174 case WELCOME :
tanyuzhuo 20:5bd9f9e406d1 175 pc.printf("Hello Pete\n\r");
tanyuzhuo 20:5bd9f9e406d1 176 break;
tanyuzhuo 20:5bd9f9e406d1 177 case ROTOR_ORG :
tanyuzhuo 20:5bd9f9e406d1 178 pc.printf("Rotor origin: %x\n\r", orState);
tanyuzhuo 20:5bd9f9e406d1 179
tanyuzhuo 20:5bd9f9e406d1 180 }
tanyuzhuo 20:5bd9f9e406d1 181 mail_box.free(mail);
tanyuzhuo 20:5bd9f9e406d1 182 }
tanyuzhuo 20:5bd9f9e406d1 183 }
tanyuzhuo 20:5bd9f9e406d1 184 }
tanyuzhuo 20:5bd9f9e406d1 185 //End of that block
tanyuzhuo 20:5bd9f9e406d1 186 /***************************************************************/
tanyuzhuo 20:5bd9f9e406d1 187
tanyuzhuo 20:5bd9f9e406d1 188 /***************************************************************/
tanyuzhuo 20:5bd9f9e406d1 189 //The following block should also be moved to a library, but I still don't the time to figure this out atm.
tanyuzhuo 20:5bd9f9e406d1 190
tanyuzhuo 20:5bd9f9e406d1 191 //CommsIn.h
tanyuzhuo 20:5bd9f9e406d1 192
tanyuzhuo 20:5bd9f9e406d1 193 void commandDecoder(char* command) {
tanyuzhuo 20:5bd9f9e406d1 194 int8_t sign =1;
tanyuzhuo 20:5bd9f9e406d1 195 uint8_t index = 1;
tanyuzhuo 20:5bd9f9e406d1 196 int intValue = 0;
tanyuzhuo 20:5bd9f9e406d1 197 float decimalValue = 0;
tanyuzhuo 20:5bd9f9e406d1 198 switch (command[0]) {
tanyuzhuo 20:5bd9f9e406d1 199 case 'R' :
tanyuzhuo 20:5bd9f9e406d1 200 // Check sign
tanyuzhuo 20:5bd9f9e406d1 201 if (command[1] == '-'){
tanyuzhuo 20:5bd9f9e406d1 202 sign = -1;
tanyuzhuo 20:5bd9f9e406d1 203 index++;
tanyuzhuo 20:5bd9f9e406d1 204 }
tanyuzhuo 20:5bd9f9e406d1 205 // Take first digit
tanyuzhuo 20:5bd9f9e406d1 206 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 207 intValue = command[index] - '0';
tanyuzhuo 20:5bd9f9e406d1 208 index++;
tanyuzhuo 20:5bd9f9e406d1 209 }
tanyuzhuo 20:5bd9f9e406d1 210 else {
tanyuzhuo 20:5bd9f9e406d1 211 putMessage(INVALID_CMD);
tanyuzhuo 20:5bd9f9e406d1 212 break;
tanyuzhuo 20:5bd9f9e406d1 213 }
tanyuzhuo 20:5bd9f9e406d1 214
tanyuzhuo 20:5bd9f9e406d1 215 //Try taking 2 more digits
tanyuzhuo 20:5bd9f9e406d1 216 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 217 intValue = intValue*10 + command[index] - '0';
tanyuzhuo 20:5bd9f9e406d1 218 index++;
tanyuzhuo 20:5bd9f9e406d1 219 }
tanyuzhuo 20:5bd9f9e406d1 220 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 221 intValue = intValue*10 + command[index] - '0';
tanyuzhuo 20:5bd9f9e406d1 222 index++;
tanyuzhuo 20:5bd9f9e406d1 223 }
tanyuzhuo 20:5bd9f9e406d1 224 //Check for '.'
tanyuzhuo 20:5bd9f9e406d1 225 if (command[index] == '.') {
tanyuzhuo 20:5bd9f9e406d1 226 index++;
tanyuzhuo 20:5bd9f9e406d1 227 //Check for decimals
tanyuzhuo 20:5bd9f9e406d1 228 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 229 decimalValue = (command[index] - '0')/10;
tanyuzhuo 20:5bd9f9e406d1 230 index++;
tanyuzhuo 20:5bd9f9e406d1 231 }
tanyuzhuo 20:5bd9f9e406d1 232 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 233 decimalValue = (decimalValue/10 + command[index] - '0')/10;
tanyuzhuo 20:5bd9f9e406d1 234 }
tanyuzhuo 20:5bd9f9e406d1 235 }
tanyuzhuo 20:5bd9f9e406d1 236 putMessage(R_ECHO1, intValue);
tanyuzhuo 20:5bd9f9e406d1 237 putMessage(R_ECHO2, (int) (100*decimalValue));
tanyuzhuo 20:5bd9f9e406d1 238 decimalValue = (decimalValue + intValue) * sign;
tanyuzhuo 20:5bd9f9e406d1 239 //HERE SEND IT TO ANY GLOBAL VARIABLE YOU WISH
tanyuzhuo 20:5bd9f9e406d1 240 break;
tanyuzhuo 20:5bd9f9e406d1 241
tanyuzhuo 20:5bd9f9e406d1 242 case 'V' :
tanyuzhuo 20:5bd9f9e406d1 243 // Take first digit
tanyuzhuo 20:5bd9f9e406d1 244 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 245 intValue = command[index] - '0';
tanyuzhuo 20:5bd9f9e406d1 246 index++;
tanyuzhuo 20:5bd9f9e406d1 247 }
tanyuzhuo 20:5bd9f9e406d1 248 else {
tanyuzhuo 20:5bd9f9e406d1 249 putMessage(INVALID_CMD);
tanyuzhuo 20:5bd9f9e406d1 250 break;
tanyuzhuo 20:5bd9f9e406d1 251 }
tanyuzhuo 20:5bd9f9e406d1 252
tanyuzhuo 20:5bd9f9e406d1 253 //Try taking 2 more digits
tanyuzhuo 20:5bd9f9e406d1 254 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 255 intValue = intValue*10 + command[index] - '0';
tanyuzhuo 20:5bd9f9e406d1 256 index++;
tanyuzhuo 20:5bd9f9e406d1 257 }
tanyuzhuo 20:5bd9f9e406d1 258 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 259 intValue = intValue*10 + command[index] - '0';
tanyuzhuo 20:5bd9f9e406d1 260 index++;
tanyuzhuo 20:5bd9f9e406d1 261 }
tanyuzhuo 20:5bd9f9e406d1 262 //Check for '.'
tanyuzhuo 20:5bd9f9e406d1 263 if (command[index] == '.') {
tanyuzhuo 20:5bd9f9e406d1 264 index++;
tanyuzhuo 20:5bd9f9e406d1 265 //Check for one decimal
tanyuzhuo 20:5bd9f9e406d1 266 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 267 decimalValue = (command[index] - '0')/10;
tanyuzhuo 20:5bd9f9e406d1 268 }
tanyuzhuo 20:5bd9f9e406d1 269 }
tanyuzhuo 20:5bd9f9e406d1 270 decimalValue = (decimalValue + intValue);
tanyuzhuo 20:5bd9f9e406d1 271 //HERE SEND IT TO ANY GLOBAL VARIABLE YOU WISH
Olaffo 22:de980b82d0ce 272 putMessage(MAX_SPEED_CMD,0, decimalValue);
tanyuzhuo 21:34f440ae0227 273 maxSpeed = decimalValue;
tanyuzhuo 20:5bd9f9e406d1 274 break;
tanyuzhuo 20:5bd9f9e406d1 275
tanyuzhuo 20:5bd9f9e406d1 276 case 'K' :
tanyuzhuo 20:5bd9f9e406d1 277 ///pc.printf("Received key: %016llx\n\r", mail->data);
tanyuzhuo 20:5bd9f9e406d1 278 break;
tanyuzhuo 20:5bd9f9e406d1 279 case 'T' :
tanyuzhuo 20:5bd9f9e406d1 280 //pc.printf("Received key: %016llx\n\r", mail->data);
tanyuzhuo 20:5bd9f9e406d1 281 break;
tanyuzhuo 20:5bd9f9e406d1 282 }
peterith 14:0481b606d10e 283 }
peterith 14:0481b606d10e 284
peterith 14:0481b606d10e 285 void commandProcessor() {
peterith 14:0481b606d10e 286 pc.attach(&serialISR);
tanyuzhuo 17:ff5300ba5442 287 char command[19];
tanyuzhuo 17:ff5300ba5442 288 char* number;
peterith 14:0481b606d10e 289 //char k;
peterith 14:0481b606d10e 290 uint64_t receivedKey;
peterith 14:0481b606d10e 291 uint8_t index = 0;
peterith 14:0481b606d10e 292 while(1) {
peterith 14:0481b606d10e 293 osEvent newEvent = inCharQ.get();
peterith 14:0481b606d10e 294 uint8_t newChar = (uint8_t) newEvent.value.p;
peterith 14:0481b606d10e 295 command[index] = newChar;
peterith 14:0481b606d10e 296 index++;
tanyuzhuo 17:ff5300ba5442 297 if (newChar == '\r') {
peterith 14:0481b606d10e 298 command[17] = '\0';
tanyuzhuo 17:ff5300ba5442 299
tanyuzhuo 17:ff5300ba5442 300 if (command[0] == 'R') {
tanyuzhuo 20:5bd9f9e406d1 301 putMessage(ROTATION_CMD);
tanyuzhuo 20:5bd9f9e406d1 302 commandDecoder(command);
tanyuzhuo 17:ff5300ba5442 303 }
tanyuzhuo 17:ff5300ba5442 304 else if (command[0] == 'V') {
Olaffo 22:de980b82d0ce 305 commandDecoder(command);
Olaffo 22:de980b82d0ce 306 //putMessage(MAX_SPEED_CMD);
tanyuzhuo 17:ff5300ba5442 307 }
tanyuzhuo 17:ff5300ba5442 308 else if (command[0] == 'K') {
tanyuzhuo 17:ff5300ba5442 309 if (index == 18){ // when index is 18 means you entered K and 16 digits
tanyuzhuo 17:ff5300ba5442 310 number = command +1; //super bad solution, but I don't know how to work with strings in C
tanyuzhuo 17:ff5300ba5442 311 receivedKey = strtoull(number, NULL, 16);
tanyuzhuo 20:5bd9f9e406d1 312 putMessage(KEY_ECHO,receivedKey);
tanyuzhuo 17:ff5300ba5442 313 //receivedKey = 2147483648;
tanyuzhuo 17:ff5300ba5442 314 //sscanf(command, "%d", &receivedKey);
tanyuzhuo 20:5bd9f9e406d1 315 //pc.printf("Received key: %016llx\n\r", receivedKey);
tanyuzhuo 17:ff5300ba5442 316 newKey_mutex.lock();
tanyuzhuo 17:ff5300ba5442 317 newKey = receivedKey;
tanyuzhuo 17:ff5300ba5442 318 newKey_mutex.unlock();
tanyuzhuo 17:ff5300ba5442 319 } else {
tanyuzhuo 20:5bd9f9e406d1 320 putMessage(KEY_DECLINED);
tanyuzhuo 17:ff5300ba5442 321 };
tanyuzhuo 17:ff5300ba5442 322 }
tanyuzhuo 17:ff5300ba5442 323 else if (command[0] == 'T') {
tanyuzhuo 17:ff5300ba5442 324 pc.printf("Melody command\n");
tanyuzhuo 17:ff5300ba5442 325 pc.printf("%s", command);
tanyuzhuo 17:ff5300ba5442 326 }
peterith 14:0481b606d10e 327 memset(command, 0, sizeof(command));
peterith 14:0481b606d10e 328 index = 0;
peterith 14:0481b606d10e 329 } else {
tanyuzhuo 20:5bd9f9e406d1 330 pc.printf("Current command: %s\n\r", command); //Not sure how to go around this one cause it requires passing string
peterith 14:0481b606d10e 331 }
peterith 14:0481b606d10e 332 }
peterith 14:0481b606d10e 333 }
tanyuzhuo 17:ff5300ba5442 334
tanyuzhuo 16:10d53b056b17 335 void bitcoin(){
tanyuzhuo 16:10d53b056b17 336 while(1) {
tanyuzhuo 16:10d53b056b17 337 SHA256 sha;
tanyuzhuo 16:10d53b056b17 338 uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,
tanyuzhuo 16:10d53b056b17 339 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73,
tanyuzhuo 16:10d53b056b17 340 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E,
tanyuzhuo 16:10d53b056b17 341 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20,
tanyuzhuo 16:10d53b056b17 342 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20,
tanyuzhuo 16:10d53b056b17 343 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20,
tanyuzhuo 16:10d53b056b17 344 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
tanyuzhuo 16:10d53b056b17 345 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
tanyuzhuo 16:10d53b056b17 346 uint64_t* key = (uint64_t*) ((int) sequence + 48);
tanyuzhuo 16:10d53b056b17 347 uint64_t* nonce = (uint64_t*) ((int) sequence + 56);
tanyuzhuo 16:10d53b056b17 348 uint8_t hash[32];
tanyuzhuo 16:10d53b056b17 349
tanyuzhuo 16:10d53b056b17 350 Timer t;
tanyuzhuo 16:10d53b056b17 351 t.start();
tanyuzhuo 16:10d53b056b17 352 unsigned currentTime = 0;
tanyuzhuo 20:5bd9f9e406d1 353 unsigned currentCount= 0;
tanyuzhuo 16:10d53b056b17 354
tanyuzhuo 16:10d53b056b17 355 for (unsigned i = 0; i <= UINT_MAX; i++) {
tanyuzhuo 16:10d53b056b17 356 (*nonce)++;
tanyuzhuo 16:10d53b056b17 357 newKey_mutex.lock();
tanyuzhuo 16:10d53b056b17 358 *key = newKey;
tanyuzhuo 16:10d53b056b17 359 newKey_mutex.unlock();
tanyuzhuo 16:10d53b056b17 360 sha.computeHash(hash, sequence, 64);
tanyuzhuo 16:10d53b056b17 361 if (hash[0] == 0 && hash[1] == 0) {
tanyuzhuo 16:10d53b056b17 362 //putMessage(nonce);
tanyuzhuo 16:10d53b056b17 363 pc.printf("Successful nonce: %016x\n\r", *nonce);
tanyuzhuo 16:10d53b056b17 364 }
tanyuzhuo 16:10d53b056b17 365 if ((unsigned) t.read() == currentTime) {
tanyuzhuo 16:10d53b056b17 366 //pc.printf("Hash rate: %d\n\r", i - currentCount);
tanyuzhuo 20:5bd9f9e406d1 367 //pc.printf("Current key: %016llx\n\r", *key);
tanyuzhuo 16:10d53b056b17 368 currentTime++;
tanyuzhuo 16:10d53b056b17 369 currentCount = i;
tanyuzhuo 16:10d53b056b17 370 }
tanyuzhuo 16:10d53b056b17 371 }
tanyuzhuo 16:10d53b056b17 372 t.stop();
tanyuzhuo 16:10d53b056b17 373 }
tanyuzhuo 16:10d53b056b17 374 }
tanyuzhuo 18:e48c0910c71e 375
tanyuzhuo 18:e48c0910c71e 376
tanyuzhuo 18:e48c0910c71e 377
tanyuzhuo 20:5bd9f9e406d1 378
tanyuzhuo 18:e48c0910c71e 379
tanyuzhuo 18:e48c0910c71e 380
estott 0:de4320f74764 381 //Set a given drive state
tanyuzhuo 20:5bd9f9e406d1 382 void motorOut(int8_t driveState){
estott 0:de4320f74764 383
estott 2:4e88faab6988 384 //Lookup the output byte from the drive state.
estott 2:4e88faab6988 385 int8_t driveOut = driveTable[driveState & 0x07];
estott 2:4e88faab6988 386
estott 2:4e88faab6988 387 //Turn off first
tanyuzhuo 20:5bd9f9e406d1 388 if (~driveOut & 0x01) L1L = 0;
estott 2:4e88faab6988 389 if (~driveOut & 0x02) L1H = 1;
tanyuzhuo 20:5bd9f9e406d1 390 if (~driveOut & 0x04) L2L = 0;
estott 2:4e88faab6988 391 if (~driveOut & 0x08) L2H = 1;
tanyuzhuo 20:5bd9f9e406d1 392 if (~driveOut & 0x10) L3L = 0;
estott 2:4e88faab6988 393 if (~driveOut & 0x20) L3H = 1;
estott 2:4e88faab6988 394
estott 2:4e88faab6988 395 //Then turn on
tanyuzhuo 20:5bd9f9e406d1 396 if (driveOut & 0x01) L1L = 1;
estott 2:4e88faab6988 397 if (driveOut & 0x02) L1H = 0;
tanyuzhuo 20:5bd9f9e406d1 398 if (driveOut & 0x04) L2L = 1;
estott 2:4e88faab6988 399 if (driveOut & 0x08) L2H = 0;
tanyuzhuo 20:5bd9f9e406d1 400 if (driveOut & 0x10) L3L = 1;
estott 2:4e88faab6988 401 if (driveOut & 0x20) L3H = 0;
tanyuzhuo 20:5bd9f9e406d1 402
tanyuzhuo 21:34f440ae0227 403 d9.period(0.002f); //Set PWM period in seconds
tanyuzhuo 21:34f440ae0227 404 d9.write(1);
tanyuzhuo 20:5bd9f9e406d1 405
peterith 13:c51d828531d5 406 }
estott 0:de4320f74764 407
peterith 13:c51d828531d5 408 //Convert photointerrupter inputs to a rotor state
estott 0:de4320f74764 409 inline int8_t readRotorState(){
estott 2:4e88faab6988 410 return stateMap[I1 + 2*I2 + 4*I3];
peterith 13:c51d828531d5 411 }
estott 0:de4320f74764 412
estott 2:4e88faab6988 413 int8_t motorHome() {
tanyuzhuo 18:e48c0910c71e 414 //Put the motor in drive state 0 and wait for it to stabilize
tanyuzhuo 20:5bd9f9e406d1 415 motorOut(0);
estott 3:569b35e2a602 416 wait(2.0);
estott 2:4e88faab6988 417 return readRotorState();
estott 0:de4320f74764 418 }
peterith 13:c51d828531d5 419
tanyuzhuo 20:5bd9f9e406d1 420
tanyuzhuo 18:e48c0910c71e 421 // ISR to handle the updating of the motor position
tanyuzhuo 18:e48c0910c71e 422 void motorISR() {
tanyuzhuo 18:e48c0910c71e 423 static int8_t oldRotorState;
tanyuzhuo 20:5bd9f9e406d1 424 //orState is subtracted from future rotor state inputs to align rotor and motor states
tanyuzhuo 20:5bd9f9e406d1 425
tanyuzhuo 18:e48c0910c71e 426 int8_t rotorState = readRotorState();
tanyuzhuo 20:5bd9f9e406d1 427 motorOut((rotorState-orState+lead+6)%6); //+6 to make sure the remainder is positive
tanyuzhuo 18:e48c0910c71e 428 if (rotorState - oldRotorState == 5) motorPosition --;
tanyuzhuo 18:e48c0910c71e 429 else if (rotorState - oldRotorState == -5) motorPosition ++;
tanyuzhuo 18:e48c0910c71e 430 else motorPosition += (rotorState - oldRotorState);
tanyuzhuo 20:5bd9f9e406d1 431 //pc.printf ("motorpPosition %f\n\r", motorPosition);
tanyuzhuo 18:e48c0910c71e 432 oldRotorState = rotorState;
tanyuzhuo 18:e48c0910c71e 433 }
tanyuzhuo 18:e48c0910c71e 434 /*void push() {
peterith 13:c51d828531d5 435 intState = readRotorState();
peterith 13:c51d828531d5 436 if (intState != intStateOld) {
peterith 13:c51d828531d5 437 intStateOld = intState;
peterith 13:c51d828531d5 438 motorOut((intState - orState + lead +6) % 6); //+6 to make sure the remainder is positive
peterith 13:c51d828531d5 439 }
tanyuzhuo 18:e48c0910c71e 440 }*/
tanyuzhuo 20:5bd9f9e406d1 441 void motorCtrlTick(){
tanyuzhuo 20:5bd9f9e406d1 442 motorCtrlT.signal_set(0x1);
tanyuzhuo 20:5bd9f9e406d1 443 }
tanyuzhuo 18:e48c0910c71e 444 void motorCtrlFn(){
tanyuzhuo 18:e48c0910c71e 445 int32_t counter=0;
tanyuzhuo 18:e48c0910c71e 446 static int32_t oldmotorPosition;
tanyuzhuo 21:34f440ae0227 447 uint32_t Ts;
tanyuzhuo 20:5bd9f9e406d1 448
tanyuzhuo 21:34f440ae0227 449 float Speed;
tanyuzhuo 21:34f440ae0227 450 float kps = 25; //proportional constant for speed
tanyuzhuo 18:e48c0910c71e 451 // Timer to count time passed between ticks to calculate velocity
tanyuzhuo 18:e48c0910c71e 452 Timer motorTime;
tanyuzhuo 18:e48c0910c71e 453 motorTime.start();
tanyuzhuo 18:e48c0910c71e 454 float motorPos;
tanyuzhuo 20:5bd9f9e406d1 455 //float ki = ??; // integration constant, to be tested for friction
tanyuzhuo 18:e48c0910c71e 456 Ticker motorCtrlTicker;
tanyuzhuo 18:e48c0910c71e 457 motorCtrlTicker.attach_us(&motorCtrlTick,100000);
tanyuzhuo 18:e48c0910c71e 458 while(1){
tanyuzhuo 18:e48c0910c71e 459 motorCtrlT.signal_wait(0x1);
tanyuzhuo 20:5bd9f9e406d1 460 // errorSum= 0;
tanyuzhuo 20:5bd9f9e406d1 461 // for(uint8_t i=9; i >0 ; i--){
tanyuzhuo 20:5bd9f9e406d1 462 // PrevErrorArray[i] = prevErrorArray[i-1];
tanyuzhuo 20:5bd9f9e406d1 463 // errorSum+= PrevErrorArray[i];
tanyuzhuo 21:34f440ae0227 464
tanyuzhuo 18:e48c0910c71e 465 // convert state change into rotations
tanyuzhuo 21:34f440ae0227 466 Speed = maxSpeed*6;
tanyuzhuo 20:5bd9f9e406d1 467
tanyuzhuo 18:e48c0910c71e 468 motorPos = motorPosition;
tanyuzhuo 20:5bd9f9e406d1 469 //pc.printf ("motor Pos: %f\n\r", motorPos);
tanyuzhuo 18:e48c0910c71e 470 motorVelocity = (motorPos - oldmotorPosition)/motorTime.read();
tanyuzhuo 18:e48c0910c71e 471 oldmotorPosition = motorPos;
tanyuzhuo 20:5bd9f9e406d1 472
tanyuzhuo 20:5bd9f9e406d1 473 //equation for controls
tanyuzhuo 21:34f440ae0227 474 Ts = kps*(Speed -abs(motorVelocity));//*errorSign;
tanyuzhuo 21:34f440ae0227 475
tanyuzhuo 20:5bd9f9e406d1 476 // Mp = ks*error + kd*(error - PrevError) /motorTime.read() + ki*errorSum;
tanyuzhuo 20:5bd9f9e406d1 477
tanyuzhuo 18:e48c0910c71e 478 motorTime.reset();
tanyuzhuo 18:e48c0910c71e 479 // Serial output to monitor speed and position
tanyuzhuo 18:e48c0910c71e 480 counter++;
tanyuzhuo 18:e48c0910c71e 481 if(counter == 10){
tanyuzhuo 18:e48c0910c71e 482 counter = 0;
tanyuzhuo 18:e48c0910c71e 483 //display velocity and motor position
tanyuzhuo 20:5bd9f9e406d1 484 // pc.printf ("motor Pos: %f\n\r", (motorPosition/6.0));
tanyuzhuo 21:34f440ae0227 485 putMessage(MOTOR_POS,0,(float)(motorPosition/6.0));
tanyuzhuo 20:5bd9f9e406d1 486 putMessage(MOTOR_SPEED,0,(float)(motorVelocity/6.0));
tanyuzhuo 21:34f440ae0227 487 //pc.printf ("torque: %f\n\r", Ts);
tanyuzhuo 18:e48c0910c71e 488 }
tanyuzhuo 18:e48c0910c71e 489 }
tanyuzhuo 21:34f440ae0227 490 }
peterith 13:c51d828531d5 491 int main() {
peterith 14:0481b606d10e 492 //Serial pc(SERIAL_TX, SERIAL_RX);
peterith 14:0481b606d10e 493
tanyuzhuo 17:ff5300ba5442 494 //Initialise bincoin mining and communication
tanyuzhuo 20:5bd9f9e406d1 495
tanyuzhuo 17:ff5300ba5442 496
tanyuzhuo 20:5bd9f9e406d1 497
tanyuzhuo 20:5bd9f9e406d1 498
tanyuzhuo 18:e48c0910c71e 499 //PWM.period(0.002f); //Set PWM period in seconds
tanyuzhuo 18:e48c0910c71e 500 //PWM.write(0.5); //Set PWM duty in %
tanyuzhuo 20:5bd9f9e406d1 501
peterith 14:0481b606d10e 502
tanyuzhuo 20:5bd9f9e406d1 503
tanyuzhuo 20:5bd9f9e406d1 504 commandProcessorthread.start(commandProcessor);
tanyuzhuo 20:5bd9f9e406d1 505
tanyuzhuo 20:5bd9f9e406d1 506 commsOut.start(commsOutFunc);
tanyuzhuo 20:5bd9f9e406d1 507
tanyuzhuo 20:5bd9f9e406d1 508 motorCtrlT.start(motorCtrlFn);
tanyuzhuo 20:5bd9f9e406d1 509 bitcointhread.start(bitcoin);
estott 0:de4320f74764 510
tanyuzhuo 20:5bd9f9e406d1 511 putMessage(WELCOME);
tanyuzhuo 20:5bd9f9e406d1 512 int8_t orState = motorHome();
tanyuzhuo 20:5bd9f9e406d1 513 putMessage(ROTOR_ORG);
tanyuzhuo 20:5bd9f9e406d1 514 //pc.printf("Rotor origin: %x\n\r", orState);
tanyuzhuo 20:5bd9f9e406d1 515 //Set PWM duty in %
tanyuzhuo 20:5bd9f9e406d1 516 I1.rise(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 517 I2.rise(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 518 I3.rise(&motorISR);
peterith 13:c51d828531d5 519
tanyuzhuo 20:5bd9f9e406d1 520 I1.fall(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 521 I2.fall(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 522 I3.fall(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 523
tanyuzhuo 16:10d53b056b17 524
estott 0:de4320f74764 525 }