David Salmon
/
ES_CW2_Starter_MDMA
ES2017 coursework 2
Fork of ES_CW2_Starter by
main.cpp@7:5932ed0bad6d, 2017-03-02 (annotated)
- Committer:
- david_s95
- Date:
- Thu Mar 02 19:05:49 2017 +0000
- Revision:
- 7:5932ed0bad6d
- Parent:
- 6:4edbe75736d9
- Child:
- 8:77627657da80
Open loop control of motor speed working.
Who changed what in which revision?
User | Revision | Line number | New 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 | |
estott | 0:de4320f74764 | 56 | //Motor Drive outputs |
estott | 0:de4320f74764 | 57 | DigitalOut L1L(L1Lpin); |
estott | 0:de4320f74764 | 58 | DigitalOut L1H(L1Hpin); |
estott | 0:de4320f74764 | 59 | DigitalOut L2L(L2Lpin); |
estott | 0:de4320f74764 | 60 | DigitalOut L2H(L2Hpin); |
estott | 0:de4320f74764 | 61 | DigitalOut L3L(L3Lpin); |
estott | 0:de4320f74764 | 62 | DigitalOut L3H(L3Hpin); |
david_s95 | 5:e5313b695302 | 63 | DigitalOut clk(LED1); |
david_s95 | 5:e5313b695302 | 64 | |
david_s95 | 5:e5313b695302 | 65 | //Timeout function for rotating at set speed |
david_s95 | 5:e5313b695302 | 66 | Timeout spinTimer; |
david_s95 | 5:e5313b695302 | 67 | float spinWait = 10; |
david_s95 | 5:e5313b695302 | 68 | float revsec = 0; |
david_s95 | 5:e5313b695302 | 69 | |
david_s95 | 7:5932ed0bad6d | 70 | //Timer used for calculating speed |
david_s95 | 7:5932ed0bad6d | 71 | Timer speedTimer; |
david_s95 | 7:5932ed0bad6d | 72 | float revs = 0, revtimer = 0; |
david_s95 | 7:5932ed0bad6d | 73 | Ticker printSpeed; |
david_s95 | 7:5932ed0bad6d | 74 | |
david_s95 | 5:e5313b695302 | 75 | Serial pc(SERIAL_TX, SERIAL_RX); |
david_s95 | 5:e5313b695302 | 76 | |
david_s95 | 5:e5313b695302 | 77 | int8_t orState = 0; //Rotor offset at motor state 0 |
david_s95 | 5:e5313b695302 | 78 | int8_t intState = 0; |
david_s95 | 5:e5313b695302 | 79 | int8_t intStateOld = 0; |
david_s95 | 5:e5313b695302 | 80 | |
david_s95 | 5:e5313b695302 | 81 | int i=0; |
david_s95 | 5:e5313b695302 | 82 | |
estott | 0:de4320f74764 | 83 | //Set a given drive state |
david_s95 | 5:e5313b695302 | 84 | void motorOut(int8_t driveState) |
david_s95 | 5:e5313b695302 | 85 | { |
david_s95 | 5:e5313b695302 | 86 | |
estott | 2:4e88faab6988 | 87 | //Lookup the output byte from the drive state. |
estott | 2:4e88faab6988 | 88 | int8_t driveOut = driveTable[driveState & 0x07]; |
david_s95 | 5:e5313b695302 | 89 | |
estott | 2:4e88faab6988 | 90 | //Turn off first |
estott | 2:4e88faab6988 | 91 | if (~driveOut & 0x01) L1L = 0; |
estott | 2:4e88faab6988 | 92 | if (~driveOut & 0x02) L1H = 1; |
estott | 2:4e88faab6988 | 93 | if (~driveOut & 0x04) L2L = 0; |
estott | 2:4e88faab6988 | 94 | if (~driveOut & 0x08) L2H = 1; |
estott | 2:4e88faab6988 | 95 | if (~driveOut & 0x10) L3L = 0; |
estott | 2:4e88faab6988 | 96 | if (~driveOut & 0x20) L3H = 1; |
david_s95 | 5:e5313b695302 | 97 | |
estott | 2:4e88faab6988 | 98 | //Then turn on |
estott | 2:4e88faab6988 | 99 | if (driveOut & 0x01) L1L = 1; |
estott | 2:4e88faab6988 | 100 | if (driveOut & 0x02) L1H = 0; |
estott | 2:4e88faab6988 | 101 | if (driveOut & 0x04) L2L = 1; |
estott | 2:4e88faab6988 | 102 | if (driveOut & 0x08) L2H = 0; |
estott | 2:4e88faab6988 | 103 | if (driveOut & 0x10) L3L = 1; |
estott | 2:4e88faab6988 | 104 | if (driveOut & 0x20) L3H = 0; |
david_s95 | 5:e5313b695302 | 105 | } |
david_s95 | 5:e5313b695302 | 106 | |
david_s95 | 5:e5313b695302 | 107 | //Convert photointerrupter inputs to a rotor state |
david_s95 | 5:e5313b695302 | 108 | inline int8_t readRotorState() |
david_s95 | 5:e5313b695302 | 109 | { |
estott | 2:4e88faab6988 | 110 | return stateMap[I1 + 2*I2 + 4*I3]; |
david_s95 | 5:e5313b695302 | 111 | } |
estott | 0:de4320f74764 | 112 | |
david_s95 | 5:e5313b695302 | 113 | //Basic synchronisation routine |
david_s95 | 5:e5313b695302 | 114 | int8_t motorHome() |
david_s95 | 5:e5313b695302 | 115 | { |
estott | 0:de4320f74764 | 116 | //Put the motor in drive state 0 and wait for it to stabilise |
estott | 0:de4320f74764 | 117 | motorOut(0); |
estott | 0:de4320f74764 | 118 | wait(1.0); |
david_s95 | 5:e5313b695302 | 119 | |
estott | 0:de4320f74764 | 120 | //Get the rotor state |
estott | 2:4e88faab6988 | 121 | return readRotorState(); |
estott | 0:de4320f74764 | 122 | } |
david_s95 | 5:e5313b695302 | 123 | |
david_s95 | 5:e5313b695302 | 124 | void fixedSpeed() |
david_s95 | 5:e5313b695302 | 125 | { |
david_s95 | 6:4edbe75736d9 | 126 | //Read current motor state |
david_s95 | 5:e5313b695302 | 127 | intState = readRotorState(); |
david_s95 | 6:4edbe75736d9 | 128 | //Increment state machine to next state |
david_s95 | 5:e5313b695302 | 129 | motorOut((intState-orState+lead+6)%6); |
david_s95 | 6:4edbe75736d9 | 130 | //If spinning is required, attach the necessary wait to the |
david_s95 | 6:4edbe75736d9 | 131 | //timeout interrupt to call this function again and |
david_s95 | 6:4edbe75736d9 | 132 | //keep the motor spinning at the right speed |
david_s95 | 5:e5313b695302 | 133 | if(revsec) spinTimer.attach(&fixedSpeed, spinWait); |
david_s95 | 5:e5313b695302 | 134 | } |
david_s95 | 5:e5313b695302 | 135 | |
david_s95 | 7:5932ed0bad6d | 136 | void rps() |
david_s95 | 7:5932ed0bad6d | 137 | { |
david_s95 | 7:5932ed0bad6d | 138 | // pc.printf("Tick\r\n"); |
david_s95 | 7:5932ed0bad6d | 139 | speedTimer.stop(); |
david_s95 | 7:5932ed0bad6d | 140 | revtimer = speedTimer.read_ms(); |
david_s95 | 7:5932ed0bad6d | 141 | revs = 1000/(revtimer); |
david_s95 | 7:5932ed0bad6d | 142 | // pc.printf("Revs / sec: %2.2f\r", revs); |
david_s95 | 7:5932ed0bad6d | 143 | speedTimer.start(); |
david_s95 | 7:5932ed0bad6d | 144 | } |
david_s95 | 7:5932ed0bad6d | 145 | |
david_s95 | 7:5932ed0bad6d | 146 | void speedo() |
david_s95 | 7:5932ed0bad6d | 147 | { |
david_s95 | 7:5932ed0bad6d | 148 | pc.printf("Revs / sec: %2.4f\r", revs); |
david_s95 | 7:5932ed0bad6d | 149 | printSpeed.attach(&speedo, 1.0); |
david_s95 | 7:5932ed0bad6d | 150 | } |
david_s95 | 7:5932ed0bad6d | 151 | |
mengkiang | 4:f8a9ce214db9 | 152 | //Main function |
david_s95 | 5:e5313b695302 | 153 | int main() |
david_s95 | 5:e5313b695302 | 154 | { |
estott | 0:de4320f74764 | 155 | pc.printf("Hello\n\r"); |
david_s95 | 7:5932ed0bad6d | 156 | |
estott | 0:de4320f74764 | 157 | //Run the motor synchronisation |
estott | 2:4e88faab6988 | 158 | orState = motorHome(); |
david_s95 | 6:4edbe75736d9 | 159 | //orState is subtracted from future rotor state inputs to align rotor and motor states |
david_s95 | 6:4edbe75736d9 | 160 | |
estott | 2:4e88faab6988 | 161 | pc.printf("Rotor origin: %x\n\r",orState); |
david_s95 | 5:e5313b695302 | 162 | |
david_s95 | 6:4edbe75736d9 | 163 | char command[ARRAYSIZE]; |
david_s95 | 6:4edbe75736d9 | 164 | int index=0; |
david_s95 | 6:4edbe75736d9 | 165 | int units = 0, tens = 0, decimals = 0; |
david_s95 | 6:4edbe75736d9 | 166 | char ch; |
david_s95 | 7:5932ed0bad6d | 167 | |
david_s95 | 7:5932ed0bad6d | 168 | speedTimer.start(); |
david_s95 | 7:5932ed0bad6d | 169 | I1.rise(&rps); |
david_s95 | 7:5932ed0bad6d | 170 | |
david_s95 | 5:e5313b695302 | 171 | while(1) { |
david_s95 | 6:4edbe75736d9 | 172 | //Toggle LED so we know something's happening |
david_s95 | 5:e5313b695302 | 173 | clk = !clk; |
david_s95 | 7:5932ed0bad6d | 174 | |
david_s95 | 6:4edbe75736d9 | 175 | //If there's a character to read from the serial port |
david_s95 | 6:4edbe75736d9 | 176 | if (pc.readable()) { |
david_s95 | 7:5932ed0bad6d | 177 | |
david_s95 | 6:4edbe75736d9 | 178 | //Clear index counter and control variables |
david_s95 | 6:4edbe75736d9 | 179 | index = 0; |
david_s95 | 7:5932ed0bad6d | 180 | // revsec = spinWait = 0; |
david_s95 | 7:5932ed0bad6d | 181 | |
david_s95 | 6:4edbe75736d9 | 182 | //Read each value from the serial port until Enter key is pressed |
david_s95 | 6:4edbe75736d9 | 183 | do { |
david_s95 | 6:4edbe75736d9 | 184 | //Read character |
david_s95 | 6:4edbe75736d9 | 185 | ch = pc.getc(); |
david_s95 | 6:4edbe75736d9 | 186 | //Print character to serial for visual feedback |
david_s95 | 6:4edbe75736d9 | 187 | pc.putc(ch); |
david_s95 | 6:4edbe75736d9 | 188 | //Add character to input array |
david_s95 | 6:4edbe75736d9 | 189 | command[index++]=ch; // put it into the value array and increment the index |
david_s95 | 7:5932ed0bad6d | 190 | //d10 and d13 used for detecting Enter key on Windows/Unix/Mac |
david_s95 | 6:4edbe75736d9 | 191 | } while(ch != 10 && ch != 13); |
david_s95 | 7:5932ed0bad6d | 192 | |
david_s95 | 6:4edbe75736d9 | 193 | //Start new line on terminal for printing data |
david_s95 | 6:4edbe75736d9 | 194 | pc.putc('\n'); |
david_s95 | 6:4edbe75736d9 | 195 | pc.putc('\r'); |
david_s95 | 7:5932ed0bad6d | 196 | |
david_s95 | 6:4edbe75736d9 | 197 | //Analyse the input string |
david_s95 | 6:4edbe75736d9 | 198 | switch (command[0]) { |
david_s95 | 7:5932ed0bad6d | 199 | //If a V was typed... |
david_s95 | 6:4edbe75736d9 | 200 | case 'V': |
david_s95 | 7:5932ed0bad6d | 201 | units = 0, tens = 0, decimals = 0; |
david_s95 | 7:5932ed0bad6d | 202 | //For each character received, subtract ASCII 0 from ASCII |
david_s95 | 7:5932ed0bad6d | 203 | //representation to obtain the integer value of the number |
david_s95 | 7:5932ed0bad6d | 204 | |
david_s95 | 6:4edbe75736d9 | 205 | //If decimal point is in the second character (eg, V.1) |
david_s95 | 6:4edbe75736d9 | 206 | if(command[1]=='.') { |
david_s95 | 6:4edbe75736d9 | 207 | //Extract decimal rev/s |
david_s95 | 6:4edbe75736d9 | 208 | decimals = command[2] - '0'; |
david_s95 | 7:5932ed0bad6d | 209 | |
david_s95 | 7:5932ed0bad6d | 210 | //If decimal point is in the third character (eg, V0.1) |
david_s95 | 6:4edbe75736d9 | 211 | } else if(command[2]=='.') { |
david_s95 | 6:4edbe75736d9 | 212 | units = command[1] - '0'; |
david_s95 | 6:4edbe75736d9 | 213 | decimals = command[3] - '0'; |
david_s95 | 7:5932ed0bad6d | 214 | |
david_s95 | 7:5932ed0bad6d | 215 | //If decimal point is in the fourth character (eg, V10.1) |
david_s95 | 6:4edbe75736d9 | 216 | } else if(command[3]=='.') { |
david_s95 | 6:4edbe75736d9 | 217 | tens = command[1] - '0'; |
david_s95 | 6:4edbe75736d9 | 218 | units = command[2] - '0'; |
david_s95 | 6:4edbe75736d9 | 219 | decimals = command[4] - '0'; |
david_s95 | 6:4edbe75736d9 | 220 | } |
david_s95 | 7:5932ed0bad6d | 221 | |
david_s95 | 6:4edbe75736d9 | 222 | //Calculate the number of revolutions per second required |
david_s95 | 6:4edbe75736d9 | 223 | revsec = float(tens)*10 + float(units) + float(decimals)/10; |
david_s95 | 6:4edbe75736d9 | 224 | //Calculate the required wait period |
david_s95 | 6:4edbe75736d9 | 225 | spinWait = (1/revsec)/6; |
david_s95 | 7:5932ed0bad6d | 226 | |
david_s95 | 6:4edbe75736d9 | 227 | //Print values for verification |
david_s95 | 7:5932ed0bad6d | 228 | pc.printf("Rev/S: %2.4f, Wait: %2.4f\n\r", revsec, spinWait); |
david_s95 | 7:5932ed0bad6d | 229 | |
david_s95 | 6:4edbe75736d9 | 230 | //Run the function to start rotating at a fixed speed |
david_s95 | 6:4edbe75736d9 | 231 | fixedSpeed(); |
david_s95 | 6:4edbe75736d9 | 232 | break; |
david_s95 | 7:5932ed0bad6d | 233 | //If anything unexpected was received |
david_s95 | 7:5932ed0bad6d | 234 | case 's': |
david_s95 | 7:5932ed0bad6d | 235 | pc.printf("Revs / sec: %2.2f\r", revs); |
david_s95 | 7:5932ed0bad6d | 236 | break; |
david_s95 | 6:4edbe75736d9 | 237 | default: |
david_s95 | 6:4edbe75736d9 | 238 | //Set speed variables to zero to stop motor spinning |
david_s95 | 6:4edbe75736d9 | 239 | revsec=0; |
david_s95 | 6:4edbe75736d9 | 240 | //Print error message |
david_s95 | 6:4edbe75736d9 | 241 | pc.printf("Error in received data\n\r"); |
david_s95 | 6:4edbe75736d9 | 242 | break; |
david_s95 | 6:4edbe75736d9 | 243 | } |
david_s95 | 6:4edbe75736d9 | 244 | } |
david_s95 | 7:5932ed0bad6d | 245 | // printSpeed.attach(&speedo, 1.0); |
david_s95 | 7:5932ed0bad6d | 246 | // pc.printf("Revs / sec: %2.2f\r", revs); |
estott | 2:4e88faab6988 | 247 | } |
david_s95 | 5:e5313b695302 | 248 | |
estott | 0:de4320f74764 | 249 | } |