SMARTASSES 2019

Dependencies:   mbed Crypto

Committer:
estott
Date:
Tue Feb 19 09:24:16 2019 +0000
Revision:
6:8167675195f6
Oh dear

Who changed what in which revision?

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