ES2017 coursework 2

Dependencies:   PID

Fork of ES_CW2_Starter by Edward Stott

Committer:
david_s95
Date:
Thu Mar 09 17:23:33 2017 +0000
Revision:
19:93ca06d2e311
Parent:
18:55cd33a3e69f
Child:
21:3cc94df7321c
Something's gone wrong, it gets stuck in a loop somewhere and just accelerates as fast as it can.

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