An embedded device

Dependencies:   Crypto

Committer:
tanyuzhuo
Date:
Wed Mar 20 12:45:06 2019 +0000
Revision:
21:34f440ae0227
Parent:
20:5bd9f9e406d1
Child:
22:de980b82d0ce
Child:
23:904721445e7a
fixed display error, need commin decoding correction

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 :
tanyuzhuo 20:5bd9f9e406d1 149 pc.printf("Max speed command\n\r");
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 MAX_SPEED_ECHO1 :
tanyuzhuo 20:5bd9f9e406d1 159 pc.printf("Max speed command:\n\r");
tanyuzhuo 20:5bd9f9e406d1 160 pc.printf("Max speed int: %d\n\r", mail->data);
tanyuzhuo 20:5bd9f9e406d1 161 break;
tanyuzhuo 20:5bd9f9e406d1 162 case MAX_SPEED_ECHO2 :
tanyuzhuo 20:5bd9f9e406d1 163 pc.printf("Max speed decimal: %d\n\r", mail->data);
tanyuzhuo 20:5bd9f9e406d1 164 break;
tanyuzhuo 20:5bd9f9e406d1 165 case KEY_ECHO :
tanyuzhuo 20:5bd9f9e406d1 166 pc.printf("Received key: %016llx\n\r", mail->data);
tanyuzhuo 20:5bd9f9e406d1 167 break;
tanyuzhuo 20:5bd9f9e406d1 168 case INVALID_CMD :
tanyuzhuo 20:5bd9f9e406d1 169 pc.printf("Invalid command!\r\n");
tanyuzhuo 20:5bd9f9e406d1 170 break;
tanyuzhuo 20:5bd9f9e406d1 171 case MOTOR_POS :
tanyuzhuo 20:5bd9f9e406d1 172 pc.printf("Motor position: %f\r\n", mail->fdata);
tanyuzhuo 20:5bd9f9e406d1 173 //pc.printf("{TIMEPLOT|%.2f|Motor Position", mail->fdata);
tanyuzhuo 20:5bd9f9e406d1 174 break;
tanyuzhuo 20:5bd9f9e406d1 175 case MOTOR_SPEED :
tanyuzhuo 20:5bd9f9e406d1 176 pc.printf("Motor speed: %f\r\n", mail->fdata);
tanyuzhuo 20:5bd9f9e406d1 177 break;
tanyuzhuo 20:5bd9f9e406d1 178 case NONCE_DETECT :
tanyuzhuo 20:5bd9f9e406d1 179 pc.printf("Successful nonce: %016x\n\r", mail->data);
tanyuzhuo 20:5bd9f9e406d1 180 break;
tanyuzhuo 20:5bd9f9e406d1 181 case WELCOME :
tanyuzhuo 20:5bd9f9e406d1 182 pc.printf("Hello Pete\n\r");
tanyuzhuo 20:5bd9f9e406d1 183 break;
tanyuzhuo 20:5bd9f9e406d1 184 case ROTOR_ORG :
tanyuzhuo 20:5bd9f9e406d1 185 pc.printf("Rotor origin: %x\n\r", orState);
tanyuzhuo 20:5bd9f9e406d1 186
tanyuzhuo 20:5bd9f9e406d1 187 }
tanyuzhuo 20:5bd9f9e406d1 188 mail_box.free(mail);
tanyuzhuo 20:5bd9f9e406d1 189 }
tanyuzhuo 20:5bd9f9e406d1 190 }
tanyuzhuo 20:5bd9f9e406d1 191 }
tanyuzhuo 20:5bd9f9e406d1 192 //End of that block
tanyuzhuo 20:5bd9f9e406d1 193 /***************************************************************/
tanyuzhuo 20:5bd9f9e406d1 194
tanyuzhuo 20:5bd9f9e406d1 195 /***************************************************************/
tanyuzhuo 20:5bd9f9e406d1 196 //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 197
tanyuzhuo 20:5bd9f9e406d1 198 //CommsIn.h
tanyuzhuo 20:5bd9f9e406d1 199
tanyuzhuo 20:5bd9f9e406d1 200 void commandDecoder(char* command) {
tanyuzhuo 20:5bd9f9e406d1 201 int8_t sign =1;
tanyuzhuo 20:5bd9f9e406d1 202 uint8_t index = 1;
tanyuzhuo 20:5bd9f9e406d1 203 int intValue = 0;
tanyuzhuo 20:5bd9f9e406d1 204 float decimalValue = 0;
tanyuzhuo 20:5bd9f9e406d1 205 switch (command[0]) {
tanyuzhuo 20:5bd9f9e406d1 206 case 'R' :
tanyuzhuo 20:5bd9f9e406d1 207 // Check sign
tanyuzhuo 20:5bd9f9e406d1 208 if (command[1] == '-'){
tanyuzhuo 20:5bd9f9e406d1 209 sign = -1;
tanyuzhuo 20:5bd9f9e406d1 210 index++;
tanyuzhuo 20:5bd9f9e406d1 211 }
tanyuzhuo 20:5bd9f9e406d1 212 // Take first digit
tanyuzhuo 20:5bd9f9e406d1 213 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 214 intValue = command[index] - '0';
tanyuzhuo 20:5bd9f9e406d1 215 index++;
tanyuzhuo 20:5bd9f9e406d1 216 }
tanyuzhuo 20:5bd9f9e406d1 217 else {
tanyuzhuo 20:5bd9f9e406d1 218 putMessage(INVALID_CMD);
tanyuzhuo 20:5bd9f9e406d1 219 break;
tanyuzhuo 20:5bd9f9e406d1 220 }
tanyuzhuo 20:5bd9f9e406d1 221
tanyuzhuo 20:5bd9f9e406d1 222 //Try taking 2 more digits
tanyuzhuo 20:5bd9f9e406d1 223 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 224 intValue = intValue*10 + command[index] - '0';
tanyuzhuo 20:5bd9f9e406d1 225 index++;
tanyuzhuo 20:5bd9f9e406d1 226 }
tanyuzhuo 20:5bd9f9e406d1 227 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 228 intValue = intValue*10 + command[index] - '0';
tanyuzhuo 20:5bd9f9e406d1 229 index++;
tanyuzhuo 20:5bd9f9e406d1 230 }
tanyuzhuo 20:5bd9f9e406d1 231 //Check for '.'
tanyuzhuo 20:5bd9f9e406d1 232 if (command[index] == '.') {
tanyuzhuo 20:5bd9f9e406d1 233 index++;
tanyuzhuo 20:5bd9f9e406d1 234 //Check for decimals
tanyuzhuo 20:5bd9f9e406d1 235 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 236 decimalValue = (command[index] - '0')/10;
tanyuzhuo 20:5bd9f9e406d1 237 index++;
tanyuzhuo 20:5bd9f9e406d1 238 }
tanyuzhuo 20:5bd9f9e406d1 239 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 240 decimalValue = (decimalValue/10 + command[index] - '0')/10;
tanyuzhuo 20:5bd9f9e406d1 241 }
tanyuzhuo 20:5bd9f9e406d1 242 }
tanyuzhuo 20:5bd9f9e406d1 243 putMessage(R_ECHO1, intValue);
tanyuzhuo 20:5bd9f9e406d1 244 putMessage(R_ECHO2, (int) (100*decimalValue));
tanyuzhuo 20:5bd9f9e406d1 245 decimalValue = (decimalValue + intValue) * sign;
tanyuzhuo 20:5bd9f9e406d1 246 //HERE SEND IT TO ANY GLOBAL VARIABLE YOU WISH
tanyuzhuo 20:5bd9f9e406d1 247 break;
tanyuzhuo 20:5bd9f9e406d1 248
tanyuzhuo 20:5bd9f9e406d1 249 case 'V' :
tanyuzhuo 20:5bd9f9e406d1 250 // Take first digit
tanyuzhuo 20:5bd9f9e406d1 251 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 252 intValue = command[index] - '0';
tanyuzhuo 20:5bd9f9e406d1 253 index++;
tanyuzhuo 20:5bd9f9e406d1 254 }
tanyuzhuo 20:5bd9f9e406d1 255 else {
tanyuzhuo 20:5bd9f9e406d1 256 putMessage(INVALID_CMD);
tanyuzhuo 20:5bd9f9e406d1 257 break;
tanyuzhuo 20:5bd9f9e406d1 258 }
tanyuzhuo 20:5bd9f9e406d1 259
tanyuzhuo 20:5bd9f9e406d1 260 //Try taking 2 more digits
tanyuzhuo 20:5bd9f9e406d1 261 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 262 intValue = intValue*10 + command[index] - '0';
tanyuzhuo 20:5bd9f9e406d1 263 index++;
tanyuzhuo 20:5bd9f9e406d1 264 }
tanyuzhuo 20:5bd9f9e406d1 265 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 266 intValue = intValue*10 + command[index] - '0';
tanyuzhuo 20:5bd9f9e406d1 267 index++;
tanyuzhuo 20:5bd9f9e406d1 268 }
tanyuzhuo 20:5bd9f9e406d1 269 //Check for '.'
tanyuzhuo 20:5bd9f9e406d1 270 if (command[index] == '.') {
tanyuzhuo 20:5bd9f9e406d1 271 index++;
tanyuzhuo 20:5bd9f9e406d1 272 //Check for one decimal
tanyuzhuo 20:5bd9f9e406d1 273 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
tanyuzhuo 20:5bd9f9e406d1 274 decimalValue = (command[index] - '0')/10;
tanyuzhuo 20:5bd9f9e406d1 275 }
tanyuzhuo 20:5bd9f9e406d1 276 }
tanyuzhuo 20:5bd9f9e406d1 277 putMessage(MAX_SPEED_ECHO1, intValue);
tanyuzhuo 20:5bd9f9e406d1 278 putMessage(MAX_SPEED_ECHO2, (int) (100*decimalValue));
tanyuzhuo 20:5bd9f9e406d1 279 decimalValue = (decimalValue + intValue);
tanyuzhuo 20:5bd9f9e406d1 280 //HERE SEND IT TO ANY GLOBAL VARIABLE YOU WISH
tanyuzhuo 21:34f440ae0227 281 maxSpeed = decimalValue;
tanyuzhuo 20:5bd9f9e406d1 282 break;
tanyuzhuo 20:5bd9f9e406d1 283
tanyuzhuo 20:5bd9f9e406d1 284 case 'K' :
tanyuzhuo 20:5bd9f9e406d1 285 ///pc.printf("Received key: %016llx\n\r", mail->data);
tanyuzhuo 20:5bd9f9e406d1 286 break;
tanyuzhuo 20:5bd9f9e406d1 287 case 'T' :
tanyuzhuo 20:5bd9f9e406d1 288 //pc.printf("Received key: %016llx\n\r", mail->data);
tanyuzhuo 20:5bd9f9e406d1 289 break;
tanyuzhuo 20:5bd9f9e406d1 290 }
peterith 14:0481b606d10e 291 }
peterith 14:0481b606d10e 292
peterith 14:0481b606d10e 293 void commandProcessor() {
peterith 14:0481b606d10e 294 pc.attach(&serialISR);
tanyuzhuo 17:ff5300ba5442 295 char command[19];
tanyuzhuo 17:ff5300ba5442 296 char* number;
peterith 14:0481b606d10e 297 //char k;
peterith 14:0481b606d10e 298 uint64_t receivedKey;
peterith 14:0481b606d10e 299 uint8_t index = 0;
peterith 14:0481b606d10e 300 while(1) {
peterith 14:0481b606d10e 301 osEvent newEvent = inCharQ.get();
peterith 14:0481b606d10e 302 uint8_t newChar = (uint8_t) newEvent.value.p;
peterith 14:0481b606d10e 303 command[index] = newChar;
peterith 14:0481b606d10e 304 index++;
tanyuzhuo 17:ff5300ba5442 305 if (newChar == '\r') {
peterith 14:0481b606d10e 306 command[17] = '\0';
tanyuzhuo 17:ff5300ba5442 307
tanyuzhuo 17:ff5300ba5442 308 if (command[0] == 'R') {
tanyuzhuo 20:5bd9f9e406d1 309 putMessage(ROTATION_CMD);
tanyuzhuo 20:5bd9f9e406d1 310 commandDecoder(command);
tanyuzhuo 17:ff5300ba5442 311 }
tanyuzhuo 17:ff5300ba5442 312 else if (command[0] == 'V') {
tanyuzhuo 20:5bd9f9e406d1 313 putMessage(MAX_SPEED_CMD);
tanyuzhuo 17:ff5300ba5442 314 }
tanyuzhuo 17:ff5300ba5442 315 else if (command[0] == 'K') {
tanyuzhuo 17:ff5300ba5442 316 if (index == 18){ // when index is 18 means you entered K and 16 digits
tanyuzhuo 17:ff5300ba5442 317 number = command +1; //super bad solution, but I don't know how to work with strings in C
tanyuzhuo 17:ff5300ba5442 318 receivedKey = strtoull(number, NULL, 16);
tanyuzhuo 20:5bd9f9e406d1 319 putMessage(KEY_ECHO,receivedKey);
tanyuzhuo 17:ff5300ba5442 320 //receivedKey = 2147483648;
tanyuzhuo 17:ff5300ba5442 321 //sscanf(command, "%d", &receivedKey);
tanyuzhuo 20:5bd9f9e406d1 322 //pc.printf("Received key: %016llx\n\r", receivedKey);
tanyuzhuo 17:ff5300ba5442 323 newKey_mutex.lock();
tanyuzhuo 17:ff5300ba5442 324 newKey = receivedKey;
tanyuzhuo 17:ff5300ba5442 325 newKey_mutex.unlock();
tanyuzhuo 17:ff5300ba5442 326 } else {
tanyuzhuo 20:5bd9f9e406d1 327 putMessage(KEY_DECLINED);
tanyuzhuo 17:ff5300ba5442 328 };
tanyuzhuo 17:ff5300ba5442 329 }
tanyuzhuo 17:ff5300ba5442 330 else if (command[0] == 'T') {
tanyuzhuo 17:ff5300ba5442 331 pc.printf("Melody command\n");
tanyuzhuo 17:ff5300ba5442 332 pc.printf("%s", command);
tanyuzhuo 17:ff5300ba5442 333 }
peterith 14:0481b606d10e 334 memset(command, 0, sizeof(command));
peterith 14:0481b606d10e 335 index = 0;
peterith 14:0481b606d10e 336 } else {
tanyuzhuo 20:5bd9f9e406d1 337 pc.printf("Current command: %s\n\r", command); //Not sure how to go around this one cause it requires passing string
peterith 14:0481b606d10e 338 }
peterith 14:0481b606d10e 339 }
peterith 14:0481b606d10e 340 }
tanyuzhuo 17:ff5300ba5442 341
tanyuzhuo 16:10d53b056b17 342 void bitcoin(){
tanyuzhuo 16:10d53b056b17 343 while(1) {
tanyuzhuo 16:10d53b056b17 344 SHA256 sha;
tanyuzhuo 16:10d53b056b17 345 uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,
tanyuzhuo 16:10d53b056b17 346 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73,
tanyuzhuo 16:10d53b056b17 347 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E,
tanyuzhuo 16:10d53b056b17 348 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20,
tanyuzhuo 16:10d53b056b17 349 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20,
tanyuzhuo 16:10d53b056b17 350 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20,
tanyuzhuo 16:10d53b056b17 351 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
tanyuzhuo 16:10d53b056b17 352 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
tanyuzhuo 16:10d53b056b17 353 uint64_t* key = (uint64_t*) ((int) sequence + 48);
tanyuzhuo 16:10d53b056b17 354 uint64_t* nonce = (uint64_t*) ((int) sequence + 56);
tanyuzhuo 16:10d53b056b17 355 uint8_t hash[32];
tanyuzhuo 16:10d53b056b17 356
tanyuzhuo 16:10d53b056b17 357 Timer t;
tanyuzhuo 16:10d53b056b17 358 t.start();
tanyuzhuo 16:10d53b056b17 359 unsigned currentTime = 0;
tanyuzhuo 20:5bd9f9e406d1 360 unsigned currentCount= 0;
tanyuzhuo 16:10d53b056b17 361
tanyuzhuo 16:10d53b056b17 362 for (unsigned i = 0; i <= UINT_MAX; i++) {
tanyuzhuo 16:10d53b056b17 363 (*nonce)++;
tanyuzhuo 16:10d53b056b17 364 newKey_mutex.lock();
tanyuzhuo 16:10d53b056b17 365 *key = newKey;
tanyuzhuo 16:10d53b056b17 366 newKey_mutex.unlock();
tanyuzhuo 16:10d53b056b17 367 sha.computeHash(hash, sequence, 64);
tanyuzhuo 16:10d53b056b17 368 if (hash[0] == 0 && hash[1] == 0) {
tanyuzhuo 16:10d53b056b17 369 //putMessage(nonce);
tanyuzhuo 16:10d53b056b17 370 pc.printf("Successful nonce: %016x\n\r", *nonce);
tanyuzhuo 16:10d53b056b17 371 }
tanyuzhuo 16:10d53b056b17 372 if ((unsigned) t.read() == currentTime) {
tanyuzhuo 16:10d53b056b17 373 //pc.printf("Hash rate: %d\n\r", i - currentCount);
tanyuzhuo 20:5bd9f9e406d1 374 //pc.printf("Current key: %016llx\n\r", *key);
tanyuzhuo 16:10d53b056b17 375 currentTime++;
tanyuzhuo 16:10d53b056b17 376 currentCount = i;
tanyuzhuo 16:10d53b056b17 377 }
tanyuzhuo 16:10d53b056b17 378 }
tanyuzhuo 16:10d53b056b17 379 t.stop();
tanyuzhuo 16:10d53b056b17 380 }
tanyuzhuo 16:10d53b056b17 381 }
tanyuzhuo 18:e48c0910c71e 382
tanyuzhuo 18:e48c0910c71e 383
tanyuzhuo 18:e48c0910c71e 384
tanyuzhuo 20:5bd9f9e406d1 385
tanyuzhuo 18:e48c0910c71e 386
tanyuzhuo 18:e48c0910c71e 387
estott 0:de4320f74764 388 //Set a given drive state
tanyuzhuo 20:5bd9f9e406d1 389 void motorOut(int8_t driveState){
estott 0:de4320f74764 390
estott 2:4e88faab6988 391 //Lookup the output byte from the drive state.
estott 2:4e88faab6988 392 int8_t driveOut = driveTable[driveState & 0x07];
estott 2:4e88faab6988 393
estott 2:4e88faab6988 394 //Turn off first
tanyuzhuo 20:5bd9f9e406d1 395 if (~driveOut & 0x01) L1L = 0;
estott 2:4e88faab6988 396 if (~driveOut & 0x02) L1H = 1;
tanyuzhuo 20:5bd9f9e406d1 397 if (~driveOut & 0x04) L2L = 0;
estott 2:4e88faab6988 398 if (~driveOut & 0x08) L2H = 1;
tanyuzhuo 20:5bd9f9e406d1 399 if (~driveOut & 0x10) L3L = 0;
estott 2:4e88faab6988 400 if (~driveOut & 0x20) L3H = 1;
estott 2:4e88faab6988 401
estott 2:4e88faab6988 402 //Then turn on
tanyuzhuo 20:5bd9f9e406d1 403 if (driveOut & 0x01) L1L = 1;
estott 2:4e88faab6988 404 if (driveOut & 0x02) L1H = 0;
tanyuzhuo 20:5bd9f9e406d1 405 if (driveOut & 0x04) L2L = 1;
estott 2:4e88faab6988 406 if (driveOut & 0x08) L2H = 0;
tanyuzhuo 20:5bd9f9e406d1 407 if (driveOut & 0x10) L3L = 1;
estott 2:4e88faab6988 408 if (driveOut & 0x20) L3H = 0;
tanyuzhuo 20:5bd9f9e406d1 409
tanyuzhuo 21:34f440ae0227 410 d9.period(0.002f); //Set PWM period in seconds
tanyuzhuo 21:34f440ae0227 411 d9.write(1);
tanyuzhuo 20:5bd9f9e406d1 412
peterith 13:c51d828531d5 413 }
estott 0:de4320f74764 414
peterith 13:c51d828531d5 415 //Convert photointerrupter inputs to a rotor state
estott 0:de4320f74764 416 inline int8_t readRotorState(){
estott 2:4e88faab6988 417 return stateMap[I1 + 2*I2 + 4*I3];
peterith 13:c51d828531d5 418 }
estott 0:de4320f74764 419
estott 2:4e88faab6988 420 int8_t motorHome() {
tanyuzhuo 18:e48c0910c71e 421 //Put the motor in drive state 0 and wait for it to stabilize
tanyuzhuo 20:5bd9f9e406d1 422 motorOut(0);
estott 3:569b35e2a602 423 wait(2.0);
estott 2:4e88faab6988 424 return readRotorState();
estott 0:de4320f74764 425 }
peterith 13:c51d828531d5 426
tanyuzhuo 20:5bd9f9e406d1 427
tanyuzhuo 18:e48c0910c71e 428 // ISR to handle the updating of the motor position
tanyuzhuo 18:e48c0910c71e 429 void motorISR() {
tanyuzhuo 18:e48c0910c71e 430 static int8_t oldRotorState;
tanyuzhuo 20:5bd9f9e406d1 431 //orState is subtracted from future rotor state inputs to align rotor and motor states
tanyuzhuo 20:5bd9f9e406d1 432
tanyuzhuo 18:e48c0910c71e 433 int8_t rotorState = readRotorState();
tanyuzhuo 20:5bd9f9e406d1 434 motorOut((rotorState-orState+lead+6)%6); //+6 to make sure the remainder is positive
tanyuzhuo 18:e48c0910c71e 435 if (rotorState - oldRotorState == 5) motorPosition --;
tanyuzhuo 18:e48c0910c71e 436 else if (rotorState - oldRotorState == -5) motorPosition ++;
tanyuzhuo 18:e48c0910c71e 437 else motorPosition += (rotorState - oldRotorState);
tanyuzhuo 20:5bd9f9e406d1 438 //pc.printf ("motorpPosition %f\n\r", motorPosition);
tanyuzhuo 18:e48c0910c71e 439 oldRotorState = rotorState;
tanyuzhuo 18:e48c0910c71e 440 }
tanyuzhuo 18:e48c0910c71e 441 /*void push() {
peterith 13:c51d828531d5 442 intState = readRotorState();
peterith 13:c51d828531d5 443 if (intState != intStateOld) {
peterith 13:c51d828531d5 444 intStateOld = intState;
peterith 13:c51d828531d5 445 motorOut((intState - orState + lead +6) % 6); //+6 to make sure the remainder is positive
peterith 13:c51d828531d5 446 }
tanyuzhuo 18:e48c0910c71e 447 }*/
tanyuzhuo 20:5bd9f9e406d1 448 void motorCtrlTick(){
tanyuzhuo 20:5bd9f9e406d1 449 motorCtrlT.signal_set(0x1);
tanyuzhuo 20:5bd9f9e406d1 450 }
tanyuzhuo 18:e48c0910c71e 451 void motorCtrlFn(){
tanyuzhuo 18:e48c0910c71e 452 int32_t counter=0;
tanyuzhuo 18:e48c0910c71e 453 static int32_t oldmotorPosition;
tanyuzhuo 21:34f440ae0227 454 uint32_t Ts;
tanyuzhuo 20:5bd9f9e406d1 455
tanyuzhuo 21:34f440ae0227 456 float Speed;
tanyuzhuo 21:34f440ae0227 457 float kps = 25; //proportional constant for speed
tanyuzhuo 18:e48c0910c71e 458 // Timer to count time passed between ticks to calculate velocity
tanyuzhuo 18:e48c0910c71e 459 Timer motorTime;
tanyuzhuo 18:e48c0910c71e 460 motorTime.start();
tanyuzhuo 18:e48c0910c71e 461 float motorPos;
tanyuzhuo 20:5bd9f9e406d1 462 //float ki = ??; // integration constant, to be tested for friction
tanyuzhuo 18:e48c0910c71e 463 Ticker motorCtrlTicker;
tanyuzhuo 18:e48c0910c71e 464 motorCtrlTicker.attach_us(&motorCtrlTick,100000);
tanyuzhuo 18:e48c0910c71e 465 while(1){
tanyuzhuo 18:e48c0910c71e 466 motorCtrlT.signal_wait(0x1);
tanyuzhuo 20:5bd9f9e406d1 467 // errorSum= 0;
tanyuzhuo 20:5bd9f9e406d1 468 // for(uint8_t i=9; i >0 ; i--){
tanyuzhuo 20:5bd9f9e406d1 469 // PrevErrorArray[i] = prevErrorArray[i-1];
tanyuzhuo 20:5bd9f9e406d1 470 // errorSum+= PrevErrorArray[i];
tanyuzhuo 21:34f440ae0227 471
tanyuzhuo 18:e48c0910c71e 472 // convert state change into rotations
tanyuzhuo 21:34f440ae0227 473 Speed = maxSpeed*6;
tanyuzhuo 20:5bd9f9e406d1 474
tanyuzhuo 18:e48c0910c71e 475 motorPos = motorPosition;
tanyuzhuo 20:5bd9f9e406d1 476 //pc.printf ("motor Pos: %f\n\r", motorPos);
tanyuzhuo 18:e48c0910c71e 477 motorVelocity = (motorPos - oldmotorPosition)/motorTime.read();
tanyuzhuo 18:e48c0910c71e 478 oldmotorPosition = motorPos;
tanyuzhuo 20:5bd9f9e406d1 479
tanyuzhuo 20:5bd9f9e406d1 480 //equation for controls
tanyuzhuo 21:34f440ae0227 481 Ts = kps*(Speed -abs(motorVelocity));//*errorSign;
tanyuzhuo 21:34f440ae0227 482
tanyuzhuo 20:5bd9f9e406d1 483 // Mp = ks*error + kd*(error - PrevError) /motorTime.read() + ki*errorSum;
tanyuzhuo 20:5bd9f9e406d1 484
tanyuzhuo 18:e48c0910c71e 485 motorTime.reset();
tanyuzhuo 18:e48c0910c71e 486 // Serial output to monitor speed and position
tanyuzhuo 18:e48c0910c71e 487 counter++;
tanyuzhuo 18:e48c0910c71e 488 if(counter == 10){
tanyuzhuo 18:e48c0910c71e 489 counter = 0;
tanyuzhuo 18:e48c0910c71e 490 //display velocity and motor position
tanyuzhuo 20:5bd9f9e406d1 491 // pc.printf ("motor Pos: %f\n\r", (motorPosition/6.0));
tanyuzhuo 21:34f440ae0227 492 putMessage(MOTOR_POS,0,(float)(motorPosition/6.0));
tanyuzhuo 20:5bd9f9e406d1 493 putMessage(MOTOR_SPEED,0,(float)(motorVelocity/6.0));
tanyuzhuo 21:34f440ae0227 494 //pc.printf ("torque: %f\n\r", Ts);
tanyuzhuo 18:e48c0910c71e 495 }
tanyuzhuo 18:e48c0910c71e 496 }
tanyuzhuo 21:34f440ae0227 497 }
peterith 13:c51d828531d5 498 int main() {
peterith 14:0481b606d10e 499 //Serial pc(SERIAL_TX, SERIAL_RX);
peterith 14:0481b606d10e 500
tanyuzhuo 17:ff5300ba5442 501 //Initialise bincoin mining and communication
tanyuzhuo 20:5bd9f9e406d1 502
tanyuzhuo 17:ff5300ba5442 503
tanyuzhuo 20:5bd9f9e406d1 504
tanyuzhuo 20:5bd9f9e406d1 505
tanyuzhuo 18:e48c0910c71e 506 //PWM.period(0.002f); //Set PWM period in seconds
tanyuzhuo 18:e48c0910c71e 507 //PWM.write(0.5); //Set PWM duty in %
tanyuzhuo 20:5bd9f9e406d1 508
peterith 14:0481b606d10e 509
tanyuzhuo 20:5bd9f9e406d1 510
tanyuzhuo 20:5bd9f9e406d1 511 commandProcessorthread.start(commandProcessor);
tanyuzhuo 20:5bd9f9e406d1 512
tanyuzhuo 20:5bd9f9e406d1 513 commsOut.start(commsOutFunc);
tanyuzhuo 20:5bd9f9e406d1 514
tanyuzhuo 20:5bd9f9e406d1 515 motorCtrlT.start(motorCtrlFn);
tanyuzhuo 20:5bd9f9e406d1 516 bitcointhread.start(bitcoin);
estott 0:de4320f74764 517
tanyuzhuo 20:5bd9f9e406d1 518 putMessage(WELCOME);
tanyuzhuo 20:5bd9f9e406d1 519 int8_t orState = motorHome();
tanyuzhuo 20:5bd9f9e406d1 520 putMessage(ROTOR_ORG);
tanyuzhuo 20:5bd9f9e406d1 521 //pc.printf("Rotor origin: %x\n\r", orState);
tanyuzhuo 20:5bd9f9e406d1 522 //Set PWM duty in %
tanyuzhuo 20:5bd9f9e406d1 523 I1.rise(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 524 I2.rise(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 525 I3.rise(&motorISR);
peterith 13:c51d828531d5 526
tanyuzhuo 20:5bd9f9e406d1 527 I1.fall(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 528 I2.fall(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 529 I3.fall(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 530
tanyuzhuo 16:10d53b056b17 531
estott 0:de4320f74764 532 }