Callum and Adel's changes on 12/02/19
Dependencies: Crypto
main.cpp@40:9fae84f111e6, 2019-03-20 (annotated)
- Committer:
- adehadd
- Date:
- Wed Mar 20 13:49:57 2019 +0000
- Revision:
- 40:9fae84f111e6
- Parent:
- 39:05a021718517
- Child:
- 41:6e730621622b
science not really applied, but PID might be better now
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CallumAlder | 14:4e312fb83330 | 1 | #include "SHA256.h" |
estott | 0:de4320f74764 | 2 | #include "mbed.h" |
adehadd | 20:c60f4785b556 | 3 | // #include <iostream> |
adehadd | 20:c60f4785b556 | 4 | // #include "rtos.h" |
CallumAlder | 14:4e312fb83330 | 5 | |
CallumAlder | 19:805c87360b55 | 6 | /*TODO: |
adehadd | 40:9fae84f111e6 | 7 | Change: |
adehadd | 40:9fae84f111e6 | 8 | Indx |
adehadd | 40:9fae84f111e6 | 9 | newCmd |
adehadd | 40:9fae84f111e6 | 10 | MAXCMDLENGTH |
CallumAlder | 35:132413ec3d65 | 11 | move the global variables to a class because we arent paeasents - Mission Failed |
adehadd | 40:9fae84f111e6 | 12 | use jack's motor motor position |
adehadd | 40:9fae84f111e6 | 13 | fix class variable naming |
CallumAlder | 19:805c87360b55 | 14 | */ |
estott | 0:de4320f74764 | 15 | |
estott | 0:de4320f74764 | 16 | //Photointerrupter input pins |
estott | 10:a4b5723b6c9d | 17 | #define I1pin D3 |
estott | 10:a4b5723b6c9d | 18 | #define I2pin D6 |
estott | 10:a4b5723b6c9d | 19 | #define I3pin D5 |
estott | 2:4e88faab6988 | 20 | |
estott | 2:4e88faab6988 | 21 | //Incremental encoder input pins |
estott | 10:a4b5723b6c9d | 22 | #define CHApin D12 |
estott | 10:a4b5723b6c9d | 23 | #define CHBpin D11 |
estott | 0:de4320f74764 | 24 | |
estott | 0:de4320f74764 | 25 | //Motor Drive output pins //Mask in output byte |
estott | 10:a4b5723b6c9d | 26 | #define L1Lpin D1 //0x01 |
estott | 10:a4b5723b6c9d | 27 | #define L1Hpin A3 //0x02 |
estott | 10:a4b5723b6c9d | 28 | #define L2Lpin D0 //0x04 |
estott | 10:a4b5723b6c9d | 29 | #define L2Hpin A6 //0x08 |
estott | 10:a4b5723b6c9d | 30 | #define L3Lpin D10 //0x10 |
estott | 10:a4b5723b6c9d | 31 | #define L3Hpin D2 //0x20 |
estott | 10:a4b5723b6c9d | 32 | |
estott | 10:a4b5723b6c9d | 33 | #define PWMpin D9 |
estott | 5:08f338b5e4d9 | 34 | |
estott | 5:08f338b5e4d9 | 35 | //Motor current sense |
estott | 5:08f338b5e4d9 | 36 | #define MCSPpin A1 |
estott | 5:08f338b5e4d9 | 37 | #define MCSNpin A0 |
estott | 0:de4320f74764 | 38 | |
adehadd | 40:9fae84f111e6 | 39 | // "Lacros" for utility |
adehadd | 40:9fae84f111e6 | 40 | #define sgn(x) ((x)/abs(x)) |
adehadd | 40:9fae84f111e6 | 41 | #define max(x,y) ((x)>=(y)?(x):(y)) |
adehadd | 40:9fae84f111e6 | 42 | #define min(x,y) ((x)>=(y)?(y):(x)) |
adehadd | 40:9fae84f111e6 | 43 | |
estott | 0:de4320f74764 | 44 | //Mapping from sequential drive states to motor phase outputs |
estott | 0:de4320f74764 | 45 | /* |
estott | 0:de4320f74764 | 46 | State L1 L2 L3 |
estott | 0:de4320f74764 | 47 | 0 H - L |
estott | 0:de4320f74764 | 48 | 1 - H L |
estott | 0:de4320f74764 | 49 | 2 L H - |
estott | 0:de4320f74764 | 50 | 3 L - H |
estott | 0:de4320f74764 | 51 | 4 - L H |
estott | 0:de4320f74764 | 52 | 5 H L - |
estott | 0:de4320f74764 | 53 | 6 - - - |
estott | 0:de4320f74764 | 54 | 7 - - - |
estott | 0:de4320f74764 | 55 | */ |
CallumAlder | 28:4f02ac845e5d | 56 | |
CallumAlder | 29:c96439a60184 | 57 | //Status LED |
CallumAlder | 29:c96439a60184 | 58 | DigitalOut led1(LED1); |
CallumAlder | 28:4f02ac845e5d | 59 | |
CallumAlder | 29:c96439a60184 | 60 | //Photointerrupter inputs |
CallumAlder | 29:c96439a60184 | 61 | InterruptIn I1(I1pin); |
CallumAlder | 29:c96439a60184 | 62 | InterruptIn I2(I2pin); |
CallumAlder | 29:c96439a60184 | 63 | InterruptIn I3(I3pin); |
CallumAlder | 28:4f02ac845e5d | 64 | |
CallumAlder | 29:c96439a60184 | 65 | //Motor Drive outputs |
CallumAlder | 29:c96439a60184 | 66 | DigitalOut L1L(L1Lpin); |
CallumAlder | 29:c96439a60184 | 67 | DigitalOut L1H(L1Hpin); |
CallumAlder | 29:c96439a60184 | 68 | DigitalOut L2L(L2Lpin); |
CallumAlder | 29:c96439a60184 | 69 | DigitalOut L2H(L2Hpin); |
CallumAlder | 29:c96439a60184 | 70 | DigitalOut L3L(L3Lpin); |
CallumAlder | 29:c96439a60184 | 71 | DigitalOut L3H(L3Hpin); |
CallumAlder | 28:4f02ac845e5d | 72 | |
CallumAlder | 29:c96439a60184 | 73 | PwmOut pwmCtrl(PWMpin); |
CallumAlder | 28:4f02ac845e5d | 74 | |
estott | 0:de4320f74764 | 75 | //Drive state to output table |
estott | 0:de4320f74764 | 76 | const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00}; |
estott | 2:4e88faab6988 | 77 | |
estott | 0:de4320f74764 | 78 | //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid |
iachinweze1 | 23:ab1cb51527d1 | 79 | const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07}; |
estott | 2:4e88faab6988 | 80 | //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 | 81 | |
CallumAlder | 33:f1dc3b160eac | 82 | class Comm{ |
iachinweze1 | 23:ab1cb51527d1 | 83 | |
CallumAlder | 33:f1dc3b160eac | 84 | public: |
CallumAlder | 33:f1dc3b160eac | 85 | |
CallumAlder | 33:f1dc3b160eac | 86 | Thread t_comm_out; |
CallumAlder | 33:f1dc3b160eac | 87 | // Thread *p_motor_ctrl; |
CallumAlder | 33:f1dc3b160eac | 88 | |
CallumAlder | 33:f1dc3b160eac | 89 | bool _RUN; |
CallumAlder | 33:f1dc3b160eac | 90 | |
CallumAlder | 33:f1dc3b160eac | 91 | RawSerial pc; |
CallumAlder | 33:f1dc3b160eac | 92 | // Queue<void, 8> inCharQ; // Input Character Queue |
CallumAlder | 33:f1dc3b160eac | 93 | |
CallumAlder | 33:f1dc3b160eac | 94 | |
CallumAlder | 33:f1dc3b160eac | 95 | static const char MsgChar[11]; |
CallumAlder | 33:f1dc3b160eac | 96 | |
CallumAlder | 33:f1dc3b160eac | 97 | uint8_t MAXCMDLENGTH; |
CallumAlder | 33:f1dc3b160eac | 98 | |
CallumAlder | 33:f1dc3b160eac | 99 | volatile uint8_t cmdIndx; |
CallumAlder | 33:f1dc3b160eac | 100 | volatile uint8_t inCharQIdx; |
CallumAlder | 33:f1dc3b160eac | 101 | |
adehadd | 39:05a021718517 | 102 | volatile uint32_t motorPower; // motor toque |
CallumAlder | 33:f1dc3b160eac | 103 | volatile float targetVel; |
CallumAlder | 33:f1dc3b160eac | 104 | volatile float targetRot; |
CallumAlder | 33:f1dc3b160eac | 105 | |
iachinweze1 | 38:a3713a09c828 | 106 | volatile bool outMining; |
iachinweze1 | 38:a3713a09c828 | 107 | |
CallumAlder | 33:f1dc3b160eac | 108 | enum msgType {motorState, posIn, velIn, posOut, velOut, |
CallumAlder | 33:f1dc3b160eac | 109 | |
CallumAlder | 33:f1dc3b160eac | 110 | hashRate, keyAdded, nonceMatch, |
CallumAlder | 33:f1dc3b160eac | 111 | |
CallumAlder | 33:f1dc3b160eac | 112 | torque, rotations, |
CallumAlder | 33:f1dc3b160eac | 113 | |
CallumAlder | 33:f1dc3b160eac | 114 | error}; |
CallumAlder | 33:f1dc3b160eac | 115 | |
CallumAlder | 33:f1dc3b160eac | 116 | typedef struct { |
CallumAlder | 33:f1dc3b160eac | 117 | msgType type; |
CallumAlder | 33:f1dc3b160eac | 118 | uint32_t message; |
CallumAlder | 33:f1dc3b160eac | 119 | } msg; |
CallumAlder | 33:f1dc3b160eac | 120 | |
CallumAlder | 33:f1dc3b160eac | 121 | Mail<msg, 32> mailStack; |
adehadd | 39:05a021718517 | 122 | |
adehadd | 39:05a021718517 | 123 | int8_t modeBitfield; // 0,0,0,0,0,Torque,Rotation,Velocity |
CallumAlder | 33:f1dc3b160eac | 124 | |
CallumAlder | 33:f1dc3b160eac | 125 | void serialISR(){ |
CallumAlder | 33:f1dc3b160eac | 126 | if (pc.readable()) { |
CallumAlder | 33:f1dc3b160eac | 127 | char newChar = pc.getc(); |
CallumAlder | 33:f1dc3b160eac | 128 | // inCharQ.put((void*)newChar); // void* = pointer to an unknown type that cannot be dereferenced |
CallumAlder | 33:f1dc3b160eac | 129 | |
CallumAlder | 33:f1dc3b160eac | 130 | if (inCharQIdx == (MAXCMDLENGTH)) { |
CallumAlder | 33:f1dc3b160eac | 131 | inCharQ[MAXCMDLENGTH] = '\0'; // force the string to have an end character |
CallumAlder | 33:f1dc3b160eac | 132 | putMessage(error, 1); |
CallumAlder | 33:f1dc3b160eac | 133 | inCharQIdx = 0; // reset buffer index |
CallumAlder | 33:f1dc3b160eac | 134 | // pc.putc('\r'); // carriage return moves to the start of the line |
CallumAlder | 33:f1dc3b160eac | 135 | // for (int i = 0; i < MAXCMDLENGTH; ++i) |
CallumAlder | 33:f1dc3b160eac | 136 | // { |
CallumAlder | 33:f1dc3b160eac | 137 | // inCharQ[i] = ' '; |
CallumAlder | 33:f1dc3b160eac | 138 | // pc.putc(' '); |
CallumAlder | 33:f1dc3b160eac | 139 | // } |
CallumAlder | 33:f1dc3b160eac | 140 | |
CallumAlder | 33:f1dc3b160eac | 141 | // pc.putc('\r'); // carriage return moves to the start of the line |
adehadd | 20:c60f4785b556 | 142 | } |
adehadd | 20:c60f4785b556 | 143 | else{ |
CallumAlder | 33:f1dc3b160eac | 144 | if(newChar != '\r'){ //While the command is not over, |
CallumAlder | 33:f1dc3b160eac | 145 | inCharQ[inCharQIdx] = newChar; //save input character and |
CallumAlder | 33:f1dc3b160eac | 146 | inCharQIdx++; //advance index |
CallumAlder | 33:f1dc3b160eac | 147 | pc.putc(newChar); |
CallumAlder | 33:f1dc3b160eac | 148 | } |
CallumAlder | 33:f1dc3b160eac | 149 | else{ |
CallumAlder | 33:f1dc3b160eac | 150 | inCharQ[inCharQIdx] = '\0'; //When the command is finally over, |
CallumAlder | 33:f1dc3b160eac | 151 | strncpy(newCmd, inCharQ, MAXCMDLENGTH); // Will copy 18 characters from inCharQ to newCmd |
CallumAlder | 33:f1dc3b160eac | 152 | cmdParser(); //parse the command for decoding. |
CallumAlder | 33:f1dc3b160eac | 153 | for (int i = 0; i < MAXCMDLENGTH; ++i) // reset buffer |
CallumAlder | 33:f1dc3b160eac | 154 | inCharQ[i] = ' '; |
CallumAlder | 33:f1dc3b160eac | 155 | inCharQIdx = 0; // reset index |
CallumAlder | 33:f1dc3b160eac | 156 | } |
CallumAlder | 19:805c87360b55 | 157 | } |
CallumAlder | 19:805c87360b55 | 158 | } |
CallumAlder | 33:f1dc3b160eac | 159 | |
CallumAlder | 33:f1dc3b160eac | 160 | |
CallumAlder | 33:f1dc3b160eac | 161 | } |
CallumAlder | 33:f1dc3b160eac | 162 | |
CallumAlder | 33:f1dc3b160eac | 163 | void returnCursor() { |
CallumAlder | 33:f1dc3b160eac | 164 | pc.putc('>'); |
CallumAlder | 33:f1dc3b160eac | 165 | for (int i = 0; i < inCharQIdx; ++i) // reset cursor position |
CallumAlder | 33:f1dc3b160eac | 166 | pc.putc(inCharQ[i]); |
CallumAlder | 33:f1dc3b160eac | 167 | // for (int i = inCharQIdx; i < MAXCMDLENGTH; ++i) // fill remaining with blanks |
CallumAlder | 33:f1dc3b160eac | 168 | // pc.putc(' '); |
CallumAlder | 33:f1dc3b160eac | 169 | // pc.putc('<'); |
CallumAlder | 33:f1dc3b160eac | 170 | } |
CallumAlder | 33:f1dc3b160eac | 171 | |
CallumAlder | 33:f1dc3b160eac | 172 | void cmdParser(){ |
CallumAlder | 33:f1dc3b160eac | 173 | switch(newCmd[0]) { |
CallumAlder | 33:f1dc3b160eac | 174 | case 'K': //(MsgChar[keyAdded]):// |
CallumAlder | 33:f1dc3b160eac | 175 | newKey_mutex.lock(); //Ensure there is no deadlock |
CallumAlder | 33:f1dc3b160eac | 176 | sscanf(newCmd, "K%x", &newKey); //Find desired the Key code |
CallumAlder | 33:f1dc3b160eac | 177 | putMessage(keyAdded, newKey); //Print it out |
CallumAlder | 33:f1dc3b160eac | 178 | newKey_mutex.unlock(); |
CallumAlder | 33:f1dc3b160eac | 179 | break; |
CallumAlder | 33:f1dc3b160eac | 180 | case 'V': //(MsgChar[velIn]):// |
CallumAlder | 33:f1dc3b160eac | 181 | sscanf(newCmd, "V%f", &targetVel); //Find desired the target velocity |
adehadd | 39:05a021718517 | 182 | modeBitfield = 0x01; |
CallumAlder | 33:f1dc3b160eac | 183 | putMessage(velIn, targetVel); //Print it out |
CallumAlder | 33:f1dc3b160eac | 184 | break; |
CallumAlder | 33:f1dc3b160eac | 185 | case 'R': //(MsgChar[posIn]):// |
CallumAlder | 33:f1dc3b160eac | 186 | sscanf(newCmd, "R%f", &targetRot); //Find desired target rotation |
adehadd | 39:05a021718517 | 187 | modeBitfield = 0x02; |
CallumAlder | 33:f1dc3b160eac | 188 | putMessage(posIn, targetRot); //Print it out |
CallumAlder | 33:f1dc3b160eac | 189 | break; |
CallumAlder | 33:f1dc3b160eac | 190 | case 'T': //(MsgChar[torque]):// |
iachinweze1 | 38:a3713a09c828 | 191 | sscanf(newCmd, "T%u", &motorPower); //Find desired target torque |
adehadd | 39:05a021718517 | 192 | modeBitfield = 0x04; |
CallumAlder | 33:f1dc3b160eac | 193 | putMessage(torque, motorPower); //Print it out |
CallumAlder | 33:f1dc3b160eac | 194 | break; |
iachinweze1 | 38:a3713a09c828 | 195 | case 'M': //(MsgChar[torque]):// |
iachinweze1 | 38:a3713a09c828 | 196 | int8_t miningTest; |
iachinweze1 | 38:a3713a09c828 | 197 | sscanf(newCmd, "M%d", &miningTest); //Find desired target torque |
iachinweze1 | 38:a3713a09c828 | 198 | if (miningTest == 1) |
iachinweze1 | 38:a3713a09c828 | 199 | outMining = true; |
iachinweze1 | 38:a3713a09c828 | 200 | else |
iachinweze1 | 38:a3713a09c828 | 201 | outMining = false; |
iachinweze1 | 38:a3713a09c828 | 202 | break; |
CallumAlder | 33:f1dc3b160eac | 203 | default: break; |
CallumAlder | 33:f1dc3b160eac | 204 | } |
CallumAlder | 19:805c87360b55 | 205 | } |
CallumAlder | 33:f1dc3b160eac | 206 | |
CallumAlder | 33:f1dc3b160eac | 207 | //~~~~~Decode messages to print on serial port~~~~~ |
CallumAlder | 33:f1dc3b160eac | 208 | void commOutFn() { |
CallumAlder | 33:f1dc3b160eac | 209 | while (_RUN) { |
CallumAlder | 33:f1dc3b160eac | 210 | osEvent newEvent = mailStack.get(); |
CallumAlder | 33:f1dc3b160eac | 211 | msg *pMessage = (msg *) newEvent.value.p; |
CallumAlder | 33:f1dc3b160eac | 212 | |
CallumAlder | 33:f1dc3b160eac | 213 | //Case switch to choose serial output based on incoming message |
CallumAlder | 33:f1dc3b160eac | 214 | switch (pMessage->type) { |
CallumAlder | 33:f1dc3b160eac | 215 | case motorState: |
CallumAlder | 35:132413ec3d65 | 216 | pc.printf("\r>%s< The motor is currently in state %x\n\r", inCharQ, pMessage->message); |
CallumAlder | 33:f1dc3b160eac | 217 | break; |
CallumAlder | 33:f1dc3b160eac | 218 | case hashRate: |
iachinweze1 | 38:a3713a09c828 | 219 | if (outMining) { |
iachinweze1 | 38:a3713a09c828 | 220 | pc.printf("\r>%s< Mining: %.4u Hash/s\r", inCharQ, (uint32_t) pMessage->message); |
iachinweze1 | 38:a3713a09c828 | 221 | returnCursor(); |
iachinweze1 | 38:a3713a09c828 | 222 | outMining = false; |
iachinweze1 | 38:a3713a09c828 | 223 | } |
CallumAlder | 33:f1dc3b160eac | 224 | break; |
CallumAlder | 33:f1dc3b160eac | 225 | case nonceMatch: |
iachinweze1 | 38:a3713a09c828 | 226 | // if (outMining) { |
iachinweze1 | 38:a3713a09c828 | 227 | pc.printf("\r>%s< Nonce found: %x\n\r", inCharQ, pMessage->message); |
CallumAlder | 33:f1dc3b160eac | 228 | returnCursor(); |
iachinweze1 | 38:a3713a09c828 | 229 | // } |
CallumAlder | 33:f1dc3b160eac | 230 | break; |
CallumAlder | 33:f1dc3b160eac | 231 | case keyAdded: |
CallumAlder | 35:132413ec3d65 | 232 | pc.printf("\r>%s< New Key Added:\t0x%016x\n\r", inCharQ, pMessage->message); |
CallumAlder | 33:f1dc3b160eac | 233 | break; |
CallumAlder | 33:f1dc3b160eac | 234 | case torque: |
adehadd | 39:05a021718517 | 235 | pc.printf("\r>%s< Motor Torque set to:\t%d\n\r", inCharQ, (int32_t) pMessage->message); |
CallumAlder | 33:f1dc3b160eac | 236 | break; |
CallumAlder | 33:f1dc3b160eac | 237 | case velIn: |
CallumAlder | 35:132413ec3d65 | 238 | pc.printf("\r>%s< Target Velocity set to:\t%.2f\n\r", inCharQ, targetVel); |
CallumAlder | 33:f1dc3b160eac | 239 | break; |
CallumAlder | 33:f1dc3b160eac | 240 | case velOut: |
adehadd | 40:9fae84f111e6 | 241 | pc.printf("\r>%s< Current Velocity:\t%.2f States/sec\n\r", inCharQ, \ |
adehadd | 39:05a021718517 | 242 | (float) ((int32_t) pMessage->message /*/ 6*/)); |
CallumAlder | 33:f1dc3b160eac | 243 | break; |
CallumAlder | 33:f1dc3b160eac | 244 | case posIn: |
adehadd | 40:9fae84f111e6 | 245 | pc.printf("\r>%s< Target # Rotations:\t%.2f\n\r", inCharQ, \ |
adehadd | 39:05a021718517 | 246 | (float) ((int32_t) pMessage->message /*/ 6*/)); |
CallumAlder | 33:f1dc3b160eac | 247 | break; |
CallumAlder | 33:f1dc3b160eac | 248 | case posOut: |
CallumAlder | 35:132413ec3d65 | 249 | pc.printf("\r>%s< Current Position:\t%.2f\n\r", inCharQ, \ |
adehadd | 39:05a021718517 | 250 | (float) ((int32_t) pMessage->message /*/ 6*/)); |
CallumAlder | 33:f1dc3b160eac | 251 | break; |
CallumAlder | 33:f1dc3b160eac | 252 | case error: |
CallumAlder | 33:f1dc3b160eac | 253 | pc.printf("\r>%s< Debugging position:%x\n\r", inCharQ, pMessage->message); |
CallumAlder | 33:f1dc3b160eac | 254 | for (int i = 0; i < MAXCMDLENGTH; ++i) // reset buffer |
CallumAlder | 33:f1dc3b160eac | 255 | inCharQ[i] = ' '; |
CallumAlder | 33:f1dc3b160eac | 256 | break; |
CallumAlder | 33:f1dc3b160eac | 257 | default: |
CallumAlder | 35:132413ec3d65 | 258 | pc.printf("\r>%s< Unknown Error. Message: %x\n\r", inCharQ, pMessage->message); |
CallumAlder | 33:f1dc3b160eac | 259 | break; |
CallumAlder | 33:f1dc3b160eac | 260 | } |
CallumAlder | 33:f1dc3b160eac | 261 | mailStack.free(pMessage); |
CallumAlder | 19:805c87360b55 | 262 | } |
iachinweze1 | 23:ab1cb51527d1 | 263 | } |
CallumAlder | 33:f1dc3b160eac | 264 | |
CallumAlder | 33:f1dc3b160eac | 265 | |
CallumAlder | 33:f1dc3b160eac | 266 | |
CallumAlder | 33:f1dc3b160eac | 267 | |
CallumAlder | 33:f1dc3b160eac | 268 | //TODO: stop function, maybe use parent de-constructor |
CallumAlder | 33:f1dc3b160eac | 269 | //void stop_comm{} |
CallumAlder | 33:f1dc3b160eac | 270 | |
CallumAlder | 33:f1dc3b160eac | 271 | // public: |
CallumAlder | 33:f1dc3b160eac | 272 | |
CallumAlder | 33:f1dc3b160eac | 273 | volatile uint64_t newKey; // hash key |
CallumAlder | 33:f1dc3b160eac | 274 | Mutex newKey_mutex; // Restrict access to prevent deadlock. |
CallumAlder | 33:f1dc3b160eac | 275 | |
CallumAlder | 33:f1dc3b160eac | 276 | Comm() : pc(SERIAL_TX, SERIAL_RX), |
CallumAlder | 33:f1dc3b160eac | 277 | t_comm_out(osPriorityAboveNormal, 1024) |
CallumAlder | 33:f1dc3b160eac | 278 | { // inherit from the RawSerial constructor |
CallumAlder | 33:f1dc3b160eac | 279 | |
CallumAlder | 33:f1dc3b160eac | 280 | pc.printf("%s\n\r", "Welcome" ); |
CallumAlder | 33:f1dc3b160eac | 281 | MAXCMDLENGTH = 18; |
CallumAlder | 33:f1dc3b160eac | 282 | |
CallumAlder | 33:f1dc3b160eac | 283 | // reset buffer |
CallumAlder | 33:f1dc3b160eac | 284 | // MbedOS prints 'Embedded Systems are fun and do awesome things!' |
CallumAlder | 33:f1dc3b160eac | 285 | // if you print a null terminator |
CallumAlder | 33:f1dc3b160eac | 286 | pc.putc('>'); |
CallumAlder | 33:f1dc3b160eac | 287 | for (int i = 0; i < MAXCMDLENGTH; ++i) { |
CallumAlder | 33:f1dc3b160eac | 288 | inCharQ[i] = '.'; |
CallumAlder | 33:f1dc3b160eac | 289 | pc.putc('.'); |
CallumAlder | 33:f1dc3b160eac | 290 | } |
CallumAlder | 33:f1dc3b160eac | 291 | pc.putc('<'); |
CallumAlder | 33:f1dc3b160eac | 292 | pc.putc('\r'); |
CallumAlder | 33:f1dc3b160eac | 293 | |
CallumAlder | 33:f1dc3b160eac | 294 | inCharQ[MAXCMDLENGTH] = '\0'; |
CallumAlder | 33:f1dc3b160eac | 295 | strncpy(newCmd, inCharQ, MAXCMDLENGTH); |
CallumAlder | 33:f1dc3b160eac | 296 | |
CallumAlder | 33:f1dc3b160eac | 297 | cmdIndx = 0; |
CallumAlder | 33:f1dc3b160eac | 298 | |
CallumAlder | 33:f1dc3b160eac | 299 | inCharQIdx = 0; |
CallumAlder | 33:f1dc3b160eac | 300 | // inCharQIdx = MAXCMDLENGTH-1; |
iachinweze1 | 38:a3713a09c828 | 301 | outMining = false; |
CallumAlder | 33:f1dc3b160eac | 302 | pc.attach(callback(this, &Comm::serialISR)); |
CallumAlder | 33:f1dc3b160eac | 303 | |
CallumAlder | 33:f1dc3b160eac | 304 | // Thread t_comm_in(osPriorityAboveNormal, 1024); |
CallumAlder | 33:f1dc3b160eac | 305 | // Thread t_comm_out(osPriorityAboveNormal, 1024); |
CallumAlder | 33:f1dc3b160eac | 306 | // Thread t_motor_ctrl(osPriorityAboveNormal, 1024); |
CallumAlder | 33:f1dc3b160eac | 307 | |
CallumAlder | 33:f1dc3b160eac | 308 | motorPower = 300; |
CallumAlder | 33:f1dc3b160eac | 309 | targetVel = 45.0; |
CallumAlder | 33:f1dc3b160eac | 310 | targetRot = 459.0; |
CallumAlder | 33:f1dc3b160eac | 311 | |
adehadd | 39:05a021718517 | 312 | modeBitfield = 0x01; // Default is velocity mode |
CallumAlder | 33:f1dc3b160eac | 313 | |
CallumAlder | 33:f1dc3b160eac | 314 | /*MsgChar = {'m', 'R', 'V', 'r', 'v', |
CallumAlder | 33:f1dc3b160eac | 315 | |
CallumAlder | 33:f1dc3b160eac | 316 | 'h', 'K', 'n', |
CallumAlder | 33:f1dc3b160eac | 317 | |
CallumAlder | 33:f1dc3b160eac | 318 | 'T', 'r', |
CallumAlder | 33:f1dc3b160eac | 319 | |
CallumAlder | 33:f1dc3b160eac | 320 | 'e'};*/ |
CallumAlder | 19:805c87360b55 | 321 | } |
CallumAlder | 33:f1dc3b160eac | 322 | |
CallumAlder | 33:f1dc3b160eac | 323 | |
CallumAlder | 33:f1dc3b160eac | 324 | void putMessage(msgType type, uint32_t message){ |
CallumAlder | 33:f1dc3b160eac | 325 | msg *p_msg = mailStack.alloc(); |
CallumAlder | 33:f1dc3b160eac | 326 | p_msg->type = type; |
CallumAlder | 33:f1dc3b160eac | 327 | p_msg->message = message; |
CallumAlder | 33:f1dc3b160eac | 328 | mailStack.put(p_msg); |
iachinweze1 | 23:ab1cb51527d1 | 329 | } |
CallumAlder | 33:f1dc3b160eac | 330 | |
CallumAlder | 33:f1dc3b160eac | 331 | void start_comm(){ |
CallumAlder | 33:f1dc3b160eac | 332 | _RUN = true; |
CallumAlder | 33:f1dc3b160eac | 333 | |
CallumAlder | 33:f1dc3b160eac | 334 | |
CallumAlder | 33:f1dc3b160eac | 335 | // reset buffer |
CallumAlder | 33:f1dc3b160eac | 336 | // MbedOS prints 'Embedded Systems are fun and do awesome things!' |
CallumAlder | 33:f1dc3b160eac | 337 | // if you print a null terminator |
CallumAlder | 33:f1dc3b160eac | 338 | pc.putc('>'); |
CallumAlder | 33:f1dc3b160eac | 339 | for (int i = 0; i < MAXCMDLENGTH; ++i) { |
CallumAlder | 33:f1dc3b160eac | 340 | inCharQ[i] = '.'; |
CallumAlder | 33:f1dc3b160eac | 341 | pc.putc('.'); |
CallumAlder | 33:f1dc3b160eac | 342 | } |
CallumAlder | 33:f1dc3b160eac | 343 | pc.putc('<'); |
CallumAlder | 33:f1dc3b160eac | 344 | pc.putc('\r'); |
CallumAlder | 33:f1dc3b160eac | 345 | |
CallumAlder | 33:f1dc3b160eac | 346 | inCharQ[MAXCMDLENGTH] = '\0'; |
CallumAlder | 33:f1dc3b160eac | 347 | strncpy(newCmd, inCharQ, MAXCMDLENGTH); |
CallumAlder | 33:f1dc3b160eac | 348 | |
CallumAlder | 33:f1dc3b160eac | 349 | // returnCursor(); |
CallumAlder | 33:f1dc3b160eac | 350 | |
CallumAlder | 33:f1dc3b160eac | 351 | // t_comm_in.start(callback(this, &Comm::commInFn)); |
CallumAlder | 33:f1dc3b160eac | 352 | // this::thread::wait() |
CallumAlder | 33:f1dc3b160eac | 353 | // wait(1.0); |
CallumAlder | 33:f1dc3b160eac | 354 | t_comm_out.start(callback(this, &Comm::commOutFn)); |
CallumAlder | 33:f1dc3b160eac | 355 | |
CallumAlder | 33:f1dc3b160eac | 356 | |
CallumAlder | 33:f1dc3b160eac | 357 | |
CallumAlder | 33:f1dc3b160eac | 358 | } |
CallumAlder | 33:f1dc3b160eac | 359 | |
CallumAlder | 33:f1dc3b160eac | 360 | char newCmd[]; // because unallocated must be defined at the bottom of the class |
CallumAlder | 33:f1dc3b160eac | 361 | char inCharQ[]; |
CallumAlder | 19:805c87360b55 | 362 | }; |
CallumAlder | 19:805c87360b55 | 363 | |
iachinweze1 | 12:41b3112021a3 | 364 | |
adehadd | 30:fbae0e5f200d | 365 | class Motor { |
CallumAlder | 28:4f02ac845e5d | 366 | |
CallumAlder | 28:4f02ac845e5d | 367 | |
CallumAlder | 33:f1dc3b160eac | 368 | protected: |
CallumAlder | 33:f1dc3b160eac | 369 | int8_t orState; //Rotor offset at motor state 0, motor specific |
CallumAlder | 33:f1dc3b160eac | 370 | volatile int8_t currentState; //Current Rotor State |
CallumAlder | 33:f1dc3b160eac | 371 | volatile int8_t stateList[6]; //All possible rotor states stored |
CallumAlder | 28:4f02ac845e5d | 372 | |
CallumAlder | 33:f1dc3b160eac | 373 | //Phase lead to make motor spin |
adehadd | 39:05a021718517 | 374 | volatile int8_t lead; |
CallumAlder | 33:f1dc3b160eac | 375 | |
CallumAlder | 33:f1dc3b160eac | 376 | Comm* p_comm; |
adehadd | 34:2c6f635cc8e7 | 377 | bool _RUN; |
CallumAlder | 33:f1dc3b160eac | 378 | |
CallumAlder | 33:f1dc3b160eac | 379 | //Run the motor synchronisation |
adehadd | 31:b10ca6cf39bf | 380 | |
CallumAlder | 33:f1dc3b160eac | 381 | float dutyC; // 1 = 100% |
adehadd | 39:05a021718517 | 382 | uint32_t mtrPeriod; // motor period |
CallumAlder | 33:f1dc3b160eac | 383 | uint8_t stateCount[3]; // State Counter |
CallumAlder | 33:f1dc3b160eac | 384 | uint8_t theStates[3]; // The Key states |
CallumAlder | 33:f1dc3b160eac | 385 | |
CallumAlder | 33:f1dc3b160eac | 386 | Thread t_motor_ctrl; // Thread for motor Control |
CallumAlder | 35:132413ec3d65 | 387 | |
adehadd | 39:05a021718517 | 388 | uint32_t MAXPWM_PRD; |
CallumAlder | 33:f1dc3b160eac | 389 | |
CallumAlder | 33:f1dc3b160eac | 390 | public: |
CallumAlder | 33:f1dc3b160eac | 391 | |
iachinweze1 | 38:a3713a09c828 | 392 | Motor() : t_motor_ctrl(osPriorityAboveNormal2, 1024) |
CallumAlder | 33:f1dc3b160eac | 393 | { |
CallumAlder | 33:f1dc3b160eac | 394 | // Set Power to maximum to drive motorHome() |
iachinweze1 | 38:a3713a09c828 | 395 | dutyC = 1.0f; |
iachinweze1 | 38:a3713a09c828 | 396 | mtrPeriod = 2e3; // motor period |
iachinweze1 | 38:a3713a09c828 | 397 | pwmCtrl.period_us(mtrPeriod); |
iachinweze1 | 38:a3713a09c828 | 398 | pwmCtrl.pulsewidth_us(mtrPeriod); |
CallumAlder | 33:f1dc3b160eac | 399 | |
CallumAlder | 33:f1dc3b160eac | 400 | orState = motorHome(); //Rotot offset at motor state 0 |
CallumAlder | 33:f1dc3b160eac | 401 | currentState = readRotorState(); //Current Rotor State |
CallumAlder | 33:f1dc3b160eac | 402 | // stateList[6] = {0,0,0, 0,0,0}; //All possible rotor states stored |
CallumAlder | 35:132413ec3d65 | 403 | lead = 2; //2 for forwards, -2 for backwards |
CallumAlder | 33:f1dc3b160eac | 404 | |
iachinweze1 | 38:a3713a09c828 | 405 | // It skips the origin state and it's 'lead' increments? |
iachinweze1 | 38:a3713a09c828 | 406 | theStates[0] = orState +1; |
iachinweze1 | 38:a3713a09c828 | 407 | theStates[1] = (orState + lead) % 6 +1; |
iachinweze1 | 38:a3713a09c828 | 408 | theStates[2] = (orState + (lead*2)) % 6 +1; |
CallumAlder | 35:132413ec3d65 | 409 | |
CallumAlder | 33:f1dc3b160eac | 410 | stateCount[0] = 0; stateCount[1] = 0; stateCount[2] = 0; |
CallumAlder | 33:f1dc3b160eac | 411 | |
CallumAlder | 33:f1dc3b160eac | 412 | p_comm = NULL; // null pointer for now |
adehadd | 34:2c6f635cc8e7 | 413 | _RUN = false; |
CallumAlder | 35:132413ec3d65 | 414 | |
iachinweze1 | 38:a3713a09c828 | 415 | MAXPWM_PRD = 2e3; |
CallumAlder | 33:f1dc3b160eac | 416 | |
CallumAlder | 33:f1dc3b160eac | 417 | } |
CallumAlder | 33:f1dc3b160eac | 418 | |
CallumAlder | 33:f1dc3b160eac | 419 | |
CallumAlder | 33:f1dc3b160eac | 420 | void motorStart(Comm *comm) { |
CallumAlder | 33:f1dc3b160eac | 421 | |
CallumAlder | 33:f1dc3b160eac | 422 | // Establish Photointerrupter Service Routines (auto choose next state) |
CallumAlder | 33:f1dc3b160eac | 423 | I1.fall(callback(this, &Motor::stateUpdate)); |
CallumAlder | 33:f1dc3b160eac | 424 | I2.fall(callback(this, &Motor::stateUpdate)); |
CallumAlder | 33:f1dc3b160eac | 425 | I3.fall(callback(this, &Motor::stateUpdate)); |
CallumAlder | 33:f1dc3b160eac | 426 | I1.rise(callback(this, &Motor::stateUpdate)); |
CallumAlder | 33:f1dc3b160eac | 427 | I2.rise(callback(this, &Motor::stateUpdate)); |
CallumAlder | 33:f1dc3b160eac | 428 | I3.rise(callback(this, &Motor::stateUpdate)); |
CallumAlder | 33:f1dc3b160eac | 429 | |
CallumAlder | 33:f1dc3b160eac | 430 | // push digitally so if motor is static it will start moving |
CallumAlder | 33:f1dc3b160eac | 431 | motorOut((currentState-orState+lead+6)%6); // We push it digitally |
CallumAlder | 33:f1dc3b160eac | 432 | |
CallumAlder | 33:f1dc3b160eac | 433 | // Default a lower duty cylce |
CallumAlder | 33:f1dc3b160eac | 434 | dutyC = 0.8; |
adehadd | 39:05a021718517 | 435 | pwmCtrl.period_us((uint32_t)mtrPeriod); |
adehadd | 39:05a021718517 | 436 | pwmCtrl.pulsewidth_us((uint32_t)mtrPeriod*dutyC); |
CallumAlder | 33:f1dc3b160eac | 437 | |
adehadd | 34:2c6f635cc8e7 | 438 | p_comm = comm; |
adehadd | 34:2c6f635cc8e7 | 439 | _RUN = true; |
adehadd | 34:2c6f635cc8e7 | 440 | |
CallumAlder | 33:f1dc3b160eac | 441 | // Start motor control thread |
CallumAlder | 33:f1dc3b160eac | 442 | t_motor_ctrl.start(callback(this, &Motor::motorCtrlFn)); |
CallumAlder | 35:132413ec3d65 | 443 | |
iachinweze1 | 38:a3713a09c828 | 444 | p_comm->pc.printf("origin=%i, theStates=[%i,%i,%i]\n", orState, theStates[0], theStates[1], theStates[2]); |
adehadd | 34:2c6f635cc8e7 | 445 | |
CallumAlder | 29:c96439a60184 | 446 | } |
CallumAlder | 33:f1dc3b160eac | 447 | |
CallumAlder | 33:f1dc3b160eac | 448 | //Set a given drive state |
CallumAlder | 33:f1dc3b160eac | 449 | void motorOut(int8_t driveState) { |
CallumAlder | 33:f1dc3b160eac | 450 | |
CallumAlder | 33:f1dc3b160eac | 451 | //Lookup the output byte from the drive state. |
CallumAlder | 33:f1dc3b160eac | 452 | int8_t driveOut = driveTable[driveState & 0x07]; |
CallumAlder | 33:f1dc3b160eac | 453 | |
CallumAlder | 33:f1dc3b160eac | 454 | //Turn off first |
CallumAlder | 33:f1dc3b160eac | 455 | if (~driveOut & 0x01) L1L = 0; |
CallumAlder | 33:f1dc3b160eac | 456 | if (~driveOut & 0x02) L1H = 1; |
CallumAlder | 33:f1dc3b160eac | 457 | if (~driveOut & 0x04) L2L = 0; |
CallumAlder | 33:f1dc3b160eac | 458 | if (~driveOut & 0x08) L2H = 1; |
CallumAlder | 33:f1dc3b160eac | 459 | if (~driveOut & 0x10) L3L = 0; |
CallumAlder | 33:f1dc3b160eac | 460 | if (~driveOut & 0x20) L3H = 1; |
CallumAlder | 33:f1dc3b160eac | 461 | |
CallumAlder | 33:f1dc3b160eac | 462 | //Then turn on |
CallumAlder | 33:f1dc3b160eac | 463 | if (driveOut & 0x01) L1L = 1; |
CallumAlder | 33:f1dc3b160eac | 464 | if (driveOut & 0x02) L1H = 0; |
CallumAlder | 33:f1dc3b160eac | 465 | if (driveOut & 0x04) L2L = 1; |
CallumAlder | 33:f1dc3b160eac | 466 | if (driveOut & 0x08) L2H = 0; |
CallumAlder | 33:f1dc3b160eac | 467 | if (driveOut & 0x10) L3L = 1; |
CallumAlder | 33:f1dc3b160eac | 468 | if (driveOut & 0x20) L3H = 0; |
CallumAlder | 33:f1dc3b160eac | 469 | } |
CallumAlder | 33:f1dc3b160eac | 470 | |
CallumAlder | 33:f1dc3b160eac | 471 | //Convert photointerrupter inputs to a rotor state |
CallumAlder | 33:f1dc3b160eac | 472 | inline int8_t readRotorState() { |
CallumAlder | 33:f1dc3b160eac | 473 | return stateMap[I1 + 2*I2 + 4*I3]; |
CallumAlder | 33:f1dc3b160eac | 474 | } |
CallumAlder | 33:f1dc3b160eac | 475 | |
CallumAlder | 33:f1dc3b160eac | 476 | //Basic synchronisation routine |
CallumAlder | 33:f1dc3b160eac | 477 | int8_t motorHome() { |
CallumAlder | 33:f1dc3b160eac | 478 | //Put the motor in drive state 0 and wait for it to stabilise |
CallumAlder | 33:f1dc3b160eac | 479 | motorOut(0); |
iachinweze1 | 38:a3713a09c828 | 480 | wait(3.0); |
CallumAlder | 33:f1dc3b160eac | 481 | |
CallumAlder | 33:f1dc3b160eac | 482 | //Get the rotor state |
CallumAlder | 33:f1dc3b160eac | 483 | return readRotorState(); |
CallumAlder | 33:f1dc3b160eac | 484 | } |
CallumAlder | 33:f1dc3b160eac | 485 | |
CallumAlder | 33:f1dc3b160eac | 486 | |
CallumAlder | 33:f1dc3b160eac | 487 | void stateUpdate() { // () { // **params |
CallumAlder | 33:f1dc3b160eac | 488 | currentState = readRotorState(); |
CallumAlder | 33:f1dc3b160eac | 489 | |
CallumAlder | 33:f1dc3b160eac | 490 | // Store into state counter |
CallumAlder | 33:f1dc3b160eac | 491 | if (currentState == theStates[0]) |
CallumAlder | 33:f1dc3b160eac | 492 | stateCount[0]++; |
CallumAlder | 33:f1dc3b160eac | 493 | else if (currentState == theStates[1]) |
CallumAlder | 33:f1dc3b160eac | 494 | stateCount[1]++; |
CallumAlder | 33:f1dc3b160eac | 495 | else if (currentState == theStates[2]) |
CallumAlder | 33:f1dc3b160eac | 496 | stateCount[2]++; |
CallumAlder | 33:f1dc3b160eac | 497 | |
CallumAlder | 33:f1dc3b160eac | 498 | |
CallumAlder | 33:f1dc3b160eac | 499 | // (Current - Offset + lead + 6) %6 |
CallumAlder | 33:f1dc3b160eac | 500 | motorOut((currentState - orState + lead + 6) % 6); |
CallumAlder | 33:f1dc3b160eac | 501 | |
CallumAlder | 33:f1dc3b160eac | 502 | } |
CallumAlder | 33:f1dc3b160eac | 503 | |
CallumAlder | 33:f1dc3b160eac | 504 | |
CallumAlder | 33:f1dc3b160eac | 505 | |
CallumAlder | 33:f1dc3b160eac | 506 | // attach_us -> runs funtion every 100ms |
CallumAlder | 33:f1dc3b160eac | 507 | void motorCtrlFn() { |
CallumAlder | 33:f1dc3b160eac | 508 | Ticker motorCtrlTicker; |
adehadd | 40:9fae84f111e6 | 509 | Timer m_timer; |
CallumAlder | 33:f1dc3b160eac | 510 | motorCtrlTicker.attach_us(callback(this,&Motor::motorCtrlTick), 1e5); |
adehadd | 34:2c6f635cc8e7 | 511 | |
adehadd | 34:2c6f635cc8e7 | 512 | // Init some things |
adehadd | 34:2c6f635cc8e7 | 513 | uint8_t cpyStateCount[3]; |
adehadd | 34:2c6f635cc8e7 | 514 | uint8_t cpyCurrentState; |
adehadd | 39:05a021718517 | 515 | int8_t cpyModeBitfield; |
adehadd | 34:2c6f635cc8e7 | 516 | |
adehadd | 40:9fae84f111e6 | 517 | int32_t ting[2] = {6,1}; // 360,60 (for degrees), 5,1 (for states) |
adehadd | 34:2c6f635cc8e7 | 518 | uint8_t iterElementMax; |
adehadd | 39:05a021718517 | 519 | int32_t totalDegrees; |
adehadd | 39:05a021718517 | 520 | int32_t stateDiff; |
adehadd | 34:2c6f635cc8e7 | 521 | |
adehadd | 40:9fae84f111e6 | 522 | int32_t cur_speed; //Variable for local velocity calculation |
adehadd | 34:2c6f635cc8e7 | 523 | int32_t locMotorPos; //Local copy of motor position |
iachinweze1 | 38:a3713a09c828 | 524 | // static int32_t oldMotorPos = 0; //Old motor position used for calculations |
iachinweze1 | 38:a3713a09c828 | 525 | // static uint8_t motorCtrlCounter = 0; //Counter to be reset every 10 iterations to get velocity calculation in seconds |
adehadd | 39:05a021718517 | 526 | volatile int32_t torque; //Local variable to set motor torque |
adehadd | 40:9fae84f111e6 | 527 | static int32_t oldTorque =0; |
adehadd | 34:2c6f635cc8e7 | 528 | float sError; //Velocity error between target and reality |
adehadd | 34:2c6f635cc8e7 | 529 | float rError; //Rotation error between target and reality |
adehadd | 34:2c6f635cc8e7 | 530 | static float rErrorOld; //Old rotation error used for calculation |
adehadd | 34:2c6f635cc8e7 | 531 | |
adehadd | 34:2c6f635cc8e7 | 532 | //~~~Controller constants~~~~ |
adehadd | 34:2c6f635cc8e7 | 533 | int32_t Kp1=22; //Proportional controller constants |
adehadd | 34:2c6f635cc8e7 | 534 | int32_t Kp2=22; //Calculated by trial and error to give optimal accuracy |
adehadd | 34:2c6f635cc8e7 | 535 | float Kd=15.5; |
adehadd | 34:2c6f635cc8e7 | 536 | |
adehadd | 39:05a021718517 | 537 | |
adehadd | 39:05a021718517 | 538 | int32_t Ys; //Initialise controller output Ys (s=speed) |
adehadd | 39:05a021718517 | 539 | int32_t Yr; //Initialise controller output Yr (r=rotations) |
adehadd | 34:2c6f635cc8e7 | 540 | |
adehadd | 40:9fae84f111e6 | 541 | int32_t old_pos = 0; |
adehadd | 40:9fae84f111e6 | 542 | |
adehadd | 40:9fae84f111e6 | 543 | uint32_t cur_time = 0, |
adehadd | 40:9fae84f111e6 | 544 | old_time = 0, |
adehadd | 40:9fae84f111e6 | 545 | time_diff; |
adehadd | 40:9fae84f111e6 | 546 | |
adehadd | 40:9fae84f111e6 | 547 | float cur_err = 0.0f, |
adehadd | 40:9fae84f111e6 | 548 | old_err = 0.0f, |
adehadd | 40:9fae84f111e6 | 549 | err_diff; |
adehadd | 40:9fae84f111e6 | 550 | |
adehadd | 40:9fae84f111e6 | 551 | m_timer.start(); |
adehadd | 40:9fae84f111e6 | 552 | |
adehadd | 34:2c6f635cc8e7 | 553 | while (_RUN) { |
CallumAlder | 33:f1dc3b160eac | 554 | t_motor_ctrl.signal_wait((int32_t)0x1); |
adehadd | 40:9fae84f111e6 | 555 | |
adehadd | 34:2c6f635cc8e7 | 556 | core_util_critical_section_enter(); |
adehadd | 39:05a021718517 | 557 | cpyModeBitfield = p_comm->modeBitfield; |
adehadd | 40:9fae84f111e6 | 558 | // p_comm->modeBitfield = 0; // nah |
adehadd | 34:2c6f635cc8e7 | 559 | //Access shared variables here |
adehadd | 34:2c6f635cc8e7 | 560 | std::copy(stateCount, stateCount+3, cpyStateCount); |
adehadd | 34:2c6f635cc8e7 | 561 | cpyCurrentState = currentState; |
adehadd | 34:2c6f635cc8e7 | 562 | for (int i = 0; i < 3; ++i) { |
adehadd | 34:2c6f635cc8e7 | 563 | stateCount[i] = 0; |
adehadd | 34:2c6f635cc8e7 | 564 | } |
adehadd | 34:2c6f635cc8e7 | 565 | core_util_critical_section_exit(); |
adehadd | 34:2c6f635cc8e7 | 566 | |
adehadd | 40:9fae84f111e6 | 567 | // read state & timestamp |
adehadd | 40:9fae84f111e6 | 568 | cur_time = m_timer.read(); |
adehadd | 40:9fae84f111e6 | 569 | |
adehadd | 40:9fae84f111e6 | 570 | // compute speed |
adehadd | 40:9fae84f111e6 | 571 | time_diff = cur_time - old_time; |
adehadd | 40:9fae84f111e6 | 572 | // cur_speed = (cur_pos - old_pos) / time_diff; |
adehadd | 40:9fae84f111e6 | 573 | |
adehadd | 40:9fae84f111e6 | 574 | // prep values for next time through loop |
adehadd | 40:9fae84f111e6 | 575 | old_time = cur_time; |
adehadd | 40:9fae84f111e6 | 576 | old_pos = cpyCurrentState; |
adehadd | 40:9fae84f111e6 | 577 | |
adehadd | 40:9fae84f111e6 | 578 | |
adehadd | 34:2c6f635cc8e7 | 579 | iterElementMax = std::max_element(cpyStateCount, cpyStateCount+3) - cpyStateCount; |
adehadd | 34:2c6f635cc8e7 | 580 | |
adehadd | 34:2c6f635cc8e7 | 581 | |
adehadd | 34:2c6f635cc8e7 | 582 | totalDegrees = ting[0] * cpyStateCount[iterElementMax]; |
adehadd | 34:2c6f635cc8e7 | 583 | stateDiff = theStates[iterElementMax]-cpyCurrentState; |
adehadd | 34:2c6f635cc8e7 | 584 | if (stateDiff >= 0) { |
adehadd | 34:2c6f635cc8e7 | 585 | totalDegrees = totalDegrees + (ting[1]* stateDiff); |
adehadd | 34:2c6f635cc8e7 | 586 | } else { |
adehadd | 34:2c6f635cc8e7 | 587 | totalDegrees = totalDegrees + (ting[1]*stateDiff*-1); |
adehadd | 34:2c6f635cc8e7 | 588 | } |
CallumAlder | 35:132413ec3d65 | 589 | //p_comm->pc.printf("%u,%u,%u,%u. %.6i \r", iterElementMax, cpyStateCount[0],cpyStateCount[1],cpyStateCount[2], (totalDegrees*10)); |
adehadd | 34:2c6f635cc8e7 | 590 | |
adehadd | 39:05a021718517 | 591 | if ((cpyModeBitfield & 0x01) | (cpyModeBitfield & 0x02)) { |
adehadd | 39:05a021718517 | 592 | //~~~~~Speed controller~~~~~~ |
adehadd | 40:9fae84f111e6 | 593 | cur_speed = totalDegrees / time_diff; |
adehadd | 40:9fae84f111e6 | 594 | sError = (p_comm->targetVel * 6) - abs(cur_speed); //Read global variable targetVel updated by interrupt and calculate error between target and reality |
adehadd | 39:05a021718517 | 595 | |
adehadd | 40:9fae84f111e6 | 596 | if (sError == -abs(cur_speed)) { //Check if user entered V0, |
adehadd | 39:05a021718517 | 597 | Ys = MAXPWM_PRD; //and set the output to maximum as specified |
adehadd | 39:05a021718517 | 598 | } else { |
adehadd | 39:05a021718517 | 599 | Ys = (int32_t)(Kp1 * sError); //If the user didn't enter V0 implement controller transfer function: Ys = Kp * (s -|v|) where, |
adehadd | 39:05a021718517 | 600 | } //Ys = controller output, Kp = prop controller constant, s = target velocity and v is the measured velocity |
adehadd | 34:2c6f635cc8e7 | 601 | |
adehadd | 40:9fae84f111e6 | 602 | // } else if (cpyModeBitfield & 0x02) { |
adehadd | 39:05a021718517 | 603 | //~~~~~Rotation control~~~~~~ |
adehadd | 40:9fae84f111e6 | 604 | rError = (p_comm->targetRot)*6 - totalDegrees; //Read global variable targetRot updated by interrupt and calculate the rotation error. |
adehadd | 39:05a021718517 | 605 | Yr = Kp2*rError + Kd*(rError - rErrorOld); //Implement controller transfer function Ys= Kp*Er + Kd* (dEr/dt) |
adehadd | 39:05a021718517 | 606 | rErrorOld = rError; //Update rotation error |
adehadd | 40:9fae84f111e6 | 607 | // if(rError < 0) //Use the sign of the error to set controller wrt direction of rotation |
adehadd | 40:9fae84f111e6 | 608 | // Ys = -Ys; |
adehadd | 34:2c6f635cc8e7 | 609 | |
adehadd | 40:9fae84f111e6 | 610 | Ys = Ys * sgn(rError); |
adehadd | 40:9fae84f111e6 | 611 | // select minimum absolute value torque |
adehadd | 40:9fae84f111e6 | 612 | if (cur_speed < 0) |
adehadd | 40:9fae84f111e6 | 613 | torque = max(Ys, Yr); |
adehadd | 40:9fae84f111e6 | 614 | else |
adehadd | 40:9fae84f111e6 | 615 | torque = min(Ys, Yr); |
adehadd | 40:9fae84f111e6 | 616 | |
adehadd | 40:9fae84f111e6 | 617 | if(torque < 0) { //Variable torque cannot be negative since it sets the PWM |
adehadd | 40:9fae84f111e6 | 618 | torque = -torque; lead = -2; //Hence we make the value positive, |
adehadd | 40:9fae84f111e6 | 619 | } else //and instead set the direction to the opposite one |
adehadd | 39:05a021718517 | 620 | lead = 2; |
adehadd | 40:9fae84f111e6 | 621 | |
adehadd | 39:05a021718517 | 622 | if(torque > MAXPWM_PRD){ //In case the calculated PWM is higher than our maximum 50% allowance, |
adehadd | 39:05a021718517 | 623 | torque = MAXPWM_PRD; //Set it to our max. |
adehadd | 39:05a021718517 | 624 | } |
adehadd | 39:05a021718517 | 625 | |
adehadd | 39:05a021718517 | 626 | p_comm->motorPower = torque; |
adehadd | 39:05a021718517 | 627 | pwmCtrl.pulsewidth_us(p_comm->motorPower); |
adehadd | 34:2c6f635cc8e7 | 628 | } |
adehadd | 40:9fae84f111e6 | 629 | |
adehadd | 40:9fae84f111e6 | 630 | if (cpyModeBitfield & 0x04) { // if it is in torque mode, do no math, just set pulsewidth |
adehadd | 40:9fae84f111e6 | 631 | torque = (int32_t)p_comm->motorPower; |
adehadd | 40:9fae84f111e6 | 632 | if (oldTorque != torque) { |
adehadd | 40:9fae84f111e6 | 633 | if(torque < 0){ //Variable torque cannot be negative since it sets the PWM |
adehadd | 40:9fae84f111e6 | 634 | torque = -torque; //Hence we make the value positive, |
adehadd | 40:9fae84f111e6 | 635 | lead = -2; //and instead set the direction to the opposite one |
adehadd | 40:9fae84f111e6 | 636 | } else { |
adehadd | 40:9fae84f111e6 | 637 | lead = 2; |
adehadd | 40:9fae84f111e6 | 638 | } |
adehadd | 40:9fae84f111e6 | 639 | if(torque > MAXPWM_PRD){ //In case the calculated PWM is higher than our maximum 50% allowance, |
adehadd | 40:9fae84f111e6 | 640 | torque = MAXPWM_PRD; //Set it to our max. |
adehadd | 40:9fae84f111e6 | 641 | |
adehadd | 40:9fae84f111e6 | 642 | } |
adehadd | 40:9fae84f111e6 | 643 | p_comm->putMessage((Comm::msgType)8, torque); |
adehadd | 40:9fae84f111e6 | 644 | p_comm->motorPower = torque; |
adehadd | 40:9fae84f111e6 | 645 | pwmCtrl.pulsewidth_us(torque); |
adehadd | 40:9fae84f111e6 | 646 | oldTorque = torque; |
adehadd | 40:9fae84f111e6 | 647 | } |
adehadd | 40:9fae84f111e6 | 648 | } else { // if not Torque mode |
adehadd | 40:9fae84f111e6 | 649 | //balls |
adehadd | 40:9fae84f111e6 | 650 | } |
iachinweze1 | 38:a3713a09c828 | 651 | // pwmCtrl.write((float)(p_comm->motorPower/MAXPWM_PRD)); |
iachinweze1 | 38:a3713a09c828 | 652 | // p_comm->motorPower = torque; //Lastly, update global variable motorPower which is updated by interrupt |
iachinweze1 | 38:a3713a09c828 | 653 | // p_comm->pc.printf("\t\t\t\t\t\t %i, %i, %i \r", torque, Ys, Yr); |
iachinweze1 | 37:71adabab284a | 654 | //p_comm->pc.printf("%u,%u,%u,%u. %.6i \r", iterElementMax, cpyStateCount[0],cpyStateCount[1],cpyStateCount[2], (totalDegrees*10)); |
CallumAlder | 33:f1dc3b160eac | 655 | } |
CallumAlder | 33:f1dc3b160eac | 656 | } |
CallumAlder | 33:f1dc3b160eac | 657 | |
CallumAlder | 33:f1dc3b160eac | 658 | void motorCtrlTick(){ |
CallumAlder | 33:f1dc3b160eac | 659 | t_motor_ctrl.signal_set(0x1); |
CallumAlder | 33:f1dc3b160eac | 660 | } |
CallumAlder | 29:c96439a60184 | 661 | }; |
adehadd | 20:c60f4785b556 | 662 | |
adehadd | 20:c60f4785b556 | 663 | |
estott | 0:de4320f74764 | 664 | int main() { |
CallumAlder | 19:805c87360b55 | 665 | |
CallumAlder | 32:fc5e00d9f74d | 666 | // Declare Objects |
CallumAlder | 32:fc5e00d9f74d | 667 | Comm comm_port; |
CallumAlder | 32:fc5e00d9f74d | 668 | SHA256 miner; |
CallumAlder | 32:fc5e00d9f74d | 669 | Motor motor; |
CallumAlder | 32:fc5e00d9f74d | 670 | |
CallumAlder | 32:fc5e00d9f74d | 671 | // Start Motor and Comm Port |
CallumAlder | 32:fc5e00d9f74d | 672 | motor.motorStart(&comm_port); |
CallumAlder | 32:fc5e00d9f74d | 673 | comm_port.start_comm(); |
adehadd | 20:c60f4785b556 | 674 | |
CallumAlder | 32:fc5e00d9f74d | 675 | // Declare Hash Variables |
CallumAlder | 14:4e312fb83330 | 676 | uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64, |
CallumAlder | 14:4e312fb83330 | 677 | 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73, |
CallumAlder | 14:4e312fb83330 | 678 | 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E, |
CallumAlder | 14:4e312fb83330 | 679 | 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20, |
CallumAlder | 14:4e312fb83330 | 680 | 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20, |
CallumAlder | 14:4e312fb83330 | 681 | 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20, |
CallumAlder | 14:4e312fb83330 | 682 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
CallumAlder | 14:4e312fb83330 | 683 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; |
CallumAlder | 14:4e312fb83330 | 684 | uint64_t* key = (uint64_t*)((int)sequence + 48); |
CallumAlder | 14:4e312fb83330 | 685 | uint64_t* nonce = (uint64_t*)((int)sequence + 56); |
CallumAlder | 14:4e312fb83330 | 686 | uint8_t hash[32]; |
CallumAlder | 14:4e312fb83330 | 687 | uint32_t length64 = 64; |
CallumAlder | 15:2f95f2fb68e3 | 688 | uint32_t hashCounter = 0; |
CallumAlder | 32:fc5e00d9f74d | 689 | |
CallumAlder | 32:fc5e00d9f74d | 690 | // Begin Main Timer |
iachinweze1 | 23:ab1cb51527d1 | 691 | Timer timer; |
CallumAlder | 32:fc5e00d9f74d | 692 | timer.start(); |
CallumAlder | 29:c96439a60184 | 693 | |
CallumAlder | 32:fc5e00d9f74d | 694 | // Loop Program |
CallumAlder | 15:2f95f2fb68e3 | 695 | while (1) { |
CallumAlder | 32:fc5e00d9f74d | 696 | |
CallumAlder | 32:fc5e00d9f74d | 697 | // Mutex For Access Control |
CallumAlder | 32:fc5e00d9f74d | 698 | comm_port.newKey_mutex.lock(); |
CallumAlder | 32:fc5e00d9f74d | 699 | *key = comm_port.newKey; |
CallumAlder | 32:fc5e00d9f74d | 700 | comm_port.newKey_mutex.unlock(); |
CallumAlder | 32:fc5e00d9f74d | 701 | |
CallumAlder | 32:fc5e00d9f74d | 702 | // Compute Hash and Counter |
CallumAlder | 32:fc5e00d9f74d | 703 | miner.computeHash(hash, sequence, length64); |
CallumAlder | 15:2f95f2fb68e3 | 704 | hashCounter++; |
CallumAlder | 32:fc5e00d9f74d | 705 | |
CallumAlder | 32:fc5e00d9f74d | 706 | // Enum Casting and Condition |
CallumAlder | 15:2f95f2fb68e3 | 707 | if ((hash[0]==0) && (hash[1]==0)){ |
CallumAlder | 32:fc5e00d9f74d | 708 | comm_port.putMessage((Comm::msgType)7, *nonce); |
CallumAlder | 15:2f95f2fb68e3 | 709 | } |
CallumAlder | 15:2f95f2fb68e3 | 710 | |
CallumAlder | 32:fc5e00d9f74d | 711 | // Try Nonce |
CallumAlder | 15:2f95f2fb68e3 | 712 | (*nonce)++; |
CallumAlder | 32:fc5e00d9f74d | 713 | |
CallumAlder | 32:fc5e00d9f74d | 714 | // Display via Comm Port |
CallumAlder | 15:2f95f2fb68e3 | 715 | if (timer.read() >= 1){ |
CallumAlder | 32:fc5e00d9f74d | 716 | comm_port.putMessage((Comm::msgType)5, hashCounter); |
CallumAlder | 15:2f95f2fb68e3 | 717 | hashCounter=0; |
CallumAlder | 15:2f95f2fb68e3 | 718 | timer.reset(); |
CallumAlder | 15:2f95f2fb68e3 | 719 | } |
CallumAlder | 15:2f95f2fb68e3 | 720 | } |
CallumAlder | 33:f1dc3b160eac | 721 | |
CallumAlder | 33:f1dc3b160eac | 722 | return 0; |
CallumAlder | 33:f1dc3b160eac | 723 | |
CallumAlder | 19:805c87360b55 | 724 | } |