This is probably never gonna get done

Dependencies:   Crypto

Committer:
Olaffo
Date:
Fri Mar 22 23:56:25 2019 +0000
Revision:
31:6ace72e12705
Parent:
30:1405ab394f02
now final;

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
estott 0:de4320f74764 29 //Mapping from sequential drive states to motor phase outputs
estott 0:de4320f74764 30 /*
estott 0:de4320f74764 31 State L1 L2 L3
estott 0:de4320f74764 32 0 H - L
estott 0:de4320f74764 33 1 - H L
estott 0:de4320f74764 34 2 L H -
estott 0:de4320f74764 35 3 L - H
estott 0:de4320f74764 36 4 - L H
estott 0:de4320f74764 37 5 H L -
estott 0:de4320f74764 38 6 - - -
estott 0:de4320f74764 39 7 - - -
estott 0:de4320f74764 40 */
estott 0:de4320f74764 41 //Drive state to output table
estott 0:de4320f74764 42 const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00};
estott 2:4e88faab6988 43
estott 0:de4320f74764 44 //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid
estott 2:4e88faab6988 45 const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07};
estott 2:4e88faab6988 46 //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 47
estott 2:4e88faab6988 48 //Phase lead to make motor spin
tanyuzhuo 24:7dd4e187dd30 49 int8_t lead = -2; //2 for forwards, -2 for backwards
estott 0:de4320f74764 50
estott 0:de4320f74764 51 //Status LED
estott 0:de4320f74764 52 DigitalOut led1(LED1);
estott 0:de4320f74764 53
estott 0:de4320f74764 54 //Photointerrupter inputs
tanyuzhuo 12:899cd6bf9844 55 InterruptIn I1(I1pin);
tanyuzhuo 12:899cd6bf9844 56 InterruptIn I2(I2pin);
tanyuzhuo 12:899cd6bf9844 57 InterruptIn I3(I3pin);
tanyuzhuo 12:899cd6bf9844 58
estott 0:de4320f74764 59 //Motor Drive outputs
tanyuzhuo 20:5bd9f9e406d1 60 DigitalOut L1L(L1Lpin);
estott 0:de4320f74764 61 DigitalOut L1H(L1Hpin);
tanyuzhuo 20:5bd9f9e406d1 62 DigitalOut L2L(L2Lpin);
estott 0:de4320f74764 63 DigitalOut L2H(L2Hpin);
tanyuzhuo 20:5bd9f9e406d1 64 DigitalOut L3L(L3Lpin);
estott 0:de4320f74764 65 DigitalOut L3H(L3Hpin);
tanyuzhuo 20:5bd9f9e406d1 66 PwmOut d9 (PWMpin);
estott 0:de4320f74764 67
peterith 13:c51d828531d5 68 int8_t orState = 0;
tanyuzhuo 17:ff5300ba5442 69 int32_t revoCounter = 0; //Counts the number of revolutions
tanyuzhuo 18:e48c0910c71e 70 int32_t motorVelocity;
Olaffo 25:ca5ccbf55b07 71 uint8_t mode = 6;
Olaffo 28:613a88b88074 72
peterith 14:0481b606d10e 73 typedef struct {
tanyuzhuo 20:5bd9f9e406d1 74 int message;
tanyuzhuo 20:5bd9f9e406d1 75 uint64_t data;
tanyuzhuo 20:5bd9f9e406d1 76 float fdata;
peterith 14:0481b606d10e 77 } mail_t;
tanyuzhuo 20:5bd9f9e406d1 78
Olaffo 29:cb4db90cfd63 79 Mail<mail_t, 16> mail_box;
Olaffo 29:cb4db90cfd63 80 Thread commsIn(osPriorityNormal2,1024);
Olaffo 29:cb4db90cfd63 81 Thread bitcointhread(osPriorityLow,1024);
Olaffo 29:cb4db90cfd63 82 Thread motorCtrlT(osPriorityHigh,1024);
Olaffo 29:cb4db90cfd63 83 Thread commsOut(osPriorityNormal1,1024);
tanyuzhuo 20:5bd9f9e406d1 84
peterith 14:0481b606d10e 85 RawSerial pc(SERIAL_TX, SERIAL_RX);
peterith 14:0481b606d10e 86 Queue<void, 8> inCharQ;
peterith 14:0481b606d10e 87 Mutex newKey_mutex;
peterith 14:0481b606d10e 88 uint64_t newKey = 0;
peterith 14:0481b606d10e 89
tanyuzhuo 18:e48c0910c71e 90 volatile float newRev;
tanyuzhuo 23:904721445e7a 91 volatile float maxSpeed = 90;
tanyuzhuo 23:904721445e7a 92 float pulseWidth;
tanyuzhuo 18:e48c0910c71e 93 float motorPosition_command;
tanyuzhuo 18:e48c0910c71e 94 float motorPosition;
Olaffo 28:613a88b88074 95
tanyuzhuo 20:5bd9f9e406d1 96 void serialISR() {
tanyuzhuo 20:5bd9f9e406d1 97 uint8_t newChar = pc.getc();
tanyuzhuo 20:5bd9f9e406d1 98 inCharQ.put((void*) newChar);
tanyuzhuo 20:5bd9f9e406d1 99 }
tanyuzhuo 20:5bd9f9e406d1 100 /***************************************************************/
tanyuzhuo 20:5bd9f9e406d1 101 //The following block should be moved to a library, but I don't the time to figure this out atm.
tanyuzhuo 20:5bd9f9e406d1 102
tanyuzhuo 20:5bd9f9e406d1 103 //CommsOut.h
tanyuzhuo 20:5bd9f9e406d1 104
tanyuzhuo 20:5bd9f9e406d1 105 enum message_keys {
Olaffo 25:ca5ccbf55b07 106 KEY_DECLINED = 0,
Olaffo 25:ca5ccbf55b07 107 ROTATION_CMD = 1,
Olaffo 25:ca5ccbf55b07 108 ROTATION_FF_CMD = 2,
Olaffo 25:ca5ccbf55b07 109 MAX_SPEED_CMD = 3,
Olaffo 25:ca5ccbf55b07 110 KEY_ECHO = 4,
Olaffo 25:ca5ccbf55b07 111 TUNE_CMD = 5,
Olaffo 25:ca5ccbf55b07 112 STANDARD = 6,
Olaffo 25:ca5ccbf55b07 113 FOREVER_FORWARD = 7,
tanyuzhuo 20:5bd9f9e406d1 114
Olaffo 25:ca5ccbf55b07 115 INVALID_CMD = 10,
Olaffo 25:ca5ccbf55b07 116 MOTOR_POS = 11,
Olaffo 25:ca5ccbf55b07 117 MOTOR_SPEED = 12,
Olaffo 30:1405ab394f02 118 HASH = 13,
tanyuzhuo 20:5bd9f9e406d1 119
tanyuzhuo 20:5bd9f9e406d1 120
Olaffo 25:ca5ccbf55b07 121 NONCE_DETECT = 14,
Olaffo 25:ca5ccbf55b07 122 ROTOR_ORG = 15,
tanyuzhuo 20:5bd9f9e406d1 123
Olaffo 29:cb4db90cfd63 124 WELCOME = 20
Olaffo 25:ca5ccbf55b07 125
Olaffo 25:ca5ccbf55b07 126
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 :
Olaffo 29:cb4db90cfd63 144 pc.printf("Not a valid key!\n\r\n\r");
tanyuzhuo 20:5bd9f9e406d1 145 break;
tanyuzhuo 20:5bd9f9e406d1 146 case ROTATION_CMD :
Olaffo 29:cb4db90cfd63 147 pc.printf("Rotation count: %3.2f\n\r\n\r", mail->fdata);
tanyuzhuo 20:5bd9f9e406d1 148 break;
Olaffo 25:ca5ccbf55b07 149 case ROTATION_FF_CMD :
Olaffo 29:cb4db90cfd63 150 pc.printf("\n\rI will rotate forever...\n\r\n\r");
tanyuzhuo 20:5bd9f9e406d1 151 break;
Olaffo 25:ca5ccbf55b07 152 case MAX_SPEED_CMD :
Olaffo 29:cb4db90cfd63 153 pc.printf("Max speed: %2.1f\n\r\n\r", mail->fdata);
Olaffo 25:ca5ccbf55b07 154 break;
tanyuzhuo 20:5bd9f9e406d1 155 case KEY_ECHO :
tanyuzhuo 20:5bd9f9e406d1 156 pc.printf("Received key: %016llx\n\r", mail->data);
Olaffo 25:ca5ccbf55b07 157 break;
Olaffo 25:ca5ccbf55b07 158 case TUNE_CMD :
Olaffo 29:cb4db90cfd63 159 pc.printf("Tune command!\n\r\n\r");
tanyuzhuo 20:5bd9f9e406d1 160 break;
tanyuzhuo 20:5bd9f9e406d1 161 case INVALID_CMD :
Olaffo 29:cb4db90cfd63 162 pc.printf("Invalid command!\r\n\n\r");
tanyuzhuo 20:5bd9f9e406d1 163 break;
tanyuzhuo 20:5bd9f9e406d1 164 case MOTOR_POS :
tanyuzhuo 20:5bd9f9e406d1 165 pc.printf("Motor position: %f\r\n", mail->fdata);
tanyuzhuo 20:5bd9f9e406d1 166 break;
tanyuzhuo 20:5bd9f9e406d1 167 case MOTOR_SPEED :
Olaffo 29:cb4db90cfd63 168 pc.printf("Motor speed: %f\r\n\n\r", mail->fdata);
tanyuzhuo 20:5bd9f9e406d1 169 break;
tanyuzhuo 20:5bd9f9e406d1 170 case NONCE_DETECT :
tanyuzhuo 20:5bd9f9e406d1 171 pc.printf("Successful nonce: %016x\n\r", mail->data);
tanyuzhuo 20:5bd9f9e406d1 172 break;
tanyuzhuo 20:5bd9f9e406d1 173 case WELCOME :
Olaffo 25:ca5ccbf55b07 174 pc.printf("Hello Olaf\n\r");
tanyuzhuo 20:5bd9f9e406d1 175 break;
tanyuzhuo 20:5bd9f9e406d1 176 case ROTOR_ORG :
tanyuzhuo 20:5bd9f9e406d1 177 pc.printf("Rotor origin: %x\n\r", orState);
Olaffo 28:613a88b88074 178 break;
Olaffo 30:1405ab394f02 179 case HASH :
Olaffo 30:1405ab394f02 180 pc.printf("Hash rate: %f\n\r", mail->data);
Olaffo 30:1405ab394f02 181 break;
tanyuzhuo 20:5bd9f9e406d1 182
tanyuzhuo 20:5bd9f9e406d1 183 }
tanyuzhuo 20:5bd9f9e406d1 184 mail_box.free(mail);
tanyuzhuo 20:5bd9f9e406d1 185 }
tanyuzhuo 20:5bd9f9e406d1 186 }
tanyuzhuo 20:5bd9f9e406d1 187 }
Olaffo 29:cb4db90cfd63 188 //End of that block
tanyuzhuo 20:5bd9f9e406d1 189 /***************************************************************/
tanyuzhuo 20:5bd9f9e406d1 190
tanyuzhuo 20:5bd9f9e406d1 191 /***************************************************************/
tanyuzhuo 20:5bd9f9e406d1 192 //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 193
tanyuzhuo 20:5bd9f9e406d1 194 //CommsIn.h
tanyuzhuo 20:5bd9f9e406d1 195
Olaffo 25:ca5ccbf55b07 196 void R_Decoder(char* command) {
tanyuzhuo 20:5bd9f9e406d1 197 int8_t sign =1;
tanyuzhuo 20:5bd9f9e406d1 198 uint8_t index = 1;
tanyuzhuo 20:5bd9f9e406d1 199 int intValue = 0;
tanyuzhuo 20:5bd9f9e406d1 200 float decimalValue = 0;
Olaffo 25:ca5ccbf55b07 201
Olaffo 25:ca5ccbf55b07 202 // Check sign
Olaffo 25:ca5ccbf55b07 203 if (command[1] == '-'){
Olaffo 25:ca5ccbf55b07 204 sign = -1;
Olaffo 25:ca5ccbf55b07 205 index++;
Olaffo 25:ca5ccbf55b07 206 }
Olaffo 25:ca5ccbf55b07 207 // Take first digit
Olaffo 25:ca5ccbf55b07 208 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
Olaffo 25:ca5ccbf55b07 209 intValue = command[index] - '0';
Olaffo 25:ca5ccbf55b07 210 index++;
Olaffo 25:ca5ccbf55b07 211 }
Olaffo 25:ca5ccbf55b07 212 else {
Olaffo 25:ca5ccbf55b07 213 putMessage(INVALID_CMD);
Olaffo 25:ca5ccbf55b07 214 return;
Olaffo 25:ca5ccbf55b07 215 }
tanyuzhuo 20:5bd9f9e406d1 216
Olaffo 25:ca5ccbf55b07 217 //Try taking 2 more digits
Olaffo 25:ca5ccbf55b07 218 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
Olaffo 25:ca5ccbf55b07 219 intValue = intValue*10 + command[index] - '0';
Olaffo 25:ca5ccbf55b07 220 index++;
Olaffo 25:ca5ccbf55b07 221 }
Olaffo 25:ca5ccbf55b07 222 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
Olaffo 25:ca5ccbf55b07 223 intValue = intValue*10 + command[index] - '0';
Olaffo 25:ca5ccbf55b07 224 index++;
Olaffo 25:ca5ccbf55b07 225 }
Olaffo 25:ca5ccbf55b07 226 //Check for '.'
Olaffo 25:ca5ccbf55b07 227 if (command[index] == '.') {
Olaffo 25:ca5ccbf55b07 228 index++;
Olaffo 25:ca5ccbf55b07 229 //Check for decimals
Olaffo 25:ca5ccbf55b07 230 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
Olaffo 25:ca5ccbf55b07 231 decimalValue = (command[index] - '0')/10;
Olaffo 25:ca5ccbf55b07 232 index++;
Olaffo 25:ca5ccbf55b07 233 }
Olaffo 25:ca5ccbf55b07 234 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
Olaffo 25:ca5ccbf55b07 235 decimalValue = (decimalValue/10 + command[index] - '0')/10;
Olaffo 25:ca5ccbf55b07 236 }
Olaffo 25:ca5ccbf55b07 237 }
Olaffo 25:ca5ccbf55b07 238 decimalValue = (decimalValue + intValue) * sign;
Olaffo 25:ca5ccbf55b07 239 if (decimalValue == 0){
Olaffo 25:ca5ccbf55b07 240 mode = FOREVER_FORWARD;
Olaffo 25:ca5ccbf55b07 241 putMessage(ROTATION_FF_CMD);
Olaffo 25:ca5ccbf55b07 242 }
Olaffo 25:ca5ccbf55b07 243 else {
Olaffo 25:ca5ccbf55b07 244 newRev = decimalValue;
Olaffo 25:ca5ccbf55b07 245 putMessage(ROTATION_CMD,0,decimalValue);
Olaffo 25:ca5ccbf55b07 246 }
Olaffo 25:ca5ccbf55b07 247 }
Olaffo 25:ca5ccbf55b07 248
Olaffo 25:ca5ccbf55b07 249 void V_Decoder(char* command) {
Olaffo 25:ca5ccbf55b07 250 uint8_t index = 1;
Olaffo 25:ca5ccbf55b07 251 int intValue = 0;
Olaffo 25:ca5ccbf55b07 252 float decimalValue = 0;
Olaffo 25:ca5ccbf55b07 253
Olaffo 25:ca5ccbf55b07 254 // Take first digit
Olaffo 25:ca5ccbf55b07 255 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
Olaffo 25:ca5ccbf55b07 256 intValue = command[index] - '0';
Olaffo 25:ca5ccbf55b07 257 index++;
Olaffo 25:ca5ccbf55b07 258 }
Olaffo 25:ca5ccbf55b07 259 else {
Olaffo 25:ca5ccbf55b07 260 putMessage(INVALID_CMD);
Olaffo 25:ca5ccbf55b07 261 return;
Olaffo 25:ca5ccbf55b07 262 }
tanyuzhuo 20:5bd9f9e406d1 263
Olaffo 25:ca5ccbf55b07 264 //Try taking 2 more digits
Olaffo 25:ca5ccbf55b07 265 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
Olaffo 25:ca5ccbf55b07 266 intValue = intValue*10 + command[index] - '0';
Olaffo 25:ca5ccbf55b07 267 index++;
Olaffo 25:ca5ccbf55b07 268 }
Olaffo 25:ca5ccbf55b07 269 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
Olaffo 25:ca5ccbf55b07 270 intValue = intValue*10 + command[index] - '0';
Olaffo 25:ca5ccbf55b07 271 index++;
Olaffo 25:ca5ccbf55b07 272 }
Olaffo 25:ca5ccbf55b07 273 //Check for '.'
Olaffo 25:ca5ccbf55b07 274 if (command[index] == '.') {
Olaffo 25:ca5ccbf55b07 275 index++;
Olaffo 25:ca5ccbf55b07 276 //Check for one decimal
Olaffo 25:ca5ccbf55b07 277 if (command[index] > ('0'-1) && command[index] < (1 + '9')) {
Olaffo 25:ca5ccbf55b07 278 decimalValue = (command[index] - '0')/10;
Olaffo 25:ca5ccbf55b07 279 }
tanyuzhuo 20:5bd9f9e406d1 280 }
Olaffo 25:ca5ccbf55b07 281 decimalValue = (decimalValue + intValue);
Olaffo 25:ca5ccbf55b07 282 maxSpeed = decimalValue;
Olaffo 25:ca5ccbf55b07 283 putMessage(MAX_SPEED_CMD,0,decimalValue);
peterith 14:0481b606d10e 284 }
peterith 14:0481b606d10e 285
peterith 14:0481b606d10e 286 void commandProcessor() {
peterith 14:0481b606d10e 287 pc.attach(&serialISR);
tanyuzhuo 17:ff5300ba5442 288 char command[19];
tanyuzhuo 17:ff5300ba5442 289 char* number;
peterith 14:0481b606d10e 290 //char k;
peterith 14:0481b606d10e 291 uint64_t receivedKey;
peterith 14:0481b606d10e 292 uint8_t index = 0;
peterith 14:0481b606d10e 293 while(1) {
peterith 14:0481b606d10e 294 osEvent newEvent = inCharQ.get();
peterith 14:0481b606d10e 295 uint8_t newChar = (uint8_t) newEvent.value.p;
peterith 14:0481b606d10e 296 command[index] = newChar;
peterith 14:0481b606d10e 297 index++;
tanyuzhuo 17:ff5300ba5442 298 if (newChar == '\r') {
peterith 14:0481b606d10e 299 command[17] = '\0';
tanyuzhuo 17:ff5300ba5442 300
tanyuzhuo 17:ff5300ba5442 301 if (command[0] == 'R') {
Olaffo 25:ca5ccbf55b07 302 R_Decoder(command);
tanyuzhuo 24:7dd4e187dd30 303 motorPosition_command = motorPosition;
tanyuzhuo 17:ff5300ba5442 304 }
tanyuzhuo 17:ff5300ba5442 305 else if (command[0] == 'V') {
Olaffo 25:ca5ccbf55b07 306 V_Decoder(command);
tanyuzhuo 17:ff5300ba5442 307 }
tanyuzhuo 17:ff5300ba5442 308 else if (command[0] == 'K') {
tanyuzhuo 17:ff5300ba5442 309 if (index == 18){ // when index is 18 means you entered K and 16 digits
tanyuzhuo 17:ff5300ba5442 310 number = command +1; //super bad solution, but I don't know how to work with strings in C
tanyuzhuo 17:ff5300ba5442 311 receivedKey = strtoull(number, NULL, 16);
tanyuzhuo 20:5bd9f9e406d1 312 putMessage(KEY_ECHO,receivedKey);
Olaffo 25:ca5ccbf55b07 313
tanyuzhuo 17:ff5300ba5442 314 newKey_mutex.lock();
tanyuzhuo 17:ff5300ba5442 315 newKey = receivedKey;
tanyuzhuo 17:ff5300ba5442 316 newKey_mutex.unlock();
tanyuzhuo 17:ff5300ba5442 317 } else {
tanyuzhuo 20:5bd9f9e406d1 318 putMessage(KEY_DECLINED);
tanyuzhuo 17:ff5300ba5442 319 };
tanyuzhuo 17:ff5300ba5442 320 }
tanyuzhuo 17:ff5300ba5442 321 else if (command[0] == 'T') {
Olaffo 25:ca5ccbf55b07 322 putMessage(TUNE_CMD);
tanyuzhuo 17:ff5300ba5442 323 }
peterith 14:0481b606d10e 324 memset(command, 0, sizeof(command));
peterith 14:0481b606d10e 325 index = 0;
Olaffo 29:cb4db90cfd63 326 } //else {
Olaffo 29:cb4db90cfd63 327 // pc.printf("Current command: %s\n\r", command); //Not sure how to go around this one cause it requires passing string
Olaffo 29:cb4db90cfd63 328 //}
peterith 14:0481b606d10e 329 }
peterith 14:0481b606d10e 330 }
tanyuzhuo 17:ff5300ba5442 331
tanyuzhuo 16:10d53b056b17 332 void bitcoin(){
tanyuzhuo 16:10d53b056b17 333 while(1) {
tanyuzhuo 16:10d53b056b17 334 SHA256 sha;
tanyuzhuo 16:10d53b056b17 335 uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,
tanyuzhuo 16:10d53b056b17 336 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73,
tanyuzhuo 16:10d53b056b17 337 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E,
tanyuzhuo 16:10d53b056b17 338 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20,
tanyuzhuo 16:10d53b056b17 339 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20,
tanyuzhuo 16:10d53b056b17 340 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20,
tanyuzhuo 16:10d53b056b17 341 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
tanyuzhuo 16:10d53b056b17 342 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
tanyuzhuo 16:10d53b056b17 343 uint64_t* key = (uint64_t*) ((int) sequence + 48);
tanyuzhuo 16:10d53b056b17 344 uint64_t* nonce = (uint64_t*) ((int) sequence + 56);
tanyuzhuo 16:10d53b056b17 345 uint8_t hash[32];
tanyuzhuo 16:10d53b056b17 346
tanyuzhuo 16:10d53b056b17 347 Timer t;
tanyuzhuo 16:10d53b056b17 348 t.start();
tanyuzhuo 16:10d53b056b17 349 unsigned currentTime = 0;
tanyuzhuo 20:5bd9f9e406d1 350 unsigned currentCount= 0;
tanyuzhuo 16:10d53b056b17 351
tanyuzhuo 16:10d53b056b17 352 for (unsigned i = 0; i <= UINT_MAX; i++) {
tanyuzhuo 16:10d53b056b17 353 (*nonce)++;
tanyuzhuo 16:10d53b056b17 354 newKey_mutex.lock();
tanyuzhuo 16:10d53b056b17 355 *key = newKey;
tanyuzhuo 16:10d53b056b17 356 newKey_mutex.unlock();
tanyuzhuo 16:10d53b056b17 357 sha.computeHash(hash, sequence, 64);
tanyuzhuo 16:10d53b056b17 358 if (hash[0] == 0 && hash[1] == 0) {
Olaffo 29:cb4db90cfd63 359 putMessage(NONCE_DETECT, *nonce);
tanyuzhuo 16:10d53b056b17 360 }
tanyuzhuo 16:10d53b056b17 361 if ((unsigned) t.read() == currentTime) {
Olaffo 30:1405ab394f02 362 putMessage(HASH, i - currentCount);
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);
peterith 14:0481b606d10e 596
Olaffo 25:ca5ccbf55b07 597 // Start all threads
Olaffo 25:ca5ccbf55b07 598 commsIn.start(commandProcessor);
tanyuzhuo 20:5bd9f9e406d1 599 commsOut.start(commsOutFunc);
tanyuzhuo 20:5bd9f9e406d1 600 motorCtrlT.start(motorCtrlFn);
tanyuzhuo 20:5bd9f9e406d1 601 bitcointhread.start(bitcoin);
estott 0:de4320f74764 602
Olaffo 25:ca5ccbf55b07 603 // Define motor ISRs
tanyuzhuo 20:5bd9f9e406d1 604 I1.rise(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 605 I2.rise(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 606 I3.rise(&motorISR);
peterith 13:c51d828531d5 607
tanyuzhuo 20:5bd9f9e406d1 608 I1.fall(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 609 I2.fall(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 610 I3.fall(&motorISR);
tanyuzhuo 20:5bd9f9e406d1 611
estott 0:de4320f74764 612 }