An embedded device

Dependencies:   Crypto

Committer:
tanyuzhuo
Date:
Thu Feb 28 11:52:05 2019 +0000
Revision:
12:899cd6bf9844
Parent:
10:a4b5723b6c9d
Child:
13:c51d828531d5
interrupt function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
estott 0:de4320f74764 1 #include "mbed.h"
estott 0:de4320f74764 2
estott 0:de4320f74764 3 //Photointerrupter input pins
estott 10:a4b5723b6c9d 4 #define I1pin D3
estott 10:a4b5723b6c9d 5 #define I2pin D6
estott 10:a4b5723b6c9d 6 #define I3pin D5
estott 2:4e88faab6988 7
estott 2:4e88faab6988 8 //Incremental encoder input pins
estott 10:a4b5723b6c9d 9 #define CHApin D12
estott 10:a4b5723b6c9d 10 #define CHBpin D11
estott 0:de4320f74764 11
estott 0:de4320f74764 12 //Motor Drive output pins //Mask in output byte
estott 10:a4b5723b6c9d 13 #define L1Lpin D1 //0x01
estott 10:a4b5723b6c9d 14 #define L1Hpin A3 //0x02
estott 10:a4b5723b6c9d 15 #define L2Lpin D0 //0x04
estott 10:a4b5723b6c9d 16 #define L2Hpin A6 //0x08
estott 10:a4b5723b6c9d 17 #define L3Lpin D10 //0x10
estott 10:a4b5723b6c9d 18 #define L3Hpin D2 //0x20
estott 10:a4b5723b6c9d 19
estott 10:a4b5723b6c9d 20 #define PWMpin D9
estott 5:08f338b5e4d9 21
estott 5:08f338b5e4d9 22 //Motor current sense
estott 5:08f338b5e4d9 23 #define MCSPpin A1
estott 5:08f338b5e4d9 24 #define MCSNpin A0
estott 0:de4320f74764 25
tanyuzhuo 12:899cd6bf9844 26 int8_t motorHome();
tanyuzhuo 12:899cd6bf9844 27 inline int8_t readRotorState();
tanyuzhuo 12:899cd6bf9844 28 void motorOut(int8_t driveState);
estott 0:de4320f74764 29 //Mapping from sequential drive states to motor phase outputs
estott 0:de4320f74764 30 /*
estott 0:de4320f74764 31 State L1 L2 L3
estott 0:de4320f74764 32 0 H - L
estott 0:de4320f74764 33 1 - H L
estott 0:de4320f74764 34 2 L H -
estott 0:de4320f74764 35 3 L - H
estott 0:de4320f74764 36 4 - L H
estott 0:de4320f74764 37 5 H L -
estott 0:de4320f74764 38 6 - - -
estott 0:de4320f74764 39 7 - - -
estott 0:de4320f74764 40 */
estott 0:de4320f74764 41 //Drive state to output table
estott 0:de4320f74764 42 const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00};
estott 2:4e88faab6988 43
estott 0:de4320f74764 44 //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid
estott 2:4e88faab6988 45 const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07};
estott 2:4e88faab6988 46 //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 47
estott 2:4e88faab6988 48 //Phase lead to make motor spin
estott 3:569b35e2a602 49 const int8_t lead = 2; //2 for forwards, -2 for backwards
estott 0:de4320f74764 50
estott 0:de4320f74764 51 //Status LED
estott 0:de4320f74764 52 DigitalOut led1(LED1);
estott 0:de4320f74764 53
estott 0:de4320f74764 54 //Photointerrupter inputs
tanyuzhuo 12:899cd6bf9844 55 InterruptIn I1(I1pin);
tanyuzhuo 12:899cd6bf9844 56 InterruptIn I2(I2pin);
tanyuzhuo 12:899cd6bf9844 57 InterruptIn I3(I3pin);
tanyuzhuo 12:899cd6bf9844 58
tanyuzhuo 12:899cd6bf9844 59
estott 0:de4320f74764 60
estott 0:de4320f74764 61 //Motor Drive outputs
estott 0:de4320f74764 62 DigitalOut L1L(L1Lpin);
estott 0:de4320f74764 63 DigitalOut L1H(L1Hpin);
estott 0:de4320f74764 64 DigitalOut L2L(L2Lpin);
estott 0:de4320f74764 65 DigitalOut L2H(L2Hpin);
estott 0:de4320f74764 66 DigitalOut L3L(L3Lpin);
estott 0:de4320f74764 67 DigitalOut L3H(L3Hpin);
estott 0:de4320f74764 68
tanyuzhuo 12:899cd6bf9844 69 int8_t orState = 0; //Rotot offset at motor state 0
tanyuzhuo 12:899cd6bf9844 70 int8_t intState = 0;
tanyuzhuo 12:899cd6bf9844 71 int8_t intStateOld = 0;
tanyuzhuo 12:899cd6bf9844 72
tanyuzhuo 12:899cd6bf9844 73 //interrupt function
tanyuzhuo 12:899cd6bf9844 74 void push(){
tanyuzhuo 12:899cd6bf9844 75 //Poll the rotor state and set the motor outputs accordingly to spin the motor
tanyuzhuo 12:899cd6bf9844 76 intState = readRotorState();
tanyuzhuo 12:899cd6bf9844 77 if (intState != intStateOld) {
tanyuzhuo 12:899cd6bf9844 78 intStateOld = intState;
tanyuzhuo 12:899cd6bf9844 79 motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive
tanyuzhuo 12:899cd6bf9844 80 //pc.printf("%d\n\r",intState);
tanyuzhuo 12:899cd6bf9844 81 }
tanyuzhuo 12:899cd6bf9844 82 }
tanyuzhuo 12:899cd6bf9844 83
estott 0:de4320f74764 84 //Set a given drive state
estott 0:de4320f74764 85 void motorOut(int8_t driveState){
estott 0:de4320f74764 86
estott 2:4e88faab6988 87 //Lookup the output byte from the drive state.
estott 2:4e88faab6988 88 int8_t driveOut = driveTable[driveState & 0x07];
estott 2:4e88faab6988 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;
estott 2:4e88faab6988 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;
estott 0:de4320f74764 105 }
estott 0:de4320f74764 106
estott 2:4e88faab6988 107 //Convert photointerrupter inputs to a rotor state
estott 0:de4320f74764 108 inline int8_t readRotorState(){
estott 2:4e88faab6988 109 return stateMap[I1 + 2*I2 + 4*I3];
estott 0:de4320f74764 110 }
estott 0:de4320f74764 111
estott 0:de4320f74764 112 //Basic synchronisation routine
estott 2:4e88faab6988 113 int8_t motorHome() {
estott 0:de4320f74764 114 //Put the motor in drive state 0 and wait for it to stabilise
estott 0:de4320f74764 115 motorOut(0);
estott 3:569b35e2a602 116 wait(2.0);
estott 0:de4320f74764 117
estott 0:de4320f74764 118 //Get the rotor state
estott 2:4e88faab6988 119 return readRotorState();
estott 0:de4320f74764 120 }
estott 0:de4320f74764 121
estott 0:de4320f74764 122 //Main
estott 0:de4320f74764 123 int main() {
estott 0:de4320f74764 124 //Initialise the serial port
estott 0:de4320f74764 125 Serial pc(SERIAL_TX, SERIAL_RX);
estott 0:de4320f74764 126 pc.printf("Hello\n\r");
estott 0:de4320f74764 127
estott 0:de4320f74764 128 //Run the motor synchronisation
estott 2:4e88faab6988 129 orState = motorHome();
estott 2:4e88faab6988 130 pc.printf("Rotor origin: %x\n\r",orState);
estott 2:4e88faab6988 131 //orState is subtracted from future rotor state inputs to align rotor and motor states
estott 0:de4320f74764 132
tanyuzhuo 12:899cd6bf9844 133 I1.rise(&push);
tanyuzhuo 12:899cd6bf9844 134 I2.rise(&push);
tanyuzhuo 12:899cd6bf9844 135 I3.rise(&push);
estott 0:de4320f74764 136 }
estott 0:de4320f74764 137