ES2017 coursework 2

Dependencies:   PID

Fork of ES_CW2_Starter by Edward Stott

Committer:
david_s95
Date:
Thu Mar 16 10:45:55 2017 +0000
Revision:
28:2a73c1878690
Parent:
27:206f781756f9
Fairly good control, quite large overshoots but settles near to setpoint.

Who changed what in which revision?

UserRevisionLine numberNew 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 27:206f781756f9 7 float PIDrate = 0.2;
david_s95 26:b9f2d6d3f40e 8 float Kc = 10.0;
david_s95 26:b9f2d6d3f40e 9 float Ti = 3.0;
david_s95 27:206f781756f9 10 float Td = 0;
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 27:206f781756f9 65 DigitalIn I2(I2pin);
david_s95 27:206f781756f9 66 InterruptIn 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 27:206f781756f9 103 motor = 0x2A>>1;
david_s95 27:206f781756f9 104 singpin = 0;
david_s95 10:0309d6c49f26 105
david_s95 22:1c329584282b 106 if(revtimer<33) {
david_s95 27:206f781756f9 107 // clk=1;
david_s95 24:4032857546f4 108 if(!spinCW) {
david_s95 24:4032857546f4 109 motor = (FastStateACW[driveState]>>1);
david_s95 24:4032857546f4 110 singpin = float(FastStateACW[driveState]&&0x01)/2;
david_s95 24:4032857546f4 111 } else {
david_s95 24:4032857546f4 112 motor = (FastStateCW[driveState]>>1);
david_s95 24:4032857546f4 113 singpin = float(FastStateCW[driveState]&&0x01)/2;
david_s95 24:4032857546f4 114 }
david_s95 22:1c329584282b 115 } else {
david_s95 27:206f781756f9 116 // clk=0;
david_s95 24:4032857546f4 117 if(!spinCW) {
david_s95 24:4032857546f4 118 motor = (AcwState[driveState]>>1);
david_s95 24:4032857546f4 119 singpin = float(AcwState[driveState]&&0x01)/2;
david_s95 24:4032857546f4 120 } else {
david_s95 24:4032857546f4 121 motor = (cwState[driveState]>>1);
david_s95 24:4032857546f4 122 singpin = float(cwState[driveState]&&0x01)/2;
david_s95 24:4032857546f4 123 }
david_s95 22:1c329584282b 124 }
david_s95 10:0309d6c49f26 125 }
david_s95 5:e5313b695302 126
david_s95 10:0309d6c49f26 127 inline void motorStop()
david_s95 10:0309d6c49f26 128 {
david_s95 10:0309d6c49f26 129 //revsec set to zero prevents recurring interrupt for constant speed
david_s95 10:0309d6c49f26 130 revsec = 0;
david_s95 23:d48d51e5db97 131 wait(spinWait);
david_s95 10:0309d6c49f26 132 //0x2A turns all motor transistors off to prevent any power usage
david_s95 26:b9f2d6d3f40e 133 motor = (0x2A>>1);
david_s95 26:b9f2d6d3f40e 134 singpin = 0;
david_s95 5:e5313b695302 135 }
david_s95 5:e5313b695302 136
david_s95 5:e5313b695302 137 //Convert photointerrupter inputs to a rotor state
david_s95 5:e5313b695302 138 inline int8_t readRotorState()
david_s95 5:e5313b695302 139 {
david_s95 9:575b29cbf5e4 140 return (I1 + 2*I2 + 4*I3);
david_s95 5:e5313b695302 141 }
estott 0:de4320f74764 142
david_s95 5:e5313b695302 143 //Basic synchronisation routine
david_s95 5:e5313b695302 144 int8_t motorHome()
david_s95 5:e5313b695302 145 {
estott 0:de4320f74764 146 //Put the motor in drive state 0 and wait for it to stabilise
david_s95 9:575b29cbf5e4 147 motor=cwState[1];
david_s95 24:4032857546f4 148 // motorOut(1);
estott 0:de4320f74764 149 wait(1.0);
david_s95 10:0309d6c49f26 150
david_s95 9:575b29cbf5e4 151 position = 0;
david_s95 26:b9f2d6d3f40e 152 motorStop();
estott 0:de4320f74764 153 //Get the rotor state
estott 2:4e88faab6988 154 return readRotorState();
estott 0:de4320f74764 155 }
david_s95 5:e5313b695302 156
david_s95 5:e5313b695302 157 void fixedSpeed()
david_s95 5:e5313b695302 158 {
david_s95 6:4edbe75736d9 159 //If spinning is required, attach the necessary wait to the
david_s95 6:4edbe75736d9 160 //timeout interrupt to call this function again and
david_s95 6:4edbe75736d9 161 //keep the motor spinning at the right speed
david_s95 27:206f781756f9 162
david_s95 24:4032857546f4 163
david_s95 23:d48d51e5db97 164 intState = readRotorState();
david_s95 23:d48d51e5db97 165 //Increment state machine to next state
david_s95 23:d48d51e5db97 166 motorOut(intState);
david_s95 23:d48d51e5db97 167
david_s95 27:206f781756f9 168 // motorOut(I1 + I2<<1 + I3<<2);
david_s95 27:206f781756f9 169 if(revsec) spinTimer.attach(&fixedSpeed, spinWait);
david_s95 27:206f781756f9 170
david_s95 5:e5313b695302 171 }
david_s95 5:e5313b695302 172
david_s95 10:0309d6c49f26 173 void rps()
david_s95 7:5932ed0bad6d 174 {
david_s95 10:0309d6c49f26 175 speedTimer.stop();
david_s95 10:0309d6c49f26 176 revtimer = speedTimer.read_ms();
david_s95 10:0309d6c49f26 177 speedTimer.reset();
david_s95 10:0309d6c49f26 178 speedTimer.start();
david_s95 10:0309d6c49f26 179
david_s95 10:0309d6c49f26 180 measuredRevs = 1000/(revtimer);
david_s95 10:0309d6c49f26 181 quadraturePosition=0;
david_s95 7:5932ed0bad6d 182 }
david_s95 7:5932ed0bad6d 183
david_s95 10:0309d6c49f26 184 void VPID()
david_s95 10:0309d6c49f26 185 {
david_s95 10:0309d6c49f26 186 while(1) {
david_s95 22:1c329584282b 187 controller.setProcessValue(measuredRevs);
david_s95 22:1c329584282b 188 speedControl = controller.compute();
david_s95 27:206f781756f9 189 // if(speedControl<measuredRevs) speedControl = measuredRevs/2;
david_s95 27:206f781756f9 190 spinWait = (1/(speedControl*6));
david_s95 27:206f781756f9 191 // Thread::wait(PIDrate);
david_s95 27:206f781756f9 192 wait(PIDrate);
david_s95 10:0309d6c49f26 193 }
david_s95 10:0309d6c49f26 194 }
david_s95 7:5932ed0bad6d 195
mengkiang 4:f8a9ce214db9 196 //Main function
david_s95 5:e5313b695302 197 int main()
david_s95 5:e5313b695302 198 {
david_s95 9:575b29cbf5e4 199 pc.printf("spin\n\r");
david_s95 24:4032857546f4 200 spinWait = 0.01;
david_s95 19:93ca06d2e311 201 motorStop();
david_s95 19:93ca06d2e311 202
estott 0:de4320f74764 203 //Run the motor synchronisation
estott 2:4e88faab6988 204 orState = motorHome();
david_s95 6:4edbe75736d9 205 //orState is subtracted from future rotor state inputs to align rotor and motor states
david_s95 6:4edbe75736d9 206
estott 2:4e88faab6988 207 pc.printf("Rotor origin: %x\n\r",orState);
david_s95 5:e5313b695302 208
david_s95 6:4edbe75736d9 209 char command[ARRAYSIZE];
david_s95 6:4edbe75736d9 210 int index=0;
david_s95 6:4edbe75736d9 211 int units = 0, tens = 0, decimals = 0;
david_s95 6:4edbe75736d9 212 char ch;
david_s95 12:8ea29b18d289 213 int vartens = 0, varunits = 0, vardecs = 0;
david_s95 18:55cd33a3e69f 214 int hdrds = 0;
david_s95 18:55cd33a3e69f 215 float bias = 0;
david_s95 7:5932ed0bad6d 216
david_s95 10:0309d6c49f26 217 speedTimer.reset();
david_s95 10:0309d6c49f26 218 speedTimer.start();
david_s95 27:206f781756f9 219 I3.mode(PullNone);
david_s95 27:206f781756f9 220 I3.rise(&rps);
david_s95 7:5932ed0bad6d 221
david_s95 24:4032857546f4 222 singpin.period_us(100);
david_s95 24:4032857546f4 223
david_s95 10:0309d6c49f26 224 VPIDthread.start(VPID);
david_s95 27:206f781756f9 225 pc.printf("%d", VPIDthread.get_priority());
david_s95 27:206f781756f9 226 VPIDthread.set_priority(osPriorityAboveNormal);
david_s95 27:206f781756f9 227
david_s95 27:206f781756f9 228 controller.setInterval(PIDrate);
david_s95 27:206f781756f9 229 controller.setMode(0);
david_s95 10:0309d6c49f26 230
david_s95 5:e5313b695302 231 while(1) {
david_s95 7:5932ed0bad6d 232
david_s95 6:4edbe75736d9 233 //If there's a character to read from the serial port
david_s95 6:4edbe75736d9 234 if (pc.readable()) {
david_s95 7:5932ed0bad6d 235
david_s95 6:4edbe75736d9 236 //Clear index counter and control variables
david_s95 6:4edbe75736d9 237 index = 0;
david_s95 7:5932ed0bad6d 238
david_s95 6:4edbe75736d9 239 //Read each value from the serial port until Enter key is pressed
david_s95 6:4edbe75736d9 240 do {
david_s95 6:4edbe75736d9 241 //Read character
david_s95 6:4edbe75736d9 242 ch = pc.getc();
david_s95 6:4edbe75736d9 243 //Print character to serial for visual feedback
david_s95 6:4edbe75736d9 244 pc.putc(ch);
david_s95 6:4edbe75736d9 245 //Add character to input array
david_s95 6:4edbe75736d9 246 command[index++]=ch; // put it into the value array and increment the index
david_s95 7:5932ed0bad6d 247 //d10 and d13 used for detecting Enter key on Windows/Unix/Mac
david_s95 6:4edbe75736d9 248 } while(ch != 10 && ch != 13);
david_s95 7:5932ed0bad6d 249
david_s95 6:4edbe75736d9 250 //Start new line on terminal for printing data
david_s95 6:4edbe75736d9 251 pc.putc('\n');
david_s95 6:4edbe75736d9 252 pc.putc('\r');
david_s95 7:5932ed0bad6d 253
david_s95 6:4edbe75736d9 254 //Analyse the input string
david_s95 6:4edbe75736d9 255 switch (command[0]) {
david_s95 7:5932ed0bad6d 256 //If a V was typed...
david_s95 6:4edbe75736d9 257 case 'V':
david_s95 27:206f781756f9 258 hdrds = 0, units = 0, tens = 0, decimals = 0;
david_s95 7:5932ed0bad6d 259 //For each character received, subtract ASCII 0 from ASCII
david_s95 7:5932ed0bad6d 260 //representation to obtain the integer value of the number
david_s95 9:575b29cbf5e4 261 if(command[1]=='-') {
david_s95 9:575b29cbf5e4 262 spinCW = 0;
david_s95 9:575b29cbf5e4 263 //If decimal point is in the second character (eg, V-.1)
david_s95 9:575b29cbf5e4 264 if(command[2]=='.') {
david_s95 9:575b29cbf5e4 265 //Extract decimal rev/s
david_s95 9:575b29cbf5e4 266 decimals = command[3] - '0';
david_s95 7:5932ed0bad6d 267
david_s95 9:575b29cbf5e4 268 //If decimal point is in the third character (eg, V-0.1)
david_s95 9:575b29cbf5e4 269 } else if(command[3]=='.') {
david_s95 9:575b29cbf5e4 270 units = command[2] - '0';
david_s95 9:575b29cbf5e4 271 decimals = command[4] - '0';
david_s95 7:5932ed0bad6d 272
david_s95 9:575b29cbf5e4 273 //If decimal point is in the fourth character (eg, V-10.1)
david_s95 9:575b29cbf5e4 274 } else if(command[4]=='.') {
david_s95 9:575b29cbf5e4 275 tens = command[2] - '0';
david_s95 9:575b29cbf5e4 276 units = command[3] - '0';
david_s95 9:575b29cbf5e4 277 decimals = command[5] - '0';
david_s95 27:206f781756f9 278 } else if(command[4]=='.') {
david_s95 27:206f781756f9 279 hdrds = command[1] - '0';
david_s95 27:206f781756f9 280 tens = command[2] - '0';
david_s95 27:206f781756f9 281 units = command[3] - '0';
david_s95 27:206f781756f9 282 decimals = command[5] - '0';
david_s95 9:575b29cbf5e4 283 }
david_s95 9:575b29cbf5e4 284 } else {
david_s95 9:575b29cbf5e4 285 spinCW = 1;
david_s95 9:575b29cbf5e4 286 //If decimal point is in the second character (eg, V.1)
david_s95 9:575b29cbf5e4 287 if(command[1]=='.') {
david_s95 9:575b29cbf5e4 288 //Extract decimal rev/s
david_s95 9:575b29cbf5e4 289 decimals = command[2] - '0';
david_s95 7:5932ed0bad6d 290
david_s95 9:575b29cbf5e4 291 //If decimal point is in the third character (eg, V0.1)
david_s95 9:575b29cbf5e4 292 } else if(command[2]=='.') {
david_s95 9:575b29cbf5e4 293 units = command[1] - '0';
david_s95 9:575b29cbf5e4 294 decimals = command[3] - '0';
david_s95 9:575b29cbf5e4 295
david_s95 9:575b29cbf5e4 296 //If decimal point is in the fourth character (eg, V10.1)
david_s95 9:575b29cbf5e4 297 } else if(command[3]=='.') {
david_s95 9:575b29cbf5e4 298 tens = command[1] - '0';
david_s95 9:575b29cbf5e4 299 units = command[2] - '0';
david_s95 9:575b29cbf5e4 300 decimals = command[4] - '0';
david_s95 27:206f781756f9 301 } else if(command[4]=='.') {
david_s95 27:206f781756f9 302 hdrds = command[1] - '0';
david_s95 27:206f781756f9 303 tens = command[2] - '0';
david_s95 27:206f781756f9 304 units = command[3] - '0';
david_s95 27:206f781756f9 305 decimals = command[5] - '0';
david_s95 9:575b29cbf5e4 306 }
david_s95 6:4edbe75736d9 307 }
david_s95 7:5932ed0bad6d 308
david_s95 6:4edbe75736d9 309 //Calculate the number of revolutions per second required
david_s95 27:206f781756f9 310 revsec = float(hdrds)*100 + float(tens)*10 + float(units) + float(decimals)/10;
david_s95 6:4edbe75736d9 311 //Calculate the required wait period
david_s95 12:8ea29b18d289 312
david_s95 19:93ca06d2e311 313 spinWait = (1/revsec)/6;
david_s95 27:206f781756f9 314 controller.setSetPoint(revsec);
david_s95 6:4edbe75736d9 315 //Print values for verification
david_s95 12:8ea29b18d289 316 pc.printf("Rev/S: %2.4f\n\r", revsec);
david_s95 7:5932ed0bad6d 317
david_s95 6:4edbe75736d9 318 //Run the function to start rotating at a fixed speed
david_s95 6:4edbe75736d9 319 fixedSpeed();
david_s95 6:4edbe75736d9 320 break;
david_s95 7:5932ed0bad6d 321 //If anything unexpected was received
david_s95 18:55cd33a3e69f 322
david_s95 7:5932ed0bad6d 323 case 's':
david_s95 10:0309d6c49f26 324 printf("Measured: %2.3f, revsec: %2.3f\r\n", measuredRevs, revsec);
david_s95 22:1c329584282b 325 printf("PID: %2.3f\r\n", speedControl);
david_s95 7:5932ed0bad6d 326 break;
david_s95 12:8ea29b18d289 327
david_s95 12:8ea29b18d289 328 case 'K':
david_s95 12:8ea29b18d289 329 vartens = command[1] - '0';
david_s95 12:8ea29b18d289 330 varunits = command[2] - '0';
david_s95 12:8ea29b18d289 331 vardecs = command[4] - '0';
david_s95 12:8ea29b18d289 332 Kc = float(vartens)*10 + float(varunits) + float(vardecs)/10;
david_s95 12:8ea29b18d289 333 printf("Kc: %2.1f\r\n", Kc);
david_s95 12:8ea29b18d289 334 controller.setTunings(Kc, Ti, Td);
david_s95 12:8ea29b18d289 335 break;
david_s95 12:8ea29b18d289 336 case 'i':
david_s95 12:8ea29b18d289 337 vartens = command[1] - '0';
david_s95 12:8ea29b18d289 338 varunits = command[2] - '0';
david_s95 12:8ea29b18d289 339 vardecs = command[4] - '0';
david_s95 12:8ea29b18d289 340 Ti = float(vartens)*10 + float(varunits) + float(vardecs)/10;
david_s95 12:8ea29b18d289 341 printf("Ti: %2.1f\r\n", Ti);
david_s95 12:8ea29b18d289 342 controller.setTunings(Kc, Ti, Td);
david_s95 12:8ea29b18d289 343 break;
david_s95 12:8ea29b18d289 344 case 'd':
david_s95 12:8ea29b18d289 345 vartens = command[1] - '0';
david_s95 12:8ea29b18d289 346 varunits = command[2] - '0';
david_s95 12:8ea29b18d289 347 vardecs = command[4] - '0';
david_s95 12:8ea29b18d289 348 Td = float(vartens)*10 + float(varunits) + float(vardecs)/10;
david_s95 12:8ea29b18d289 349 printf("Td: %2.1f\r\n", Td);
david_s95 12:8ea29b18d289 350 controller.setTunings(Kc, Ti, Td);
david_s95 12:8ea29b18d289 351 break;
david_s95 26:b9f2d6d3f40e 352 case 'P':
david_s95 26:b9f2d6d3f40e 353 vartens = command[1] - '0';
david_s95 26:b9f2d6d3f40e 354 varunits = command[2] - '0';
david_s95 26:b9f2d6d3f40e 355 vardecs = command[4] - '0';
david_s95 26:b9f2d6d3f40e 356 PIDrate = float(vartens)*10 + float(varunits) + float(vardecs)/10;
david_s95 27:206f781756f9 357 controller.setInterval(PIDrate);
david_s95 27:206f781756f9 358 controller.setMode(1);
david_s95 26:b9f2d6d3f40e 359 printf("Rate: %2.1f\r\n", PIDrate);
david_s95 18:55cd33a3e69f 360 case 'b':
david_s95 18:55cd33a3e69f 361 if(command[1]=='.') {
david_s95 18:55cd33a3e69f 362 //Extract decimal rev/s
david_s95 18:55cd33a3e69f 363 vardecs = command[2] - '0';
david_s95 18:55cd33a3e69f 364
david_s95 18:55cd33a3e69f 365 //If decimal point is in the third character (eg, V-0.1)
david_s95 18:55cd33a3e69f 366 } else if(command[2]=='.') {
david_s95 18:55cd33a3e69f 367 varunits = command[1] - '0';
david_s95 18:55cd33a3e69f 368 vardecs = command[3] - '0';
david_s95 18:55cd33a3e69f 369
david_s95 18:55cd33a3e69f 370 //If decimal point is in the fourth character (eg, V-10.1)
david_s95 18:55cd33a3e69f 371 } else if(command[3]=='.') {
david_s95 18:55cd33a3e69f 372 vartens = command[1] - '0';
david_s95 18:55cd33a3e69f 373 varunits = command[2] - '0';
david_s95 18:55cd33a3e69f 374 vardecs = command[4] - '0';
david_s95 18:55cd33a3e69f 375 }
david_s95 18:55cd33a3e69f 376 bias = float(vartens)*10 + float(varunits) + float(vardecs)/10;
david_s95 18:55cd33a3e69f 377 printf("Bias: %2.1f\r\n", bias);
david_s95 18:55cd33a3e69f 378 controller.setBias(bias);
david_s95 18:55cd33a3e69f 379 break;
david_s95 25:9e6e870821d8 380
david_s95 27:206f781756f9 381 case 'T':
david_s95 27:206f781756f9 382 hdrds = 0, units = 0, tens = 0, decimals = 0;
david_s95 27:206f781756f9 383
david_s95 27:206f781756f9 384 //If decimal point is in the second character (eg, V.1)
david_s95 27:206f781756f9 385 if(command[1]=='.') {
david_s95 27:206f781756f9 386 //Extract decimal rev/s
david_s95 27:206f781756f9 387 decimals = command[2] - '0';
david_s95 27:206f781756f9 388
david_s95 27:206f781756f9 389 //If decimal point is in the third character (eg, V0.1)
david_s95 27:206f781756f9 390 } else if(command[2]=='.') {
david_s95 27:206f781756f9 391 units = command[1] - '0';
david_s95 27:206f781756f9 392 decimals = command[3] - '0';
david_s95 27:206f781756f9 393
david_s95 27:206f781756f9 394 //If decimal point is in the fourth character (eg, V10.1)
david_s95 27:206f781756f9 395 } else if(command[3]=='.') {
david_s95 27:206f781756f9 396 tens = command[1] - '0';
david_s95 27:206f781756f9 397 units = command[2] - '0';
david_s95 27:206f781756f9 398 decimals = command[4] - '0';
david_s95 27:206f781756f9 399 } else if(command[4]=='.') {
david_s95 27:206f781756f9 400 hdrds = command[1] - '0';
david_s95 27:206f781756f9 401 tens = command[2] - '0';
david_s95 27:206f781756f9 402 units = command[3] - '0';
david_s95 27:206f781756f9 403 decimals = command[5] - '0';
david_s95 27:206f781756f9 404 }
david_s95 27:206f781756f9 405 singpin.period_us(float(hdrds)*100 + float(tens)*10 + float(units) + float(decimals)/10);
david_s95 27:206f781756f9 406 break;
david_s95 27:206f781756f9 407
david_s95 6:4edbe75736d9 408 default:
david_s95 6:4edbe75736d9 409 //Set speed variables to zero to stop motor spinning
david_s95 6:4edbe75736d9 410 //Print error message
david_s95 10:0309d6c49f26 411 motorStop();
david_s95 10:0309d6c49f26 412 pc.printf("Error in received data 0\n\r");
david_s95 23:d48d51e5db97 413 motorStop();
david_s95 6:4edbe75736d9 414 break;
david_s95 6:4edbe75736d9 415 }
david_s95 6:4edbe75736d9 416 }
david_s95 27:206f781756f9 417 wait(0.01);
estott 2:4e88faab6988 418 }
david_s95 5:e5313b695302 419
david_s95 25:9e6e870821d8 420 }