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