ES2017 coursework 2

Dependencies:   PID

Fork of ES_CW2_Starter by Edward Stott

Committer:
david_s95
Date:
Fri Mar 10 19:53:08 2017 +0000
Revision:
23:d48d51e5db97
Parent:
22:1c329584282b
Child:
24:4032857546f4
Tidied up a bit more code.

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