This is probably never gonna get done

Dependencies:   Crypto

Committer:
Olaffo
Date:
Fri Mar 22 23:53:28 2019 +0000
Revision:
30:1405ab394f02
Parent:
29:cb4db90cfd63
Child:
31:6ace72e12705
final commit, hope this is working

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