ES2017 coursework 2

Dependencies:   PID

Fork of ES_CW2_Starter by Edward Stott

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