This is probably never gonna get done

Dependencies:   Crypto

Committer:
Olaffo
Date:
Fri Mar 22 20:28:28 2019 +0000
Revision:
29:cb4db90cfd63
Parent:
28:613a88b88074
Child:
30:1405ab394f02
final version???

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
Olaffo 29:cb4db90cfd63 27
Olaffo 29:cb4db90cfd63 28 //Test pin
Olaffo 29:cb4db90cfd63 29 #define Test D4
Olaffo 29:cb4db90cfd63 30
estott 0:de4320f74764 31 //Mapping from sequential drive states to motor phase outputs
estott 0:de4320f74764 32 /*
estott 0:de4320f74764 33 State L1 L2 L3
estott 0:de4320f74764 34 0 H - L
estott 0:de4320f74764 35 1 - H L
estott 0:de4320f74764 36 2 L H -
estott 0:de4320f74764 37 3 L - H
estott 0:de4320f74764 38 4 - L H
estott 0:de4320f74764 39 5 H L -
estott 0:de4320f74764 40 6 - - -
estott 0:de4320f74764 41 7 - - -
estott 0:de4320f74764 42 */
estott 0:de4320f74764 43 //Drive state to output table
estott 0:de4320f74764 44 const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00};
estott 2:4e88faab6988 45
estott 0:de4320f74764 46 //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid
estott 2:4e88faab6988 47 const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07};
estott 2:4e88faab6988 48 //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 49
estott 2:4e88faab6988 50 //Phase lead to make motor spin
tanyuzhuo 24:7dd4e187dd30 51 int8_t lead = -2; //2 for forwards, -2 for backwards
estott 0:de4320f74764 52
estott 0:de4320f74764 53 //Status LED
estott 0:de4320f74764 54 DigitalOut led1(LED1);
Olaffo 29:cb4db90cfd63 55 DigitalOut TestPin(Test)
estott 0:de4320f74764 56
estott 0:de4320f74764 57 //Photointerrupter inputs
tanyuzhuo 12:899cd6bf9844 58 InterruptIn I1(I1pin);
tanyuzhuo 12:899cd6bf9844 59 InterruptIn I2(I2pin);
tanyuzhuo 12:899cd6bf9844 60 InterruptIn I3(I3pin);
tanyuzhuo 12:899cd6bf9844 61
estott 0:de4320f74764 62 //Motor Drive outputs
tanyuzhuo 20:5bd9f9e406d1 63 DigitalOut L1L(L1Lpin);
estott 0:de4320f74764 64 DigitalOut L1H(L1Hpin);
tanyuzhuo 20:5bd9f9e406d1 65 DigitalOut L2L(L2Lpin);
estott 0:de4320f74764 66 DigitalOut L2H(L2Hpin);
tanyuzhuo 20:5bd9f9e406d1 67 DigitalOut L3L(L3Lpin);
estott 0:de4320f74764 68 DigitalOut L3H(L3Hpin);
tanyuzhuo 20:5bd9f9e406d1 69 PwmOut d9 (PWMpin);
estott 0:de4320f74764 70
peterith 13:c51d828531d5 71 int8_t orState = 0;
tanyuzhuo 17:ff5300ba5442 72 int32_t revoCounter = 0; //Counts the number of revolutions
tanyuzhuo 18:e48c0910c71e 73 int32_t motorVelocity;
Olaffo 25:ca5ccbf55b07 74 uint8_t mode = 6;
Olaffo 28:613a88b88074 75
peterith 14:0481b606d10e 76 typedef struct {
tanyuzhuo 20:5bd9f9e406d1 77 int message;
tanyuzhuo 20:5bd9f9e406d1 78 uint64_t data;
tanyuzhuo 20:5bd9f9e406d1 79 float fdata;
peterith 14:0481b606d10e 80 } mail_t;
tanyuzhuo 20:5bd9f9e406d1 81
Olaffo 29:cb4db90cfd63 82 Mail<mail_t, 16> mail_box;
Olaffo 29:cb4db90cfd63 83 Thread commsIn(osPriorityNormal2,1024);
Olaffo 29:cb4db90cfd63 84 Thread bitcointhread(osPriorityLow,1024);
Olaffo 29:cb4db90cfd63 85 Thread motorCtrlT(osPriorityHigh,1024);
Olaffo 29:cb4db90cfd63 86 Thread commsOut(osPriorityNormal1,1024);
tanyuzhuo 20:5bd9f9e406d1 87
peterith 14:0481b606d10e 88 RawSerial pc(SERIAL_TX, SERIAL_RX);
peterith 14:0481b606d10e 89 Queue<void, 8> inCharQ;
peterith 14:0481b606d10e 90 Mutex newKey_mutex;
peterith 14:0481b606d10e 91 uint64_t newKey = 0;
peterith 14:0481b606d10e 92
tanyuzhuo 18:e48c0910c71e 93 volatile float newRev;
tanyuzhuo 23:904721445e7a 94 volatile float maxSpeed = 90;
tanyuzhuo 23:904721445e7a 95 float pulseWidth;
tanyuzhuo 18:e48c0910c71e 96 float motorPosition_command;
tanyuzhuo 18:e48c0910c71e 97 float motorPosition;
Olaffo 28:613a88b88074 98
tanyuzhuo 20:5bd9f9e406d1 99 void serialISR() {
tanyuzhuo 20:5bd9f9e406d1 100 uint8_t newChar = pc.getc();
tanyuzhuo 20:5bd9f9e406d1 101 inCharQ.put((void*) newChar);
tanyuzhuo 20:5bd9f9e406d1 102 }
tanyuzhuo 20:5bd9f9e406d1 103 /***************************************************************/
tanyuzhuo 20:5bd9f9e406d1 104 //The following block should be moved to a library, but I don't the time to figure this out atm.
tanyuzhuo 20:5bd9f9e406d1 105
tanyuzhuo 20:5bd9f9e406d1 106 //CommsOut.h
tanyuzhuo 20:5bd9f9e406d1 107
tanyuzhuo 20:5bd9f9e406d1 108 enum message_keys {
Olaffo 25:ca5ccbf55b07 109 KEY_DECLINED = 0,
Olaffo 25:ca5ccbf55b07 110 ROTATION_CMD = 1,
Olaffo 25:ca5ccbf55b07 111 ROTATION_FF_CMD = 2,
Olaffo 25:ca5ccbf55b07 112 MAX_SPEED_CMD = 3,
Olaffo 25:ca5ccbf55b07 113 KEY_ECHO = 4,
Olaffo 25:ca5ccbf55b07 114 TUNE_CMD = 5,
Olaffo 25:ca5ccbf55b07 115 STANDARD = 6,
Olaffo 25:ca5ccbf55b07 116 FOREVER_FORWARD = 7,
tanyuzhuo 20:5bd9f9e406d1 117
Olaffo 25:ca5ccbf55b07 118 INVALID_CMD = 10,
Olaffo 25:ca5ccbf55b07 119 MOTOR_POS = 11,
Olaffo 25:ca5ccbf55b07 120 MOTOR_SPEED = 12,
tanyuzhuo 20:5bd9f9e406d1 121
tanyuzhuo 20:5bd9f9e406d1 122
Olaffo 25:ca5ccbf55b07 123 NONCE_DETECT = 14,
Olaffo 25:ca5ccbf55b07 124 ROTOR_ORG = 15,
tanyuzhuo 20:5bd9f9e406d1 125
Olaffo 29:cb4db90cfd63 126 WELCOME = 20
Olaffo 25:ca5ccbf55b07 127
Olaffo 25:ca5ccbf55b07 128
tanyuzhuo 20:5bd9f9e406d1 129 };
tanyuzhuo 20:5bd9f9e406d1 130
tanyuzhuo 20:5bd9f9e406d1 131 void putMessage(int message, uint64_t data = 0, float fdata=0){
peterith 14:0481b606d10e 132 mail_t *mail = mail_box.alloc();
tanyuzhuo 20:5bd9f9e406d1 133 mail->message = message;
tanyuzhuo 20:5bd9f9e406d1 134 mail->data = data;
tanyuzhuo 20:5bd9f9e406d1 135 mail->fdata = fdata;
peterith 14:0481b606d10e 136 mail_box.put(mail);
peterith 14:0481b606d10e 137 }
peterith 14:0481b606d10e 138
tanyuzhuo 20:5bd9f9e406d1 139 void commsOutFunc() {
tanyuzhuo 20:5bd9f9e406d1 140 while (true) {
tanyuzhuo 20:5bd9f9e406d1 141 osEvent evt = mail_box.get();
tanyuzhuo 20:5bd9f9e406d1 142 if (evt.status == osEventMail) {
tanyuzhuo 20:5bd9f9e406d1 143 mail_t *mail = (mail_t*)evt.value.p;
tanyuzhuo 20:5bd9f9e406d1 144 switch (mail->message) {
tanyuzhuo 20:5bd9f9e406d1 145 case KEY_DECLINED :
Olaffo 29:cb4db90cfd63 146 pc.printf("Not a valid key!\n\r\n\r");
tanyuzhuo 20:5bd9f9e406d1 147 break;
tanyuzhuo 20:5bd9f9e406d1 148 case ROTATION_CMD :
Olaffo 29:cb4db90cfd63 149 pc.printf("Rotation count: %3.2f\n\r\n\r", mail->fdata);
tanyuzhuo 20:5bd9f9e406d1 150 break;
Olaffo 25:ca5ccbf55b07 151 case ROTATION_FF_CMD :
Olaffo 29:cb4db90cfd63 152 pc.printf("\n\rI will rotate forever...\n\r\n\r");
tanyuzhuo 20:5bd9f9e406d1 153 break;
Olaffo 25:ca5ccbf55b07 154 case MAX_SPEED_CMD :
Olaffo 29:cb4db90cfd63 155 pc.printf("Max speed: %2.1f\n\r\n\r", mail->fdata);
Olaffo 25:ca5ccbf55b07 156 break;
tanyuzhuo 20:5bd9f9e406d1 157 case KEY_ECHO :
tanyuzhuo 20:5bd9f9e406d1 158 pc.printf("Received key: %016llx\n\r", mail->data);
Olaffo 25:ca5ccbf55b07 159 break;
Olaffo 25:ca5ccbf55b07 160 case TUNE_CMD :
Olaffo 29:cb4db90cfd63 161 pc.printf("Tune command!\n\r\n\r");
tanyuzhuo 20:5bd9f9e406d1 162 break;
tanyuzhuo 20:5bd9f9e406d1 163 case INVALID_CMD :
Olaffo 29:cb4db90cfd63 164 pc.printf("Invalid command!\r\n\n\r");
tanyuzhuo 20:5bd9f9e406d1 165 break;
tanyuzhuo 20:5bd9f9e406d1 166 case MOTOR_POS :
tanyuzhuo 20:5bd9f9e406d1 167 pc.printf("Motor position: %f\r\n", mail->fdata);
tanyuzhuo 20:5bd9f9e406d1 168 break;
tanyuzhuo 20:5bd9f9e406d1 169 case MOTOR_SPEED :
Olaffo 29:cb4db90cfd63 170 pc.printf("Motor speed: %f\r\n\n\r", 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 :
Olaffo 25:ca5ccbf55b07 176 pc.printf("Hello Olaf\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);
Olaffo 28:613a88b88074 180 break;
tanyuzhuo 20:5bd9f9e406d1 181
tanyuzhuo 20:5bd9f9e406d1 182 }
tanyuzhuo 20:5bd9f9e406d1 183 mail_box.free(mail);
tanyuzhuo 20:5bd9f9e406d1 184 }
tanyuzhuo 20:5bd9f9e406d1 185 }
tanyuzhuo 20:5bd9f9e406d1 186 }
Olaffo 29:cb4db90cfd63 187 //End of that block
tanyuzhuo 20:5bd9f9e406d1 188 /***************************************************************/
tanyuzhuo 20:5bd9f9e406d1 189
tanyuzhuo 20:5bd9f9e406d1 190 /***************************************************************/
tanyuzhuo 20:5bd9f9e406d1 191 //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 192
tanyuzhuo 20:5bd9f9e406d1 193 //CommsIn.h
tanyuzhuo 20:5bd9f9e406d1 194
Olaffo 25:ca5ccbf55b07 195 void R_Decoder(char* command) {
tanyuzhuo 20:5bd9f9e406d1 196 int8_t sign =1;
tanyuzhuo 20:5bd9f9e406d1 197 uint8_t index = 1;
tanyuzhuo 20:5bd9f9e406d1 198 int intValue = 0;
tanyuzhuo 20:5bd9f9e406d1 199 float decimalValue = 0;
Olaffo 25:ca5ccbf55b07 200
Olaffo 25:ca5ccbf55b07 201 // Check sign
Olaffo 25:ca5ccbf55b07 202 if (command[1] == '-'){
Olaffo 25:ca5ccbf55b07 203 sign = -1;
Olaffo 25:ca5ccbf55b07 204 index++;
Olaffo 25:ca5ccbf55b07 205 }
Olaffo 25:ca5ccbf55b07 206 // Take first digit
Olaffo 25:ca5ccbf55b07 207 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
Olaffo 25:ca5ccbf55b07 208 intValue = command[index] - '0';
Olaffo 25:ca5ccbf55b07 209 index++;
Olaffo 25:ca5ccbf55b07 210 }
Olaffo 25:ca5ccbf55b07 211 else {
Olaffo 25:ca5ccbf55b07 212 putMessage(INVALID_CMD);
Olaffo 25:ca5ccbf55b07 213 return;
Olaffo 25:ca5ccbf55b07 214 }
tanyuzhuo 20:5bd9f9e406d1 215
Olaffo 25:ca5ccbf55b07 216 //Try taking 2 more digits
Olaffo 25:ca5ccbf55b07 217 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
Olaffo 25:ca5ccbf55b07 218 intValue = intValue*10 + command[index] - '0';
Olaffo 25:ca5ccbf55b07 219 index++;
Olaffo 25:ca5ccbf55b07 220 }
Olaffo 25:ca5ccbf55b07 221 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
Olaffo 25:ca5ccbf55b07 222 intValue = intValue*10 + command[index] - '0';
Olaffo 25:ca5ccbf55b07 223 index++;
Olaffo 25:ca5ccbf55b07 224 }
Olaffo 25:ca5ccbf55b07 225 //Check for '.'
Olaffo 25:ca5ccbf55b07 226 if (command[index] == '.') {
Olaffo 25:ca5ccbf55b07 227 index++;
Olaffo 25:ca5ccbf55b07 228 //Check for decimals
Olaffo 25:ca5ccbf55b07 229 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
Olaffo 25:ca5ccbf55b07 230 decimalValue = (command[index] - '0')/10;
Olaffo 25:ca5ccbf55b07 231 index++;
Olaffo 25:ca5ccbf55b07 232 }
Olaffo 25:ca5ccbf55b07 233 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
Olaffo 25:ca5ccbf55b07 234 decimalValue = (decimalValue/10 + command[index] - '0')/10;
Olaffo 25:ca5ccbf55b07 235 }
Olaffo 25:ca5ccbf55b07 236 }
Olaffo 25:ca5ccbf55b07 237 decimalValue = (decimalValue + intValue) * sign;
Olaffo 25:ca5ccbf55b07 238 if (decimalValue == 0){
Olaffo 25:ca5ccbf55b07 239 mode = FOREVER_FORWARD;
Olaffo 25:ca5ccbf55b07 240 putMessage(ROTATION_FF_CMD);
Olaffo 25:ca5ccbf55b07 241 }
Olaffo 25:ca5ccbf55b07 242 else {
Olaffo 25:ca5ccbf55b07 243 newRev = decimalValue;
Olaffo 25:ca5ccbf55b07 244 putMessage(ROTATION_CMD,0,decimalValue);
Olaffo 25:ca5ccbf55b07 245 }
Olaffo 25:ca5ccbf55b07 246 }
Olaffo 25:ca5ccbf55b07 247
Olaffo 25:ca5ccbf55b07 248 void V_Decoder(char* command) {
Olaffo 25:ca5ccbf55b07 249 uint8_t index = 1;
Olaffo 25:ca5ccbf55b07 250 int intValue = 0;
Olaffo 25:ca5ccbf55b07 251 float decimalValue = 0;
Olaffo 25:ca5ccbf55b07 252
Olaffo 25:ca5ccbf55b07 253 // Take first digit
Olaffo 25:ca5ccbf55b07 254 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
Olaffo 25:ca5ccbf55b07 255 intValue = command[index] - '0';
Olaffo 25:ca5ccbf55b07 256 index++;
Olaffo 25:ca5ccbf55b07 257 }
Olaffo 25:ca5ccbf55b07 258 else {
Olaffo 25:ca5ccbf55b07 259 putMessage(INVALID_CMD);
Olaffo 25:ca5ccbf55b07 260 return;
Olaffo 25:ca5ccbf55b07 261 }
tanyuzhuo 20:5bd9f9e406d1 262
Olaffo 25:ca5ccbf55b07 263 //Try taking 2 more digits
Olaffo 25:ca5ccbf55b07 264 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
Olaffo 25:ca5ccbf55b07 265 intValue = intValue*10 + command[index] - '0';
Olaffo 25:ca5ccbf55b07 266 index++;
Olaffo 25:ca5ccbf55b07 267 }
Olaffo 25:ca5ccbf55b07 268 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
Olaffo 25:ca5ccbf55b07 269 intValue = intValue*10 + command[index] - '0';
Olaffo 25:ca5ccbf55b07 270 index++;
Olaffo 25:ca5ccbf55b07 271 }
Olaffo 25:ca5ccbf55b07 272 //Check for '.'
Olaffo 25:ca5ccbf55b07 273 if (command[index] == '.') {
Olaffo 25:ca5ccbf55b07 274 index++;
Olaffo 25:ca5ccbf55b07 275 //Check for one decimal
Olaffo 25:ca5ccbf55b07 276 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
Olaffo 25:ca5ccbf55b07 277 decimalValue = (command[index] - '0')/10;
Olaffo 25:ca5ccbf55b07 278 }
tanyuzhuo 20:5bd9f9e406d1 279 }
Olaffo 25:ca5ccbf55b07 280 decimalValue = (decimalValue + intValue);
Olaffo 25:ca5ccbf55b07 281 maxSpeed = decimalValue;
Olaffo 25:ca5ccbf55b07 282 putMessage(MAX_SPEED_CMD,0,decimalValue);
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') {
Olaffo 25:ca5ccbf55b07 301 R_Decoder(command);
tanyuzhuo 24:7dd4e187dd30 302 motorPosition_command = motorPosition;
tanyuzhuo 17:ff5300ba5442 303 }
tanyuzhuo 17:ff5300ba5442 304 else if (command[0] == 'V') {
Olaffo 25:ca5ccbf55b07 305 V_Decoder(command);
tanyuzhuo 17:ff5300ba5442 306 }
tanyuzhuo 17:ff5300ba5442 307 else if (command[0] == 'K') {
tanyuzhuo 17:ff5300ba5442 308 if (index == 18){ // when index is 18 means you entered K and 16 digits
tanyuzhuo 17:ff5300ba5442 309 number = command +1; //super bad solution, but I don't know how to work with strings in C
tanyuzhuo 17:ff5300ba5442 310 receivedKey = strtoull(number, NULL, 16);
tanyuzhuo 20:5bd9f9e406d1 311 putMessage(KEY_ECHO,receivedKey);
Olaffo 25:ca5ccbf55b07 312
tanyuzhuo 17:ff5300ba5442 313 newKey_mutex.lock();
tanyuzhuo 17:ff5300ba5442 314 newKey = receivedKey;
tanyuzhuo 17:ff5300ba5442 315 newKey_mutex.unlock();
tanyuzhuo 17:ff5300ba5442 316 } else {
tanyuzhuo 20:5bd9f9e406d1 317 putMessage(KEY_DECLINED);
tanyuzhuo 17:ff5300ba5442 318 };
tanyuzhuo 17:ff5300ba5442 319 }
tanyuzhuo 17:ff5300ba5442 320 else if (command[0] == 'T') {
Olaffo 25:ca5ccbf55b07 321 putMessage(TUNE_CMD);
tanyuzhuo 17:ff5300ba5442 322 }
peterith 14:0481b606d10e 323 memset(command, 0, sizeof(command));
peterith 14:0481b606d10e 324 index = 0;
Olaffo 29:cb4db90cfd63 325 } //else {
Olaffo 29:cb4db90cfd63 326 // pc.printf("Current command: %s\n\r", command); //Not sure how to go around this one cause it requires passing string
Olaffo 29:cb4db90cfd63 327 //}
peterith 14:0481b606d10e 328 }
peterith 14:0481b606d10e 329 }
tanyuzhuo 17:ff5300ba5442 330
tanyuzhuo 16:10d53b056b17 331 void bitcoin(){
tanyuzhuo 16:10d53b056b17 332 while(1) {
tanyuzhuo 16:10d53b056b17 333 SHA256 sha;
tanyuzhuo 16:10d53b056b17 334 uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,
tanyuzhuo 16:10d53b056b17 335 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73,
tanyuzhuo 16:10d53b056b17 336 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E,
tanyuzhuo 16:10d53b056b17 337 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20,
tanyuzhuo 16:10d53b056b17 338 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20,
tanyuzhuo 16:10d53b056b17 339 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20,
tanyuzhuo 16:10d53b056b17 340 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
tanyuzhuo 16:10d53b056b17 341 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
tanyuzhuo 16:10d53b056b17 342 uint64_t* key = (uint64_t*) ((int) sequence + 48);
tanyuzhuo 16:10d53b056b17 343 uint64_t* nonce = (uint64_t*) ((int) sequence + 56);
tanyuzhuo 16:10d53b056b17 344 uint8_t hash[32];
tanyuzhuo 16:10d53b056b17 345
tanyuzhuo 16:10d53b056b17 346 Timer t;
tanyuzhuo 16:10d53b056b17 347 t.start();
tanyuzhuo 16:10d53b056b17 348 unsigned currentTime = 0;
tanyuzhuo 20:5bd9f9e406d1 349 unsigned currentCount= 0;
tanyuzhuo 16:10d53b056b17 350
tanyuzhuo 16:10d53b056b17 351 for (unsigned i = 0; i <= UINT_MAX; i++) {
tanyuzhuo 16:10d53b056b17 352 (*nonce)++;
tanyuzhuo 16:10d53b056b17 353 newKey_mutex.lock();
tanyuzhuo 16:10d53b056b17 354 *key = newKey;
tanyuzhuo 16:10d53b056b17 355 newKey_mutex.unlock();
tanyuzhuo 16:10d53b056b17 356 sha.computeHash(hash, sequence, 64);
tanyuzhuo 16:10d53b056b17 357 if (hash[0] == 0 && hash[1] == 0) {
Olaffo 29:cb4db90cfd63 358 putMessage(NONCE_DETECT, *nonce);
tanyuzhuo 16:10d53b056b17 359 }
tanyuzhuo 16:10d53b056b17 360 if ((unsigned) t.read() == currentTime) {
tanyuzhuo 16:10d53b056b17 361 //pc.printf("Hash rate: %d\n\r", i - currentCount);
tanyuzhuo 20:5bd9f9e406d1 362 //pc.printf("Current key: %016llx\n\r", *key);
tanyuzhuo 16:10d53b056b17 363 currentTime++;
tanyuzhuo 16:10d53b056b17 364 currentCount = i;
tanyuzhuo 16:10d53b056b17 365 }
tanyuzhuo 16:10d53b056b17 366 }
tanyuzhuo 16:10d53b056b17 367 t.stop();
tanyuzhuo 16:10d53b056b17 368 }
Olaffo 25:ca5ccbf55b07 369 }
tanyuzhuo 20:5bd9f9e406d1 370
tanyuzhuo 18:e48c0910c71e 371
tanyuzhuo 18:e48c0910c71e 372
estott 0:de4320f74764 373 //Set a given drive state
Olaffo 29:cb4db90cfd63 374 void motorOut(int8_t driveState){
estott 0:de4320f74764 375
estott 2:4e88faab6988 376 //Lookup the output byte from the drive state.
estott 2:4e88faab6988 377 int8_t driveOut = driveTable[driveState & 0x07];
estott 2:4e88faab6988 378
estott 2:4e88faab6988 379 //Turn off first
tanyuzhuo 20:5bd9f9e406d1 380 if (~driveOut & 0x01) L1L = 0;
estott 2:4e88faab6988 381 if (~driveOut & 0x02) L1H = 1;
tanyuzhuo 20:5bd9f9e406d1 382 if (~driveOut & 0x04) L2L = 0;
estott 2:4e88faab6988 383 if (~driveOut & 0x08) L2H = 1;
tanyuzhuo 20:5bd9f9e406d1 384 if (~driveOut & 0x10) L3L = 0;
estott 2:4e88faab6988 385 if (~driveOut & 0x20) L3H = 1;
estott 2:4e88faab6988 386
estott 2:4e88faab6988 387 //Then turn on
tanyuzhuo 20:5bd9f9e406d1 388 if (driveOut & 0x01) L1L = 1;
estott 2:4e88faab6988 389 if (driveOut & 0x02) L1H = 0;
tanyuzhuo 20:5bd9f9e406d1 390 if (driveOut & 0x04) L2L = 1;
estott 2:4e88faab6988 391 if (driveOut & 0x08) L2H = 0;
tanyuzhuo 20:5bd9f9e406d1 392 if (driveOut & 0x10) L3L = 1;
estott 2:4e88faab6988 393 if (driveOut & 0x20) L3H = 0;
tanyuzhuo 20:5bd9f9e406d1 394
tanyuzhuo 20:5bd9f9e406d1 395
tanyuzhuo 23:904721445e7a 396 //d9.write(1);
tanyuzhuo 23:904721445e7a 397 d9.pulsewidth_us(pulseWidth);
peterith 13:c51d828531d5 398 }
estott 0:de4320f74764 399
peterith 13:c51d828531d5 400 //Convert photointerrupter inputs to a rotor state
estott 0:de4320f74764 401 inline int8_t readRotorState(){
estott 2:4e88faab6988 402 return stateMap[I1 + 2*I2 + 4*I3];
peterith 13:c51d828531d5 403 }
estott 0:de4320f74764 404
estott 2:4e88faab6988 405 int8_t motorHome() {
tanyuzhuo 18:e48c0910c71e 406 //Put the motor in drive state 0 and wait for it to stabilize
Olaffo 29:cb4db90cfd63 407 motorOut(0);
estott 3:569b35e2a602 408 wait(2.0);
estott 2:4e88faab6988 409 return readRotorState();
estott 0:de4320f74764 410 }
peterith 13:c51d828531d5 411
tanyuzhuo 20:5bd9f9e406d1 412
tanyuzhuo 18:e48c0910c71e 413 // ISR to handle the updating of the motor position
tanyuzhuo 18:e48c0910c71e 414 void motorISR() {
tanyuzhuo 18:e48c0910c71e 415 static int8_t oldRotorState;
tanyuzhuo 20:5bd9f9e406d1 416 //orState is subtracted from future rotor state inputs to align rotor and motor states
tanyuzhuo 20:5bd9f9e406d1 417
tanyuzhuo 18:e48c0910c71e 418 int8_t rotorState = readRotorState();
Olaffo 29:cb4db90cfd63 419 motorOut((rotorState-orState+lead+6)%6); //+6 to make sure the remainder is positive
tanyuzhuo 18:e48c0910c71e 420 if (rotorState - oldRotorState == 5) motorPosition --;
tanyuzhuo 18:e48c0910c71e 421 else if (rotorState - oldRotorState == -5) motorPosition ++;
tanyuzhuo 18:e48c0910c71e 422 else motorPosition += (rotorState - oldRotorState);
Olaffo 29:cb4db90cfd63 423
tanyuzhuo 18:e48c0910c71e 424 oldRotorState = rotorState;
tanyuzhuo 18:e48c0910c71e 425 }
tanyuzhuo 18:e48c0910c71e 426 /*void push() {
peterith 13:c51d828531d5 427 intState = readRotorState();
peterith 13:c51d828531d5 428 if (intState != intStateOld) {
peterith 13:c51d828531d5 429 intStateOld = intState;
peterith 13:c51d828531d5 430 motorOut((intState - orState + lead +6) % 6); //+6 to make sure the remainder is positive
peterith 13:c51d828531d5 431 }
tanyuzhuo 18:e48c0910c71e 432 }*/
Olaffo 29:cb4db90cfd63 433
Olaffo 29:cb4db90cfd63 434
tanyuzhuo 20:5bd9f9e406d1 435 void motorCtrlTick(){
tanyuzhuo 20:5bd9f9e406d1 436 motorCtrlT.signal_set(0x1);
tanyuzhuo 20:5bd9f9e406d1 437 }
Olaffo 29:cb4db90cfd63 438
Olaffo 29:cb4db90cfd63 439 // Motor control
tanyuzhuo 18:e48c0910c71e 440 void motorCtrlFn(){
tanyuzhuo 18:e48c0910c71e 441 int32_t counter=0;
tanyuzhuo 18:e48c0910c71e 442 static int32_t oldmotorPosition;
tanyuzhuo 24:7dd4e187dd30 443 float Ts;
Olaffo 29:cb4db90cfd63 444 float Tr;
tanyuzhuo 24:7dd4e187dd30 445 float sError; //speed error
tanyuzhuo 24:7dd4e187dd30 446 float intError = 0; //integral of velError
tanyuzhuo 24:7dd4e187dd30 447 float error;
tanyuzhuo 24:7dd4e187dd30 448 float olderror = 0;
tanyuzhuo 21:34f440ae0227 449 float Speed;
tanyuzhuo 24:7dd4e187dd30 450 float Rev;
Olaffo 29:cb4db90cfd63 451 float kps = 27; //proportional constant for speed
Olaffo 29:cb4db90cfd63 452 float kis = 0.4; //integral constant for speed
Olaffo 29:cb4db90cfd63 453 float kpr = 35; //proportional constant for rotation
Olaffo 29:cb4db90cfd63 454 float kdr = 14.75; //differential constant for speed
Olaffo 29:cb4db90cfd63 455
tanyuzhuo 18:e48c0910c71e 456 // Timer to count time passed between ticks to calculate velocity
tanyuzhuo 18:e48c0910c71e 457 Timer motorTime;
tanyuzhuo 18:e48c0910c71e 458 motorTime.start();
tanyuzhuo 18:e48c0910c71e 459 float motorPos;
tanyuzhuo 24:7dd4e187dd30 460 float myTime;
tanyuzhuo 24:7dd4e187dd30 461
tanyuzhuo 18:e48c0910c71e 462 Ticker motorCtrlTicker;
tanyuzhuo 18:e48c0910c71e 463 motorCtrlTicker.attach_us(&motorCtrlTick,100000);
tanyuzhuo 18:e48c0910c71e 464 while(1){
tanyuzhuo 18:e48c0910c71e 465 motorCtrlT.signal_wait(0x1);
tanyuzhuo 24:7dd4e187dd30 466
Olaffo 25:ca5ccbf55b07 467 // Convert variabls to int
tanyuzhuo 21:34f440ae0227 468 Speed = maxSpeed*6;
tanyuzhuo 24:7dd4e187dd30 469 Rev = newRev *6;
tanyuzhuo 18:e48c0910c71e 470 motorPos = motorPosition;
tanyuzhuo 24:7dd4e187dd30 471
tanyuzhuo 24:7dd4e187dd30 472 myTime = motorTime.read();
tanyuzhuo 24:7dd4e187dd30 473 motorVelocity = (motorPos - oldmotorPosition)/myTime;
tanyuzhuo 24:7dd4e187dd30 474 error = Rev + motorPosition_command - motorPos;
tanyuzhuo 18:e48c0910c71e 475 oldmotorPosition = motorPos;
tanyuzhuo 20:5bd9f9e406d1 476
tanyuzhuo 20:5bd9f9e406d1 477 //equation for controls
tanyuzhuo 24:7dd4e187dd30 478 sError = Speed -abs(motorVelocity);
Olaffo 29:cb4db90cfd63 479 if ((motorVelocity != 0) && (abs(sError)<300)){
Olaffo 29:cb4db90cfd63 480 intError = intError + sError;
Olaffo 29:cb4db90cfd63 481 if (abs(intError*kis)>2000) {
Olaffo 29:cb4db90cfd63 482 intError = intError - sError;
Olaffo 29:cb4db90cfd63 483 }
tanyuzhuo 24:7dd4e187dd30 484 }
Olaffo 25:ca5ccbf55b07 485 Ts = (kps * sError + kis * intError)*(error/abs(error));
Olaffo 25:ca5ccbf55b07 486 Tr = kpr*error+kdr*(error-olderror)/ myTime;
Olaffo 25:ca5ccbf55b07 487
Olaffo 25:ca5ccbf55b07 488 // Speed AND rotation control (aka standard mode)
Olaffo 29:cb4db90cfd63 489 if (mode == STANDARD)
Olaffo 25:ca5ccbf55b07 490 {
Olaffo 29:cb4db90cfd63 491 // Case for forward rotation
Olaffo 25:ca5ccbf55b07 492 if (error < 0){
Olaffo 25:ca5ccbf55b07 493
Olaffo 25:ca5ccbf55b07 494 // Case for choosing rotational control
Olaffo 25:ca5ccbf55b07 495 if (Ts <=Tr ){
Olaffo 25:ca5ccbf55b07 496
Olaffo 25:ca5ccbf55b07 497 // Flip lead
Olaffo 25:ca5ccbf55b07 498 if ( Tr >= 0) { lead = -2; }
Olaffo 25:ca5ccbf55b07 499 else if ( Tr < 0) { lead = 2; }
Olaffo 25:ca5ccbf55b07 500
Olaffo 25:ca5ccbf55b07 501 // Assure torque is in bounds
Olaffo 25:ca5ccbf55b07 502 if (Tr > 2000){ Tr = 2000; }
Olaffo 25:ca5ccbf55b07 503 else if (Tr < 0){ Tr = -Tr; }
Olaffo 25:ca5ccbf55b07 504 else if (Tr < -2000){ Tr = 2000; }
Olaffo 25:ca5ccbf55b07 505
Olaffo 25:ca5ccbf55b07 506 // Pass torque
Olaffo 25:ca5ccbf55b07 507 pulseWidth = Tr;
Olaffo 25:ca5ccbf55b07 508 }
Olaffo 25:ca5ccbf55b07 509
Olaffo 25:ca5ccbf55b07 510 // Case for choosing speed control
Olaffo 25:ca5ccbf55b07 511 else {
Olaffo 25:ca5ccbf55b07 512 // Flip lead
Olaffo 25:ca5ccbf55b07 513 if ( Ts >= 0) { lead = -2; }
Olaffo 25:ca5ccbf55b07 514 else if ( Ts < 0) { lead = 2; }
Olaffo 25:ca5ccbf55b07 515
Olaffo 25:ca5ccbf55b07 516 // Assure torque is in bounds
Olaffo 25:ca5ccbf55b07 517 if (Ts > 2000){ Ts = 2000; }
Olaffo 25:ca5ccbf55b07 518 else if (Ts < 0){ Ts = -Ts; }
Olaffo 25:ca5ccbf55b07 519 else if (Ts < -2000){ Ts = 2000; }
Olaffo 25:ca5ccbf55b07 520
Olaffo 25:ca5ccbf55b07 521 // Pass torque
Olaffo 25:ca5ccbf55b07 522 pulseWidth = Ts;
Olaffo 25:ca5ccbf55b07 523 }
Olaffo 25:ca5ccbf55b07 524 }
Olaffo 25:ca5ccbf55b07 525
Olaffo 29:cb4db90cfd63 526 // Case for reverse rotation
Olaffo 25:ca5ccbf55b07 527 else if (error >= 0){
Olaffo 25:ca5ccbf55b07 528
Olaffo 25:ca5ccbf55b07 529 // Case for choosing speed control
Olaffo 25:ca5ccbf55b07 530 if (Ts <=Tr ){
Olaffo 25:ca5ccbf55b07 531 // Flip lead
Olaffo 25:ca5ccbf55b07 532 if ( Ts >= 0) { lead = -2; }
Olaffo 25:ca5ccbf55b07 533 else if ( Ts < 0) { lead = 2; }
Olaffo 25:ca5ccbf55b07 534
Olaffo 25:ca5ccbf55b07 535 // Assure torque is in bounds
Olaffo 25:ca5ccbf55b07 536 if (Ts > 2000){ Ts = 2000; }
Olaffo 25:ca5ccbf55b07 537 else if (Ts < 0){ Ts = -Ts; }
Olaffo 25:ca5ccbf55b07 538 else if (Ts < -2000){ Ts = 2000; }
Olaffo 25:ca5ccbf55b07 539
Olaffo 25:ca5ccbf55b07 540 // Pass torque
Olaffo 25:ca5ccbf55b07 541 pulseWidth = Ts;
Olaffo 25:ca5ccbf55b07 542 }
Olaffo 25:ca5ccbf55b07 543
Olaffo 25:ca5ccbf55b07 544 // Case for rotational speed control
Olaffo 25:ca5ccbf55b07 545 else {
Olaffo 25:ca5ccbf55b07 546
Olaffo 25:ca5ccbf55b07 547 // Flip lead
Olaffo 25:ca5ccbf55b07 548 if ( Tr >= 0) { lead = -2; }
Olaffo 25:ca5ccbf55b07 549 else if ( Tr < 0) { lead = 2; }
Olaffo 25:ca5ccbf55b07 550
Olaffo 25:ca5ccbf55b07 551 // Assure torque is in bounds
Olaffo 25:ca5ccbf55b07 552 if (Tr > 2000){ Tr = 2000; }
Olaffo 25:ca5ccbf55b07 553 else if (Tr < 0){ Tr = -Tr; }
Olaffo 25:ca5ccbf55b07 554 else if (Tr < -2000){ Tr = 2000; }
Olaffo 25:ca5ccbf55b07 555
Olaffo 25:ca5ccbf55b07 556 // Pass torque
Olaffo 25:ca5ccbf55b07 557 pulseWidth = Tr;
Olaffo 25:ca5ccbf55b07 558 }
Olaffo 25:ca5ccbf55b07 559 }
tanyuzhuo 24:7dd4e187dd30 560 }
tanyuzhuo 24:7dd4e187dd30 561
Olaffo 25:ca5ccbf55b07 562
Olaffo 25:ca5ccbf55b07 563 // ONLY speed control (aka forever forward mode)
Olaffo 28:613a88b88074 564 else if (mode == FOREVER_FORWARD){
Olaffo 25:ca5ccbf55b07 565
Olaffo 29:cb4db90cfd63 566 if ( Ts >= 0) { lead = 2; }
Olaffo 29:cb4db90cfd63 567 else if ( Ts < 0) { lead = -2; }
Olaffo 25:ca5ccbf55b07 568
Olaffo 25:ca5ccbf55b07 569 // Assure torque is in bounds
Olaffo 25:ca5ccbf55b07 570 if (Ts > 2000){ Ts = 2000; }
Olaffo 25:ca5ccbf55b07 571 else if (Ts < 0){ Ts = -Ts; }
Olaffo 25:ca5ccbf55b07 572 else if (Ts < -2000){ Ts = 2000; }
Olaffo 25:ca5ccbf55b07 573
tanyuzhuo 24:7dd4e187dd30 574 pulseWidth = Ts;
tanyuzhuo 24:7dd4e187dd30 575 }
tanyuzhuo 24:7dd4e187dd30 576
Olaffo 25:ca5ccbf55b07 577 counter++;
tanyuzhuo 18:e48c0910c71e 578 motorTime.reset();
tanyuzhuo 18:e48c0910c71e 579 // Serial output to monitor speed and position
tanyuzhuo 18:e48c0910c71e 580 if(counter == 10){
tanyuzhuo 18:e48c0910c71e 581 counter = 0;
tanyuzhuo 18:e48c0910c71e 582 //display velocity and motor position
tanyuzhuo 21:34f440ae0227 583 putMessage(MOTOR_POS,0,(float)(motorPosition/6.0));
tanyuzhuo 20:5bd9f9e406d1 584 putMessage(MOTOR_SPEED,0,(float)(motorVelocity/6.0));
tanyuzhuo 18:e48c0910c71e 585 }
tanyuzhuo 24:7dd4e187dd30 586 olderror=error;
tanyuzhuo 18:e48c0910c71e 587 }
tanyuzhuo 21:34f440ae0227 588 }
peterith 13:c51d828531d5 589 int main() {
tanyuzhuo 20:5bd9f9e406d1 590
Olaffo 25:ca5ccbf55b07 591 // Start up checkup
tanyuzhuo 23:904721445e7a 592 d9.period(0.002f); //Set PWM period in seconds
Olaffo 25:ca5ccbf55b07 593 int8_t orState = motorHome();
Olaffo 25:ca5ccbf55b07 594 putMessage(WELCOME);
Olaffo 25:ca5ccbf55b07 595 putMessage(ROTOR_ORG);
Olaffo 29:cb4db90cfd63 596 TestPin = 0;
peterith 14:0481b606d10e 597
Olaffo 25:ca5ccbf55b07 598 // Start all threads
Olaffo 25:ca5ccbf55b07 599 commsIn.start(commandProcessor);
tanyuzhuo 20:5bd9f9e406d1 600 commsOut.start(commsOutFunc);
tanyuzhuo 20:5bd9f9e406d1 601 motorCtrlT.start(motorCtrlFn);
tanyuzhuo 20:5bd9f9e406d1 602 bitcointhread.start(bitcoin);
estott 0:de4320f74764 603
Olaffo 25:ca5ccbf55b07 604 // Define motor ISRs
tanyuzhuo 20:5bd9f9e406d1 605 I1.rise(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 606 I2.rise(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 607 I3.rise(&motorISR);
peterith 13:c51d828531d5 608
tanyuzhuo 20:5bd9f9e406d1 609 I1.fall(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 610 I2.fall(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 611 I3.fall(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 612
estott 0:de4320f74764 613 }