ES2017 coursework 2

Dependencies:   PID

Fork of ES_CW2_Starter by Edward Stott

Committer:
david_s95
Date:
Thu Mar 02 20:27:10 2017 +0000
Revision:
8:77627657da80
Parent:
7:5932ed0bad6d
Child:
9:575b29cbf5e4
Quadrature encoders now working to give direction and 1 degree absolute position.

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>
estott 0:de4320f74764 4
estott 0:de4320f74764 5 //Photointerrupter input pins
estott 0:de4320f74764 6 #define I1pin D2
estott 2:4e88faab6988 7 #define I2pin D11
estott 2:4e88faab6988 8 #define I3pin D12
estott 2:4e88faab6988 9
estott 2:4e88faab6988 10 //Incremental encoder input pins
estott 2:4e88faab6988 11 #define CHA D7
david_s95 5:e5313b695302 12 #define CHB D8
estott 0:de4320f74764 13
estott 0:de4320f74764 14 //Motor Drive output pins //Mask in output byte
estott 2:4e88faab6988 15 #define L1Lpin D4 //0x01
estott 2:4e88faab6988 16 #define L1Hpin D5 //0x02
estott 2:4e88faab6988 17 #define L2Lpin D3 //0x04
estott 2:4e88faab6988 18 #define L2Hpin D6 //0x08
estott 2:4e88faab6988 19 #define L3Lpin D9 //0x10
estott 0:de4320f74764 20 #define L3Hpin D10 //0x20
estott 0:de4320f74764 21
david_s95 5:e5313b695302 22 //Define sized for command arrays
david_s95 5:e5313b695302 23 #define ARRAYSIZE 8
david_s95 5:e5313b695302 24
estott 0:de4320f74764 25 //Mapping from sequential drive states to motor phase outputs
estott 0:de4320f74764 26 /*
estott 0:de4320f74764 27 State L1 L2 L3
estott 0:de4320f74764 28 0 H - L
estott 0:de4320f74764 29 1 - H L
estott 0:de4320f74764 30 2 L H -
estott 0:de4320f74764 31 3 L - H
estott 0:de4320f74764 32 4 - L H
estott 0:de4320f74764 33 5 H L -
estott 0:de4320f74764 34 6 - - -
estott 0:de4320f74764 35 7 - - -
estott 0:de4320f74764 36 */
estott 0:de4320f74764 37 //Drive state to output table
estott 0:de4320f74764 38 const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00};
estott 2:4e88faab6988 39
estott 0:de4320f74764 40 //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid
david_s95 5:e5313b695302 41 const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07};
estott 2:4e88faab6988 42 //const int8_t stateMap[] = {0x07,0x01,0x03,0x02,0x05,0x00,0x04,0x07}; //Alternative if phase order of input or drive is reversed
estott 2:4e88faab6988 43
estott 2:4e88faab6988 44 //Phase lead to make motor spin
david_s95 3:e7133505f542 45 const int8_t lead = 2; //2 for forwards, -2 for backwards
estott 0:de4320f74764 46
estott 0:de4320f74764 47 //Status LED
estott 0:de4320f74764 48 DigitalOut led1(LED1);
estott 0:de4320f74764 49
estott 0:de4320f74764 50 //Photointerrupter inputs
david_s95 7:5932ed0bad6d 51 //DigitalIn I1(I1pin);
david_s95 7:5932ed0bad6d 52 InterruptIn I1(I1pin);
estott 2:4e88faab6988 53 DigitalIn I2(I2pin);
estott 2:4e88faab6988 54 DigitalIn I3(I3pin);
estott 0:de4320f74764 55
david_s95 8:77627657da80 56 InterruptIn qA(CHA);
david_s95 8:77627657da80 57 InterruptIn qB(CHB);
david_s95 8:77627657da80 58
estott 0:de4320f74764 59 //Motor Drive outputs
estott 0:de4320f74764 60 DigitalOut L1L(L1Lpin);
estott 0:de4320f74764 61 DigitalOut L1H(L1Hpin);
estott 0:de4320f74764 62 DigitalOut L2L(L2Lpin);
estott 0:de4320f74764 63 DigitalOut L2H(L2Hpin);
estott 0:de4320f74764 64 DigitalOut L3L(L3Lpin);
estott 0:de4320f74764 65 DigitalOut L3H(L3Hpin);
david_s95 5:e5313b695302 66 DigitalOut clk(LED1);
david_s95 8:77627657da80 67 DigitalOut Direction(LED2);
david_s95 8:77627657da80 68 //DigitalOut testpin(D13);
david_s95 5:e5313b695302 69
david_s95 5:e5313b695302 70 //Timeout function for rotating at set speed
david_s95 5:e5313b695302 71 Timeout spinTimer;
david_s95 5:e5313b695302 72 float spinWait = 10;
david_s95 5:e5313b695302 73 float revsec = 0;
david_s95 5:e5313b695302 74
david_s95 7:5932ed0bad6d 75 //Timer used for calculating speed
david_s95 7:5932ed0bad6d 76 Timer speedTimer;
david_s95 7:5932ed0bad6d 77 float revs = 0, revtimer = 0;
david_s95 7:5932ed0bad6d 78 Ticker printSpeed;
david_s95 7:5932ed0bad6d 79
david_s95 5:e5313b695302 80 Serial pc(SERIAL_TX, SERIAL_RX);
david_s95 5:e5313b695302 81
david_s95 5:e5313b695302 82 int8_t orState = 0; //Rotor offset at motor state 0
david_s95 5:e5313b695302 83 int8_t intState = 0;
david_s95 5:e5313b695302 84 int8_t intStateOld = 0;
david_s95 5:e5313b695302 85
david_s95 5:e5313b695302 86 int i=0;
david_s95 8:77627657da80 87 int pos=0;
david_s95 8:77627657da80 88 bool DIR=0;
david_s95 5:e5313b695302 89
estott 0:de4320f74764 90 //Set a given drive state
david_s95 5:e5313b695302 91 void motorOut(int8_t driveState)
david_s95 5:e5313b695302 92 {
david_s95 5:e5313b695302 93
estott 2:4e88faab6988 94 //Lookup the output byte from the drive state.
estott 2:4e88faab6988 95 int8_t driveOut = driveTable[driveState & 0x07];
david_s95 5:e5313b695302 96
estott 2:4e88faab6988 97 //Turn off first
estott 2:4e88faab6988 98 if (~driveOut & 0x01) L1L = 0;
estott 2:4e88faab6988 99 if (~driveOut & 0x02) L1H = 1;
estott 2:4e88faab6988 100 if (~driveOut & 0x04) L2L = 0;
estott 2:4e88faab6988 101 if (~driveOut & 0x08) L2H = 1;
estott 2:4e88faab6988 102 if (~driveOut & 0x10) L3L = 0;
estott 2:4e88faab6988 103 if (~driveOut & 0x20) L3H = 1;
david_s95 5:e5313b695302 104
estott 2:4e88faab6988 105 //Then turn on
estott 2:4e88faab6988 106 if (driveOut & 0x01) L1L = 1;
estott 2:4e88faab6988 107 if (driveOut & 0x02) L1H = 0;
estott 2:4e88faab6988 108 if (driveOut & 0x04) L2L = 1;
estott 2:4e88faab6988 109 if (driveOut & 0x08) L2H = 0;
estott 2:4e88faab6988 110 if (driveOut & 0x10) L3L = 1;
estott 2:4e88faab6988 111 if (driveOut & 0x20) L3H = 0;
david_s95 5:e5313b695302 112 }
david_s95 5:e5313b695302 113
david_s95 5:e5313b695302 114 //Convert photointerrupter inputs to a rotor state
david_s95 5:e5313b695302 115 inline int8_t readRotorState()
david_s95 5:e5313b695302 116 {
estott 2:4e88faab6988 117 return stateMap[I1 + 2*I2 + 4*I3];
david_s95 5:e5313b695302 118 }
estott 0:de4320f74764 119
david_s95 5:e5313b695302 120 //Basic synchronisation routine
david_s95 5:e5313b695302 121 int8_t motorHome()
david_s95 5:e5313b695302 122 {
estott 0:de4320f74764 123 //Put the motor in drive state 0 and wait for it to stabilise
estott 0:de4320f74764 124 motorOut(0);
estott 0:de4320f74764 125 wait(1.0);
david_s95 5:e5313b695302 126
estott 0:de4320f74764 127 //Get the rotor state
estott 2:4e88faab6988 128 return readRotorState();
estott 0:de4320f74764 129 }
david_s95 5:e5313b695302 130
david_s95 5:e5313b695302 131 void fixedSpeed()
david_s95 5:e5313b695302 132 {
david_s95 6:4edbe75736d9 133 //Read current motor state
david_s95 5:e5313b695302 134 intState = readRotorState();
david_s95 6:4edbe75736d9 135 //Increment state machine to next state
david_s95 5:e5313b695302 136 motorOut((intState-orState+lead+6)%6);
david_s95 6:4edbe75736d9 137 //If spinning is required, attach the necessary wait to the
david_s95 6:4edbe75736d9 138 //timeout interrupt to call this function again and
david_s95 6:4edbe75736d9 139 //keep the motor spinning at the right speed
david_s95 5:e5313b695302 140 if(revsec) spinTimer.attach(&fixedSpeed, spinWait);
david_s95 5:e5313b695302 141 }
david_s95 5:e5313b695302 142
david_s95 7:5932ed0bad6d 143 void rps()
david_s95 7:5932ed0bad6d 144 {
david_s95 7:5932ed0bad6d 145 // pc.printf("Tick\r\n");
david_s95 7:5932ed0bad6d 146 speedTimer.stop();
david_s95 7:5932ed0bad6d 147 revtimer = speedTimer.read_ms();
david_s95 7:5932ed0bad6d 148 revs = 1000/(revtimer);
david_s95 7:5932ed0bad6d 149 // pc.printf("Revs / sec: %2.2f\r", revs);
david_s95 7:5932ed0bad6d 150 speedTimer.start();
david_s95 7:5932ed0bad6d 151 }
david_s95 7:5932ed0bad6d 152
david_s95 7:5932ed0bad6d 153 void speedo()
david_s95 7:5932ed0bad6d 154 {
david_s95 7:5932ed0bad6d 155 pc.printf("Revs / sec: %2.4f\r", revs);
david_s95 7:5932ed0bad6d 156 printSpeed.attach(&speedo, 1.0);
david_s95 7:5932ed0bad6d 157 }
david_s95 7:5932ed0bad6d 158
david_s95 8:77627657da80 159 void edgeRiseA(){
david_s95 8:77627657da80 160 pos++;
david_s95 8:77627657da80 161 if(pos>=468){
david_s95 8:77627657da80 162 // Direction=!Direction;
david_s95 8:77627657da80 163 pos=pos%468;
david_s95 8:77627657da80 164 // testpin=!testpin;
david_s95 8:77627657da80 165 }
david_s95 8:77627657da80 166 if(qB) DIR = 0;
david_s95 8:77627657da80 167 else DIR = 1;
david_s95 8:77627657da80 168 clk=DIR;
david_s95 8:77627657da80 169 //CLOCKWISE: A rises before B -> On A edge, B low -> DIR = 1
david_s95 8:77627657da80 170 //ANTICLOCKWISE: B rises before A -> On A edge, B high-> DIR = 0
david_s95 8:77627657da80 171 }
david_s95 8:77627657da80 172
david_s95 8:77627657da80 173 void edgeIncr(){
david_s95 8:77627657da80 174 pos++;
david_s95 8:77627657da80 175 if(pos>=468){
david_s95 8:77627657da80 176 // Direction=!Direction;
david_s95 8:77627657da80 177 pos=pos%468;
david_s95 8:77627657da80 178 // testpin=!testpin;
david_s95 8:77627657da80 179 }
david_s95 8:77627657da80 180 }
david_s95 8:77627657da80 181
mengkiang 4:f8a9ce214db9 182 //Main function
david_s95 5:e5313b695302 183 int main()
david_s95 5:e5313b695302 184 {
estott 0:de4320f74764 185 pc.printf("Hello\n\r");
david_s95 7:5932ed0bad6d 186
estott 0:de4320f74764 187 //Run the motor synchronisation
estott 2:4e88faab6988 188 orState = motorHome();
david_s95 6:4edbe75736d9 189 //orState is subtracted from future rotor state inputs to align rotor and motor states
david_s95 6:4edbe75736d9 190
estott 2:4e88faab6988 191 pc.printf("Rotor origin: %x\n\r",orState);
david_s95 5:e5313b695302 192
david_s95 6:4edbe75736d9 193 char command[ARRAYSIZE];
david_s95 6:4edbe75736d9 194 int index=0;
david_s95 6:4edbe75736d9 195 int units = 0, tens = 0, decimals = 0;
david_s95 6:4edbe75736d9 196 char ch;
david_s95 8:77627657da80 197 // testpin=0;
david_s95 7:5932ed0bad6d 198
david_s95 7:5932ed0bad6d 199 speedTimer.start();
david_s95 7:5932ed0bad6d 200 I1.rise(&rps);
david_s95 8:77627657da80 201
david_s95 8:77627657da80 202 qA.rise(&edgeRiseA);
david_s95 8:77627657da80 203 qB.rise(&edgeIncr);
david_s95 8:77627657da80 204 qA.fall(&edgeIncr);
david_s95 8:77627657da80 205 qB.fall(&edgeIncr);
david_s95 7:5932ed0bad6d 206
david_s95 5:e5313b695302 207 while(1) {
david_s95 6:4edbe75736d9 208 //Toggle LED so we know something's happening
david_s95 8:77627657da80 209 // clk = !clk;
david_s95 7:5932ed0bad6d 210
david_s95 6:4edbe75736d9 211 //If there's a character to read from the serial port
david_s95 6:4edbe75736d9 212 if (pc.readable()) {
david_s95 7:5932ed0bad6d 213
david_s95 6:4edbe75736d9 214 //Clear index counter and control variables
david_s95 6:4edbe75736d9 215 index = 0;
david_s95 7:5932ed0bad6d 216 // revsec = spinWait = 0;
david_s95 7:5932ed0bad6d 217
david_s95 6:4edbe75736d9 218 //Read each value from the serial port until Enter key is pressed
david_s95 6:4edbe75736d9 219 do {
david_s95 6:4edbe75736d9 220 //Read character
david_s95 6:4edbe75736d9 221 ch = pc.getc();
david_s95 6:4edbe75736d9 222 //Print character to serial for visual feedback
david_s95 6:4edbe75736d9 223 pc.putc(ch);
david_s95 6:4edbe75736d9 224 //Add character to input array
david_s95 6:4edbe75736d9 225 command[index++]=ch; // put it into the value array and increment the index
david_s95 7:5932ed0bad6d 226 //d10 and d13 used for detecting Enter key on Windows/Unix/Mac
david_s95 6:4edbe75736d9 227 } while(ch != 10 && ch != 13);
david_s95 7:5932ed0bad6d 228
david_s95 6:4edbe75736d9 229 //Start new line on terminal for printing data
david_s95 6:4edbe75736d9 230 pc.putc('\n');
david_s95 6:4edbe75736d9 231 pc.putc('\r');
david_s95 7:5932ed0bad6d 232
david_s95 6:4edbe75736d9 233 //Analyse the input string
david_s95 6:4edbe75736d9 234 switch (command[0]) {
david_s95 7:5932ed0bad6d 235 //If a V was typed...
david_s95 6:4edbe75736d9 236 case 'V':
david_s95 7:5932ed0bad6d 237 units = 0, tens = 0, decimals = 0;
david_s95 7:5932ed0bad6d 238 //For each character received, subtract ASCII 0 from ASCII
david_s95 7:5932ed0bad6d 239 //representation to obtain the integer value of the number
david_s95 7:5932ed0bad6d 240
david_s95 6:4edbe75736d9 241 //If decimal point is in the second character (eg, V.1)
david_s95 6:4edbe75736d9 242 if(command[1]=='.') {
david_s95 6:4edbe75736d9 243 //Extract decimal rev/s
david_s95 6:4edbe75736d9 244 decimals = command[2] - '0';
david_s95 7:5932ed0bad6d 245
david_s95 7:5932ed0bad6d 246 //If decimal point is in the third character (eg, V0.1)
david_s95 6:4edbe75736d9 247 } else if(command[2]=='.') {
david_s95 6:4edbe75736d9 248 units = command[1] - '0';
david_s95 6:4edbe75736d9 249 decimals = command[3] - '0';
david_s95 7:5932ed0bad6d 250
david_s95 7:5932ed0bad6d 251 //If decimal point is in the fourth character (eg, V10.1)
david_s95 6:4edbe75736d9 252 } else if(command[3]=='.') {
david_s95 6:4edbe75736d9 253 tens = command[1] - '0';
david_s95 6:4edbe75736d9 254 units = command[2] - '0';
david_s95 6:4edbe75736d9 255 decimals = command[4] - '0';
david_s95 6:4edbe75736d9 256 }
david_s95 7:5932ed0bad6d 257
david_s95 6:4edbe75736d9 258 //Calculate the number of revolutions per second required
david_s95 6:4edbe75736d9 259 revsec = float(tens)*10 + float(units) + float(decimals)/10;
david_s95 6:4edbe75736d9 260 //Calculate the required wait period
david_s95 6:4edbe75736d9 261 spinWait = (1/revsec)/6;
david_s95 7:5932ed0bad6d 262
david_s95 6:4edbe75736d9 263 //Print values for verification
david_s95 7:5932ed0bad6d 264 pc.printf("Rev/S: %2.4f, Wait: %2.4f\n\r", revsec, spinWait);
david_s95 7:5932ed0bad6d 265
david_s95 6:4edbe75736d9 266 //Run the function to start rotating at a fixed speed
david_s95 6:4edbe75736d9 267 fixedSpeed();
david_s95 6:4edbe75736d9 268 break;
david_s95 7:5932ed0bad6d 269 //If anything unexpected was received
david_s95 7:5932ed0bad6d 270 case 's':
david_s95 7:5932ed0bad6d 271 pc.printf("Revs / sec: %2.2f\r", revs);
david_s95 7:5932ed0bad6d 272 break;
david_s95 8:77627657da80 273 case 't':
david_s95 8:77627657da80 274 pc.printf("%d\n\r", pos);
david_s95 8:77627657da80 275 break;
david_s95 6:4edbe75736d9 276 default:
david_s95 6:4edbe75736d9 277 //Set speed variables to zero to stop motor spinning
david_s95 6:4edbe75736d9 278 revsec=0;
david_s95 6:4edbe75736d9 279 //Print error message
david_s95 6:4edbe75736d9 280 pc.printf("Error in received data\n\r");
david_s95 6:4edbe75736d9 281 break;
david_s95 6:4edbe75736d9 282 }
david_s95 6:4edbe75736d9 283 }
david_s95 7:5932ed0bad6d 284 // printSpeed.attach(&speedo, 1.0);
david_s95 7:5932ed0bad6d 285 // pc.printf("Revs / sec: %2.2f\r", revs);
estott 2:4e88faab6988 286 }
david_s95 5:e5313b695302 287
estott 0:de4320f74764 288 }