Edward Stott
/
ES_CW2_Starter
Starter code for brushless motor controller
main.cpp@3:569b35e2a602, 2018-03-01 (annotated)
- Committer:
- estott
- Date:
- Thu Mar 01 09:41:46 2018 +0000
- Revision:
- 3:569b35e2a602
- Parent:
- 2:4e88faab6988
- Child:
- 4:1cb32cb438ee
- Child:
- 5:08f338b5e4d9
Changed library version
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 | |
estott | 0:de4320f74764 | 3 | //Photointerrupter input pins |
estott | 0:de4320f74764 | 4 | #define I1pin D2 |
estott | 2:4e88faab6988 | 5 | #define I2pin D11 |
estott | 2:4e88faab6988 | 6 | #define I3pin D12 |
estott | 2:4e88faab6988 | 7 | |
estott | 2:4e88faab6988 | 8 | //Incremental encoder input pins |
estott | 2:4e88faab6988 | 9 | #define CHA D7 |
estott | 2:4e88faab6988 | 10 | #define CHB D8 |
estott | 0:de4320f74764 | 11 | |
estott | 0:de4320f74764 | 12 | //Motor Drive output pins //Mask in output byte |
estott | 2:4e88faab6988 | 13 | #define L1Lpin D4 //0x01 |
estott | 2:4e88faab6988 | 14 | #define L1Hpin D5 //0x02 |
estott | 2:4e88faab6988 | 15 | #define L2Lpin D3 //0x04 |
estott | 2:4e88faab6988 | 16 | #define L2Hpin D6 //0x08 |
estott | 2:4e88faab6988 | 17 | #define L3Lpin D9 //0x10 |
estott | 0:de4320f74764 | 18 | #define L3Hpin D10 //0x20 |
estott | 0:de4320f74764 | 19 | |
estott | 0:de4320f74764 | 20 | //Mapping from sequential drive states to motor phase outputs |
estott | 0:de4320f74764 | 21 | /* |
estott | 0:de4320f74764 | 22 | State L1 L2 L3 |
estott | 0:de4320f74764 | 23 | 0 H - L |
estott | 0:de4320f74764 | 24 | 1 - H L |
estott | 0:de4320f74764 | 25 | 2 L H - |
estott | 0:de4320f74764 | 26 | 3 L - H |
estott | 0:de4320f74764 | 27 | 4 - L H |
estott | 0:de4320f74764 | 28 | 5 H L - |
estott | 0:de4320f74764 | 29 | 6 - - - |
estott | 0:de4320f74764 | 30 | 7 - - - |
estott | 0:de4320f74764 | 31 | */ |
estott | 0:de4320f74764 | 32 | //Drive state to output table |
estott | 0:de4320f74764 | 33 | const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00}; |
estott | 2:4e88faab6988 | 34 | |
estott | 0:de4320f74764 | 35 | //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid |
estott | 2:4e88faab6988 | 36 | const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07}; |
estott | 2:4e88faab6988 | 37 | //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 | 38 | |
estott | 2:4e88faab6988 | 39 | //Phase lead to make motor spin |
estott | 3:569b35e2a602 | 40 | const int8_t lead = 2; //2 for forwards, -2 for backwards |
estott | 0:de4320f74764 | 41 | |
estott | 0:de4320f74764 | 42 | //Status LED |
estott | 0:de4320f74764 | 43 | DigitalOut led1(LED1); |
estott | 0:de4320f74764 | 44 | |
estott | 0:de4320f74764 | 45 | //Photointerrupter inputs |
estott | 2:4e88faab6988 | 46 | DigitalIn I1(I1pin); |
estott | 2:4e88faab6988 | 47 | DigitalIn I2(I2pin); |
estott | 2:4e88faab6988 | 48 | DigitalIn I3(I3pin); |
estott | 0:de4320f74764 | 49 | |
estott | 0:de4320f74764 | 50 | //Motor Drive outputs |
estott | 0:de4320f74764 | 51 | DigitalOut L1L(L1Lpin); |
estott | 0:de4320f74764 | 52 | DigitalOut L1H(L1Hpin); |
estott | 0:de4320f74764 | 53 | DigitalOut L2L(L2Lpin); |
estott | 0:de4320f74764 | 54 | DigitalOut L2H(L2Hpin); |
estott | 0:de4320f74764 | 55 | DigitalOut L3L(L3Lpin); |
estott | 0:de4320f74764 | 56 | DigitalOut L3H(L3Hpin); |
estott | 0:de4320f74764 | 57 | |
estott | 0:de4320f74764 | 58 | //Set a given drive state |
estott | 0:de4320f74764 | 59 | void motorOut(int8_t driveState){ |
estott | 0:de4320f74764 | 60 | |
estott | 2:4e88faab6988 | 61 | //Lookup the output byte from the drive state. |
estott | 2:4e88faab6988 | 62 | int8_t driveOut = driveTable[driveState & 0x07]; |
estott | 2:4e88faab6988 | 63 | |
estott | 2:4e88faab6988 | 64 | //Turn off first |
estott | 2:4e88faab6988 | 65 | if (~driveOut & 0x01) L1L = 0; |
estott | 2:4e88faab6988 | 66 | if (~driveOut & 0x02) L1H = 1; |
estott | 2:4e88faab6988 | 67 | if (~driveOut & 0x04) L2L = 0; |
estott | 2:4e88faab6988 | 68 | if (~driveOut & 0x08) L2H = 1; |
estott | 2:4e88faab6988 | 69 | if (~driveOut & 0x10) L3L = 0; |
estott | 2:4e88faab6988 | 70 | if (~driveOut & 0x20) L3H = 1; |
estott | 2:4e88faab6988 | 71 | |
estott | 2:4e88faab6988 | 72 | //Then turn on |
estott | 2:4e88faab6988 | 73 | if (driveOut & 0x01) L1L = 1; |
estott | 2:4e88faab6988 | 74 | if (driveOut & 0x02) L1H = 0; |
estott | 2:4e88faab6988 | 75 | if (driveOut & 0x04) L2L = 1; |
estott | 2:4e88faab6988 | 76 | if (driveOut & 0x08) L2H = 0; |
estott | 2:4e88faab6988 | 77 | if (driveOut & 0x10) L3L = 1; |
estott | 2:4e88faab6988 | 78 | if (driveOut & 0x20) L3H = 0; |
estott | 0:de4320f74764 | 79 | } |
estott | 0:de4320f74764 | 80 | |
estott | 2:4e88faab6988 | 81 | //Convert photointerrupter inputs to a rotor state |
estott | 0:de4320f74764 | 82 | inline int8_t readRotorState(){ |
estott | 2:4e88faab6988 | 83 | return stateMap[I1 + 2*I2 + 4*I3]; |
estott | 0:de4320f74764 | 84 | } |
estott | 0:de4320f74764 | 85 | |
estott | 0:de4320f74764 | 86 | //Basic synchronisation routine |
estott | 2:4e88faab6988 | 87 | int8_t motorHome() { |
estott | 0:de4320f74764 | 88 | //Put the motor in drive state 0 and wait for it to stabilise |
estott | 0:de4320f74764 | 89 | motorOut(0); |
estott | 3:569b35e2a602 | 90 | wait(2.0); |
estott | 0:de4320f74764 | 91 | |
estott | 0:de4320f74764 | 92 | //Get the rotor state |
estott | 2:4e88faab6988 | 93 | return readRotorState(); |
estott | 0:de4320f74764 | 94 | } |
estott | 0:de4320f74764 | 95 | |
estott | 0:de4320f74764 | 96 | //Main |
estott | 0:de4320f74764 | 97 | int main() { |
estott | 2:4e88faab6988 | 98 | int8_t orState = 0; //Rotot offset at motor state 0 |
estott | 3:569b35e2a602 | 99 | int8_t intState = 0; |
estott | 3:569b35e2a602 | 100 | int8_t intStateOld = 0; |
estott | 2:4e88faab6988 | 101 | |
estott | 0:de4320f74764 | 102 | //Initialise the serial port |
estott | 0:de4320f74764 | 103 | Serial pc(SERIAL_TX, SERIAL_RX); |
estott | 0:de4320f74764 | 104 | pc.printf("Hello\n\r"); |
estott | 0:de4320f74764 | 105 | |
estott | 0:de4320f74764 | 106 | //Run the motor synchronisation |
estott | 2:4e88faab6988 | 107 | orState = motorHome(); |
estott | 2:4e88faab6988 | 108 | pc.printf("Rotor origin: %x\n\r",orState); |
estott | 2:4e88faab6988 | 109 | //orState is subtracted from future rotor state inputs to align rotor and motor states |
estott | 0:de4320f74764 | 110 | |
estott | 0:de4320f74764 | 111 | //Poll the rotor state and set the motor outputs accordingly to spin the motor |
estott | 1:184cb0870c04 | 112 | while (1) { |
estott | 2:4e88faab6988 | 113 | intState = readRotorState(); |
estott | 2:4e88faab6988 | 114 | if (intState != intStateOld) { |
estott | 2:4e88faab6988 | 115 | intStateOld = intState; |
estott | 2:4e88faab6988 | 116 | motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive |
estott | 0:de4320f74764 | 117 | } |
estott | 2:4e88faab6988 | 118 | } |
estott | 0:de4320f74764 | 119 | } |
estott | 0:de4320f74764 | 120 |