ES2017 coursework 2

Dependencies:   PID

Fork of ES_CW2_Starter by Edward Stott

Committer:
theMaO
Date:
Thu Mar 09 15:00:44 2017 +0000
Revision:
16:372c720015ab
Parent:
15:d9e50101a17e
Child:
17:9cd9f82027ca
cleaned up unused 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 9:575b29cbf5e4 50 const int8_t AcwState[7] = {0x00, 0x23, 0x38, 0x32, 0x0E, 0x0B, 0x2C};
david_s95 9:575b29cbf5e4 51 const int8_t cwState[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
theMaO 14:155e9a9147d4 151 void spinToPos(){
theMaO 14:155e9a9147d4 152 currRevs = 0;
theMaO 14:155e9a9147d4 153
theMaO 14:155e9a9147d4 154 }
theMaO 14:155e9a9147d4 155
david_s95 10:0309d6c49f26 156 void rps()
david_s95 7:5932ed0bad6d 157 {
david_s95 10:0309d6c49f26 158
david_s95 10:0309d6c49f26 159 clk=!clk;
david_s95 10:0309d6c49f26 160 speedTimer.stop();
david_s95 10:0309d6c49f26 161 revtimer = speedTimer.read_ms();
david_s95 10:0309d6c49f26 162 speedTimer.reset();
david_s95 10:0309d6c49f26 163 speedTimer.start();
david_s95 10:0309d6c49f26 164
david_s95 10:0309d6c49f26 165 measuredRevs = 1000/(revtimer);
david_s95 10:0309d6c49f26 166 quadraturePosition=0;
david_s95 10:0309d6c49f26 167
david_s95 7:5932ed0bad6d 168 }
david_s95 7:5932ed0bad6d 169
david_s95 10:0309d6c49f26 170 void VPID()
david_s95 10:0309d6c49f26 171 {
david_s95 12:8ea29b18d289 172 controller.setMode(1);
david_s95 10:0309d6c49f26 173 while(1) {
david_s95 10:0309d6c49f26 174 controller.setSetPoint(revsec);
david_s95 10:0309d6c49f26 175 // printf("revsec: %2.3f\r\n", revsec);
david_s95 10:0309d6c49f26 176 controller.setProcessValue(measuredRevs);
david_s95 10:0309d6c49f26 177 speedControl = controller.compute();
david_s95 10:0309d6c49f26 178 // printf("speed setpoint: %2.3f\r\n", speedControl);
david_s95 12:8ea29b18d289 179 if(speedControl<0) speedControl = -speedControl;
david_s95 12:8ea29b18d289 180 else if (speedControl==0) speedControl = 1;
david_s95 12:8ea29b18d289 181 spinWait = (1/speedControl)/6;
david_s95 10:0309d6c49f26 182 Thread::wait(PIDrate);
david_s95 10:0309d6c49f26 183 }
david_s95 10:0309d6c49f26 184 }
david_s95 7:5932ed0bad6d 185
david_s95 13:87ab3b008803 186 void sing(float singFreq)
david_s95 9:575b29cbf5e4 187 {
david_s95 13:87ab3b008803 188 motor = driveTable[1];
david_s95 13:87ab3b008803 189 wait(1.0);
david_s95 13:87ab3b008803 190 float singDelayus = 1000000/singFreq;
david_s95 13:87ab3b008803 191 for(int count=0; count<singFreq; count++) {
david_s95 13:87ab3b008803 192 motor = driveTable[2];
david_s95 13:87ab3b008803 193 wait_us(singDelayus);
david_s95 13:87ab3b008803 194 motor = driveTable[1];
david_s95 8:77627657da80 195 }
david_s95 13:87ab3b008803 196 singFreq = 15000;
david_s95 13:87ab3b008803 197 singDelayus = 1000000/singFreq;
david_s95 13:87ab3b008803 198 for(int count=0; count<singFreq; count++) {
david_s95 13:87ab3b008803 199 motor = driveTable[2];
david_s95 13:87ab3b008803 200 wait_us(singDelayus);
david_s95 13:87ab3b008803 201 motor = driveTable[1];
david_s95 13:87ab3b008803 202 }
david_s95 8:77627657da80 203 }
david_s95 8:77627657da80 204
david_s95 8:77627657da80 205
mengkiang 4:f8a9ce214db9 206 //Main function
theMaO 15:d9e50101a17e 207 >>>>>>> other
david_s95 5:e5313b695302 208 int main()
david_s95 5:e5313b695302 209 {
david_s95 9:575b29cbf5e4 210 pc.printf("spin\n\r");
david_s95 10:0309d6c49f26 211
estott 0:de4320f74764 212 //Run the motor synchronisation
estott 2:4e88faab6988 213 orState = motorHome();
david_s95 6:4edbe75736d9 214 //orState is subtracted from future rotor state inputs to align rotor and motor states
david_s95 6:4edbe75736d9 215
estott 2:4e88faab6988 216 pc.printf("Rotor origin: %x\n\r",orState);
david_s95 5:e5313b695302 217
david_s95 6:4edbe75736d9 218 char command[ARRAYSIZE];
david_s95 6:4edbe75736d9 219 int index=0;
david_s95 6:4edbe75736d9 220 int units = 0, tens = 0, decimals = 0;
david_s95 6:4edbe75736d9 221 char ch;
david_s95 9:575b29cbf5e4 222 testpin=0;
david_s95 12:8ea29b18d289 223 int vartens = 0, varunits = 0, vardecs = 0;
david_s95 7:5932ed0bad6d 224
david_s95 10:0309d6c49f26 225 speedTimer.reset();
david_s95 10:0309d6c49f26 226 speedTimer.start();
david_s95 10:0309d6c49f26 227 I2.mode(PullNone);
david_s95 10:0309d6c49f26 228 I2.fall(&rps);
david_s95 7:5932ed0bad6d 229
david_s95 10:0309d6c49f26 230 VPIDthread.start(VPID);
david_s95 10:0309d6c49f26 231
david_s95 5:e5313b695302 232 while(1) {
david_s95 9:575b29cbf5e4 233 // clk = I2;
david_s95 6:4edbe75736d9 234 //Toggle LED so we know something's happening
david_s95 8:77627657da80 235 // clk = !clk;
david_s95 7:5932ed0bad6d 236
david_s95 6:4edbe75736d9 237 //If there's a character to read from the serial port
david_s95 6:4edbe75736d9 238 if (pc.readable()) {
david_s95 7:5932ed0bad6d 239
david_s95 6:4edbe75736d9 240 //Clear index counter and control variables
david_s95 6:4edbe75736d9 241 index = 0;
david_s95 7:5932ed0bad6d 242 // revsec = spinWait = 0;
david_s95 7:5932ed0bad6d 243
david_s95 6:4edbe75736d9 244 //Read each value from the serial port until Enter key is pressed
david_s95 6:4edbe75736d9 245 do {
david_s95 6:4edbe75736d9 246 //Read character
david_s95 6:4edbe75736d9 247 ch = pc.getc();
david_s95 6:4edbe75736d9 248 //Print character to serial for visual feedback
david_s95 6:4edbe75736d9 249 pc.putc(ch);
david_s95 6:4edbe75736d9 250 //Add character to input array
david_s95 6:4edbe75736d9 251 command[index++]=ch; // put it into the value array and increment the index
david_s95 7:5932ed0bad6d 252 //d10 and d13 used for detecting Enter key on Windows/Unix/Mac
david_s95 6:4edbe75736d9 253 } while(ch != 10 && ch != 13);
david_s95 7:5932ed0bad6d 254
david_s95 6:4edbe75736d9 255 //Start new line on terminal for printing data
david_s95 6:4edbe75736d9 256 pc.putc('\n');
david_s95 6:4edbe75736d9 257 pc.putc('\r');
david_s95 7:5932ed0bad6d 258
david_s95 6:4edbe75736d9 259 //Analyse the input string
david_s95 6:4edbe75736d9 260 switch (command[0]) {
david_s95 7:5932ed0bad6d 261 //If a V was typed...
david_s95 6:4edbe75736d9 262 case 'V':
david_s95 7:5932ed0bad6d 263 units = 0, tens = 0, decimals = 0;
david_s95 7:5932ed0bad6d 264 //For each character received, subtract ASCII 0 from ASCII
david_s95 7:5932ed0bad6d 265 //representation to obtain the integer value of the number
david_s95 9:575b29cbf5e4 266 if(command[1]=='-') {
david_s95 9:575b29cbf5e4 267 spinCW = 0;
david_s95 9:575b29cbf5e4 268 //If decimal point is in the second character (eg, V-.1)
david_s95 9:575b29cbf5e4 269 if(command[2]=='.') {
david_s95 9:575b29cbf5e4 270 //Extract decimal rev/s
david_s95 9:575b29cbf5e4 271 decimals = command[3] - '0';
david_s95 7:5932ed0bad6d 272
david_s95 9:575b29cbf5e4 273 //If decimal point is in the third character (eg, V-0.1)
david_s95 9:575b29cbf5e4 274 } else if(command[3]=='.') {
david_s95 9:575b29cbf5e4 275 units = command[2] - '0';
david_s95 9:575b29cbf5e4 276 decimals = command[4] - '0';
david_s95 7:5932ed0bad6d 277
david_s95 9:575b29cbf5e4 278 //If decimal point is in the fourth character (eg, V-10.1)
david_s95 9:575b29cbf5e4 279 } else if(command[4]=='.') {
david_s95 9:575b29cbf5e4 280 tens = command[2] - '0';
david_s95 9:575b29cbf5e4 281 units = command[3] - '0';
david_s95 9:575b29cbf5e4 282 decimals = command[5] - '0';
david_s95 9:575b29cbf5e4 283 }
david_s95 9:575b29cbf5e4 284 } else {
david_s95 9:575b29cbf5e4 285 spinCW = 1;
david_s95 9:575b29cbf5e4 286 //If decimal point is in the second character (eg, V.1)
david_s95 9:575b29cbf5e4 287 if(command[1]=='.') {
david_s95 9:575b29cbf5e4 288 //Extract decimal rev/s
david_s95 9:575b29cbf5e4 289 decimals = command[2] - '0';
david_s95 7:5932ed0bad6d 290
david_s95 9:575b29cbf5e4 291 //If decimal point is in the third character (eg, V0.1)
david_s95 9:575b29cbf5e4 292 } else if(command[2]=='.') {
david_s95 9:575b29cbf5e4 293 units = command[1] - '0';
david_s95 9:575b29cbf5e4 294 decimals = command[3] - '0';
david_s95 9:575b29cbf5e4 295
david_s95 9:575b29cbf5e4 296 //If decimal point is in the fourth character (eg, V10.1)
david_s95 9:575b29cbf5e4 297 } else if(command[3]=='.') {
david_s95 9:575b29cbf5e4 298 tens = command[1] - '0';
david_s95 9:575b29cbf5e4 299 units = command[2] - '0';
david_s95 9:575b29cbf5e4 300 decimals = command[4] - '0';
david_s95 9:575b29cbf5e4 301 }
david_s95 6:4edbe75736d9 302 }
david_s95 7:5932ed0bad6d 303
david_s95 6:4edbe75736d9 304 //Calculate the number of revolutions per second required
david_s95 6:4edbe75736d9 305 revsec = float(tens)*10 + float(units) + float(decimals)/10;
david_s95 6:4edbe75736d9 306 //Calculate the required wait period
david_s95 12:8ea29b18d289 307
david_s95 7:5932ed0bad6d 308
david_s95 6:4edbe75736d9 309 //Print values for verification
david_s95 12:8ea29b18d289 310 pc.printf("Rev/S: %2.4f\n\r", revsec);
david_s95 7:5932ed0bad6d 311
david_s95 6:4edbe75736d9 312 //Run the function to start rotating at a fixed speed
david_s95 6:4edbe75736d9 313 fixedSpeed();
david_s95 6:4edbe75736d9 314 break;
david_s95 7:5932ed0bad6d 315 //If anything unexpected was received
theMaO 14:155e9a9147d4 316
theMaO 14:155e9a9147d4 317 case 'R':
theMaO 14:155e9a9147d4 318 hdrds = 0; units = 0, tens = 0, decimals = 0;
theMaO 14:155e9a9147d4 319 //For each character received, subtract ASCII 0 from ASCII
theMaO 14:155e9a9147d4 320 //representation to obtain the integer value of the number
theMaO 14:155e9a9147d4 321 if(command[1]=='-') {
theMaO 14:155e9a9147d4 322 spinCW = 0;
theMaO 14:155e9a9147d4 323 //If decimal point is in the second character (eg, V-.1)
theMaO 14:155e9a9147d4 324 if(command[2]=='.') {
theMaO 14:155e9a9147d4 325 //Extract decimal rev/s
theMaO 14:155e9a9147d4 326 decimals = command[3] - '0';
theMaO 14:155e9a9147d4 327
theMaO 14:155e9a9147d4 328 //If decimal point is in the third character (eg, V-0.1)
theMaO 14:155e9a9147d4 329 } else if(command[3]=='.') {
theMaO 14:155e9a9147d4 330 units = command[2] - '0';
theMaO 14:155e9a9147d4 331 decimals = command[4] - '0';
theMaO 14:155e9a9147d4 332
theMaO 14:155e9a9147d4 333 //If decimal point is in the fourth character (eg, V-10.1)
theMaO 14:155e9a9147d4 334 } else if(command[4]=='.') {
theMaO 14:155e9a9147d4 335 tens = command[2] - '0';
theMaO 14:155e9a9147d4 336 units = command[3] - '0';
theMaO 14:155e9a9147d4 337 decimals = command[5] - '0';
theMaO 14:155e9a9147d4 338 } else if(command[5]=='.'){
theMaO 14:155e9a9147d4 339 hdrds = command[2] - '0';
theMaO 14:155e9a9147d4 340 tens = command[3] - '0';
theMaO 14:155e9a9147d4 341 units = command[4] - '0';
theMaO 14:155e9a9147d4 342 decimals = command[6] - '0';
theMaO 14:155e9a9147d4 343 }
theMaO 14:155e9a9147d4 344 } else {
theMaO 14:155e9a9147d4 345 spinCW = 1;
theMaO 14:155e9a9147d4 346 //If decimal point is in the second character (eg, V.1)
theMaO 14:155e9a9147d4 347 if(command[1]=='.') {
theMaO 14:155e9a9147d4 348 //Extract decimal rev/s
theMaO 14:155e9a9147d4 349 decimals = command[2] - '0';
theMaO 14:155e9a9147d4 350
theMaO 14:155e9a9147d4 351 //If decimal point is in the third character (eg, V0.1)
theMaO 14:155e9a9147d4 352 } else if(command[2]=='.') {
theMaO 14:155e9a9147d4 353 units = command[1] - '0';
theMaO 14:155e9a9147d4 354 decimals = command[3] - '0';
theMaO 14:155e9a9147d4 355
theMaO 14:155e9a9147d4 356 //If decimal point is in the fourth character (eg, V10.1)
theMaO 14:155e9a9147d4 357 } else if(command[3]=='.') {
theMaO 14:155e9a9147d4 358 tens = command[1] - '0';
theMaO 14:155e9a9147d4 359 units = command[2] - '0';
theMaO 14:155e9a9147d4 360 decimals = command[4] - '0';
theMaO 14:155e9a9147d4 361 }
theMaO 14:155e9a9147d4 362 else if(command[5]=='.'){
theMaO 14:155e9a9147d4 363 hdrds = command[2] - '0';
theMaO 14:155e9a9147d4 364 tens = command[3] - '0';
theMaO 14:155e9a9147d4 365 units = command[4] - '0';
theMaO 14:155e9a9147d4 366 decimals = command[6] - '0';
theMaO 14:155e9a9147d4 367 }
theMaO 14:155e9a9147d4 368 }
theMaO 14:155e9a9147d4 369 //Calculate the number of revolutions required
theMaO 14:155e9a9147d4 370 targetRevs = float(hundreds)*100 + float(tens)*10 + float(units) + float(decimals)/10;
theMaO 14:155e9a9147d4 371
theMaO 14:155e9a9147d4 372 //Print values for verification
theMaO 14:155e9a9147d4 373 pc.printf("Target revs: %2.4f\n\r", targetRevs);
theMaO 14:155e9a9147d4 374
theMaO 14:155e9a9147d4 375 //Run the function to start rotating at a fixed speed
theMaO 14:155e9a9147d4 376 spinToPos();
theMaO 14:155e9a9147d4 377 break;
theMaO 14:155e9a9147d4 378
theMaO 14:155e9a9147d4 379
david_s95 7:5932ed0bad6d 380 case 's':
david_s95 9:575b29cbf5e4 381 // pc.printf("Revs / sec: %2.2f\r", revs);
david_s95 9:575b29cbf5e4 382 // printSpeed.attach(&speedo, 1.0);
david_s95 10:0309d6c49f26 383 printf("Measured: %2.3f, revsec: %2.3f\r\n", measuredRevs, revsec);
david_s95 10:0309d6c49f26 384 printf("speed setpoint: %2.3f\r\n", speedControl);
david_s95 7:5932ed0bad6d 385 break;
david_s95 8:77627657da80 386 case 't':
david_s95 10:0309d6c49f26 387 // pc.printf("%d\n\r", pos);
david_s95 8:77627657da80 388 break;
david_s95 12:8ea29b18d289 389
david_s95 12:8ea29b18d289 390 case 'K':
david_s95 12:8ea29b18d289 391 vartens = command[1] - '0';
david_s95 12:8ea29b18d289 392 varunits = command[2] - '0';
david_s95 12:8ea29b18d289 393 vardecs = command[4] - '0';
david_s95 12:8ea29b18d289 394 Kc = float(vartens)*10 + float(varunits) + float(vardecs)/10;
david_s95 12:8ea29b18d289 395 printf("Kc: %2.1f\r\n", Kc);
david_s95 12:8ea29b18d289 396 controller.setTunings(Kc, Ti, Td);
david_s95 12:8ea29b18d289 397 // controller.setMode(1);
david_s95 12:8ea29b18d289 398 break;
david_s95 12:8ea29b18d289 399 case 'i':
david_s95 12:8ea29b18d289 400 vartens = command[1] - '0';
david_s95 12:8ea29b18d289 401 varunits = command[2] - '0';
david_s95 12:8ea29b18d289 402 vardecs = command[4] - '0';
david_s95 12:8ea29b18d289 403 Ti = float(vartens)*10 + float(varunits) + float(vardecs)/10;
david_s95 12:8ea29b18d289 404 printf("Ti: %2.1f\r\n", Ti);
david_s95 12:8ea29b18d289 405 controller.setTunings(Kc, Ti, Td);
david_s95 12:8ea29b18d289 406 // controller.setMode(1);
david_s95 12:8ea29b18d289 407 break;
david_s95 12:8ea29b18d289 408 case 'd':
david_s95 12:8ea29b18d289 409 vartens = command[1] - '0';
david_s95 12:8ea29b18d289 410 varunits = command[2] - '0';
david_s95 12:8ea29b18d289 411 vardecs = command[4] - '0';
david_s95 12:8ea29b18d289 412 Td = float(vartens)*10 + float(varunits) + float(vardecs)/10;
david_s95 12:8ea29b18d289 413 printf("Td: %2.1f\r\n", Td);
david_s95 12:8ea29b18d289 414 controller.setTunings(Kc, Ti, Td);
david_s95 12:8ea29b18d289 415 // controller.setMode(1);
david_s95 12:8ea29b18d289 416 break;
david_s95 13:87ab3b008803 417 case 'l':
david_s95 13:87ab3b008803 418 sing(10000);
david_s95 13:87ab3b008803 419 break;
david_s95 6:4edbe75736d9 420 default:
david_s95 6:4edbe75736d9 421 //Set speed variables to zero to stop motor spinning
david_s95 6:4edbe75736d9 422 //Print error message
david_s95 10:0309d6c49f26 423 motorStop();
david_s95 10:0309d6c49f26 424 pc.printf("Error in received data 0\n\r");
david_s95 6:4edbe75736d9 425 break;
david_s95 6:4edbe75736d9 426 }
david_s95 6:4edbe75736d9 427 }
david_s95 7:5932ed0bad6d 428 // printSpeed.attach(&speedo, 1.0);
david_s95 7:5932ed0bad6d 429 // pc.printf("Revs / sec: %2.2f\r", revs);
estott 2:4e88faab6988 430 }
david_s95 5:e5313b695302 431
estott 0:de4320f74764 432 }