ES2017 coursework 2

Dependencies:   PID

Fork of ES_CW2_Starter by Edward Stott

Committer:
david_s95
Date:
Thu Mar 09 13:02:44 2017 +0000
Revision:
12:8ea29b18d289
Parent:
11:1f596bf4182b
Child:
13:87ab3b008803
Child:
14:155e9a9147d4
PID controller

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 10:0309d6c49f26 7 float PIDrate = 0.2;
david_s95 10:0309d6c49f26 8 float Kc = 1.0;
david_s95 10:0309d6c49f26 9 float Ti = 0.0;
david_s95 10:0309d6c49f26 10 float Td = 0.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 9:575b29cbf5e4 48 //const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00};
david_s95 9:575b29cbf5e4 49 const int8_t driveTable[6] = {0x38, 0x2C, 0x0E, 0x0B, 0x23, 0x32};
david_s95 9:575b29cbf5e4 50
david_s95 9:575b29cbf5e4 51 //const int8_t cwState[7] = {0, 4, 0, 5, 2, 3, 1};
david_s95 9:575b29cbf5e4 52 //const int8_t AcwState[7] = {0, 2, 4, 3, 0, 1, 5};
david_s95 9:575b29cbf5e4 53 //const int8_t cwState[7] = {0x00, 0x23, 0x38, 0x32, 0x0E, 0x0B, 0x2C};
david_s95 9:575b29cbf5e4 54 //const int8_t AcwState[7] = {0x00, 0x0E, 0x23, 0x0B, 0x38, 0x2C, 0x32};
david_s95 9:575b29cbf5e4 55 const int8_t AcwState[7] = {0x00, 0x23, 0x38, 0x32, 0x0E, 0x0B, 0x2C};
david_s95 9:575b29cbf5e4 56 const int8_t cwState[7] = {0x00, 0x0E, 0x23, 0x0B, 0x38, 0x2C, 0x32};
estott 2:4e88faab6988 57
estott 0:de4320f74764 58 //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid
david_s95 9:575b29cbf5e4 59 //const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07};
estott 2:4e88faab6988 60 //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 61
estott 2:4e88faab6988 62 //Phase lead to make motor spin
david_s95 9:575b29cbf5e4 63 //int8_t lead = -2; //2 for forwards, -2 for backwards
estott 0:de4320f74764 64
estott 0:de4320f74764 65 //Status LED
estott 0:de4320f74764 66 DigitalOut led1(LED1);
estott 0:de4320f74764 67
estott 0:de4320f74764 68 //Photointerrupter inputs
david_s95 9:575b29cbf5e4 69 DigitalIn I1(I1pin);
david_s95 9:575b29cbf5e4 70 //InterruptIn I1(I1pin);
david_s95 9:575b29cbf5e4 71 InterruptIn I2(I2pin);
estott 2:4e88faab6988 72 DigitalIn I3(I3pin);
estott 0:de4320f74764 73
david_s95 8:77627657da80 74 InterruptIn qA(CHA);
david_s95 8:77627657da80 75 InterruptIn qB(CHB);
david_s95 8:77627657da80 76
estott 0:de4320f74764 77 //Motor Drive outputs
david_s95 9:575b29cbf5e4 78 //DigitalOut L1L(L1Lpin);
david_s95 9:575b29cbf5e4 79 //DigitalOut L1H(L1Hpin);
david_s95 9:575b29cbf5e4 80 //DigitalOut L2L(L2Lpin);
david_s95 9:575b29cbf5e4 81 //DigitalOut L2H(L2Hpin);
david_s95 9:575b29cbf5e4 82 //DigitalOut L3L(L3Lpin);
david_s95 9:575b29cbf5e4 83 //DigitalOut L3H(L3Hpin);
david_s95 5:e5313b695302 84 DigitalOut clk(LED1);
david_s95 8:77627657da80 85 DigitalOut Direction(LED2);
david_s95 9:575b29cbf5e4 86 DigitalOut testpin(D13);
david_s95 9:575b29cbf5e4 87
david_s95 9:575b29cbf5e4 88 //NOTE, BusOut declares things in reverse (ie, 0, 1, 2, 3) compared to binary represenation
david_s95 9:575b29cbf5e4 89 BusOut motor(L1Lpin, L1Hpin, L2Lpin, L2Hpin, L3Lpin, L3Hpin);
david_s95 5:e5313b695302 90
david_s95 5:e5313b695302 91 //Timeout function for rotating at set speed
david_s95 5:e5313b695302 92 Timeout spinTimer;
david_s95 5:e5313b695302 93 float spinWait = 10;
david_s95 5:e5313b695302 94 float revsec = 0;
david_s95 5:e5313b695302 95
david_s95 7:5932ed0bad6d 96 //Timer used for calculating speed
david_s95 7:5932ed0bad6d 97 Timer speedTimer;
david_s95 10:0309d6c49f26 98 float measuredRevs = 0, revtimer = 0;
david_s95 7:5932ed0bad6d 99 Ticker printSpeed;
david_s95 7:5932ed0bad6d 100
david_s95 5:e5313b695302 101 Serial pc(SERIAL_TX, SERIAL_RX);
david_s95 5:e5313b695302 102
david_s95 5:e5313b695302 103 int8_t orState = 0; //Rotor offset at motor state 0
david_s95 5:e5313b695302 104 int8_t intState = 0;
david_s95 5:e5313b695302 105 int8_t intStateOld = 0;
david_s95 9:575b29cbf5e4 106 int position = 0;
david_s95 5:e5313b695302 107
david_s95 5:e5313b695302 108 int i=0;
david_s95 10:0309d6c49f26 109 int quadraturePosition=0;
david_s95 9:575b29cbf5e4 110 bool spinCW=0;
david_s95 10:0309d6c49f26 111 float u = 0; //Set point for VPI
david_s95 5:e5313b695302 112
estott 0:de4320f74764 113 //Set a given drive state
david_s95 5:e5313b695302 114 void motorOut(int8_t driveState)
david_s95 5:e5313b695302 115 {
david_s95 5:e5313b695302 116
david_s95 9:575b29cbf5e4 117 //Set to zero
david_s95 9:575b29cbf5e4 118 motor=0x2A;
david_s95 10:0309d6c49f26 119
david_s95 9:575b29cbf5e4 120 //Go to next state
david_s95 9:575b29cbf5e4 121 if(!spinCW) motor = AcwState[driveState];
david_s95 9:575b29cbf5e4 122 else motor = cwState[driveState];
estott 2:4e88faab6988 123 //Lookup the output byte from the drive state.
david_s95 9:575b29cbf5e4 124 // int8_t driveOut = driveTable[driveState & 0x07];
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 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];
estott 0:de4320f74764 146 wait(1.0);
david_s95 10:0309d6c49f26 147
david_s95 9:575b29cbf5e4 148 position = 0;
david_s95 5:e5313b695302 149
estott 0:de4320f74764 150 //Get the rotor state
estott 2:4e88faab6988 151 return readRotorState();
estott 0:de4320f74764 152 }
david_s95 5:e5313b695302 153
david_s95 5:e5313b695302 154 void fixedSpeed()
david_s95 5:e5313b695302 155 {
david_s95 6:4edbe75736d9 156 //Read current motor state
david_s95 10:0309d6c49f26 157 // clk=!clk;
david_s95 10:0309d6c49f26 158 // {0x38, 0x2C, 0x0E, 0x0B, 0x23, 0x32};
david_s95 10:0309d6c49f26 159 //if(spinCW) {
david_s95 10:0309d6c49f26 160 // switch(motor) {
david_s95 10:0309d6c49f26 161 // case 0x38:
david_s95 10:0309d6c49f26 162 // motor=0x2C;
david_s95 10:0309d6c49f26 163 // break;
david_s95 10:0309d6c49f26 164 // case 0x2C:
david_s95 10:0309d6c49f26 165 // motor=0x0E;
david_s95 10:0309d6c49f26 166 // break;
david_s95 10:0309d6c49f26 167 // case 0x0E:
david_s95 10:0309d6c49f26 168 // motor=0x0B;
david_s95 10:0309d6c49f26 169 // break;
david_s95 10:0309d6c49f26 170 // case 0x0B:
david_s95 10:0309d6c49f26 171 // motor=0x23;
david_s95 10:0309d6c49f26 172 // break;
david_s95 10:0309d6c49f26 173 // case 0x23:
david_s95 10:0309d6c49f26 174 // motor=0x32;
david_s95 10:0309d6c49f26 175 // break;
david_s95 10:0309d6c49f26 176 // case 0x32:
david_s95 10:0309d6c49f26 177 // motor=0x38;
david_s95 10:0309d6c49f26 178 // break;
david_s95 10:0309d6c49f26 179 // }
david_s95 10:0309d6c49f26 180 // } else {
david_s95 10:0309d6c49f26 181 // switch(motor) {
david_s95 10:0309d6c49f26 182 // case 0x38:
david_s95 10:0309d6c49f26 183 // motor=0x32;
david_s95 10:0309d6c49f26 184 // break;
david_s95 10:0309d6c49f26 185 // case 0x2C:
david_s95 10:0309d6c49f26 186 // motor=0x38;
david_s95 10:0309d6c49f26 187 // break;
david_s95 10:0309d6c49f26 188 // case 0x0E:
david_s95 10:0309d6c49f26 189 // motor=0x2C;
david_s95 10:0309d6c49f26 190 // break;
david_s95 10:0309d6c49f26 191 // case 0x0B:
david_s95 10:0309d6c49f26 192 // motor=0x0E;
david_s95 10:0309d6c49f26 193 // break;
david_s95 10:0309d6c49f26 194 // case 0x23:
david_s95 10:0309d6c49f26 195 // motor=0x0B;
david_s95 10:0309d6c49f26 196 // break;
david_s95 10:0309d6c49f26 197 // case 0x32:
david_s95 10:0309d6c49f26 198 // motor=0x23;
david_s95 10:0309d6c49f26 199 // break;
david_s95 10:0309d6c49f26 200 // }
david_s95 10:0309d6c49f26 201 // }
david_s95 5:e5313b695302 202 intState = readRotorState();
david_s95 6:4edbe75736d9 203 //Increment state machine to next state
david_s95 9:575b29cbf5e4 204 motorOut(intState);
david_s95 6:4edbe75736d9 205 //If spinning is required, attach the necessary wait to the
david_s95 6:4edbe75736d9 206 //timeout interrupt to call this function again and
david_s95 6:4edbe75736d9 207 //keep the motor spinning at the right speed
david_s95 5:e5313b695302 208 if(revsec) spinTimer.attach(&fixedSpeed, spinWait);
david_s95 5:e5313b695302 209 }
david_s95 5:e5313b695302 210
david_s95 10:0309d6c49f26 211 void rps()
david_s95 7:5932ed0bad6d 212 {
david_s95 10:0309d6c49f26 213
david_s95 10:0309d6c49f26 214 clk=!clk;
david_s95 10:0309d6c49f26 215 speedTimer.stop();
david_s95 10:0309d6c49f26 216 revtimer = speedTimer.read_ms();
david_s95 10:0309d6c49f26 217 speedTimer.reset();
david_s95 10:0309d6c49f26 218 speedTimer.start();
david_s95 10:0309d6c49f26 219
david_s95 10:0309d6c49f26 220 measuredRevs = 1000/(revtimer);
david_s95 10:0309d6c49f26 221 quadraturePosition=0;
david_s95 10:0309d6c49f26 222
david_s95 7:5932ed0bad6d 223 }
david_s95 7:5932ed0bad6d 224
david_s95 10:0309d6c49f26 225 void VPID()
david_s95 10:0309d6c49f26 226 {
david_s95 12:8ea29b18d289 227 controller.setMode(1);
david_s95 10:0309d6c49f26 228 while(1) {
david_s95 10:0309d6c49f26 229 controller.setSetPoint(revsec);
david_s95 10:0309d6c49f26 230 // printf("revsec: %2.3f\r\n", revsec);
david_s95 10:0309d6c49f26 231 controller.setProcessValue(measuredRevs);
david_s95 10:0309d6c49f26 232 speedControl = controller.compute();
david_s95 10:0309d6c49f26 233 // printf("speed setpoint: %2.3f\r\n", speedControl);
david_s95 12:8ea29b18d289 234 if(speedControl<0) speedControl = -speedControl;
david_s95 12:8ea29b18d289 235 else if (speedControl==0) speedControl = 1;
david_s95 12:8ea29b18d289 236 spinWait = (1/speedControl)/6;
david_s95 10:0309d6c49f26 237 Thread::wait(PIDrate);
david_s95 10:0309d6c49f26 238 }
david_s95 10:0309d6c49f26 239 }
david_s95 10:0309d6c49f26 240 /*
david_s95 7:5932ed0bad6d 241
david_s95 9:575b29cbf5e4 242 void edgeRiseA()
david_s95 9:575b29cbf5e4 243 {
david_s95 8:77627657da80 244 pos++;
david_s95 9:575b29cbf5e4 245 if(pos>=468) {
david_s95 8:77627657da80 246 // Direction=!Direction;
david_s95 8:77627657da80 247 pos=pos%468;
david_s95 8:77627657da80 248 // testpin=!testpin;
david_s95 8:77627657da80 249 }
david_s95 8:77627657da80 250 if(qB) DIR = 0;
david_s95 8:77627657da80 251 else DIR = 1;
david_s95 9:575b29cbf5e4 252 // clk=DIR;
david_s95 8:77627657da80 253 //CLOCKWISE: A rises before B -> On A edge, B low -> DIR = 1
david_s95 8:77627657da80 254 //ANTICLOCKWISE: B rises before A -> On A edge, B high-> DIR = 0
david_s95 8:77627657da80 255 }
david_s95 8:77627657da80 256
david_s95 9:575b29cbf5e4 257 void edgeIncr()
david_s95 9:575b29cbf5e4 258 {
david_s95 8:77627657da80 259 pos++;
david_s95 9:575b29cbf5e4 260 if(pos>=468) {
david_s95 8:77627657da80 261 // Direction=!Direction;
david_s95 8:77627657da80 262 pos=pos%468;
david_s95 8:77627657da80 263 // testpin=!testpin;
david_s95 8:77627657da80 264 }
david_s95 9:575b29cbf5e4 265 }*/
david_s95 8:77627657da80 266
david_s95 9:575b29cbf5e4 267 //#define WAIT 2
mengkiang 4:f8a9ce214db9 268 //Main function
david_s95 5:e5313b695302 269 int main()
david_s95 5:e5313b695302 270 {
david_s95 9:575b29cbf5e4 271 pc.printf("spin\n\r");
david_s95 10:0309d6c49f26 272
david_s95 9:575b29cbf5e4 273 // motor = 0x0E;
david_s95 10:0309d6c49f26 274 /* while(1){
david_s95 10:0309d6c49f26 275 motor=0x38;
david_s95 10:0309d6c49f26 276 printf("0x38\r\n");
david_s95 10:0309d6c49f26 277 wait(WAIT);
david_s95 10:0309d6c49f26 278 position = I1 + 2*I2 + 4*I3;
david_s95 10:0309d6c49f26 279 printf("position: %d\r\n", position);
david_s95 10:0309d6c49f26 280 motor=0x2C;
david_s95 10:0309d6c49f26 281 printf("0x2C\r\n");
david_s95 10:0309d6c49f26 282 wait(WAIT);
david_s95 10:0309d6c49f26 283 position = I1 + 2*I2 + 4*I3;
david_s95 10:0309d6c49f26 284 printf("position: %d\r\n", position);
david_s95 10:0309d6c49f26 285 motor=0x0E;
david_s95 10:0309d6c49f26 286 printf("0x0E\r\n");
david_s95 10:0309d6c49f26 287 wait(WAIT);
david_s95 10:0309d6c49f26 288 position = I1 + 2*I2 + 4*I3;
david_s95 10:0309d6c49f26 289 printf("position: %d\r\n", position);
david_s95 10:0309d6c49f26 290 motor=0x0B;
david_s95 10:0309d6c49f26 291 printf("0x0B\r\n");
david_s95 10:0309d6c49f26 292 wait(WAIT);
david_s95 10:0309d6c49f26 293 position = I1 + 2*I2 + 4*I3;
david_s95 10:0309d6c49f26 294 printf("position: %d\r\n", position);
david_s95 10:0309d6c49f26 295 motor=0x23;
david_s95 10:0309d6c49f26 296 printf("0x23\r\n");
david_s95 10:0309d6c49f26 297 wait(WAIT);
david_s95 10:0309d6c49f26 298 position = I1 + 2*I2 + 4*I3;
david_s95 10:0309d6c49f26 299 printf("position: %d\r\n", position);
david_s95 10:0309d6c49f26 300 motor=0x32;
david_s95 10:0309d6c49f26 301 printf("0x32\r\n");
david_s95 10:0309d6c49f26 302 wait(WAIT);
david_s95 10:0309d6c49f26 303 position = I1 + 2*I2 + 4*I3;
david_s95 10:0309d6c49f26 304 printf("position: %d\r\n", position);
david_s95 10:0309d6c49f26 305 }*/
david_s95 7:5932ed0bad6d 306
estott 0:de4320f74764 307 //Run the motor synchronisation
estott 2:4e88faab6988 308 orState = motorHome();
david_s95 6:4edbe75736d9 309 //orState is subtracted from future rotor state inputs to align rotor and motor states
david_s95 6:4edbe75736d9 310
estott 2:4e88faab6988 311 pc.printf("Rotor origin: %x\n\r",orState);
david_s95 5:e5313b695302 312
david_s95 6:4edbe75736d9 313 char command[ARRAYSIZE];
david_s95 6:4edbe75736d9 314 int index=0;
david_s95 6:4edbe75736d9 315 int units = 0, tens = 0, decimals = 0;
david_s95 6:4edbe75736d9 316 char ch;
david_s95 9:575b29cbf5e4 317 testpin=0;
david_s95 12:8ea29b18d289 318 int vartens = 0, varunits = 0, vardecs = 0;
david_s95 7:5932ed0bad6d 319
david_s95 10:0309d6c49f26 320 speedTimer.reset();
david_s95 10:0309d6c49f26 321 speedTimer.start();
david_s95 10:0309d6c49f26 322 I2.mode(PullNone);
david_s95 10:0309d6c49f26 323 I2.fall(&rps);
david_s95 9:575b29cbf5e4 324 //
david_s95 9:575b29cbf5e4 325 // qA.rise(&edgeRiseA);
david_s95 9:575b29cbf5e4 326 // qB.rise(&edgeIncr);
david_s95 9:575b29cbf5e4 327 // qA.fall(&edgeIncr);
david_s95 9:575b29cbf5e4 328 // qB.fall(&edgeIncr);
david_s95 7:5932ed0bad6d 329
david_s95 10:0309d6c49f26 330 VPIDthread.start(VPID);
david_s95 10:0309d6c49f26 331
david_s95 5:e5313b695302 332 while(1) {
david_s95 9:575b29cbf5e4 333 // clk = I2;
david_s95 6:4edbe75736d9 334 //Toggle LED so we know something's happening
david_s95 8:77627657da80 335 // clk = !clk;
david_s95 7:5932ed0bad6d 336
david_s95 6:4edbe75736d9 337 //If there's a character to read from the serial port
david_s95 6:4edbe75736d9 338 if (pc.readable()) {
david_s95 7:5932ed0bad6d 339
david_s95 6:4edbe75736d9 340 //Clear index counter and control variables
david_s95 6:4edbe75736d9 341 index = 0;
david_s95 7:5932ed0bad6d 342 // revsec = spinWait = 0;
david_s95 7:5932ed0bad6d 343
david_s95 6:4edbe75736d9 344 //Read each value from the serial port until Enter key is pressed
david_s95 6:4edbe75736d9 345 do {
david_s95 6:4edbe75736d9 346 //Read character
david_s95 6:4edbe75736d9 347 ch = pc.getc();
david_s95 6:4edbe75736d9 348 //Print character to serial for visual feedback
david_s95 6:4edbe75736d9 349 pc.putc(ch);
david_s95 6:4edbe75736d9 350 //Add character to input array
david_s95 6:4edbe75736d9 351 command[index++]=ch; // put it into the value array and increment the index
david_s95 7:5932ed0bad6d 352 //d10 and d13 used for detecting Enter key on Windows/Unix/Mac
david_s95 6:4edbe75736d9 353 } while(ch != 10 && ch != 13);
david_s95 7:5932ed0bad6d 354
david_s95 6:4edbe75736d9 355 //Start new line on terminal for printing data
david_s95 6:4edbe75736d9 356 pc.putc('\n');
david_s95 6:4edbe75736d9 357 pc.putc('\r');
david_s95 7:5932ed0bad6d 358
david_s95 6:4edbe75736d9 359 //Analyse the input string
david_s95 6:4edbe75736d9 360 switch (command[0]) {
david_s95 7:5932ed0bad6d 361 //If a V was typed...
david_s95 6:4edbe75736d9 362 case 'V':
david_s95 7:5932ed0bad6d 363 units = 0, tens = 0, decimals = 0;
david_s95 7:5932ed0bad6d 364 //For each character received, subtract ASCII 0 from ASCII
david_s95 7:5932ed0bad6d 365 //representation to obtain the integer value of the number
david_s95 9:575b29cbf5e4 366 if(command[1]=='-') {
david_s95 9:575b29cbf5e4 367 spinCW = 0;
david_s95 9:575b29cbf5e4 368 //If decimal point is in the second character (eg, V-.1)
david_s95 9:575b29cbf5e4 369 if(command[2]=='.') {
david_s95 9:575b29cbf5e4 370 //Extract decimal rev/s
david_s95 9:575b29cbf5e4 371 decimals = command[3] - '0';
david_s95 7:5932ed0bad6d 372
david_s95 9:575b29cbf5e4 373 //If decimal point is in the third character (eg, V-0.1)
david_s95 9:575b29cbf5e4 374 } else if(command[3]=='.') {
david_s95 9:575b29cbf5e4 375 units = command[2] - '0';
david_s95 9:575b29cbf5e4 376 decimals = command[4] - '0';
david_s95 7:5932ed0bad6d 377
david_s95 9:575b29cbf5e4 378 //If decimal point is in the fourth character (eg, V-10.1)
david_s95 9:575b29cbf5e4 379 } else if(command[4]=='.') {
david_s95 9:575b29cbf5e4 380 tens = command[2] - '0';
david_s95 9:575b29cbf5e4 381 units = command[3] - '0';
david_s95 9:575b29cbf5e4 382 decimals = command[5] - '0';
david_s95 9:575b29cbf5e4 383 }
david_s95 9:575b29cbf5e4 384 } else {
david_s95 9:575b29cbf5e4 385 spinCW = 1;
david_s95 9:575b29cbf5e4 386 //If decimal point is in the second character (eg, V.1)
david_s95 9:575b29cbf5e4 387 if(command[1]=='.') {
david_s95 9:575b29cbf5e4 388 //Extract decimal rev/s
david_s95 9:575b29cbf5e4 389 decimals = command[2] - '0';
david_s95 7:5932ed0bad6d 390
david_s95 9:575b29cbf5e4 391 //If decimal point is in the third character (eg, V0.1)
david_s95 9:575b29cbf5e4 392 } else if(command[2]=='.') {
david_s95 9:575b29cbf5e4 393 units = command[1] - '0';
david_s95 9:575b29cbf5e4 394 decimals = command[3] - '0';
david_s95 9:575b29cbf5e4 395
david_s95 9:575b29cbf5e4 396 //If decimal point is in the fourth character (eg, V10.1)
david_s95 9:575b29cbf5e4 397 } else if(command[3]=='.') {
david_s95 9:575b29cbf5e4 398 tens = command[1] - '0';
david_s95 9:575b29cbf5e4 399 units = command[2] - '0';
david_s95 9:575b29cbf5e4 400 decimals = command[4] - '0';
david_s95 9:575b29cbf5e4 401 }
david_s95 6:4edbe75736d9 402 }
david_s95 7:5932ed0bad6d 403
david_s95 6:4edbe75736d9 404 //Calculate the number of revolutions per second required
david_s95 6:4edbe75736d9 405 revsec = float(tens)*10 + float(units) + float(decimals)/10;
david_s95 6:4edbe75736d9 406 //Calculate the required wait period
david_s95 12:8ea29b18d289 407
david_s95 7:5932ed0bad6d 408
david_s95 6:4edbe75736d9 409 //Print values for verification
david_s95 12:8ea29b18d289 410 pc.printf("Rev/S: %2.4f\n\r", revsec);
david_s95 7:5932ed0bad6d 411
david_s95 6:4edbe75736d9 412 //Run the function to start rotating at a fixed speed
david_s95 6:4edbe75736d9 413 fixedSpeed();
david_s95 6:4edbe75736d9 414 break;
david_s95 7:5932ed0bad6d 415 //If anything unexpected was received
david_s95 7:5932ed0bad6d 416 case 's':
david_s95 9:575b29cbf5e4 417 // pc.printf("Revs / sec: %2.2f\r", revs);
david_s95 9:575b29cbf5e4 418 // printSpeed.attach(&speedo, 1.0);
david_s95 10:0309d6c49f26 419 printf("Measured: %2.3f, revsec: %2.3f\r\n", measuredRevs, revsec);
david_s95 10:0309d6c49f26 420 printf("speed setpoint: %2.3f\r\n", speedControl);
david_s95 7:5932ed0bad6d 421 break;
david_s95 8:77627657da80 422 case 't':
david_s95 10:0309d6c49f26 423 // pc.printf("%d\n\r", pos);
david_s95 8:77627657da80 424 break;
david_s95 12:8ea29b18d289 425
david_s95 12:8ea29b18d289 426 case 'K':
david_s95 12:8ea29b18d289 427 vartens = command[1] - '0';
david_s95 12:8ea29b18d289 428 varunits = command[2] - '0';
david_s95 12:8ea29b18d289 429 vardecs = command[4] - '0';
david_s95 12:8ea29b18d289 430 Kc = float(vartens)*10 + float(varunits) + float(vardecs)/10;
david_s95 12:8ea29b18d289 431 printf("Kc: %2.1f\r\n", Kc);
david_s95 12:8ea29b18d289 432 controller.setTunings(Kc, Ti, Td);
david_s95 12:8ea29b18d289 433 // controller.setMode(1);
david_s95 12:8ea29b18d289 434 break;
david_s95 12:8ea29b18d289 435 case 'i':
david_s95 12:8ea29b18d289 436 vartens = command[1] - '0';
david_s95 12:8ea29b18d289 437 varunits = command[2] - '0';
david_s95 12:8ea29b18d289 438 vardecs = command[4] - '0';
david_s95 12:8ea29b18d289 439 Ti = float(vartens)*10 + float(varunits) + float(vardecs)/10;
david_s95 12:8ea29b18d289 440 printf("Ti: %2.1f\r\n", Ti);
david_s95 12:8ea29b18d289 441 controller.setTunings(Kc, Ti, Td);
david_s95 12:8ea29b18d289 442 // controller.setMode(1);
david_s95 12:8ea29b18d289 443 break;
david_s95 12:8ea29b18d289 444 case 'd':
david_s95 12:8ea29b18d289 445 vartens = command[1] - '0';
david_s95 12:8ea29b18d289 446 varunits = command[2] - '0';
david_s95 12:8ea29b18d289 447 vardecs = command[4] - '0';
david_s95 12:8ea29b18d289 448 Td = float(vartens)*10 + float(varunits) + float(vardecs)/10;
david_s95 12:8ea29b18d289 449 printf("Td: %2.1f\r\n", Td);
david_s95 12:8ea29b18d289 450 controller.setTunings(Kc, Ti, Td);
david_s95 12:8ea29b18d289 451 // controller.setMode(1);
david_s95 12:8ea29b18d289 452 break;
david_s95 6:4edbe75736d9 453 default:
david_s95 6:4edbe75736d9 454 //Set speed variables to zero to stop motor spinning
david_s95 6:4edbe75736d9 455 //Print error message
david_s95 10:0309d6c49f26 456 motorStop();
david_s95 10:0309d6c49f26 457 pc.printf("Error in received data 0\n\r");
david_s95 6:4edbe75736d9 458 break;
david_s95 6:4edbe75736d9 459 }
david_s95 6:4edbe75736d9 460 }
david_s95 7:5932ed0bad6d 461 // printSpeed.attach(&speedo, 1.0);
david_s95 7:5932ed0bad6d 462 // pc.printf("Revs / sec: %2.2f\r", revs);
estott 2:4e88faab6988 463 }
david_s95 5:e5313b695302 464
estott 0:de4320f74764 465 }