An embedded device

Dependencies:   Crypto

Committer:
tanyuzhuo
Date:
Wed Mar 20 23:46:05 2019 +0000
Revision:
24:7dd4e187dd30
Parent:
23:904721445e7a
Child:
25:ca5ccbf55b07
fuck EEE

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 24:7dd4e187dd30 47 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 23:904721445e7a 92 volatile float maxSpeed = 90;
tanyuzhuo 23:904721445e7a 93 float pulseWidth;
tanyuzhuo 18:e48c0910c71e 94 float motorPosition_command;
tanyuzhuo 18:e48c0910c71e 95 float motorPosition;
tanyuzhuo 23:904721445e7a 96 //float pulseWidth;
tanyuzhuo 18:e48c0910c71e 97
tanyuzhuo 20:5bd9f9e406d1 98 void serialISR() {
tanyuzhuo 20:5bd9f9e406d1 99 uint8_t newChar = pc.getc();
tanyuzhuo 20:5bd9f9e406d1 100 inCharQ.put((void*) newChar);
tanyuzhuo 20:5bd9f9e406d1 101 }
tanyuzhuo 20:5bd9f9e406d1 102 /***************************************************************/
tanyuzhuo 20:5bd9f9e406d1 103 //The following block should be moved to a library, but I don't the time to figure this out atm.
tanyuzhuo 20:5bd9f9e406d1 104
tanyuzhuo 20:5bd9f9e406d1 105 //CommsOut.h
tanyuzhuo 20:5bd9f9e406d1 106
tanyuzhuo 20:5bd9f9e406d1 107 enum message_keys {
tanyuzhuo 20:5bd9f9e406d1 108 KEY_DECLINED = 0,
tanyuzhuo 20:5bd9f9e406d1 109 ROTATION_CMD = 1,
tanyuzhuo 20:5bd9f9e406d1 110 MAX_SPEED_CMD = 2,
tanyuzhuo 20:5bd9f9e406d1 111 KEY_ECHO = 3,
tanyuzhuo 20:5bd9f9e406d1 112 R_ECHO1 = 4,
tanyuzhuo 20:5bd9f9e406d1 113 R_ECHO2 = 5,
tanyuzhuo 20:5bd9f9e406d1 114 MAX_SPEED_ECHO1= 6,
tanyuzhuo 20:5bd9f9e406d1 115 MAX_SPEED_ECHO2= 7,
tanyuzhuo 20:5bd9f9e406d1 116
tanyuzhuo 20:5bd9f9e406d1 117
tanyuzhuo 20:5bd9f9e406d1 118 INVALID_CMD = 10,
tanyuzhuo 20:5bd9f9e406d1 119 MOTOR_POS = 11,
tanyuzhuo 20:5bd9f9e406d1 120 MOTOR_SPEED = 12,
tanyuzhuo 20:5bd9f9e406d1 121
tanyuzhuo 20:5bd9f9e406d1 122
tanyuzhuo 20:5bd9f9e406d1 123 NONCE_DETECT = 14,
tanyuzhuo 20:5bd9f9e406d1 124 ROTOR_ORG = 15,
tanyuzhuo 20:5bd9f9e406d1 125
tanyuzhuo 20:5bd9f9e406d1 126 WELCOME = 20
tanyuzhuo 20:5bd9f9e406d1 127 };
tanyuzhuo 20:5bd9f9e406d1 128
tanyuzhuo 20:5bd9f9e406d1 129 void putMessage(int message, uint64_t data = 0, float fdata=0){
peterith 14:0481b606d10e 130 mail_t *mail = mail_box.alloc();
tanyuzhuo 20:5bd9f9e406d1 131 mail->message = message;
tanyuzhuo 20:5bd9f9e406d1 132 mail->data = data;
tanyuzhuo 20:5bd9f9e406d1 133 mail->fdata = fdata;
peterith 14:0481b606d10e 134 mail_box.put(mail);
peterith 14:0481b606d10e 135 }
peterith 14:0481b606d10e 136
tanyuzhuo 20:5bd9f9e406d1 137 void commsOutFunc() {
tanyuzhuo 20:5bd9f9e406d1 138 while (true) {
tanyuzhuo 20:5bd9f9e406d1 139 osEvent evt = mail_box.get();
tanyuzhuo 20:5bd9f9e406d1 140 if (evt.status == osEventMail) {
tanyuzhuo 20:5bd9f9e406d1 141 mail_t *mail = (mail_t*)evt.value.p;
tanyuzhuo 20:5bd9f9e406d1 142 switch (mail->message) {
tanyuzhuo 20:5bd9f9e406d1 143 case KEY_DECLINED :
tanyuzhuo 20:5bd9f9e406d1 144 pc.printf("Not a valid key!\n\r");
tanyuzhuo 20:5bd9f9e406d1 145 break;
tanyuzhuo 20:5bd9f9e406d1 146 case ROTATION_CMD :
tanyuzhuo 24:7dd4e187dd30 147 pc.printf("Rotation count: %3.2f\n\r", mail->fdata);
tanyuzhuo 20:5bd9f9e406d1 148 break;
tanyuzhuo 20:5bd9f9e406d1 149 case MAX_SPEED_CMD :
tanyuzhuo 20:5bd9f9e406d1 150 pc.printf("Max speed command\n\r");
tanyuzhuo 20:5bd9f9e406d1 151 break;
tanyuzhuo 20:5bd9f9e406d1 152 case MAX_SPEED_ECHO1 :
tanyuzhuo 20:5bd9f9e406d1 153 pc.printf("Max speed command:\n\r");
tanyuzhuo 20:5bd9f9e406d1 154 pc.printf("Max speed int: %d\n\r", mail->data);
tanyuzhuo 20:5bd9f9e406d1 155 break;
tanyuzhuo 20:5bd9f9e406d1 156 case MAX_SPEED_ECHO2 :
tanyuzhuo 20:5bd9f9e406d1 157 pc.printf("Max speed decimal: %d\n\r", mail->data);
tanyuzhuo 20:5bd9f9e406d1 158 break;
tanyuzhuo 20:5bd9f9e406d1 159 case KEY_ECHO :
tanyuzhuo 20:5bd9f9e406d1 160 pc.printf("Received key: %016llx\n\r", mail->data);
tanyuzhuo 20:5bd9f9e406d1 161 break;
tanyuzhuo 20:5bd9f9e406d1 162 case INVALID_CMD :
tanyuzhuo 20:5bd9f9e406d1 163 pc.printf("Invalid command!\r\n");
tanyuzhuo 20:5bd9f9e406d1 164 break;
tanyuzhuo 20:5bd9f9e406d1 165 case MOTOR_POS :
tanyuzhuo 20:5bd9f9e406d1 166 pc.printf("Motor position: %f\r\n", mail->fdata);
tanyuzhuo 20:5bd9f9e406d1 167 //pc.printf("{TIMEPLOT|%.2f|Motor Position", mail->fdata);
tanyuzhuo 20:5bd9f9e406d1 168 break;
tanyuzhuo 20:5bd9f9e406d1 169 case MOTOR_SPEED :
tanyuzhuo 20:5bd9f9e406d1 170 pc.printf("Motor speed: %f\r\n", mail->fdata);
tanyuzhuo 20:5bd9f9e406d1 171 break;
tanyuzhuo 20:5bd9f9e406d1 172 case NONCE_DETECT :
tanyuzhuo 20:5bd9f9e406d1 173 pc.printf("Successful nonce: %016x\n\r", mail->data);
tanyuzhuo 20:5bd9f9e406d1 174 break;
tanyuzhuo 20:5bd9f9e406d1 175 case WELCOME :
tanyuzhuo 20:5bd9f9e406d1 176 pc.printf("Hello Pete\n\r");
tanyuzhuo 20:5bd9f9e406d1 177 break;
tanyuzhuo 20:5bd9f9e406d1 178 case ROTOR_ORG :
tanyuzhuo 20:5bd9f9e406d1 179 pc.printf("Rotor origin: %x\n\r", orState);
tanyuzhuo 20:5bd9f9e406d1 180
tanyuzhuo 20:5bd9f9e406d1 181 }
tanyuzhuo 20:5bd9f9e406d1 182 mail_box.free(mail);
tanyuzhuo 20:5bd9f9e406d1 183 }
tanyuzhuo 20:5bd9f9e406d1 184 }
tanyuzhuo 20:5bd9f9e406d1 185 }
tanyuzhuo 20:5bd9f9e406d1 186 //End of that block
tanyuzhuo 20:5bd9f9e406d1 187 /***************************************************************/
tanyuzhuo 20:5bd9f9e406d1 188
tanyuzhuo 20:5bd9f9e406d1 189 /***************************************************************/
tanyuzhuo 20:5bd9f9e406d1 190 //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 191
tanyuzhuo 20:5bd9f9e406d1 192 //CommsIn.h
tanyuzhuo 20:5bd9f9e406d1 193
tanyuzhuo 20:5bd9f9e406d1 194 void commandDecoder(char* command) {
tanyuzhuo 20:5bd9f9e406d1 195 int8_t sign =1;
tanyuzhuo 20:5bd9f9e406d1 196 uint8_t index = 1;
tanyuzhuo 20:5bd9f9e406d1 197 int intValue = 0;
tanyuzhuo 20:5bd9f9e406d1 198 float decimalValue = 0;
tanyuzhuo 20:5bd9f9e406d1 199 switch (command[0]) {
tanyuzhuo 20:5bd9f9e406d1 200 case 'R' :
tanyuzhuo 20:5bd9f9e406d1 201 // Check sign
tanyuzhuo 20:5bd9f9e406d1 202 if (command[1] == '-'){
tanyuzhuo 20:5bd9f9e406d1 203 sign = -1;
tanyuzhuo 20:5bd9f9e406d1 204 index++;
tanyuzhuo 20:5bd9f9e406d1 205 }
tanyuzhuo 20:5bd9f9e406d1 206 // Take first digit
tanyuzhuo 20:5bd9f9e406d1 207 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 208 intValue = command[index] - '0';
tanyuzhuo 20:5bd9f9e406d1 209 index++;
tanyuzhuo 20:5bd9f9e406d1 210 }
tanyuzhuo 20:5bd9f9e406d1 211 else {
tanyuzhuo 20:5bd9f9e406d1 212 putMessage(INVALID_CMD);
tanyuzhuo 20:5bd9f9e406d1 213 break;
tanyuzhuo 20:5bd9f9e406d1 214 }
tanyuzhuo 20:5bd9f9e406d1 215
tanyuzhuo 20:5bd9f9e406d1 216 //Try taking 2 more digits
tanyuzhuo 20:5bd9f9e406d1 217 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 218 intValue = intValue*10 + command[index] - '0';
tanyuzhuo 20:5bd9f9e406d1 219 index++;
tanyuzhuo 20:5bd9f9e406d1 220 }
tanyuzhuo 20:5bd9f9e406d1 221 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 222 intValue = intValue*10 + command[index] - '0';
tanyuzhuo 20:5bd9f9e406d1 223 index++;
tanyuzhuo 20:5bd9f9e406d1 224 }
tanyuzhuo 20:5bd9f9e406d1 225 //Check for '.'
tanyuzhuo 20:5bd9f9e406d1 226 if (command[index] == '.') {
tanyuzhuo 20:5bd9f9e406d1 227 index++;
tanyuzhuo 20:5bd9f9e406d1 228 //Check for decimals
tanyuzhuo 20:5bd9f9e406d1 229 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 230 decimalValue = (command[index] - '0')/10;
tanyuzhuo 20:5bd9f9e406d1 231 index++;
tanyuzhuo 20:5bd9f9e406d1 232 }
tanyuzhuo 20:5bd9f9e406d1 233 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 234 decimalValue = (decimalValue/10 + command[index] - '0')/10;
tanyuzhuo 20:5bd9f9e406d1 235 }
tanyuzhuo 20:5bd9f9e406d1 236 }
tanyuzhuo 20:5bd9f9e406d1 237 decimalValue = (decimalValue + intValue) * sign;
tanyuzhuo 24:7dd4e187dd30 238 putMessage(ROTATION_CMD,0,decimalValue);
tanyuzhuo 24:7dd4e187dd30 239 //HERE SEND IT TO ANY GLOBAL VARIABLE YOU WISH
tanyuzhuo 24:7dd4e187dd30 240
tanyuzhuo 24:7dd4e187dd30 241 newRev = decimalValue;
tanyuzhuo 20:5bd9f9e406d1 242 break;
tanyuzhuo 20:5bd9f9e406d1 243
tanyuzhuo 20:5bd9f9e406d1 244 case 'V' :
tanyuzhuo 20:5bd9f9e406d1 245 // Take first digit
tanyuzhuo 20:5bd9f9e406d1 246 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 247 intValue = command[index] - '0';
tanyuzhuo 20:5bd9f9e406d1 248 index++;
tanyuzhuo 20:5bd9f9e406d1 249 }
tanyuzhuo 20:5bd9f9e406d1 250 else {
tanyuzhuo 20:5bd9f9e406d1 251 putMessage(INVALID_CMD);
tanyuzhuo 20:5bd9f9e406d1 252 break;
tanyuzhuo 20:5bd9f9e406d1 253 }
tanyuzhuo 20:5bd9f9e406d1 254
tanyuzhuo 20:5bd9f9e406d1 255 //Try taking 2 more digits
tanyuzhuo 20:5bd9f9e406d1 256 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 257 intValue = intValue*10 + command[index] - '0';
tanyuzhuo 20:5bd9f9e406d1 258 index++;
tanyuzhuo 20:5bd9f9e406d1 259 }
tanyuzhuo 20:5bd9f9e406d1 260 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 261 intValue = intValue*10 + command[index] - '0';
tanyuzhuo 20:5bd9f9e406d1 262 index++;
tanyuzhuo 20:5bd9f9e406d1 263 }
tanyuzhuo 20:5bd9f9e406d1 264 //Check for '.'
tanyuzhuo 20:5bd9f9e406d1 265 if (command[index] == '.') {
tanyuzhuo 20:5bd9f9e406d1 266 index++;
tanyuzhuo 20:5bd9f9e406d1 267 //Check for one decimal
tanyuzhuo 20:5bd9f9e406d1 268 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 269 decimalValue = (command[index] - '0')/10;
tanyuzhuo 20:5bd9f9e406d1 270 }
tanyuzhuo 20:5bd9f9e406d1 271 }
tanyuzhuo 20:5bd9f9e406d1 272 putMessage(MAX_SPEED_ECHO1, intValue);
tanyuzhuo 20:5bd9f9e406d1 273 putMessage(MAX_SPEED_ECHO2, (int) (100*decimalValue));
tanyuzhuo 20:5bd9f9e406d1 274 decimalValue = (decimalValue + intValue);
tanyuzhuo 20:5bd9f9e406d1 275 //HERE SEND IT TO ANY GLOBAL VARIABLE YOU WISH
tanyuzhuo 21:34f440ae0227 276 maxSpeed = decimalValue;
tanyuzhuo 20:5bd9f9e406d1 277 break;
tanyuzhuo 20:5bd9f9e406d1 278
tanyuzhuo 20:5bd9f9e406d1 279 case 'K' :
tanyuzhuo 20:5bd9f9e406d1 280 ///pc.printf("Received key: %016llx\n\r", mail->data);
tanyuzhuo 20:5bd9f9e406d1 281 break;
tanyuzhuo 20:5bd9f9e406d1 282 case 'T' :
tanyuzhuo 20:5bd9f9e406d1 283 //pc.printf("Received key: %016llx\n\r", mail->data);
tanyuzhuo 20:5bd9f9e406d1 284 break;
tanyuzhuo 20:5bd9f9e406d1 285 }
peterith 14:0481b606d10e 286 }
peterith 14:0481b606d10e 287
peterith 14:0481b606d10e 288 void commandProcessor() {
peterith 14:0481b606d10e 289 pc.attach(&serialISR);
tanyuzhuo 17:ff5300ba5442 290 char command[19];
tanyuzhuo 17:ff5300ba5442 291 char* number;
peterith 14:0481b606d10e 292 //char k;
peterith 14:0481b606d10e 293 uint64_t receivedKey;
peterith 14:0481b606d10e 294 uint8_t index = 0;
peterith 14:0481b606d10e 295 while(1) {
peterith 14:0481b606d10e 296 osEvent newEvent = inCharQ.get();
peterith 14:0481b606d10e 297 uint8_t newChar = (uint8_t) newEvent.value.p;
peterith 14:0481b606d10e 298 command[index] = newChar;
peterith 14:0481b606d10e 299 index++;
tanyuzhuo 17:ff5300ba5442 300 if (newChar == '\r') {
peterith 14:0481b606d10e 301 command[17] = '\0';
tanyuzhuo 17:ff5300ba5442 302
tanyuzhuo 17:ff5300ba5442 303 if (command[0] == 'R') {
tanyuzhuo 24:7dd4e187dd30 304 commandDecoder(command);
tanyuzhuo 24:7dd4e187dd30 305 motorPosition_command = motorPosition;
tanyuzhuo 20:5bd9f9e406d1 306 putMessage(ROTATION_CMD);
tanyuzhuo 24:7dd4e187dd30 307
tanyuzhuo 24:7dd4e187dd30 308
tanyuzhuo 17:ff5300ba5442 309 }
tanyuzhuo 17:ff5300ba5442 310 else if (command[0] == 'V') {
tanyuzhuo 23:904721445e7a 311 commandDecoder(command);
tanyuzhuo 20:5bd9f9e406d1 312 putMessage(MAX_SPEED_CMD);
tanyuzhuo 17:ff5300ba5442 313 }
tanyuzhuo 17:ff5300ba5442 314 else if (command[0] == 'K') {
tanyuzhuo 17:ff5300ba5442 315 if (index == 18){ // when index is 18 means you entered K and 16 digits
tanyuzhuo 17:ff5300ba5442 316 number = command +1; //super bad solution, but I don't know how to work with strings in C
tanyuzhuo 17:ff5300ba5442 317 receivedKey = strtoull(number, NULL, 16);
tanyuzhuo 20:5bd9f9e406d1 318 putMessage(KEY_ECHO,receivedKey);
tanyuzhuo 17:ff5300ba5442 319 //receivedKey = 2147483648;
tanyuzhuo 17:ff5300ba5442 320 //sscanf(command, "%d", &receivedKey);
tanyuzhuo 20:5bd9f9e406d1 321 //pc.printf("Received key: %016llx\n\r", receivedKey);
tanyuzhuo 17:ff5300ba5442 322 newKey_mutex.lock();
tanyuzhuo 17:ff5300ba5442 323 newKey = receivedKey;
tanyuzhuo 17:ff5300ba5442 324 newKey_mutex.unlock();
tanyuzhuo 17:ff5300ba5442 325 } else {
tanyuzhuo 20:5bd9f9e406d1 326 putMessage(KEY_DECLINED);
tanyuzhuo 17:ff5300ba5442 327 };
tanyuzhuo 17:ff5300ba5442 328 }
tanyuzhuo 17:ff5300ba5442 329 else if (command[0] == 'T') {
tanyuzhuo 17:ff5300ba5442 330 pc.printf("Melody command\n");
tanyuzhuo 17:ff5300ba5442 331 pc.printf("%s", command);
tanyuzhuo 17:ff5300ba5442 332 }
peterith 14:0481b606d10e 333 memset(command, 0, sizeof(command));
peterith 14:0481b606d10e 334 index = 0;
peterith 14:0481b606d10e 335 } else {
tanyuzhuo 20:5bd9f9e406d1 336 pc.printf("Current command: %s\n\r", command); //Not sure how to go around this one cause it requires passing string
peterith 14:0481b606d10e 337 }
peterith 14:0481b606d10e 338 }
peterith 14:0481b606d10e 339 }
tanyuzhuo 17:ff5300ba5442 340
tanyuzhuo 16:10d53b056b17 341 void bitcoin(){
tanyuzhuo 16:10d53b056b17 342 while(1) {
tanyuzhuo 16:10d53b056b17 343 SHA256 sha;
tanyuzhuo 16:10d53b056b17 344 uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,
tanyuzhuo 16:10d53b056b17 345 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73,
tanyuzhuo 16:10d53b056b17 346 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E,
tanyuzhuo 16:10d53b056b17 347 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20,
tanyuzhuo 16:10d53b056b17 348 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20,
tanyuzhuo 16:10d53b056b17 349 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20,
tanyuzhuo 16:10d53b056b17 350 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
tanyuzhuo 16:10d53b056b17 351 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
tanyuzhuo 16:10d53b056b17 352 uint64_t* key = (uint64_t*) ((int) sequence + 48);
tanyuzhuo 16:10d53b056b17 353 uint64_t* nonce = (uint64_t*) ((int) sequence + 56);
tanyuzhuo 16:10d53b056b17 354 uint8_t hash[32];
tanyuzhuo 16:10d53b056b17 355
tanyuzhuo 16:10d53b056b17 356 Timer t;
tanyuzhuo 16:10d53b056b17 357 t.start();
tanyuzhuo 16:10d53b056b17 358 unsigned currentTime = 0;
tanyuzhuo 20:5bd9f9e406d1 359 unsigned currentCount= 0;
tanyuzhuo 16:10d53b056b17 360
tanyuzhuo 16:10d53b056b17 361 for (unsigned i = 0; i <= UINT_MAX; i++) {
tanyuzhuo 16:10d53b056b17 362 (*nonce)++;
tanyuzhuo 16:10d53b056b17 363 newKey_mutex.lock();
tanyuzhuo 16:10d53b056b17 364 *key = newKey;
tanyuzhuo 16:10d53b056b17 365 newKey_mutex.unlock();
tanyuzhuo 16:10d53b056b17 366 sha.computeHash(hash, sequence, 64);
tanyuzhuo 16:10d53b056b17 367 if (hash[0] == 0 && hash[1] == 0) {
tanyuzhuo 16:10d53b056b17 368 //putMessage(nonce);
tanyuzhuo 16:10d53b056b17 369 pc.printf("Successful nonce: %016x\n\r", *nonce);
tanyuzhuo 16:10d53b056b17 370 }
tanyuzhuo 16:10d53b056b17 371 if ((unsigned) t.read() == currentTime) {
tanyuzhuo 16:10d53b056b17 372 //pc.printf("Hash rate: %d\n\r", i - currentCount);
tanyuzhuo 20:5bd9f9e406d1 373 //pc.printf("Current key: %016llx\n\r", *key);
tanyuzhuo 16:10d53b056b17 374 currentTime++;
tanyuzhuo 16:10d53b056b17 375 currentCount = i;
tanyuzhuo 16:10d53b056b17 376 }
tanyuzhuo 16:10d53b056b17 377 }
tanyuzhuo 16:10d53b056b17 378 t.stop();
tanyuzhuo 16:10d53b056b17 379 }
tanyuzhuo 16:10d53b056b17 380 }
tanyuzhuo 18:e48c0910c71e 381
tanyuzhuo 18:e48c0910c71e 382
tanyuzhuo 18:e48c0910c71e 383
tanyuzhuo 20:5bd9f9e406d1 384
tanyuzhuo 18:e48c0910c71e 385
tanyuzhuo 18:e48c0910c71e 386
estott 0:de4320f74764 387 //Set a given drive state
tanyuzhuo 23:904721445e7a 388 void motorOut(int8_t driveState, float pulseWidth){
estott 0:de4320f74764 389
estott 2:4e88faab6988 390 //Lookup the output byte from the drive state.
estott 2:4e88faab6988 391 int8_t driveOut = driveTable[driveState & 0x07];
estott 2:4e88faab6988 392
estott 2:4e88faab6988 393 //Turn off first
tanyuzhuo 20:5bd9f9e406d1 394 if (~driveOut & 0x01) L1L = 0;
estott 2:4e88faab6988 395 if (~driveOut & 0x02) L1H = 1;
tanyuzhuo 20:5bd9f9e406d1 396 if (~driveOut & 0x04) L2L = 0;
estott 2:4e88faab6988 397 if (~driveOut & 0x08) L2H = 1;
tanyuzhuo 20:5bd9f9e406d1 398 if (~driveOut & 0x10) L3L = 0;
estott 2:4e88faab6988 399 if (~driveOut & 0x20) L3H = 1;
estott 2:4e88faab6988 400
estott 2:4e88faab6988 401 //Then turn on
tanyuzhuo 20:5bd9f9e406d1 402 if (driveOut & 0x01) L1L = 1;
estott 2:4e88faab6988 403 if (driveOut & 0x02) L1H = 0;
tanyuzhuo 20:5bd9f9e406d1 404 if (driveOut & 0x04) L2L = 1;
estott 2:4e88faab6988 405 if (driveOut & 0x08) L2H = 0;
tanyuzhuo 20:5bd9f9e406d1 406 if (driveOut & 0x10) L3L = 1;
estott 2:4e88faab6988 407 if (driveOut & 0x20) L3H = 0;
tanyuzhuo 20:5bd9f9e406d1 408
tanyuzhuo 20:5bd9f9e406d1 409
tanyuzhuo 23:904721445e7a 410 //d9.write(1);
tanyuzhuo 23:904721445e7a 411 d9.pulsewidth_us(pulseWidth);
peterith 13:c51d828531d5 412 }
estott 0:de4320f74764 413
peterith 13:c51d828531d5 414 //Convert photointerrupter inputs to a rotor state
estott 0:de4320f74764 415 inline int8_t readRotorState(){
estott 2:4e88faab6988 416 return stateMap[I1 + 2*I2 + 4*I3];
peterith 13:c51d828531d5 417 }
estott 0:de4320f74764 418
estott 2:4e88faab6988 419 int8_t motorHome() {
tanyuzhuo 18:e48c0910c71e 420 //Put the motor in drive state 0 and wait for it to stabilize
tanyuzhuo 23:904721445e7a 421 motorOut(0,pulseWidth);
estott 3:569b35e2a602 422 wait(2.0);
estott 2:4e88faab6988 423 return readRotorState();
estott 0:de4320f74764 424 }
peterith 13:c51d828531d5 425
tanyuzhuo 20:5bd9f9e406d1 426
tanyuzhuo 18:e48c0910c71e 427 // ISR to handle the updating of the motor position
tanyuzhuo 18:e48c0910c71e 428 void motorISR() {
tanyuzhuo 18:e48c0910c71e 429 static int8_t oldRotorState;
tanyuzhuo 20:5bd9f9e406d1 430 //orState is subtracted from future rotor state inputs to align rotor and motor states
tanyuzhuo 20:5bd9f9e406d1 431
tanyuzhuo 18:e48c0910c71e 432 int8_t rotorState = readRotorState();
tanyuzhuo 23:904721445e7a 433 motorOut((rotorState-orState+lead+6)%6,pulseWidth); //+6 to make sure the remainder is positive
tanyuzhuo 18:e48c0910c71e 434 if (rotorState - oldRotorState == 5) motorPosition --;
tanyuzhuo 18:e48c0910c71e 435 else if (rotorState - oldRotorState == -5) motorPosition ++;
tanyuzhuo 18:e48c0910c71e 436 else motorPosition += (rotorState - oldRotorState);
tanyuzhuo 20:5bd9f9e406d1 437 //pc.printf ("motorpPosition %f\n\r", motorPosition);
tanyuzhuo 18:e48c0910c71e 438 oldRotorState = rotorState;
tanyuzhuo 18:e48c0910c71e 439 }
tanyuzhuo 18:e48c0910c71e 440 /*void push() {
peterith 13:c51d828531d5 441 intState = readRotorState();
peterith 13:c51d828531d5 442 if (intState != intStateOld) {
peterith 13:c51d828531d5 443 intStateOld = intState;
peterith 13:c51d828531d5 444 motorOut((intState - orState + lead +6) % 6); //+6 to make sure the remainder is positive
peterith 13:c51d828531d5 445 }
tanyuzhuo 18:e48c0910c71e 446 }*/
tanyuzhuo 20:5bd9f9e406d1 447 void motorCtrlTick(){
tanyuzhuo 20:5bd9f9e406d1 448 motorCtrlT.signal_set(0x1);
tanyuzhuo 20:5bd9f9e406d1 449 }
tanyuzhuo 18:e48c0910c71e 450 void motorCtrlFn(){
tanyuzhuo 18:e48c0910c71e 451 int32_t counter=0;
tanyuzhuo 18:e48c0910c71e 452 static int32_t oldmotorPosition;
tanyuzhuo 24:7dd4e187dd30 453 float Ts;
tanyuzhuo 24:7dd4e187dd30 454 float sError; //speed error
tanyuzhuo 24:7dd4e187dd30 455 float intError = 0; //integral of velError
tanyuzhuo 24:7dd4e187dd30 456 float Tr;
tanyuzhuo 24:7dd4e187dd30 457 float error;
tanyuzhuo 24:7dd4e187dd30 458 float olderror = 0;
tanyuzhuo 21:34f440ae0227 459 float Speed;
tanyuzhuo 24:7dd4e187dd30 460 float Rev;
tanyuzhuo 24:7dd4e187dd30 461 int8_t leads = -2;
tanyuzhuo 24:7dd4e187dd30 462 int8_t leadr = -2;
tanyuzhuo 24:7dd4e187dd30 463 float kps = 80;
tanyuzhuo 24:7dd4e187dd30 464 float kis = 0.07;
tanyuzhuo 24:7dd4e187dd30 465 float kpr = 35;
tanyuzhuo 24:7dd4e187dd30 466 float kdr = 14.75; //proportional constant for speed
tanyuzhuo 18:e48c0910c71e 467 // Timer to count time passed between ticks to calculate velocity
tanyuzhuo 18:e48c0910c71e 468 Timer motorTime;
tanyuzhuo 18:e48c0910c71e 469 motorTime.start();
tanyuzhuo 18:e48c0910c71e 470 float motorPos;
tanyuzhuo 24:7dd4e187dd30 471 float myTime;
tanyuzhuo 24:7dd4e187dd30 472
tanyuzhuo 18:e48c0910c71e 473 Ticker motorCtrlTicker;
tanyuzhuo 18:e48c0910c71e 474 motorCtrlTicker.attach_us(&motorCtrlTick,100000);
tanyuzhuo 18:e48c0910c71e 475 while(1){
tanyuzhuo 18:e48c0910c71e 476 motorCtrlT.signal_wait(0x1);
tanyuzhuo 24:7dd4e187dd30 477
tanyuzhuo 21:34f440ae0227 478 Speed = maxSpeed*6;
tanyuzhuo 24:7dd4e187dd30 479 Rev = newRev *6;
tanyuzhuo 18:e48c0910c71e 480 motorPos = motorPosition;
tanyuzhuo 24:7dd4e187dd30 481
tanyuzhuo 24:7dd4e187dd30 482 myTime = motorTime.read();
tanyuzhuo 24:7dd4e187dd30 483 motorVelocity = (motorPos - oldmotorPosition)/myTime;
tanyuzhuo 24:7dd4e187dd30 484 error = Rev + motorPosition_command - motorPos;
tanyuzhuo 18:e48c0910c71e 485 oldmotorPosition = motorPos;
tanyuzhuo 20:5bd9f9e406d1 486
tanyuzhuo 20:5bd9f9e406d1 487 //equation for controls
tanyuzhuo 24:7dd4e187dd30 488 sError = Speed -abs(motorVelocity);
tanyuzhuo 24:7dd4e187dd30 489 intError = intError + sError;
tanyuzhuo 24:7dd4e187dd30 490 Ts = (kps * sError + kis * intError)*(error-olderror/abs(error-olderror));
tanyuzhuo 24:7dd4e187dd30 491 Tr = kpr*error+kdr*(error-olderror)/ myTime;
tanyuzhuo 24:7dd4e187dd30 492 //pc.printf("Error: %f\n\r", Tr);
tanyuzhuo 24:7dd4e187dd30 493
tanyuzhuo 24:7dd4e187dd30 494 // Lead flip
tanyuzhuo 24:7dd4e187dd30 495 if ( Ts >0){
tanyuzhuo 24:7dd4e187dd30 496 leads = 2;
tanyuzhuo 24:7dd4e187dd30 497 }
tanyuzhuo 24:7dd4e187dd30 498 else if ( Ts <0){
tanyuzhuo 24:7dd4e187dd30 499 leads = -2;
tanyuzhuo 24:7dd4e187dd30 500 }
tanyuzhuo 24:7dd4e187dd30 501
tanyuzhuo 24:7dd4e187dd30 502 if (Tr >= 0){
tanyuzhuo 24:7dd4e187dd30 503 leadr= -2;
tanyuzhuo 24:7dd4e187dd30 504 }
tanyuzhuo 24:7dd4e187dd30 505 else if (Tr < 0){
tanyuzhuo 24:7dd4e187dd30 506 leadr = 2;
tanyuzhuo 24:7dd4e187dd30 507 }
tanyuzhuo 24:7dd4e187dd30 508
tanyuzhuo 24:7dd4e187dd30 509 //Torque adjustment
tanyuzhuo 23:904721445e7a 510 if (Ts > 2000){
tanyuzhuo 24:7dd4e187dd30 511
tanyuzhuo 23:904721445e7a 512 Ts = 2000;
tanyuzhuo 23:904721445e7a 513 }
tanyuzhuo 23:904721445e7a 514 else if (Ts < 0){
tanyuzhuo 24:7dd4e187dd30 515
tanyuzhuo 24:7dd4e187dd30 516 Ts = -Ts;
tanyuzhuo 24:7dd4e187dd30 517 }
tanyuzhuo 24:7dd4e187dd30 518 else if (Ts < -2000){
tanyuzhuo 24:7dd4e187dd30 519
tanyuzhuo 24:7dd4e187dd30 520 Ts = 2000;
tanyuzhuo 24:7dd4e187dd30 521 }
tanyuzhuo 24:7dd4e187dd30 522 if (Tr > 2000){
tanyuzhuo 24:7dd4e187dd30 523
tanyuzhuo 24:7dd4e187dd30 524 Tr = 2000;
tanyuzhuo 24:7dd4e187dd30 525 }
tanyuzhuo 24:7dd4e187dd30 526 else if (Tr < 0){
tanyuzhuo 24:7dd4e187dd30 527
tanyuzhuo 24:7dd4e187dd30 528 Tr = -Tr;
tanyuzhuo 24:7dd4e187dd30 529 }
tanyuzhuo 24:7dd4e187dd30 530 else if (Tr < -2000){
tanyuzhuo 24:7dd4e187dd30 531
tanyuzhuo 24:7dd4e187dd30 532 Tr = 2000;
tanyuzhuo 23:904721445e7a 533 }
tanyuzhuo 24:7dd4e187dd30 534 ;
tanyuzhuo 24:7dd4e187dd30 535 //if (motorVelocity < 0){
tanyuzhuo 24:7dd4e187dd30 536 if (Ts <=Tr ){
tanyuzhuo 24:7dd4e187dd30 537 lead = leads;
tanyuzhuo 24:7dd4e187dd30 538 pulseWidth = Ts;
tanyuzhuo 24:7dd4e187dd30 539
tanyuzhuo 24:7dd4e187dd30 540 }
tanyuzhuo 24:7dd4e187dd30 541 else{
tanyuzhuo 24:7dd4e187dd30 542 lead = leadr;
tanyuzhuo 24:7dd4e187dd30 543 pulseWidth = Tr;
tanyuzhuo 24:7dd4e187dd30 544
tanyuzhuo 24:7dd4e187dd30 545 } /*
tanyuzhuo 24:7dd4e187dd30 546 }
tanyuzhuo 24:7dd4e187dd30 547 else{
tanyuzhuo 24:7dd4e187dd30 548 if (Ts <=Tr ){
tanyuzhuo 24:7dd4e187dd30 549 lead = leads;
tanyuzhuo 24:7dd4e187dd30 550 pulseWidth = Ts;
tanyuzhuo 24:7dd4e187dd30 551
tanyuzhuo 24:7dd4e187dd30 552 }
tanyuzhuo 24:7dd4e187dd30 553 else{
tanyuzhuo 24:7dd4e187dd30 554 lead = leadr;
tanyuzhuo 24:7dd4e187dd30 555 pulseWidth = Tr;
tanyuzhuo 24:7dd4e187dd30 556
tanyuzhuo 24:7dd4e187dd30 557 }
tanyuzhuo 24:7dd4e187dd30 558 }*/
tanyuzhuo 24:7dd4e187dd30 559
tanyuzhuo 24:7dd4e187dd30 560
tanyuzhuo 24:7dd4e187dd30 561
tanyuzhuo 24:7dd4e187dd30 562
tanyuzhuo 18:e48c0910c71e 563 motorTime.reset();
tanyuzhuo 18:e48c0910c71e 564 // Serial output to monitor speed and position
tanyuzhuo 18:e48c0910c71e 565 counter++;
tanyuzhuo 18:e48c0910c71e 566 if(counter == 10){
tanyuzhuo 18:e48c0910c71e 567 counter = 0;
tanyuzhuo 18:e48c0910c71e 568 //display velocity and motor position
tanyuzhuo 20:5bd9f9e406d1 569 // pc.printf ("motor Pos: %f\n\r", (motorPosition/6.0));
tanyuzhuo 21:34f440ae0227 570 putMessage(MOTOR_POS,0,(float)(motorPosition/6.0));
tanyuzhuo 20:5bd9f9e406d1 571 putMessage(MOTOR_SPEED,0,(float)(motorVelocity/6.0));
tanyuzhuo 21:34f440ae0227 572 //pc.printf ("torque: %f\n\r", Ts);
tanyuzhuo 18:e48c0910c71e 573 }
tanyuzhuo 24:7dd4e187dd30 574 olderror=error;
tanyuzhuo 18:e48c0910c71e 575 }
tanyuzhuo 21:34f440ae0227 576 }
peterith 13:c51d828531d5 577 int main() {
peterith 14:0481b606d10e 578 //Serial pc(SERIAL_TX, SERIAL_RX);
peterith 14:0481b606d10e 579
tanyuzhuo 17:ff5300ba5442 580 //Initialise bincoin mining and communication
tanyuzhuo 20:5bd9f9e406d1 581
tanyuzhuo 17:ff5300ba5442 582
tanyuzhuo 20:5bd9f9e406d1 583
tanyuzhuo 20:5bd9f9e406d1 584
tanyuzhuo 20:5bd9f9e406d1 585
tanyuzhuo 23:904721445e7a 586 d9.period(0.002f); //Set PWM period in seconds
peterith 14:0481b606d10e 587
tanyuzhuo 20:5bd9f9e406d1 588
tanyuzhuo 20:5bd9f9e406d1 589 commandProcessorthread.start(commandProcessor);
tanyuzhuo 20:5bd9f9e406d1 590
tanyuzhuo 20:5bd9f9e406d1 591 commsOut.start(commsOutFunc);
tanyuzhuo 20:5bd9f9e406d1 592
tanyuzhuo 20:5bd9f9e406d1 593 motorCtrlT.start(motorCtrlFn);
tanyuzhuo 20:5bd9f9e406d1 594 bitcointhread.start(bitcoin);
estott 0:de4320f74764 595
tanyuzhuo 20:5bd9f9e406d1 596 putMessage(WELCOME);
tanyuzhuo 20:5bd9f9e406d1 597 int8_t orState = motorHome();
tanyuzhuo 20:5bd9f9e406d1 598 putMessage(ROTOR_ORG);
tanyuzhuo 20:5bd9f9e406d1 599 //pc.printf("Rotor origin: %x\n\r", orState);
tanyuzhuo 20:5bd9f9e406d1 600 //Set PWM duty in %
tanyuzhuo 20:5bd9f9e406d1 601 I1.rise(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 602 I2.rise(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 603 I3.rise(&motorISR);
peterith 13:c51d828531d5 604
tanyuzhuo 20:5bd9f9e406d1 605 I1.fall(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 606 I2.fall(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 607 I3.fall(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 608
tanyuzhuo 16:10d53b056b17 609
estott 0:de4320f74764 610 }