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