An embedded device
Dependencies: Crypto
main.cpp@19:ca08111237ab, 2019-03-19 (annotated)
- Committer:
- cz3015
- Date:
- Tue Mar 19 18:05:37 2019 +0000
- Revision:
- 19:ca08111237ab
- Parent:
- 10:a4b5723b6c9d
direction + speed control needed; integration constant needed to be tested; start engine mechanism needed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
estott | 0:de4320f74764 | 1 | #include "mbed.h" |
cz3015 | 19:ca08111237ab | 2 | #include "Crypto.h" |
cz3015 | 19:ca08111237ab | 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 |
cz3015 | 19:ca08111237ab | 8 | |
estott | 2:4e88faab6988 | 9 | //Incremental encoder input pins |
cz3015 | 19:ca08111237ab | 10 | #define CHApin D12 |
cz3015 | 19:ca08111237ab | 11 | #define CHBpin D11 |
cz3015 | 19:ca08111237ab | 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 |
cz3015 | 19:ca08111237ab | 17 | #define L2Hpin A6 //0x08 |
cz3015 | 19:ca08111237ab | 18 | #define L3Lpin D10 //0x10 |
cz3015 | 19:ca08111237ab | 19 | #define L3Hpin D2 //0x20 |
cz3015 | 19:ca08111237ab | 20 | |
estott | 10:a4b5723b6c9d | 21 | #define PWMpin D9 |
cz3015 | 19:ca08111237ab | 22 | |
estott | 5:08f338b5e4d9 | 23 | //Motor current sense |
cz3015 | 19:ca08111237ab | 24 | #define MCSPpin A1 |
cz3015 | 19:ca08111237ab | 25 | #define MCSNpin A0 |
cz3015 | 19:ca08111237ab | 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}; |
cz3015 | 19:ca08111237ab | 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 |
cz3015 | 19:ca08111237ab | 45 | |
estott | 2:4e88faab6988 | 46 | //Phase lead to make motor spin |
estott | 3:569b35e2a602 | 47 | const int8_t lead = 2; //2 for forwards, -2 for backwards |
cz3015 | 19:ca08111237ab | 48 | |
estott | 0:de4320f74764 | 49 | //Status LED |
estott | 0:de4320f74764 | 50 | DigitalOut led1(LED1); |
cz3015 | 19:ca08111237ab | 51 | |
estott | 0:de4320f74764 | 52 | //Photointerrupter inputs |
cz3015 | 19:ca08111237ab | 53 | InterruptIn I1(I1pin); |
cz3015 | 19:ca08111237ab | 54 | InterruptIn I2(I2pin); |
cz3015 | 19:ca08111237ab | 55 | InterruptIn I3(I3pin); |
cz3015 | 19:ca08111237ab | 56 | |
estott | 0:de4320f74764 | 57 | //Motor Drive outputs |
cz3015 | 19:ca08111237ab | 58 | PwmOut L1L(L1Lpin); |
estott | 0:de4320f74764 | 59 | DigitalOut L1H(L1Hpin); |
cz3015 | 19:ca08111237ab | 60 | PwmOut L2L(L2Lpin); |
estott | 0:de4320f74764 | 61 | DigitalOut L2H(L2Hpin); |
cz3015 | 19:ca08111237ab | 62 | PwmOut L3L(L3Lpin); |
estott | 0:de4320f74764 | 63 | DigitalOut L3H(L3Hpin); |
cz3015 | 19:ca08111237ab | 64 | |
cz3015 | 19:ca08111237ab | 65 | |
cz3015 | 19:ca08111237ab | 66 | int8_t orState = 0; |
cz3015 | 19:ca08111237ab | 67 | int8_t intState = 0; |
cz3015 | 19:ca08111237ab | 68 | int8_t intStateOld = 0; |
cz3015 | 19:ca08111237ab | 69 | int32_t revoCounter = 0; //Counts the number of revolutions |
cz3015 | 19:ca08111237ab | 70 | int32_t motorVelocity; |
cz3015 | 19:ca08111237ab | 71 | //Phase lead to make motor spin |
cz3015 | 19:ca08111237ab | 72 | int8_t lead = -2; //2 for forwards, -2 for backwards |
cz3015 | 19:ca08111237ab | 73 | |
cz3015 | 19:ca08111237ab | 74 | typedef struct { |
cz3015 | 19:ca08111237ab | 75 | uint64_t nonce; |
cz3015 | 19:ca08111237ab | 76 | float data; |
cz3015 | 19:ca08111237ab | 77 | } mail_t; |
cz3015 | 19:ca08111237ab | 78 | |
cz3015 | 19:ca08111237ab | 79 | Mail<mail_t, 16> mail_box; |
cz3015 | 19:ca08111237ab | 80 | Thread commandProcessorthread; |
cz3015 | 19:ca08111237ab | 81 | Thread bitcointhread; |
cz3015 | 19:ca08111237ab | 82 | RawSerial pc(SERIAL_TX, SERIAL_RX); |
cz3015 | 19:ca08111237ab | 83 | Queue<void, 8> inCharQ; |
cz3015 | 19:ca08111237ab | 84 | Mutex newKey_mutex; |
cz3015 | 19:ca08111237ab | 85 | uint64_t newKey = 0; |
cz3015 | 19:ca08111237ab | 86 | |
cz3015 | 19:ca08111237ab | 87 | volatile float newRev; |
cz3015 | 19:ca08111237ab | 88 | volatile float maxSpeed = 300; |
cz3015 | 19:ca08111237ab | 89 | uint32_t pulseWidth; |
cz3015 | 19:ca08111237ab | 90 | float motorPosition_command; |
cz3015 | 19:ca08111237ab | 91 | float motorPosition; |
cz3015 | 19:ca08111237ab | 92 | |
cz3015 | 19:ca08111237ab | 93 | // mail to queue messages for serial port |
cz3015 | 19:ca08111237ab | 94 | void putMessage(uint64_t *nonce,float data){ |
cz3015 | 19:ca08111237ab | 95 | mail_t *mail = mail_box.alloc(); |
cz3015 | 19:ca08111237ab | 96 | mail->nonce = *nonce; |
cz3015 | 19:ca08111237ab | 97 | mail->data = *data; |
cz3015 | 19:ca08111237ab | 98 | mail_box.put(mail); |
cz3015 | 19:ca08111237ab | 99 | } |
cz3015 | 19:ca08111237ab | 100 | |
cz3015 | 19:ca08111237ab | 101 | void serialISR() { |
cz3015 | 19:ca08111237ab | 102 | uint8_t newChar = pc.getc(); |
cz3015 | 19:ca08111237ab | 103 | inCharQ.put((void*) newChar); |
cz3015 | 19:ca08111237ab | 104 | } |
cz3015 | 19:ca08111237ab | 105 | |
cz3015 | 19:ca08111237ab | 106 | void commandProcessor() { |
cz3015 | 19:ca08111237ab | 107 | pc.attach(&serialISR); |
cz3015 | 19:ca08111237ab | 108 | char command[19]; |
cz3015 | 19:ca08111237ab | 109 | char* number; |
cz3015 | 19:ca08111237ab | 110 | //char k; |
cz3015 | 19:ca08111237ab | 111 | uint64_t receivedKey; |
cz3015 | 19:ca08111237ab | 112 | uint8_t index = 0; |
cz3015 | 19:ca08111237ab | 113 | while(1) { |
cz3015 | 19:ca08111237ab | 114 | osEvent newEvent = inCharQ.get(); |
cz3015 | 19:ca08111237ab | 115 | uint8_t newChar = (uint8_t) newEvent.value.p; |
cz3015 | 19:ca08111237ab | 116 | command[index] = newChar; |
cz3015 | 19:ca08111237ab | 117 | index++; |
cz3015 | 19:ca08111237ab | 118 | if (newChar == '\r') { |
cz3015 | 19:ca08111237ab | 119 | command[17] = '\0'; |
cz3015 | 19:ca08111237ab | 120 | |
cz3015 | 19:ca08111237ab | 121 | if (command[0] == 'R') { |
cz3015 | 19:ca08111237ab | 122 | pc.printf("Rotation command\n"); |
cz3015 | 19:ca08111237ab | 123 | |
cz3015 | 19:ca08111237ab | 124 | pc.printf("%s", command); |
cz3015 | 19:ca08111237ab | 125 | } |
cz3015 | 19:ca08111237ab | 126 | else if (command[0] == 'V') { |
cz3015 | 19:ca08111237ab | 127 | pc.printf("Max speed command\n"); |
cz3015 | 19:ca08111237ab | 128 | pc.printf("%s", command); |
cz3015 | 19:ca08111237ab | 129 | } |
cz3015 | 19:ca08111237ab | 130 | else if (command[0] == 'K') { |
cz3015 | 19:ca08111237ab | 131 | if (index == 18){ // when index is 18 means you entered K and 16 digits |
cz3015 | 19:ca08111237ab | 132 | number = command +1; //super bad solution, but I don't know how to work with strings in C |
cz3015 | 19:ca08111237ab | 133 | receivedKey = strtoull(number, NULL, 16); |
cz3015 | 19:ca08111237ab | 134 | //receivedKey = 2147483648; |
cz3015 | 19:ca08111237ab | 135 | //sscanf(command, "%d", &receivedKey); |
cz3015 | 19:ca08111237ab | 136 | pc.printf("Received key: %016llx\n\r", receivedKey); |
cz3015 | 19:ca08111237ab | 137 | newKey_mutex.lock(); |
cz3015 | 19:ca08111237ab | 138 | newKey = receivedKey; |
cz3015 | 19:ca08111237ab | 139 | newKey_mutex.unlock(); |
cz3015 | 19:ca08111237ab | 140 | } else { |
cz3015 | 19:ca08111237ab | 141 | pc.printf("Not a valid key!"); |
cz3015 | 19:ca08111237ab | 142 | }; |
cz3015 | 19:ca08111237ab | 143 | } |
cz3015 | 19:ca08111237ab | 144 | else if (command[0] == 'T') { |
cz3015 | 19:ca08111237ab | 145 | pc.printf("Melody command\n"); |
cz3015 | 19:ca08111237ab | 146 | pc.printf("%s", command); |
cz3015 | 19:ca08111237ab | 147 | } |
cz3015 | 19:ca08111237ab | 148 | memset(command, 0, sizeof(command)); |
cz3015 | 19:ca08111237ab | 149 | index = 0; |
cz3015 | 19:ca08111237ab | 150 | } else { |
cz3015 | 19:ca08111237ab | 151 | pc.printf("Current command: %s\n\r", command); |
cz3015 | 19:ca08111237ab | 152 | } |
cz3015 | 19:ca08111237ab | 153 | } |
cz3015 | 19:ca08111237ab | 154 | } |
cz3015 | 19:ca08111237ab | 155 | |
cz3015 | 19:ca08111237ab | 156 | void bitcoin(){ |
cz3015 | 19:ca08111237ab | 157 | while(1) { |
cz3015 | 19:ca08111237ab | 158 | SHA256 sha; |
cz3015 | 19:ca08111237ab | 159 | uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64, |
cz3015 | 19:ca08111237ab | 160 | 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73, |
cz3015 | 19:ca08111237ab | 161 | 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E, |
cz3015 | 19:ca08111237ab | 162 | 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20, |
cz3015 | 19:ca08111237ab | 163 | 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20, |
cz3015 | 19:ca08111237ab | 164 | 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20, |
cz3015 | 19:ca08111237ab | 165 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
cz3015 | 19:ca08111237ab | 166 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; |
cz3015 | 19:ca08111237ab | 167 | uint64_t* key = (uint64_t*) ((int) sequence + 48); |
cz3015 | 19:ca08111237ab | 168 | uint64_t* nonce = (uint64_t*) ((int) sequence + 56); |
cz3015 | 19:ca08111237ab | 169 | uint8_t hash[32]; |
cz3015 | 19:ca08111237ab | 170 | |
cz3015 | 19:ca08111237ab | 171 | Timer t; |
cz3015 | 19:ca08111237ab | 172 | t.start(); |
cz3015 | 19:ca08111237ab | 173 | unsigned currentTime = 0; |
cz3015 | 19:ca08111237ab | 174 | unsigned currentCount = 0; |
cz3015 | 19:ca08111237ab | 175 | |
cz3015 | 19:ca08111237ab | 176 | for (unsigned i = 0; i <= UINT_MAX; i++) { |
cz3015 | 19:ca08111237ab | 177 | (*nonce)++; |
cz3015 | 19:ca08111237ab | 178 | newKey_mutex.lock(); |
cz3015 | 19:ca08111237ab | 179 | *key = newKey; |
cz3015 | 19:ca08111237ab | 180 | newKey_mutex.unlock(); |
cz3015 | 19:ca08111237ab | 181 | sha.computeHash(hash, sequence, 64); |
cz3015 | 19:ca08111237ab | 182 | if (hash[0] == 0 && hash[1] == 0) { |
cz3015 | 19:ca08111237ab | 183 | //putMessage(nonce); |
cz3015 | 19:ca08111237ab | 184 | pc.printf("Successful nonce: %016x\n\r", *nonce); |
cz3015 | 19:ca08111237ab | 185 | } |
cz3015 | 19:ca08111237ab | 186 | if ((unsigned) t.read() == currentTime) { |
cz3015 | 19:ca08111237ab | 187 | //pc.printf("Hash rate: %d\n\r", i - currentCount); |
cz3015 | 19:ca08111237ab | 188 | pc.printf("Current key: %016llx\n\r", *key); |
cz3015 | 19:ca08111237ab | 189 | currentTime++; |
cz3015 | 19:ca08111237ab | 190 | currentCount = i; |
cz3015 | 19:ca08111237ab | 191 | } |
cz3015 | 19:ca08111237ab | 192 | } |
cz3015 | 19:ca08111237ab | 193 | t.stop(); |
cz3015 | 19:ca08111237ab | 194 | } |
cz3015 | 19:ca08111237ab | 195 | } |
cz3015 | 19:ca08111237ab | 196 | |
cz3015 | 19:ca08111237ab | 197 | |
cz3015 | 19:ca08111237ab | 198 | |
cz3015 | 19:ca08111237ab | 199 | void motorCtrlTick(){ |
cz3015 | 19:ca08111237ab | 200 | motorCtrlT.signal_set(0x1); |
cz3015 | 19:ca08111237ab | 201 | } |
cz3015 | 19:ca08111237ab | 202 | |
cz3015 | 19:ca08111237ab | 203 | |
estott | 0:de4320f74764 | 204 | //Set a given drive state |
cz3015 | 19:ca08111237ab | 205 | void motorOut(int8_t driveState,uint32_t motorTorque){ |
estott | 0:de4320f74764 | 206 | |
estott | 2:4e88faab6988 | 207 | //Lookup the output byte from the drive state. |
estott | 2:4e88faab6988 | 208 | int8_t driveOut = driveTable[driveState & 0x07]; |
estott | 2:4e88faab6988 | 209 | |
estott | 2:4e88faab6988 | 210 | //Turn off first |
cz3015 | 19:ca08111237ab | 211 | if (~driveOut & 0x01) L1L.pulsewidth(0); |
estott | 2:4e88faab6988 | 212 | if (~driveOut & 0x02) L1H = 1; |
cz3015 | 19:ca08111237ab | 213 | if (~driveOut & 0x04) L2L.pulsewidth(0); |
estott | 2:4e88faab6988 | 214 | if (~driveOut & 0x08) L2H = 1; |
cz3015 | 19:ca08111237ab | 215 | if (~driveOut & 0x10) L3L.pulsewidth(0); |
estott | 2:4e88faab6988 | 216 | if (~driveOut & 0x20) L3H = 1; |
estott | 2:4e88faab6988 | 217 | |
estott | 2:4e88faab6988 | 218 | //Then turn on |
cz3015 | 19:ca08111237ab | 219 | if (driveOut & 0x01) L1L.pulsewidth(motorTorque); |
estott | 2:4e88faab6988 | 220 | if (driveOut & 0x02) L1H = 0; |
cz3015 | 19:ca08111237ab | 221 | if (driveOut & 0x04) L2L.pulsewidth(motorTorque); |
estott | 2:4e88faab6988 | 222 | if (driveOut & 0x08) L2H = 0; |
cz3015 | 19:ca08111237ab | 223 | if (driveOut & 0x10) L3L.pulsewidth(motorTorque); |
estott | 2:4e88faab6988 | 224 | if (driveOut & 0x20) L3H = 0; |
estott | 0:de4320f74764 | 225 | } |
estott | 0:de4320f74764 | 226 | |
cz3015 | 19:ca08111237ab | 227 | //Convert photointerrupter inputs to a rotor state |
cz3015 | 19:ca08111237ab | 228 | inline int8_t readRotorState(){ |
cz3015 | 19:ca08111237ab | 229 | return stateMap[I1 + 2*I2 + 4*I3]; |
cz3015 | 19:ca08111237ab | 230 | } |
cz3015 | 19:ca08111237ab | 231 | |
cz3015 | 19:ca08111237ab | 232 | int8_t motorHome() { |
cz3015 | 19:ca08111237ab | 233 | //Put the motor in drive state 0 and wait for it to stabilize |
cz3015 | 19:ca08111237ab | 234 | L1L.period(2000); |
cz3015 | 19:ca08111237ab | 235 | L2L.period(2000); |
cz3015 | 19:ca08111237ab | 236 | L3L.period(2000); |
cz3015 | 19:ca08111237ab | 237 | motorOut(0,200); |
cz3015 | 19:ca08111237ab | 238 | wait(2.0); |
cz3015 | 19:ca08111237ab | 239 | return readRotorState(); |
cz3015 | 19:ca08111237ab | 240 | } |
cz3015 | 19:ca08111237ab | 241 | |
cz3015 | 19:ca08111237ab | 242 | //orState is subtracted from future rotor state inputs to align rotor and motor states |
cz3015 | 19:ca08111237ab | 243 | int8_t orState = motorHome(); |
cz3015 | 19:ca08111237ab | 244 | // ISR to handle the updating of the motor position |
cz3015 | 19:ca08111237ab | 245 | void motorISR() { |
cz3015 | 19:ca08111237ab | 246 | static int8_t oldRotorState; |
cz3015 | 19:ca08111237ab | 247 | int8_t rotorState = readRotorState(); |
cz3015 | 19:ca08111237ab | 248 | motorOut((rotorState-orState+lead+6)%6,pulseWidth); //+6 to make sure the remainder is positive |
cz3015 | 19:ca08111237ab | 249 | if (rotorState - oldRotorState == 5) motorPosition --; |
cz3015 | 19:ca08111237ab | 250 | else if (rotorState - oldRotorState == -5) motorPosition ++; |
cz3015 | 19:ca08111237ab | 251 | else motorPosition += (rotorState - oldRotorState); |
cz3015 | 19:ca08111237ab | 252 | oldRotorState = rotorState; |
cz3015 | 19:ca08111237ab | 253 | } |
cz3015 | 19:ca08111237ab | 254 | /*void push() { |
cz3015 | 19:ca08111237ab | 255 | intState = readRotorState(); |
cz3015 | 19:ca08111237ab | 256 | if (intState != intStateOld) { |
cz3015 | 19:ca08111237ab | 257 | intStateOld = intState; |
cz3015 | 19:ca08111237ab | 258 | motorOut((intState - orState + lead +6) % 6); //+6 to make sure the remainder is positive |
cz3015 | 19:ca08111237ab | 259 | } |
cz3015 | 19:ca08111237ab | 260 | }*/ |
cz3015 | 19:ca08111237ab | 261 | |
cz3015 | 19:ca08111237ab | 262 | void motorCtrlFn(){ |
cz3015 | 19:ca08111237ab | 263 | int32_t counter=0; |
cz3015 | 19:ca08111237ab | 264 | static int32_t oldmotorPosition; |
cz3015 | 19:ca08111237ab | 265 | int32_t error =0; |
cz3015 | 19:ca08111237ab | 266 | int32_t PrevError = 0;// diff btw one possition and current position |
cz3015 | 19:ca08111237ab | 267 | int32_t errorSum; |
cz3015 | 19:ca08111237ab | 268 | int32_t PrevErrorArray[10]; //10 errors for integration |
cz3015 | 19:ca08111237ab | 269 | int8_t errorSign = 1; // get rid of the minus sign when motor is turning negative direction |
cz3015 | 19:ca08111237ab | 270 | // Timer to count time passed between ticks to calculate velocity |
cz3015 | 19:ca08111237ab | 271 | Timer motorTime; |
cz3015 | 19:ca08111237ab | 272 | motorTime.start(); |
cz3015 | 19:ca08111237ab | 273 | float motorPos; |
cz3015 | 19:ca08111237ab | 274 | float windingSpeed; |
cz3015 | 19:ca08111237ab | 275 | float windingRev; |
cz3015 | 19:ca08111237ab | 276 | float Ms; //proportional motor speed control |
cz3015 | 19:ca08111237ab | 277 | float Mp; // diff motor postion control |
cz3015 | 19:ca08111237ab | 278 | float ks = 15; //proportional constant for speed |
cz3015 | 19:ca08111237ab | 279 | float kd = 11; // 11 values in 100ms, diff constant for position control |
cz3015 | 19:ca08111237ab | 280 | float ki = ??; // integration constant, to be tested for friction |
cz3015 | 19:ca08111237ab | 281 | int8_t leadMs = -2; |
cz3015 | 19:ca08111237ab | 282 | int8_t leadMp = -2; // different leads to know which controller used |
cz3015 | 19:ca08111237ab | 283 | Ticker motorCtrlTicker; |
cz3015 | 19:ca08111237ab | 284 | |
cz3015 | 19:ca08111237ab | 285 | motorCtrlTicker.attach_us(&motorCtrlTick,100000); |
cz3015 | 19:ca08111237ab | 286 | while(1){ |
cz3015 | 19:ca08111237ab | 287 | motorCtrlT.signal_wait(0x1); |
cz3015 | 19:ca08111237ab | 288 | errorSum= 0; |
cz3015 | 19:ca08111237ab | 289 | for(uint8_t i=9; i >0 ; i--){ |
cz3015 | 19:ca08111237ab | 290 | PrevErrorArray[i] = prevErrorArray[i-1]; |
cz3015 | 19:ca08111237ab | 291 | errorSum+= PrevErrorArray[i]; |
cz3015 | 19:ca08111237ab | 292 | } |
cz3015 | 19:ca08111237ab | 293 | // convert state change into rotations |
cz3015 | 19:ca08111237ab | 294 | windingSpeed = maxSpeed*6; |
cz3015 | 19:ca08111237ab | 295 | windingRev = newRev*6; |
cz3015 | 19:ca08111237ab | 296 | motorPos = motorPosition; |
cz3015 | 19:ca08111237ab | 297 | motorVelocity = (motorPos - oldmotorPosition)/motorTime.read(); |
cz3015 | 19:ca08111237ab | 298 | |
cz3015 | 19:ca08111237ab | 299 | error = windingRev+ motorPosition_command- motorPos; |
cz3015 | 19:ca08111237ab | 300 | |
cz3015 | 19:ca08111237ab | 301 | if (error < 0) errorSign = -1; |
cz3015 | 19:ca08111237ab | 302 | else errorSign =1; |
cz3015 | 19:ca08111237ab | 303 | |
cz3015 | 19:ca08111237ab | 304 | PrevErrorArray[0] = error * motorTime.read(); |
cz3015 | 19:ca08111237ab | 305 | errorSum += PrevErrorArray [0]; |
cz3015 | 19:ca08111237ab | 306 | oldmotorPosition = motorPos; |
cz3015 | 19:ca08111237ab | 307 | |
cz3015 | 19:ca08111237ab | 308 | //equation for controls |
cz3015 | 19:ca08111237ab | 309 | Ms = ks*(windingSpeed -abs(motorVelocity))*errorSign; |
cz3015 | 19:ca08111237ab | 310 | Mp = ks*error + kd*(error - PrevError) /motorTime.read() + ki*errorSum; |
cz3015 | 19:ca08111237ab | 311 | |
cz3015 | 19:ca08111237ab | 312 | motorTime.reset(); |
cz3015 | 19:ca08111237ab | 313 | // Serial output to monitor speed and position |
cz3015 | 19:ca08111237ab | 314 | counter++; |
cz3015 | 19:ca08111237ab | 315 | if(counter == 10){ |
cz3015 | 19:ca08111237ab | 316 | counter = 0; |
cz3015 | 19:ca08111237ab | 317 | //display velocity and motor position |
cz3015 | 19:ca08111237ab | 318 | putMessage(3,(float)(motorPos/6.0)); |
cz3015 | 19:ca08111237ab | 319 | putMessage(4,(float)(motorVelocity/6.0)); |
estott | 0:de4320f74764 | 320 | } |
estott | 2:4e88faab6988 | 321 | } |
cz3015 | 19:ca08111237ab | 322 | int main() { |
cz3015 | 19:ca08111237ab | 323 | //Serial pc(SERIAL_TX, SERIAL_RX); |
cz3015 | 19:ca08111237ab | 324 | |
cz3015 | 19:ca08111237ab | 325 | //Initialise bincoin mining and communication |
cz3015 | 19:ca08111237ab | 326 | bitcointhread.set_priority(osPriorityNormal); |
cz3015 | 19:ca08111237ab | 327 | commandProcessorthread.set_priority(osPriorityHigh); |
cz3015 | 19:ca08111237ab | 328 | commandProcessorthread.start(commandProcessor); |
cz3015 | 19:ca08111237ab | 329 | bitcointhread.start(bitcoin); |
cz3015 | 19:ca08111237ab | 330 | |
cz3015 | 19:ca08111237ab | 331 | //PWM.period(0.002f); //Set PWM period in seconds |
cz3015 | 19:ca08111237ab | 332 | //PWM.write(0.5); //Set PWM duty in % |
cz3015 | 19:ca08111237ab | 333 | |
cz3015 | 19:ca08111237ab | 334 | pc.printf("Hello Pete\n\r"); |
cz3015 | 19:ca08111237ab | 335 | |
cz3015 | 19:ca08111237ab | 336 | orState = motorHome(); |
cz3015 | 19:ca08111237ab | 337 | pc.printf("Rotor origin: %x\n\r", orState); |
cz3015 | 19:ca08111237ab | 338 | |
cz3015 | 19:ca08111237ab | 339 | I1.rise(&push); |
cz3015 | 19:ca08111237ab | 340 | I2.rise(&push); |
cz3015 | 19:ca08111237ab | 341 | I3.rise(&push); |
cz3015 | 19:ca08111237ab | 342 | |
cz3015 | 19:ca08111237ab | 343 | I1.fall(&push); |
cz3015 | 19:ca08111237ab | 344 | I2.fall(&push); |
cz3015 | 19:ca08111237ab | 345 | I3.fall(&push); |
cz3015 | 19:ca08111237ab | 346 | |
cz3015 | 19:ca08111237ab | 347 | |
estott | 0:de4320f74764 | 348 | } |
cz3015 | 19:ca08111237ab | 349 | |
cz3015 | 19:ca08111237ab | 350 |