![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ES2017 coursework 2
Fork of ES_CW2_Starter by
main.cpp@25:9e6e870821d8, 2017-03-11 (annotated)
- Committer:
- david_s95
- Date:
- Sat Mar 11 15:03:46 2017 +0000
- Revision:
- 25:9e6e870821d8
- Parent:
- 24:4032857546f4
- Child:
- 26:b9f2d6d3f40e
Meng's streamlined code.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
estott | 0:de4320f74764 | 1 | #include "mbed.h" |
estott | 0:de4320f74764 | 2 | #include "rtos.h" |
david_s95 | 5:e5313b695302 | 3 | #include <string> |
david_s95 | 10:0309d6c49f26 | 4 | #include "PID.h" |
david_s95 | 10:0309d6c49f26 | 5 | |
david_s95 | 10:0309d6c49f26 | 6 | //PID controller configuration |
david_s95 | 24:4032857546f4 | 7 | float PIDrate = 0.1; |
david_s95 | 13:87ab3b008803 | 8 | float Kc = 4.5; |
david_s95 | 13:87ab3b008803 | 9 | float Ti = 0.1; |
david_s95 | 13:87ab3b008803 | 10 | float Td = 0.5; |
david_s95 | 10:0309d6c49f26 | 11 | float speedControl = 0; |
david_s95 | 10:0309d6c49f26 | 12 | PID controller(Kc, Ti, Td, PIDrate); |
david_s95 | 10:0309d6c49f26 | 13 | Thread VPIDthread; |
estott | 0:de4320f74764 | 14 | |
estott | 0:de4320f74764 | 15 | //Photointerrupter input pins |
estott | 0:de4320f74764 | 16 | #define I1pin D2 |
estott | 2:4e88faab6988 | 17 | #define I2pin D11 |
estott | 2:4e88faab6988 | 18 | #define I3pin D12 |
estott | 2:4e88faab6988 | 19 | |
estott | 2:4e88faab6988 | 20 | //Incremental encoder input pins |
estott | 2:4e88faab6988 | 21 | #define CHA D7 |
david_s95 | 5:e5313b695302 | 22 | #define CHB D8 |
estott | 0:de4320f74764 | 23 | |
estott | 0:de4320f74764 | 24 | //Motor Drive output pins //Mask in output byte |
estott | 2:4e88faab6988 | 25 | #define L1Lpin D4 //0x01 |
estott | 2:4e88faab6988 | 26 | #define L1Hpin D5 //0x02 |
estott | 2:4e88faab6988 | 27 | #define L2Lpin D3 //0x04 |
estott | 2:4e88faab6988 | 28 | #define L2Hpin D6 //0x08 |
estott | 2:4e88faab6988 | 29 | #define L3Lpin D9 //0x10 |
estott | 0:de4320f74764 | 30 | #define L3Hpin D10 //0x20 |
estott | 0:de4320f74764 | 31 | |
david_s95 | 5:e5313b695302 | 32 | //Define sized for command arrays |
david_s95 | 5:e5313b695302 | 33 | #define ARRAYSIZE 8 |
david_s95 | 5:e5313b695302 | 34 | |
estott | 0:de4320f74764 | 35 | //Mapping from sequential drive states to motor phase outputs |
estott | 0:de4320f74764 | 36 | /* |
estott | 0:de4320f74764 | 37 | State L1 L2 L3 |
estott | 0:de4320f74764 | 38 | 0 H - L |
estott | 0:de4320f74764 | 39 | 1 - H L |
estott | 0:de4320f74764 | 40 | 2 L H - |
estott | 0:de4320f74764 | 41 | 3 L - H |
estott | 0:de4320f74764 | 42 | 4 - L H |
estott | 0:de4320f74764 | 43 | 5 H L - |
estott | 0:de4320f74764 | 44 | 6 - - - |
estott | 0:de4320f74764 | 45 | 7 - - - |
estott | 0:de4320f74764 | 46 | */ |
estott | 0:de4320f74764 | 47 | //Drive state to output table |
david_s95 | 24:4032857546f4 | 48 | //const int8_t driveTable[6] = {0x38, 0x2C, 0x0E, 0x0B, 0x23, 0x32}; |
david_s95 | 24:4032857546f4 | 49 | |
david_s95 | 24:4032857546f4 | 50 | //const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00}; |
david_s95 | 24:4032857546f4 | 51 | |
david_s95 | 24:4032857546f4 | 52 | |
david_s95 | 24:4032857546f4 | 53 | //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid |
david_s95 | 24:4032857546f4 | 54 | //const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07}; |
david_s95 | 9:575b29cbf5e4 | 55 | |
david_s95 | 18:55cd33a3e69f | 56 | const int8_t cwState[7] = {0x00, 0x23, 0x38, 0x32, 0x0E, 0x0B, 0x2C}; |
david_s95 | 18:55cd33a3e69f | 57 | const int8_t AcwState[7] = {0x00, 0x0E, 0x23, 0x0B, 0x38, 0x2C, 0x32}; |
estott | 2:4e88faab6988 | 58 | |
david_s95 | 22:1c329584282b | 59 | |
david_s95 | 22:1c329584282b | 60 | const int8_t FastStateCW[7] = {0x00, 0x32, 0x2C, 0x38, 0x0B, 0x23, 0x0E}; |
david_s95 | 22:1c329584282b | 61 | const int8_t FastStateACW[7] = {0x00, 0x2C, 0x0B, 0x0E, 0x32, 0x38, 0x23}; |
david_s95 | 22:1c329584282b | 62 | |
estott | 0:de4320f74764 | 63 | //Photointerrupter inputs |
david_s95 | 9:575b29cbf5e4 | 64 | DigitalIn I1(I1pin); |
david_s95 | 9:575b29cbf5e4 | 65 | InterruptIn I2(I2pin); |
estott | 2:4e88faab6988 | 66 | DigitalIn I3(I3pin); |
estott | 0:de4320f74764 | 67 | |
estott | 0:de4320f74764 | 68 | //Motor Drive outputs |
david_s95 | 5:e5313b695302 | 69 | DigitalOut clk(LED1); |
david_s95 | 9:575b29cbf5e4 | 70 | |
david_s95 | 9:575b29cbf5e4 | 71 | //NOTE, BusOut declares things in reverse (ie, 0, 1, 2, 3) compared to binary represenation |
david_s95 | 24:4032857546f4 | 72 | BusOut motor(L1Hpin, L2Lpin, L2Hpin, L3Lpin, L3Hpin);//L1Lpin, |
david_s95 | 24:4032857546f4 | 73 | PwmOut singpin(L1Lpin); |
david_s95 | 24:4032857546f4 | 74 | |
david_s95 | 5:e5313b695302 | 75 | //Timeout function for rotating at set speed |
david_s95 | 5:e5313b695302 | 76 | Timeout spinTimer; |
david_s95 | 5:e5313b695302 | 77 | float spinWait = 10; |
david_s95 | 5:e5313b695302 | 78 | float revsec = 0; |
david_s95 | 5:e5313b695302 | 79 | |
theMaO | 14:155e9a9147d4 | 80 | //Variables for spinning N revolutions |
theMaO | 14:155e9a9147d4 | 81 | int8_t targetRevs = 0; |
theMaO | 14:155e9a9147d4 | 82 | int8_t currRevs = 0; |
theMaO | 14:155e9a9147d4 | 83 | |
david_s95 | 7:5932ed0bad6d | 84 | //Timer used for calculating speed |
david_s95 | 7:5932ed0bad6d | 85 | Timer speedTimer; |
david_s95 | 10:0309d6c49f26 | 86 | float measuredRevs = 0, revtimer = 0; |
david_s95 | 7:5932ed0bad6d | 87 | |
david_s95 | 5:e5313b695302 | 88 | Serial pc(SERIAL_TX, SERIAL_RX); |
david_s95 | 5:e5313b695302 | 89 | |
david_s95 | 5:e5313b695302 | 90 | int8_t orState = 0; //Rotor offset at motor state 0 |
david_s95 | 5:e5313b695302 | 91 | int8_t intState = 0; |
david_s95 | 5:e5313b695302 | 92 | int8_t intStateOld = 0; |
david_s95 | 9:575b29cbf5e4 | 93 | int position = 0; |
david_s95 | 5:e5313b695302 | 94 | |
david_s95 | 5:e5313b695302 | 95 | int i=0; |
david_s95 | 10:0309d6c49f26 | 96 | int quadraturePosition=0; |
david_s95 | 9:575b29cbf5e4 | 97 | bool spinCW=0; |
david_s95 | 5:e5313b695302 | 98 | |
estott | 0:de4320f74764 | 99 | //Set a given drive state |
david_s95 | 5:e5313b695302 | 100 | void motorOut(int8_t driveState) |
david_s95 | 5:e5313b695302 | 101 | { |
david_s95 | 5:e5313b695302 | 102 | |
david_s95 | 24:4032857546f4 | 103 | motor = 0x2A; |
david_s95 | 10:0309d6c49f26 | 104 | |
david_s95 | 22:1c329584282b | 105 | if(revtimer<33) { |
david_s95 | 22:1c329584282b | 106 | clk=1; |
david_s95 | 24:4032857546f4 | 107 | if(!spinCW) { |
david_s95 | 24:4032857546f4 | 108 | motor = (FastStateACW[driveState]>>1); |
david_s95 | 24:4032857546f4 | 109 | singpin = float(FastStateACW[driveState]&&0x01)/2; |
david_s95 | 24:4032857546f4 | 110 | } else { |
david_s95 | 24:4032857546f4 | 111 | motor = (FastStateCW[driveState]>>1); |
david_s95 | 24:4032857546f4 | 112 | singpin = float(FastStateCW[driveState]&&0x01)/2; |
david_s95 | 24:4032857546f4 | 113 | } |
david_s95 | 22:1c329584282b | 114 | } else { |
david_s95 | 22:1c329584282b | 115 | clk=0; |
david_s95 | 24:4032857546f4 | 116 | if(!spinCW) { |
david_s95 | 24:4032857546f4 | 117 | motor = (AcwState[driveState]>>1); |
david_s95 | 24:4032857546f4 | 118 | singpin = float(AcwState[driveState]&&0x01)/2; |
david_s95 | 24:4032857546f4 | 119 | } else { |
david_s95 | 24:4032857546f4 | 120 | motor = (cwState[driveState]>>1); |
david_s95 | 24:4032857546f4 | 121 | singpin = float(cwState[driveState]&&0x01)/2; |
david_s95 | 24:4032857546f4 | 122 | } |
david_s95 | 22:1c329584282b | 123 | } |
david_s95 | 10:0309d6c49f26 | 124 | } |
david_s95 | 5:e5313b695302 | 125 | |
david_s95 | 10:0309d6c49f26 | 126 | inline void motorStop() |
david_s95 | 10:0309d6c49f26 | 127 | { |
david_s95 | 10:0309d6c49f26 | 128 | //revsec set to zero prevents recurring interrupt for constant speed |
david_s95 | 10:0309d6c49f26 | 129 | revsec = 0; |
david_s95 | 23:d48d51e5db97 | 130 | wait(spinWait); |
david_s95 | 10:0309d6c49f26 | 131 | //0x2A turns all motor transistors off to prevent any power usage |
david_s95 | 10:0309d6c49f26 | 132 | motor = 0x2A; |
david_s95 | 5:e5313b695302 | 133 | } |
david_s95 | 5:e5313b695302 | 134 | |
david_s95 | 5:e5313b695302 | 135 | //Convert photointerrupter inputs to a rotor state |
david_s95 | 5:e5313b695302 | 136 | inline int8_t readRotorState() |
david_s95 | 5:e5313b695302 | 137 | { |
david_s95 | 9:575b29cbf5e4 | 138 | return (I1 + 2*I2 + 4*I3); |
david_s95 | 5:e5313b695302 | 139 | } |
estott | 0:de4320f74764 | 140 | |
david_s95 | 5:e5313b695302 | 141 | //Basic synchronisation routine |
david_s95 | 5:e5313b695302 | 142 | int8_t motorHome() |
david_s95 | 5:e5313b695302 | 143 | { |
estott | 0:de4320f74764 | 144 | //Put the motor in drive state 0 and wait for it to stabilise |
david_s95 | 9:575b29cbf5e4 | 145 | motor=cwState[1]; |
david_s95 | 24:4032857546f4 | 146 | // motorOut(1); |
estott | 0:de4320f74764 | 147 | wait(1.0); |
david_s95 | 10:0309d6c49f26 | 148 | |
david_s95 | 9:575b29cbf5e4 | 149 | position = 0; |
david_s95 | 5:e5313b695302 | 150 | |
estott | 0:de4320f74764 | 151 | //Get the rotor state |
estott | 2:4e88faab6988 | 152 | return readRotorState(); |
estott | 0:de4320f74764 | 153 | } |
david_s95 | 5:e5313b695302 | 154 | |
david_s95 | 5:e5313b695302 | 155 | void fixedSpeed() |
david_s95 | 5:e5313b695302 | 156 | { |
david_s95 | 6:4edbe75736d9 | 157 | //If spinning is required, attach the necessary wait to the |
david_s95 | 6:4edbe75736d9 | 158 | //timeout interrupt to call this function again and |
david_s95 | 6:4edbe75736d9 | 159 | //keep the motor spinning at the right speed |
david_s95 | 5:e5313b695302 | 160 | if(revsec) spinTimer.attach(&fixedSpeed, spinWait); |
david_s95 | 24:4032857546f4 | 161 | |
david_s95 | 23:d48d51e5db97 | 162 | intState = readRotorState(); |
david_s95 | 23:d48d51e5db97 | 163 | //Increment state machine to next state |
david_s95 | 23:d48d51e5db97 | 164 | motorOut(intState); |
david_s95 | 23:d48d51e5db97 | 165 | |
david_s95 | 5:e5313b695302 | 166 | } |
david_s95 | 5:e5313b695302 | 167 | |
david_s95 | 10:0309d6c49f26 | 168 | void rps() |
david_s95 | 7:5932ed0bad6d | 169 | { |
david_s95 | 10:0309d6c49f26 | 170 | speedTimer.stop(); |
david_s95 | 10:0309d6c49f26 | 171 | revtimer = speedTimer.read_ms(); |
david_s95 | 10:0309d6c49f26 | 172 | speedTimer.reset(); |
david_s95 | 10:0309d6c49f26 | 173 | speedTimer.start(); |
david_s95 | 10:0309d6c49f26 | 174 | |
david_s95 | 10:0309d6c49f26 | 175 | measuredRevs = 1000/(revtimer); |
david_s95 | 10:0309d6c49f26 | 176 | quadraturePosition=0; |
david_s95 | 7:5932ed0bad6d | 177 | } |
david_s95 | 7:5932ed0bad6d | 178 | |
david_s95 | 10:0309d6c49f26 | 179 | void VPID() |
david_s95 | 10:0309d6c49f26 | 180 | { |
david_s95 | 12:8ea29b18d289 | 181 | controller.setMode(1); |
david_s95 | 10:0309d6c49f26 | 182 | while(1) { |
david_s95 | 22:1c329584282b | 183 | controller.setSetPoint(revsec); |
david_s95 | 22:1c329584282b | 184 | controller.setProcessValue(measuredRevs); |
david_s95 | 22:1c329584282b | 185 | speedControl = controller.compute(); |
david_s95 | 22:1c329584282b | 186 | spinWait = (1/speedControl)/6; |
david_s95 | 10:0309d6c49f26 | 187 | Thread::wait(PIDrate); |
david_s95 | 10:0309d6c49f26 | 188 | } |
david_s95 | 10:0309d6c49f26 | 189 | } |
david_s95 | 7:5932ed0bad6d | 190 | |
mengkiang | 4:f8a9ce214db9 | 191 | //Main function |
david_s95 | 5:e5313b695302 | 192 | int main() |
david_s95 | 5:e5313b695302 | 193 | { |
david_s95 | 9:575b29cbf5e4 | 194 | pc.printf("spin\n\r"); |
david_s95 | 24:4032857546f4 | 195 | spinWait = 0.01; |
david_s95 | 19:93ca06d2e311 | 196 | motorStop(); |
david_s95 | 19:93ca06d2e311 | 197 | |
estott | 0:de4320f74764 | 198 | //Run the motor synchronisation |
estott | 2:4e88faab6988 | 199 | orState = motorHome(); |
david_s95 | 6:4edbe75736d9 | 200 | //orState is subtracted from future rotor state inputs to align rotor and motor states |
david_s95 | 6:4edbe75736d9 | 201 | |
estott | 2:4e88faab6988 | 202 | pc.printf("Rotor origin: %x\n\r",orState); |
david_s95 | 5:e5313b695302 | 203 | |
david_s95 | 6:4edbe75736d9 | 204 | char command[ARRAYSIZE]; |
david_s95 | 6:4edbe75736d9 | 205 | int index=0; |
david_s95 | 6:4edbe75736d9 | 206 | int units = 0, tens = 0, decimals = 0; |
david_s95 | 6:4edbe75736d9 | 207 | char ch; |
david_s95 | 12:8ea29b18d289 | 208 | int vartens = 0, varunits = 0, vardecs = 0; |
david_s95 | 18:55cd33a3e69f | 209 | int hdrds = 0; |
david_s95 | 18:55cd33a3e69f | 210 | float bias = 0; |
david_s95 | 7:5932ed0bad6d | 211 | |
david_s95 | 10:0309d6c49f26 | 212 | speedTimer.reset(); |
david_s95 | 10:0309d6c49f26 | 213 | speedTimer.start(); |
david_s95 | 10:0309d6c49f26 | 214 | I2.mode(PullNone); |
david_s95 | 10:0309d6c49f26 | 215 | I2.fall(&rps); |
david_s95 | 7:5932ed0bad6d | 216 | |
david_s95 | 24:4032857546f4 | 217 | singpin.period_us(100); |
david_s95 | 24:4032857546f4 | 218 | |
david_s95 | 10:0309d6c49f26 | 219 | VPIDthread.start(VPID); |
david_s95 | 10:0309d6c49f26 | 220 | |
david_s95 | 5:e5313b695302 | 221 | while(1) { |
david_s95 | 7:5932ed0bad6d | 222 | |
david_s95 | 6:4edbe75736d9 | 223 | //If there's a character to read from the serial port |
david_s95 | 6:4edbe75736d9 | 224 | if (pc.readable()) { |
david_s95 | 7:5932ed0bad6d | 225 | |
david_s95 | 6:4edbe75736d9 | 226 | //Clear index counter and control variables |
david_s95 | 6:4edbe75736d9 | 227 | index = 0; |
david_s95 | 7:5932ed0bad6d | 228 | |
david_s95 | 6:4edbe75736d9 | 229 | //Read each value from the serial port until Enter key is pressed |
david_s95 | 6:4edbe75736d9 | 230 | do { |
david_s95 | 6:4edbe75736d9 | 231 | //Read character |
david_s95 | 6:4edbe75736d9 | 232 | ch = pc.getc(); |
david_s95 | 6:4edbe75736d9 | 233 | //Print character to serial for visual feedback |
david_s95 | 6:4edbe75736d9 | 234 | pc.putc(ch); |
david_s95 | 6:4edbe75736d9 | 235 | //Add character to input array |
david_s95 | 6:4edbe75736d9 | 236 | command[index++]=ch; // put it into the value array and increment the index |
david_s95 | 7:5932ed0bad6d | 237 | //d10 and d13 used for detecting Enter key on Windows/Unix/Mac |
david_s95 | 6:4edbe75736d9 | 238 | } while(ch != 10 && ch != 13); |
david_s95 | 7:5932ed0bad6d | 239 | |
david_s95 | 6:4edbe75736d9 | 240 | //Start new line on terminal for printing data |
david_s95 | 6:4edbe75736d9 | 241 | pc.putc('\n'); |
david_s95 | 6:4edbe75736d9 | 242 | pc.putc('\r'); |
david_s95 | 7:5932ed0bad6d | 243 | |
david_s95 | 6:4edbe75736d9 | 244 | //Analyse the input string |
david_s95 | 6:4edbe75736d9 | 245 | switch (command[0]) { |
david_s95 | 7:5932ed0bad6d | 246 | //If a V was typed... |
david_s95 | 6:4edbe75736d9 | 247 | case 'V': |
david_s95 | 7:5932ed0bad6d | 248 | units = 0, tens = 0, decimals = 0; |
david_s95 | 7:5932ed0bad6d | 249 | //For each character received, subtract ASCII 0 from ASCII |
david_s95 | 7:5932ed0bad6d | 250 | //representation to obtain the integer value of the number |
david_s95 | 9:575b29cbf5e4 | 251 | if(command[1]=='-') { |
david_s95 | 9:575b29cbf5e4 | 252 | spinCW = 0; |
david_s95 | 9:575b29cbf5e4 | 253 | //If decimal point is in the second character (eg, V-.1) |
david_s95 | 9:575b29cbf5e4 | 254 | if(command[2]=='.') { |
david_s95 | 9:575b29cbf5e4 | 255 | //Extract decimal rev/s |
david_s95 | 9:575b29cbf5e4 | 256 | decimals = command[3] - '0'; |
david_s95 | 7:5932ed0bad6d | 257 | |
david_s95 | 9:575b29cbf5e4 | 258 | //If decimal point is in the third character (eg, V-0.1) |
david_s95 | 9:575b29cbf5e4 | 259 | } else if(command[3]=='.') { |
david_s95 | 9:575b29cbf5e4 | 260 | units = command[2] - '0'; |
david_s95 | 9:575b29cbf5e4 | 261 | decimals = command[4] - '0'; |
david_s95 | 7:5932ed0bad6d | 262 | |
david_s95 | 9:575b29cbf5e4 | 263 | //If decimal point is in the fourth character (eg, V-10.1) |
david_s95 | 9:575b29cbf5e4 | 264 | } else if(command[4]=='.') { |
david_s95 | 9:575b29cbf5e4 | 265 | tens = command[2] - '0'; |
david_s95 | 9:575b29cbf5e4 | 266 | units = command[3] - '0'; |
david_s95 | 9:575b29cbf5e4 | 267 | decimals = command[5] - '0'; |
david_s95 | 9:575b29cbf5e4 | 268 | } |
david_s95 | 9:575b29cbf5e4 | 269 | } else { |
david_s95 | 9:575b29cbf5e4 | 270 | spinCW = 1; |
david_s95 | 9:575b29cbf5e4 | 271 | //If decimal point is in the second character (eg, V.1) |
david_s95 | 9:575b29cbf5e4 | 272 | if(command[1]=='.') { |
david_s95 | 9:575b29cbf5e4 | 273 | //Extract decimal rev/s |
david_s95 | 9:575b29cbf5e4 | 274 | decimals = command[2] - '0'; |
david_s95 | 7:5932ed0bad6d | 275 | |
david_s95 | 9:575b29cbf5e4 | 276 | //If decimal point is in the third character (eg, V0.1) |
david_s95 | 9:575b29cbf5e4 | 277 | } else if(command[2]=='.') { |
david_s95 | 9:575b29cbf5e4 | 278 | units = command[1] - '0'; |
david_s95 | 9:575b29cbf5e4 | 279 | decimals = command[3] - '0'; |
david_s95 | 9:575b29cbf5e4 | 280 | |
david_s95 | 9:575b29cbf5e4 | 281 | //If decimal point is in the fourth character (eg, V10.1) |
david_s95 | 9:575b29cbf5e4 | 282 | } else if(command[3]=='.') { |
david_s95 | 9:575b29cbf5e4 | 283 | tens = command[1] - '0'; |
david_s95 | 9:575b29cbf5e4 | 284 | units = command[2] - '0'; |
david_s95 | 9:575b29cbf5e4 | 285 | decimals = command[4] - '0'; |
david_s95 | 9:575b29cbf5e4 | 286 | } |
david_s95 | 6:4edbe75736d9 | 287 | } |
david_s95 | 7:5932ed0bad6d | 288 | |
david_s95 | 6:4edbe75736d9 | 289 | //Calculate the number of revolutions per second required |
david_s95 | 6:4edbe75736d9 | 290 | revsec = float(tens)*10 + float(units) + float(decimals)/10; |
david_s95 | 6:4edbe75736d9 | 291 | //Calculate the required wait period |
david_s95 | 12:8ea29b18d289 | 292 | |
david_s95 | 19:93ca06d2e311 | 293 | spinWait = (1/revsec)/6; |
david_s95 | 6:4edbe75736d9 | 294 | //Print values for verification |
david_s95 | 12:8ea29b18d289 | 295 | pc.printf("Rev/S: %2.4f\n\r", revsec); |
david_s95 | 7:5932ed0bad6d | 296 | |
david_s95 | 6:4edbe75736d9 | 297 | //Run the function to start rotating at a fixed speed |
david_s95 | 6:4edbe75736d9 | 298 | fixedSpeed(); |
david_s95 | 6:4edbe75736d9 | 299 | break; |
david_s95 | 7:5932ed0bad6d | 300 | //If anything unexpected was received |
david_s95 | 18:55cd33a3e69f | 301 | |
david_s95 | 7:5932ed0bad6d | 302 | case 's': |
david_s95 | 10:0309d6c49f26 | 303 | printf("Measured: %2.3f, revsec: %2.3f\r\n", measuredRevs, revsec); |
david_s95 | 22:1c329584282b | 304 | printf("PID: %2.3f\r\n", speedControl); |
david_s95 | 7:5932ed0bad6d | 305 | break; |
david_s95 | 12:8ea29b18d289 | 306 | |
david_s95 | 12:8ea29b18d289 | 307 | case 'K': |
david_s95 | 12:8ea29b18d289 | 308 | vartens = command[1] - '0'; |
david_s95 | 12:8ea29b18d289 | 309 | varunits = command[2] - '0'; |
david_s95 | 12:8ea29b18d289 | 310 | vardecs = command[4] - '0'; |
david_s95 | 12:8ea29b18d289 | 311 | Kc = float(vartens)*10 + float(varunits) + float(vardecs)/10; |
david_s95 | 12:8ea29b18d289 | 312 | printf("Kc: %2.1f\r\n", Kc); |
david_s95 | 12:8ea29b18d289 | 313 | controller.setTunings(Kc, Ti, Td); |
david_s95 | 12:8ea29b18d289 | 314 | break; |
david_s95 | 12:8ea29b18d289 | 315 | case 'i': |
david_s95 | 12:8ea29b18d289 | 316 | vartens = command[1] - '0'; |
david_s95 | 12:8ea29b18d289 | 317 | varunits = command[2] - '0'; |
david_s95 | 12:8ea29b18d289 | 318 | vardecs = command[4] - '0'; |
david_s95 | 12:8ea29b18d289 | 319 | Ti = float(vartens)*10 + float(varunits) + float(vardecs)/10; |
david_s95 | 12:8ea29b18d289 | 320 | printf("Ti: %2.1f\r\n", Ti); |
david_s95 | 12:8ea29b18d289 | 321 | controller.setTunings(Kc, Ti, Td); |
david_s95 | 12:8ea29b18d289 | 322 | break; |
david_s95 | 12:8ea29b18d289 | 323 | case 'd': |
david_s95 | 12:8ea29b18d289 | 324 | vartens = command[1] - '0'; |
david_s95 | 12:8ea29b18d289 | 325 | varunits = command[2] - '0'; |
david_s95 | 12:8ea29b18d289 | 326 | vardecs = command[4] - '0'; |
david_s95 | 12:8ea29b18d289 | 327 | Td = float(vartens)*10 + float(varunits) + float(vardecs)/10; |
david_s95 | 12:8ea29b18d289 | 328 | printf("Td: %2.1f\r\n", Td); |
david_s95 | 12:8ea29b18d289 | 329 | controller.setTunings(Kc, Ti, Td); |
david_s95 | 12:8ea29b18d289 | 330 | break; |
david_s95 | 18:55cd33a3e69f | 331 | case 'b': |
david_s95 | 18:55cd33a3e69f | 332 | if(command[1]=='.') { |
david_s95 | 18:55cd33a3e69f | 333 | //Extract decimal rev/s |
david_s95 | 18:55cd33a3e69f | 334 | vardecs = command[2] - '0'; |
david_s95 | 18:55cd33a3e69f | 335 | |
david_s95 | 18:55cd33a3e69f | 336 | //If decimal point is in the third character (eg, V-0.1) |
david_s95 | 18:55cd33a3e69f | 337 | } else if(command[2]=='.') { |
david_s95 | 18:55cd33a3e69f | 338 | varunits = command[1] - '0'; |
david_s95 | 18:55cd33a3e69f | 339 | vardecs = command[3] - '0'; |
david_s95 | 18:55cd33a3e69f | 340 | |
david_s95 | 18:55cd33a3e69f | 341 | //If decimal point is in the fourth character (eg, V-10.1) |
david_s95 | 18:55cd33a3e69f | 342 | } else if(command[3]=='.') { |
david_s95 | 18:55cd33a3e69f | 343 | vartens = command[1] - '0'; |
david_s95 | 18:55cd33a3e69f | 344 | varunits = command[2] - '0'; |
david_s95 | 18:55cd33a3e69f | 345 | vardecs = command[4] - '0'; |
david_s95 | 18:55cd33a3e69f | 346 | } |
david_s95 | 18:55cd33a3e69f | 347 | bias = float(vartens)*10 + float(varunits) + float(vardecs)/10; |
david_s95 | 18:55cd33a3e69f | 348 | printf("Bias: %2.1f\r\n", bias); |
david_s95 | 18:55cd33a3e69f | 349 | controller.setBias(bias); |
david_s95 | 18:55cd33a3e69f | 350 | break; |
david_s95 | 25:9e6e870821d8 | 351 | |
david_s95 | 6:4edbe75736d9 | 352 | default: |
david_s95 | 6:4edbe75736d9 | 353 | //Set speed variables to zero to stop motor spinning |
david_s95 | 6:4edbe75736d9 | 354 | //Print error message |
david_s95 | 10:0309d6c49f26 | 355 | motorStop(); |
david_s95 | 10:0309d6c49f26 | 356 | pc.printf("Error in received data 0\n\r"); |
david_s95 | 23:d48d51e5db97 | 357 | motorStop(); |
david_s95 | 6:4edbe75736d9 | 358 | break; |
david_s95 | 6:4edbe75736d9 | 359 | } |
david_s95 | 6:4edbe75736d9 | 360 | } |
estott | 2:4e88faab6988 | 361 | } |
david_s95 | 5:e5313b695302 | 362 | |
david_s95 | 25:9e6e870821d8 | 363 | } |