ES2017 coursework 2

Dependencies:   PID

Fork of ES_CW2_Starter by Edward Stott

Committer:
david_s95
Date:
Fri Mar 10 21:11:49 2017 +0000
Revision:
24:4032857546f4
Parent:
23:d48d51e5db97
Child:
25:9e6e870821d8
Motor can now sing when given a period from the command line (currently remapped R as: R100.0 gives 10kHz). Something is pulsing at about 1Hz so somewhere the microcontroller is being held up once a second.

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