An embedded device

Dependencies:   Crypto

Committer:
tanyuzhuo
Date:
Wed Mar 20 17:44:06 2019 +0000
Revision:
23:904721445e7a
Parent:
21:34f440ae0227
Child:
24:7dd4e187dd30
need position control

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