An embedded device
Dependencies: Crypto
main.cpp@25:ca5ccbf55b07, 2019-03-21 (annotated)
- Committer:
- Olaffo
- Date:
- Thu Mar 21 16:53:23 2019 +0000
- Revision:
- 25:ca5ccbf55b07
- Parent:
- 24:7dd4e187dd30
- Child:
- 26:d3e69e507616
fixed motor control, code cleaned up
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
estott | 0:de4320f74764 | 1 | #include "mbed.h" |
peterith | 13:c51d828531d5 | 2 | #include "Crypto.h" |
estott | 0:de4320f74764 | 3 | |
estott | 0:de4320f74764 | 4 | //Photointerrupter input pins |
estott | 10:a4b5723b6c9d | 5 | #define I1pin D3 |
estott | 10:a4b5723b6c9d | 6 | #define I2pin D6 |
estott | 10:a4b5723b6c9d | 7 | #define I3pin D5 |
estott | 2:4e88faab6988 | 8 | |
estott | 2:4e88faab6988 | 9 | //Incremental encoder input pins |
peterith | 13:c51d828531d5 | 10 | #define CHApin D12 |
peterith | 13:c51d828531d5 | 11 | #define CHBpin D11 |
estott | 0:de4320f74764 | 12 | |
estott | 0:de4320f74764 | 13 | //Motor Drive output pins //Mask in output byte |
estott | 10:a4b5723b6c9d | 14 | #define L1Lpin D1 //0x01 |
estott | 10:a4b5723b6c9d | 15 | #define L1Hpin A3 //0x02 |
estott | 10:a4b5723b6c9d | 16 | #define L2Lpin D0 //0x04 |
peterith | 13:c51d828531d5 | 17 | #define L2Hpin A6 //0x08 |
peterith | 13:c51d828531d5 | 18 | #define L3Lpin D10 //0x10 |
peterith | 13:c51d828531d5 | 19 | #define L3Hpin D2 //0x20 |
estott | 10:a4b5723b6c9d | 20 | |
estott | 10:a4b5723b6c9d | 21 | #define PWMpin D9 |
estott | 5:08f338b5e4d9 | 22 | |
estott | 5:08f338b5e4d9 | 23 | //Motor current sense |
peterith | 13:c51d828531d5 | 24 | #define MCSPpin A1 |
peterith | 13:c51d828531d5 | 25 | #define MCSNpin A0 |
estott | 0:de4320f74764 | 26 | |
estott | 0:de4320f74764 | 27 | //Mapping from sequential drive states to motor phase outputs |
estott | 0:de4320f74764 | 28 | /* |
estott | 0:de4320f74764 | 29 | State L1 L2 L3 |
estott | 0:de4320f74764 | 30 | 0 H - L |
estott | 0:de4320f74764 | 31 | 1 - H L |
estott | 0:de4320f74764 | 32 | 2 L H - |
estott | 0:de4320f74764 | 33 | 3 L - H |
estott | 0:de4320f74764 | 34 | 4 - L H |
estott | 0:de4320f74764 | 35 | 5 H L - |
estott | 0:de4320f74764 | 36 | 6 - - - |
estott | 0:de4320f74764 | 37 | 7 - - - |
estott | 0:de4320f74764 | 38 | */ |
estott | 0:de4320f74764 | 39 | //Drive state to output table |
estott | 0:de4320f74764 | 40 | const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00}; |
estott | 2:4e88faab6988 | 41 | |
estott | 0:de4320f74764 | 42 | //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid |
estott | 2:4e88faab6988 | 43 | const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07}; |
estott | 2:4e88faab6988 | 44 | //const int8_t stateMap[] = {0x07,0x01,0x03,0x02,0x05,0x00,0x04,0x07}; //Alternative if phase order of input or drive is reversed |
estott | 2:4e88faab6988 | 45 | |
estott | 2:4e88faab6988 | 46 | //Phase lead to make motor spin |
tanyuzhuo | 24:7dd4e187dd30 | 47 | int8_t lead = -2; //2 for forwards, -2 for backwards |
estott | 0:de4320f74764 | 48 | |
estott | 0:de4320f74764 | 49 | //Status LED |
estott | 0:de4320f74764 | 50 | DigitalOut led1(LED1); |
estott | 0:de4320f74764 | 51 | |
estott | 0:de4320f74764 | 52 | //Photointerrupter inputs |
tanyuzhuo | 12:899cd6bf9844 | 53 | InterruptIn I1(I1pin); |
tanyuzhuo | 12:899cd6bf9844 | 54 | InterruptIn I2(I2pin); |
tanyuzhuo | 12:899cd6bf9844 | 55 | InterruptIn I3(I3pin); |
tanyuzhuo | 12:899cd6bf9844 | 56 | |
estott | 0:de4320f74764 | 57 | //Motor Drive outputs |
tanyuzhuo | 20:5bd9f9e406d1 | 58 | DigitalOut L1L(L1Lpin); |
estott | 0:de4320f74764 | 59 | DigitalOut L1H(L1Hpin); |
tanyuzhuo | 20:5bd9f9e406d1 | 60 | DigitalOut L2L(L2Lpin); |
estott | 0:de4320f74764 | 61 | DigitalOut L2H(L2Hpin); |
tanyuzhuo | 20:5bd9f9e406d1 | 62 | DigitalOut L3L(L3Lpin); |
estott | 0:de4320f74764 | 63 | DigitalOut L3H(L3Hpin); |
tanyuzhuo | 20:5bd9f9e406d1 | 64 | PwmOut d9 (PWMpin); |
estott | 0:de4320f74764 | 65 | |
peterith | 13:c51d828531d5 | 66 | int8_t orState = 0; |
tanyuzhuo | 20:5bd9f9e406d1 | 67 | //int8_t intState = 0; |
tanyuzhuo | 20:5bd9f9e406d1 | 68 | //int8_t intStateOld = 0; |
tanyuzhuo | 17:ff5300ba5442 | 69 | int32_t revoCounter = 0; //Counts the number of revolutions |
tanyuzhuo | 18:e48c0910c71e | 70 | int32_t motorVelocity; |
Olaffo | 25:ca5ccbf55b07 | 71 | uint8_t mode = 6; |
tanyuzhuo | 18:e48c0910c71e | 72 | //Phase lead to make motor spin |
tanyuzhuo | 20:5bd9f9e406d1 | 73 | //int8_t lead = -2; //2 for forwards, -2 for backwards |
tanyuzhuo | 12:899cd6bf9844 | 74 | |
peterith | 14:0481b606d10e | 75 | typedef struct { |
tanyuzhuo | 20:5bd9f9e406d1 | 76 | int message; |
tanyuzhuo | 20:5bd9f9e406d1 | 77 | uint64_t data; |
tanyuzhuo | 20:5bd9f9e406d1 | 78 | float fdata; |
peterith | 14:0481b606d10e | 79 | } mail_t; |
tanyuzhuo | 20:5bd9f9e406d1 | 80 | |
peterith | 14:0481b606d10e | 81 | Mail<mail_t, 16> mail_box; |
Olaffo | 25:ca5ccbf55b07 | 82 | Thread commsIn(osPriorityNormal,1024); |
tanyuzhuo | 20:5bd9f9e406d1 | 83 | Thread bitcointhread(osPriorityNormal,1024); |
tanyuzhuo | 20:5bd9f9e406d1 | 84 | Thread motorCtrlT(osPriorityNormal,1024); |
tanyuzhuo | 20:5bd9f9e406d1 | 85 | Thread commsOut(osPriorityNormal,1024); |
tanyuzhuo | 20:5bd9f9e406d1 | 86 | |
peterith | 14:0481b606d10e | 87 | RawSerial pc(SERIAL_TX, SERIAL_RX); |
peterith | 14:0481b606d10e | 88 | Queue<void, 8> inCharQ; |
peterith | 14:0481b606d10e | 89 | Mutex newKey_mutex; |
peterith | 14:0481b606d10e | 90 | uint64_t newKey = 0; |
peterith | 14:0481b606d10e | 91 | |
tanyuzhuo | 18:e48c0910c71e | 92 | volatile float newRev; |
tanyuzhuo | 23:904721445e7a | 93 | volatile float maxSpeed = 90; |
tanyuzhuo | 23:904721445e7a | 94 | float pulseWidth; |
tanyuzhuo | 18:e48c0910c71e | 95 | float motorPosition_command; |
tanyuzhuo | 18:e48c0910c71e | 96 | float motorPosition; |
tanyuzhuo | 23:904721445e7a | 97 | //float pulseWidth; |
tanyuzhuo | 18:e48c0910c71e | 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, |
Olaffo | 25:ca5ccbf55b07 | 117 | LEAD = 9, |
tanyuzhuo | 20:5bd9f9e406d1 | 118 | |
tanyuzhuo | 20:5bd9f9e406d1 | 119 | |
Olaffo | 25:ca5ccbf55b07 | 120 | INVALID_CMD = 10, |
Olaffo | 25:ca5ccbf55b07 | 121 | MOTOR_POS = 11, |
Olaffo | 25:ca5ccbf55b07 | 122 | MOTOR_SPEED = 12, |
tanyuzhuo | 20:5bd9f9e406d1 | 123 | |
tanyuzhuo | 20:5bd9f9e406d1 | 124 | |
Olaffo | 25:ca5ccbf55b07 | 125 | NONCE_DETECT = 14, |
Olaffo | 25:ca5ccbf55b07 | 126 | ROTOR_ORG = 15, |
tanyuzhuo | 20:5bd9f9e406d1 | 127 | |
Olaffo | 25:ca5ccbf55b07 | 128 | WELCOME = 20 |
Olaffo | 25:ca5ccbf55b07 | 129 | |
Olaffo | 25:ca5ccbf55b07 | 130 | |
tanyuzhuo | 20:5bd9f9e406d1 | 131 | }; |
tanyuzhuo | 20:5bd9f9e406d1 | 132 | |
tanyuzhuo | 20:5bd9f9e406d1 | 133 | void putMessage(int message, uint64_t data = 0, float fdata=0){ |
peterith | 14:0481b606d10e | 134 | mail_t *mail = mail_box.alloc(); |
tanyuzhuo | 20:5bd9f9e406d1 | 135 | mail->message = message; |
tanyuzhuo | 20:5bd9f9e406d1 | 136 | mail->data = data; |
tanyuzhuo | 20:5bd9f9e406d1 | 137 | mail->fdata = fdata; |
peterith | 14:0481b606d10e | 138 | mail_box.put(mail); |
peterith | 14:0481b606d10e | 139 | } |
peterith | 14:0481b606d10e | 140 | |
tanyuzhuo | 20:5bd9f9e406d1 | 141 | void commsOutFunc() { |
tanyuzhuo | 20:5bd9f9e406d1 | 142 | while (true) { |
tanyuzhuo | 20:5bd9f9e406d1 | 143 | osEvent evt = mail_box.get(); |
tanyuzhuo | 20:5bd9f9e406d1 | 144 | if (evt.status == osEventMail) { |
tanyuzhuo | 20:5bd9f9e406d1 | 145 | mail_t *mail = (mail_t*)evt.value.p; |
tanyuzhuo | 20:5bd9f9e406d1 | 146 | switch (mail->message) { |
tanyuzhuo | 20:5bd9f9e406d1 | 147 | case KEY_DECLINED : |
tanyuzhuo | 20:5bd9f9e406d1 | 148 | pc.printf("Not a valid key!\n\r"); |
tanyuzhuo | 20:5bd9f9e406d1 | 149 | break; |
tanyuzhuo | 20:5bd9f9e406d1 | 150 | case ROTATION_CMD : |
tanyuzhuo | 24:7dd4e187dd30 | 151 | pc.printf("Rotation count: %3.2f\n\r", mail->fdata); |
tanyuzhuo | 20:5bd9f9e406d1 | 152 | break; |
Olaffo | 25:ca5ccbf55b07 | 153 | case ROTATION_FF_CMD : |
Olaffo | 25:ca5ccbf55b07 | 154 | pc.printf("I will rotate forever..."); |
tanyuzhuo | 20:5bd9f9e406d1 | 155 | break; |
Olaffo | 25:ca5ccbf55b07 | 156 | case MAX_SPEED_CMD : |
Olaffo | 25:ca5ccbf55b07 | 157 | pc.printf("Max speed: %2.1f\n\r", mail->fdata); |
Olaffo | 25:ca5ccbf55b07 | 158 | break; |
tanyuzhuo | 20:5bd9f9e406d1 | 159 | case KEY_ECHO : |
tanyuzhuo | 20:5bd9f9e406d1 | 160 | pc.printf("Received key: %016llx\n\r", mail->data); |
Olaffo | 25:ca5ccbf55b07 | 161 | break; |
Olaffo | 25:ca5ccbf55b07 | 162 | case TUNE_CMD : |
Olaffo | 25:ca5ccbf55b07 | 163 | pc.printf("Tune command!\n\r"); |
tanyuzhuo | 20:5bd9f9e406d1 | 164 | break; |
tanyuzhuo | 20:5bd9f9e406d1 | 165 | case INVALID_CMD : |
tanyuzhuo | 20:5bd9f9e406d1 | 166 | pc.printf("Invalid command!\r\n"); |
tanyuzhuo | 20:5bd9f9e406d1 | 167 | break; |
tanyuzhuo | 20:5bd9f9e406d1 | 168 | case MOTOR_POS : |
tanyuzhuo | 20:5bd9f9e406d1 | 169 | pc.printf("Motor position: %f\r\n", mail->fdata); |
Olaffo | 25:ca5ccbf55b07 | 170 | //pc.printf("{TIMEPLOT|%.2f|Motor Position}", mail->fdata); |
tanyuzhuo | 20:5bd9f9e406d1 | 171 | break; |
tanyuzhuo | 20:5bd9f9e406d1 | 172 | case MOTOR_SPEED : |
tanyuzhuo | 20:5bd9f9e406d1 | 173 | pc.printf("Motor speed: %f\r\n", mail->fdata); |
tanyuzhuo | 20:5bd9f9e406d1 | 174 | break; |
tanyuzhuo | 20:5bd9f9e406d1 | 175 | case NONCE_DETECT : |
tanyuzhuo | 20:5bd9f9e406d1 | 176 | pc.printf("Successful nonce: %016x\n\r", mail->data); |
tanyuzhuo | 20:5bd9f9e406d1 | 177 | break; |
tanyuzhuo | 20:5bd9f9e406d1 | 178 | case WELCOME : |
Olaffo | 25:ca5ccbf55b07 | 179 | pc.printf("Hello Olaf\n\r"); |
tanyuzhuo | 20:5bd9f9e406d1 | 180 | break; |
tanyuzhuo | 20:5bd9f9e406d1 | 181 | case ROTOR_ORG : |
tanyuzhuo | 20:5bd9f9e406d1 | 182 | pc.printf("Rotor origin: %x\n\r", orState); |
Olaffo | 25:ca5ccbf55b07 | 183 | case LEAD : |
Olaffo | 25:ca5ccbf55b07 | 184 | pc.printf("Lead: %d\n\r\n\r", mail->data); |
Olaffo | 25:ca5ccbf55b07 | 185 | break; |
tanyuzhuo | 20:5bd9f9e406d1 | 186 | |
tanyuzhuo | 20:5bd9f9e406d1 | 187 | } |
tanyuzhuo | 20:5bd9f9e406d1 | 188 | mail_box.free(mail); |
tanyuzhuo | 20:5bd9f9e406d1 | 189 | } |
tanyuzhuo | 20:5bd9f9e406d1 | 190 | } |
tanyuzhuo | 20:5bd9f9e406d1 | 191 | } |
tanyuzhuo | 20:5bd9f9e406d1 | 192 | //End of that block |
tanyuzhuo | 20:5bd9f9e406d1 | 193 | /***************************************************************/ |
tanyuzhuo | 20:5bd9f9e406d1 | 194 | |
tanyuzhuo | 20:5bd9f9e406d1 | 195 | /***************************************************************/ |
tanyuzhuo | 20:5bd9f9e406d1 | 196 | //The following block should also be moved to a library, but I still don't the time to figure this out atm. |
tanyuzhuo | 20:5bd9f9e406d1 | 197 | |
tanyuzhuo | 20:5bd9f9e406d1 | 198 | //CommsIn.h |
tanyuzhuo | 20:5bd9f9e406d1 | 199 | |
Olaffo | 25:ca5ccbf55b07 | 200 | void R_Decoder(char* command) { |
tanyuzhuo | 20:5bd9f9e406d1 | 201 | int8_t sign =1; |
tanyuzhuo | 20:5bd9f9e406d1 | 202 | uint8_t index = 1; |
tanyuzhuo | 20:5bd9f9e406d1 | 203 | int intValue = 0; |
tanyuzhuo | 20:5bd9f9e406d1 | 204 | float decimalValue = 0; |
Olaffo | 25:ca5ccbf55b07 | 205 | |
Olaffo | 25:ca5ccbf55b07 | 206 | // Check sign |
Olaffo | 25:ca5ccbf55b07 | 207 | if (command[1] == '-'){ |
Olaffo | 25:ca5ccbf55b07 | 208 | sign = -1; |
Olaffo | 25:ca5ccbf55b07 | 209 | index++; |
Olaffo | 25:ca5ccbf55b07 | 210 | } |
Olaffo | 25:ca5ccbf55b07 | 211 | // Take first digit |
Olaffo | 25:ca5ccbf55b07 | 212 | if (command[index] > ('0'-1) && command[index] < (1 + '9')) { |
Olaffo | 25:ca5ccbf55b07 | 213 | intValue = command[index] - '0'; |
Olaffo | 25:ca5ccbf55b07 | 214 | index++; |
Olaffo | 25:ca5ccbf55b07 | 215 | } |
Olaffo | 25:ca5ccbf55b07 | 216 | else { |
Olaffo | 25:ca5ccbf55b07 | 217 | putMessage(INVALID_CMD); |
Olaffo | 25:ca5ccbf55b07 | 218 | return; |
Olaffo | 25:ca5ccbf55b07 | 219 | } |
tanyuzhuo | 20:5bd9f9e406d1 | 220 | |
Olaffo | 25:ca5ccbf55b07 | 221 | //Try taking 2 more digits |
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 | if (command[index] > ('0'-1) && command[index] < (1 + '9')) { |
Olaffo | 25:ca5ccbf55b07 | 227 | intValue = intValue*10 + command[index] - '0'; |
Olaffo | 25:ca5ccbf55b07 | 228 | index++; |
Olaffo | 25:ca5ccbf55b07 | 229 | } |
Olaffo | 25:ca5ccbf55b07 | 230 | //Check for '.' |
Olaffo | 25:ca5ccbf55b07 | 231 | if (command[index] == '.') { |
Olaffo | 25:ca5ccbf55b07 | 232 | index++; |
Olaffo | 25:ca5ccbf55b07 | 233 | //Check for decimals |
Olaffo | 25:ca5ccbf55b07 | 234 | if (command[index] > ('0'-1) && command[index] < (1 + '9')) { |
Olaffo | 25:ca5ccbf55b07 | 235 | decimalValue = (command[index] - '0')/10; |
Olaffo | 25:ca5ccbf55b07 | 236 | index++; |
Olaffo | 25:ca5ccbf55b07 | 237 | } |
Olaffo | 25:ca5ccbf55b07 | 238 | if (command[index] > ('0'-1) && command[index] < (1 + '9')) { |
Olaffo | 25:ca5ccbf55b07 | 239 | decimalValue = (decimalValue/10 + command[index] - '0')/10; |
Olaffo | 25:ca5ccbf55b07 | 240 | } |
Olaffo | 25:ca5ccbf55b07 | 241 | } |
Olaffo | 25:ca5ccbf55b07 | 242 | decimalValue = (decimalValue + intValue) * sign; |
Olaffo | 25:ca5ccbf55b07 | 243 | if (decimalValue == 0){ |
Olaffo | 25:ca5ccbf55b07 | 244 | mode = FOREVER_FORWARD; |
Olaffo | 25:ca5ccbf55b07 | 245 | putMessage(ROTATION_FF_CMD); |
Olaffo | 25:ca5ccbf55b07 | 246 | } |
Olaffo | 25:ca5ccbf55b07 | 247 | else { |
Olaffo | 25:ca5ccbf55b07 | 248 | newRev = decimalValue; |
Olaffo | 25:ca5ccbf55b07 | 249 | putMessage(ROTATION_CMD,0,decimalValue); |
Olaffo | 25:ca5ccbf55b07 | 250 | } |
Olaffo | 25:ca5ccbf55b07 | 251 | } |
Olaffo | 25:ca5ccbf55b07 | 252 | |
Olaffo | 25:ca5ccbf55b07 | 253 | void V_Decoder(char* command) { |
Olaffo | 25:ca5ccbf55b07 | 254 | uint8_t index = 1; |
Olaffo | 25:ca5ccbf55b07 | 255 | int intValue = 0; |
Olaffo | 25:ca5ccbf55b07 | 256 | float decimalValue = 0; |
Olaffo | 25:ca5ccbf55b07 | 257 | |
Olaffo | 25:ca5ccbf55b07 | 258 | // Take first digit |
Olaffo | 25:ca5ccbf55b07 | 259 | if (command[index] > ('0'-1) && command[index] < (1 + '9')) { |
Olaffo | 25:ca5ccbf55b07 | 260 | intValue = command[index] - '0'; |
Olaffo | 25:ca5ccbf55b07 | 261 | index++; |
Olaffo | 25:ca5ccbf55b07 | 262 | } |
Olaffo | 25:ca5ccbf55b07 | 263 | else { |
Olaffo | 25:ca5ccbf55b07 | 264 | putMessage(INVALID_CMD); |
Olaffo | 25:ca5ccbf55b07 | 265 | return; |
Olaffo | 25:ca5ccbf55b07 | 266 | } |
tanyuzhuo | 20:5bd9f9e406d1 | 267 | |
Olaffo | 25:ca5ccbf55b07 | 268 | //Try taking 2 more digits |
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 | if (command[index] > ('0'-1) && command[index] < (1 + '9')) { |
Olaffo | 25:ca5ccbf55b07 | 274 | intValue = intValue*10 + command[index] - '0'; |
Olaffo | 25:ca5ccbf55b07 | 275 | index++; |
Olaffo | 25:ca5ccbf55b07 | 276 | } |
Olaffo | 25:ca5ccbf55b07 | 277 | //Check for '.' |
Olaffo | 25:ca5ccbf55b07 | 278 | if (command[index] == '.') { |
Olaffo | 25:ca5ccbf55b07 | 279 | index++; |
Olaffo | 25:ca5ccbf55b07 | 280 | //Check for one decimal |
Olaffo | 25:ca5ccbf55b07 | 281 | if (command[index] > ('0'-1) && command[index] < (1 + '9')) { |
Olaffo | 25:ca5ccbf55b07 | 282 | decimalValue = (command[index] - '0')/10; |
Olaffo | 25:ca5ccbf55b07 | 283 | } |
tanyuzhuo | 20:5bd9f9e406d1 | 284 | } |
Olaffo | 25:ca5ccbf55b07 | 285 | decimalValue = (decimalValue + intValue); |
Olaffo | 25:ca5ccbf55b07 | 286 | maxSpeed = decimalValue; |
Olaffo | 25:ca5ccbf55b07 | 287 | putMessage(MAX_SPEED_CMD,0,decimalValue); |
peterith | 14:0481b606d10e | 288 | } |
peterith | 14:0481b606d10e | 289 | |
peterith | 14:0481b606d10e | 290 | void commandProcessor() { |
peterith | 14:0481b606d10e | 291 | pc.attach(&serialISR); |
tanyuzhuo | 17:ff5300ba5442 | 292 | char command[19]; |
tanyuzhuo | 17:ff5300ba5442 | 293 | char* number; |
peterith | 14:0481b606d10e | 294 | //char k; |
peterith | 14:0481b606d10e | 295 | uint64_t receivedKey; |
peterith | 14:0481b606d10e | 296 | uint8_t index = 0; |
peterith | 14:0481b606d10e | 297 | while(1) { |
peterith | 14:0481b606d10e | 298 | osEvent newEvent = inCharQ.get(); |
peterith | 14:0481b606d10e | 299 | uint8_t newChar = (uint8_t) newEvent.value.p; |
peterith | 14:0481b606d10e | 300 | command[index] = newChar; |
peterith | 14:0481b606d10e | 301 | index++; |
tanyuzhuo | 17:ff5300ba5442 | 302 | if (newChar == '\r') { |
peterith | 14:0481b606d10e | 303 | command[17] = '\0'; |
tanyuzhuo | 17:ff5300ba5442 | 304 | |
tanyuzhuo | 17:ff5300ba5442 | 305 | if (command[0] == 'R') { |
Olaffo | 25:ca5ccbf55b07 | 306 | R_Decoder(command); |
tanyuzhuo | 24:7dd4e187dd30 | 307 | motorPosition_command = motorPosition; |
tanyuzhuo | 17:ff5300ba5442 | 308 | } |
tanyuzhuo | 17:ff5300ba5442 | 309 | else if (command[0] == 'V') { |
Olaffo | 25:ca5ccbf55b07 | 310 | V_Decoder(command); |
tanyuzhuo | 17:ff5300ba5442 | 311 | } |
tanyuzhuo | 17:ff5300ba5442 | 312 | else if (command[0] == 'K') { |
tanyuzhuo | 17:ff5300ba5442 | 313 | if (index == 18){ // when index is 18 means you entered K and 16 digits |
tanyuzhuo | 17:ff5300ba5442 | 314 | number = command +1; //super bad solution, but I don't know how to work with strings in C |
tanyuzhuo | 17:ff5300ba5442 | 315 | receivedKey = strtoull(number, NULL, 16); |
tanyuzhuo | 20:5bd9f9e406d1 | 316 | putMessage(KEY_ECHO,receivedKey); |
Olaffo | 25:ca5ccbf55b07 | 317 | |
tanyuzhuo | 17:ff5300ba5442 | 318 | newKey_mutex.lock(); |
tanyuzhuo | 17:ff5300ba5442 | 319 | newKey = receivedKey; |
tanyuzhuo | 17:ff5300ba5442 | 320 | newKey_mutex.unlock(); |
tanyuzhuo | 17:ff5300ba5442 | 321 | } else { |
tanyuzhuo | 20:5bd9f9e406d1 | 322 | putMessage(KEY_DECLINED); |
tanyuzhuo | 17:ff5300ba5442 | 323 | }; |
tanyuzhuo | 17:ff5300ba5442 | 324 | } |
tanyuzhuo | 17:ff5300ba5442 | 325 | else if (command[0] == 'T') { |
Olaffo | 25:ca5ccbf55b07 | 326 | putMessage(TUNE_CMD); |
tanyuzhuo | 17:ff5300ba5442 | 327 | } |
peterith | 14:0481b606d10e | 328 | memset(command, 0, sizeof(command)); |
peterith | 14:0481b606d10e | 329 | index = 0; |
peterith | 14:0481b606d10e | 330 | } else { |
tanyuzhuo | 20:5bd9f9e406d1 | 331 | pc.printf("Current command: %s\n\r", command); //Not sure how to go around this one cause it requires passing string |
peterith | 14:0481b606d10e | 332 | } |
peterith | 14:0481b606d10e | 333 | } |
peterith | 14:0481b606d10e | 334 | } |
tanyuzhuo | 17:ff5300ba5442 | 335 | |
tanyuzhuo | 16:10d53b056b17 | 336 | void bitcoin(){ |
tanyuzhuo | 16:10d53b056b17 | 337 | while(1) { |
tanyuzhuo | 16:10d53b056b17 | 338 | SHA256 sha; |
tanyuzhuo | 16:10d53b056b17 | 339 | uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64, |
tanyuzhuo | 16:10d53b056b17 | 340 | 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73, |
tanyuzhuo | 16:10d53b056b17 | 341 | 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E, |
tanyuzhuo | 16:10d53b056b17 | 342 | 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20, |
tanyuzhuo | 16:10d53b056b17 | 343 | 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20, |
tanyuzhuo | 16:10d53b056b17 | 344 | 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20, |
tanyuzhuo | 16:10d53b056b17 | 345 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
tanyuzhuo | 16:10d53b056b17 | 346 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; |
tanyuzhuo | 16:10d53b056b17 | 347 | uint64_t* key = (uint64_t*) ((int) sequence + 48); |
tanyuzhuo | 16:10d53b056b17 | 348 | uint64_t* nonce = (uint64_t*) ((int) sequence + 56); |
tanyuzhuo | 16:10d53b056b17 | 349 | uint8_t hash[32]; |
tanyuzhuo | 16:10d53b056b17 | 350 | |
tanyuzhuo | 16:10d53b056b17 | 351 | Timer t; |
tanyuzhuo | 16:10d53b056b17 | 352 | t.start(); |
tanyuzhuo | 16:10d53b056b17 | 353 | unsigned currentTime = 0; |
tanyuzhuo | 20:5bd9f9e406d1 | 354 | unsigned currentCount= 0; |
tanyuzhuo | 16:10d53b056b17 | 355 | |
tanyuzhuo | 16:10d53b056b17 | 356 | for (unsigned i = 0; i <= UINT_MAX; i++) { |
tanyuzhuo | 16:10d53b056b17 | 357 | (*nonce)++; |
tanyuzhuo | 16:10d53b056b17 | 358 | newKey_mutex.lock(); |
tanyuzhuo | 16:10d53b056b17 | 359 | *key = newKey; |
tanyuzhuo | 16:10d53b056b17 | 360 | newKey_mutex.unlock(); |
tanyuzhuo | 16:10d53b056b17 | 361 | sha.computeHash(hash, sequence, 64); |
tanyuzhuo | 16:10d53b056b17 | 362 | if (hash[0] == 0 && hash[1] == 0) { |
tanyuzhuo | 16:10d53b056b17 | 363 | //putMessage(nonce); |
tanyuzhuo | 16:10d53b056b17 | 364 | pc.printf("Successful nonce: %016x\n\r", *nonce); |
tanyuzhuo | 16:10d53b056b17 | 365 | } |
tanyuzhuo | 16:10d53b056b17 | 366 | if ((unsigned) t.read() == currentTime) { |
tanyuzhuo | 16:10d53b056b17 | 367 | //pc.printf("Hash rate: %d\n\r", i - currentCount); |
tanyuzhuo | 20:5bd9f9e406d1 | 368 | //pc.printf("Current key: %016llx\n\r", *key); |
tanyuzhuo | 16:10d53b056b17 | 369 | currentTime++; |
tanyuzhuo | 16:10d53b056b17 | 370 | currentCount = i; |
tanyuzhuo | 16:10d53b056b17 | 371 | } |
tanyuzhuo | 16:10d53b056b17 | 372 | } |
tanyuzhuo | 16:10d53b056b17 | 373 | t.stop(); |
tanyuzhuo | 16:10d53b056b17 | 374 | } |
Olaffo | 25:ca5ccbf55b07 | 375 | } |
tanyuzhuo | 20:5bd9f9e406d1 | 376 | |
tanyuzhuo | 18:e48c0910c71e | 377 | |
tanyuzhuo | 18:e48c0910c71e | 378 | |
estott | 0:de4320f74764 | 379 | //Set a given drive state |
tanyuzhuo | 23:904721445e7a | 380 | void motorOut(int8_t driveState, float pulseWidth){ |
estott | 0:de4320f74764 | 381 | |
estott | 2:4e88faab6988 | 382 | //Lookup the output byte from the drive state. |
estott | 2:4e88faab6988 | 383 | int8_t driveOut = driveTable[driveState & 0x07]; |
estott | 2:4e88faab6988 | 384 | |
estott | 2:4e88faab6988 | 385 | //Turn off first |
tanyuzhuo | 20:5bd9f9e406d1 | 386 | if (~driveOut & 0x01) L1L = 0; |
estott | 2:4e88faab6988 | 387 | if (~driveOut & 0x02) L1H = 1; |
tanyuzhuo | 20:5bd9f9e406d1 | 388 | if (~driveOut & 0x04) L2L = 0; |
estott | 2:4e88faab6988 | 389 | if (~driveOut & 0x08) L2H = 1; |
tanyuzhuo | 20:5bd9f9e406d1 | 390 | if (~driveOut & 0x10) L3L = 0; |
estott | 2:4e88faab6988 | 391 | if (~driveOut & 0x20) L3H = 1; |
estott | 2:4e88faab6988 | 392 | |
estott | 2:4e88faab6988 | 393 | //Then turn on |
tanyuzhuo | 20:5bd9f9e406d1 | 394 | if (driveOut & 0x01) L1L = 1; |
estott | 2:4e88faab6988 | 395 | if (driveOut & 0x02) L1H = 0; |
tanyuzhuo | 20:5bd9f9e406d1 | 396 | if (driveOut & 0x04) L2L = 1; |
estott | 2:4e88faab6988 | 397 | if (driveOut & 0x08) L2H = 0; |
tanyuzhuo | 20:5bd9f9e406d1 | 398 | if (driveOut & 0x10) L3L = 1; |
estott | 2:4e88faab6988 | 399 | if (driveOut & 0x20) L3H = 0; |
tanyuzhuo | 20:5bd9f9e406d1 | 400 | |
tanyuzhuo | 20:5bd9f9e406d1 | 401 | |
tanyuzhuo | 23:904721445e7a | 402 | //d9.write(1); |
tanyuzhuo | 23:904721445e7a | 403 | d9.pulsewidth_us(pulseWidth); |
peterith | 13:c51d828531d5 | 404 | } |
estott | 0:de4320f74764 | 405 | |
peterith | 13:c51d828531d5 | 406 | //Convert photointerrupter inputs to a rotor state |
estott | 0:de4320f74764 | 407 | inline int8_t readRotorState(){ |
estott | 2:4e88faab6988 | 408 | return stateMap[I1 + 2*I2 + 4*I3]; |
peterith | 13:c51d828531d5 | 409 | } |
estott | 0:de4320f74764 | 410 | |
estott | 2:4e88faab6988 | 411 | int8_t motorHome() { |
tanyuzhuo | 18:e48c0910c71e | 412 | //Put the motor in drive state 0 and wait for it to stabilize |
tanyuzhuo | 23:904721445e7a | 413 | motorOut(0,pulseWidth); |
estott | 3:569b35e2a602 | 414 | wait(2.0); |
estott | 2:4e88faab6988 | 415 | return readRotorState(); |
estott | 0:de4320f74764 | 416 | } |
peterith | 13:c51d828531d5 | 417 | |
tanyuzhuo | 20:5bd9f9e406d1 | 418 | |
tanyuzhuo | 18:e48c0910c71e | 419 | // ISR to handle the updating of the motor position |
tanyuzhuo | 18:e48c0910c71e | 420 | void motorISR() { |
tanyuzhuo | 18:e48c0910c71e | 421 | static int8_t oldRotorState; |
tanyuzhuo | 20:5bd9f9e406d1 | 422 | //orState is subtracted from future rotor state inputs to align rotor and motor states |
tanyuzhuo | 20:5bd9f9e406d1 | 423 | |
tanyuzhuo | 18:e48c0910c71e | 424 | int8_t rotorState = readRotorState(); |
tanyuzhuo | 23:904721445e7a | 425 | motorOut((rotorState-orState+lead+6)%6,pulseWidth); //+6 to make sure the remainder is positive |
tanyuzhuo | 18:e48c0910c71e | 426 | if (rotorState - oldRotorState == 5) motorPosition --; |
tanyuzhuo | 18:e48c0910c71e | 427 | else if (rotorState - oldRotorState == -5) motorPosition ++; |
tanyuzhuo | 18:e48c0910c71e | 428 | else motorPosition += (rotorState - oldRotorState); |
tanyuzhuo | 20:5bd9f9e406d1 | 429 | //pc.printf ("motorpPosition %f\n\r", motorPosition); |
tanyuzhuo | 18:e48c0910c71e | 430 | oldRotorState = rotorState; |
tanyuzhuo | 18:e48c0910c71e | 431 | } |
tanyuzhuo | 18:e48c0910c71e | 432 | /*void push() { |
peterith | 13:c51d828531d5 | 433 | intState = readRotorState(); |
peterith | 13:c51d828531d5 | 434 | if (intState != intStateOld) { |
peterith | 13:c51d828531d5 | 435 | intStateOld = intState; |
peterith | 13:c51d828531d5 | 436 | motorOut((intState - orState + lead +6) % 6); //+6 to make sure the remainder is positive |
peterith | 13:c51d828531d5 | 437 | } |
tanyuzhuo | 18:e48c0910c71e | 438 | }*/ |
tanyuzhuo | 20:5bd9f9e406d1 | 439 | void motorCtrlTick(){ |
tanyuzhuo | 20:5bd9f9e406d1 | 440 | motorCtrlT.signal_set(0x1); |
tanyuzhuo | 20:5bd9f9e406d1 | 441 | } |
tanyuzhuo | 18:e48c0910c71e | 442 | void motorCtrlFn(){ |
tanyuzhuo | 18:e48c0910c71e | 443 | int32_t counter=0; |
tanyuzhuo | 18:e48c0910c71e | 444 | static int32_t oldmotorPosition; |
tanyuzhuo | 24:7dd4e187dd30 | 445 | float Ts; |
tanyuzhuo | 24:7dd4e187dd30 | 446 | float sError; //speed error |
tanyuzhuo | 24:7dd4e187dd30 | 447 | float intError = 0; //integral of velError |
tanyuzhuo | 24:7dd4e187dd30 | 448 | float Tr; |
tanyuzhuo | 24:7dd4e187dd30 | 449 | float error; |
tanyuzhuo | 24:7dd4e187dd30 | 450 | float olderror = 0; |
tanyuzhuo | 21:34f440ae0227 | 451 | float Speed; |
tanyuzhuo | 24:7dd4e187dd30 | 452 | float Rev; |
tanyuzhuo | 24:7dd4e187dd30 | 453 | int8_t leads = -2; |
tanyuzhuo | 24:7dd4e187dd30 | 454 | int8_t leadr = -2; |
Olaffo | 25:ca5ccbf55b07 | 455 | float kps = 25; |
tanyuzhuo | 24:7dd4e187dd30 | 456 | float kis = 0.07; |
tanyuzhuo | 24:7dd4e187dd30 | 457 | float kpr = 35; |
tanyuzhuo | 24:7dd4e187dd30 | 458 | float kdr = 14.75; //proportional constant for speed |
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 | 25:ca5ccbf55b07 | 482 | if (motorVelocity != 0){ |
Olaffo | 25:ca5ccbf55b07 | 483 | intError = intError + sError; |
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 | 25:ca5ccbf55b07 | 489 | if (mode = STANDARD) |
Olaffo | 25:ca5ccbf55b07 | 490 | { |
Olaffo | 25:ca5ccbf55b07 | 491 | // Case for reverse 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 | pc.printf("Chose Ts %f\n\r", Ts); |
Olaffo | 25:ca5ccbf55b07 | 513 | // Flip lead |
Olaffo | 25:ca5ccbf55b07 | 514 | if ( Ts >= 0) { lead = -2; } |
Olaffo | 25:ca5ccbf55b07 | 515 | else if ( Ts < 0) { lead = 2; } |
Olaffo | 25:ca5ccbf55b07 | 516 | |
Olaffo | 25:ca5ccbf55b07 | 517 | // Assure torque is in bounds |
Olaffo | 25:ca5ccbf55b07 | 518 | if (Ts > 2000){ Ts = 2000; } |
Olaffo | 25:ca5ccbf55b07 | 519 | else if (Ts < 0){ Ts = -Ts; } |
Olaffo | 25:ca5ccbf55b07 | 520 | else if (Ts < -2000){ Ts = 2000; } |
Olaffo | 25:ca5ccbf55b07 | 521 | |
Olaffo | 25:ca5ccbf55b07 | 522 | // Pass torque |
Olaffo | 25:ca5ccbf55b07 | 523 | pulseWidth = Ts; |
Olaffo | 25:ca5ccbf55b07 | 524 | } |
Olaffo | 25:ca5ccbf55b07 | 525 | } |
Olaffo | 25:ca5ccbf55b07 | 526 | |
Olaffo | 25:ca5ccbf55b07 | 527 | // Case for forward rotation |
Olaffo | 25:ca5ccbf55b07 | 528 | else if (error >= 0){ |
Olaffo | 25:ca5ccbf55b07 | 529 | |
Olaffo | 25:ca5ccbf55b07 | 530 | // Case for choosing speed control |
Olaffo | 25:ca5ccbf55b07 | 531 | if (Ts <=Tr ){ |
Olaffo | 25:ca5ccbf55b07 | 532 | pc.printf("Chose Ts %f\n\r", Ts); |
Olaffo | 25:ca5ccbf55b07 | 533 | // Flip lead |
Olaffo | 25:ca5ccbf55b07 | 534 | if ( Ts >= 0) { lead = -2; } |
Olaffo | 25:ca5ccbf55b07 | 535 | else if ( Ts < 0) { lead = 2; } |
Olaffo | 25:ca5ccbf55b07 | 536 | |
Olaffo | 25:ca5ccbf55b07 | 537 | // Assure torque is in bounds |
Olaffo | 25:ca5ccbf55b07 | 538 | if (Ts > 2000){ Ts = 2000; } |
Olaffo | 25:ca5ccbf55b07 | 539 | else if (Ts < 0){ Ts = -Ts; } |
Olaffo | 25:ca5ccbf55b07 | 540 | else if (Ts < -2000){ Ts = 2000; } |
Olaffo | 25:ca5ccbf55b07 | 541 | |
Olaffo | 25:ca5ccbf55b07 | 542 | // Pass torque |
Olaffo | 25:ca5ccbf55b07 | 543 | pulseWidth = Ts; |
Olaffo | 25:ca5ccbf55b07 | 544 | } |
Olaffo | 25:ca5ccbf55b07 | 545 | |
Olaffo | 25:ca5ccbf55b07 | 546 | // Case for rotational speed control |
Olaffo | 25:ca5ccbf55b07 | 547 | else { |
Olaffo | 25:ca5ccbf55b07 | 548 | |
Olaffo | 25:ca5ccbf55b07 | 549 | // Flip lead |
Olaffo | 25:ca5ccbf55b07 | 550 | if ( Tr >= 0) { lead = -2; } |
Olaffo | 25:ca5ccbf55b07 | 551 | else if ( Tr < 0) { lead = 2; } |
Olaffo | 25:ca5ccbf55b07 | 552 | |
Olaffo | 25:ca5ccbf55b07 | 553 | // Assure torque is in bounds |
Olaffo | 25:ca5ccbf55b07 | 554 | if (Tr > 2000){ Tr = 2000; } |
Olaffo | 25:ca5ccbf55b07 | 555 | else if (Tr < 0){ Tr = -Tr; } |
Olaffo | 25:ca5ccbf55b07 | 556 | else if (Tr < -2000){ Tr = 2000; } |
Olaffo | 25:ca5ccbf55b07 | 557 | |
Olaffo | 25:ca5ccbf55b07 | 558 | // Pass torque |
Olaffo | 25:ca5ccbf55b07 | 559 | pulseWidth = Tr; |
Olaffo | 25:ca5ccbf55b07 | 560 | } |
Olaffo | 25:ca5ccbf55b07 | 561 | } |
tanyuzhuo | 24:7dd4e187dd30 | 562 | } |
tanyuzhuo | 24:7dd4e187dd30 | 563 | |
Olaffo | 25:ca5ccbf55b07 | 564 | |
Olaffo | 25:ca5ccbf55b07 | 565 | // ONLY speed control (aka forever forward mode) |
Olaffo | 25:ca5ccbf55b07 | 566 | else if (mode = FOREVER_FORWARD){ |
Olaffo | 25:ca5ccbf55b07 | 567 | |
Olaffo | 25:ca5ccbf55b07 | 568 | if ( Ts >= 0) { lead = -2; } |
Olaffo | 25:ca5ccbf55b07 | 569 | else if ( Ts < 0) { lead = 2; } |
Olaffo | 25:ca5ccbf55b07 | 570 | |
Olaffo | 25:ca5ccbf55b07 | 571 | // Assure torque is in bounds |
Olaffo | 25:ca5ccbf55b07 | 572 | if (Ts > 2000){ Ts = 2000; } |
Olaffo | 25:ca5ccbf55b07 | 573 | else if (Ts < 0){ Ts = -Ts; } |
Olaffo | 25:ca5ccbf55b07 | 574 | else if (Ts < -2000){ Ts = 2000; } |
Olaffo | 25:ca5ccbf55b07 | 575 | |
tanyuzhuo | 24:7dd4e187dd30 | 576 | pulseWidth = Ts; |
tanyuzhuo | 24:7dd4e187dd30 | 577 | } |
tanyuzhuo | 24:7dd4e187dd30 | 578 | |
Olaffo | 25:ca5ccbf55b07 | 579 | counter++; |
tanyuzhuo | 18:e48c0910c71e | 580 | motorTime.reset(); |
tanyuzhuo | 18:e48c0910c71e | 581 | // Serial output to monitor speed and position |
tanyuzhuo | 18:e48c0910c71e | 582 | if(counter == 10){ |
tanyuzhuo | 18:e48c0910c71e | 583 | counter = 0; |
tanyuzhuo | 18:e48c0910c71e | 584 | //display velocity and motor position |
tanyuzhuo | 21:34f440ae0227 | 585 | putMessage(MOTOR_POS,0,(float)(motorPosition/6.0)); |
tanyuzhuo | 20:5bd9f9e406d1 | 586 | putMessage(MOTOR_SPEED,0,(float)(motorVelocity/6.0)); |
Olaffo | 25:ca5ccbf55b07 | 587 | putMessage(LEAD,lead); |
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 | 17:ff5300ba5442 | 593 | //Initialise bincoin mining and communication |
tanyuzhuo | 20:5bd9f9e406d1 | 594 | |
Olaffo | 25:ca5ccbf55b07 | 595 | // Start up checkup |
tanyuzhuo | 23:904721445e7a | 596 | d9.period(0.002f); //Set PWM period in seconds |
Olaffo | 25:ca5ccbf55b07 | 597 | int8_t orState = motorHome(); |
Olaffo | 25:ca5ccbf55b07 | 598 | putMessage(WELCOME); |
Olaffo | 25:ca5ccbf55b07 | 599 | putMessage(ROTOR_ORG); |
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 | |
tanyuzhuo | 16:10d53b056b17 | 616 | |
estott | 0:de4320f74764 | 617 | } |