Callum and Adel's changes on 12/02/19

Dependencies:   Crypto

Committer:
iachinweze1
Date:
Tue Mar 05 13:37:55 2019 +0000
Revision:
12:41b3112021a3
Parent:
11:37801818b10f
Child:
14:4e312fb83330
Interrupt science

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
estott 0:de4320f74764 26 //Mapping from sequential drive states to motor phase outputs
estott 0:de4320f74764 27 /*
estott 0:de4320f74764 28 State L1 L2 L3
estott 0:de4320f74764 29 0 H - L
estott 0:de4320f74764 30 1 - H L
estott 0:de4320f74764 31 2 L H -
estott 0:de4320f74764 32 3 L - H
estott 0:de4320f74764 33 4 - L H
estott 0:de4320f74764 34 5 H L -
estott 0:de4320f74764 35 6 - - -
estott 0:de4320f74764 36 7 - - -
estott 0:de4320f74764 37 */
estott 0:de4320f74764 38 //Drive state to output table
estott 0:de4320f74764 39 const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00};
estott 2:4e88faab6988 40
estott 0:de4320f74764 41 //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid
estott 2:4e88faab6988 42 const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07};
estott 2:4e88faab6988 43 //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 44
estott 2:4e88faab6988 45 //Phase lead to make motor spin
estott 3:569b35e2a602 46 const int8_t lead = 2; //2 for forwards, -2 for backwards
estott 0:de4320f74764 47
iachinweze1 12:41b3112021a3 48 // Global States
iachinweze1 12:41b3112021a3 49 // TODO: Can we not use globals?
iachinweze1 12:41b3112021a3 50 int8_t orState = 0; //Rotot offset at motor state 0
iachinweze1 12:41b3112021a3 51 int8_t currentState = 0; //Rotot offset at motor state 0
iachinweze1 12:41b3112021a3 52
estott 0:de4320f74764 53 //Status LED
estott 0:de4320f74764 54 DigitalOut led1(LED1);
estott 0:de4320f74764 55
estott 0:de4320f74764 56 //Photointerrupter inputs
iachinweze1 12:41b3112021a3 57 InterruptIn I1(I1pin);
iachinweze1 12:41b3112021a3 58 InterruptIn I2(I2pin);
iachinweze1 12:41b3112021a3 59 InterruptIn I3(I3pin);
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
estott 0:de4320f74764 69 //Set a given drive state
estott 0:de4320f74764 70 void motorOut(int8_t driveState){
estott 0:de4320f74764 71
estott 2:4e88faab6988 72 //Lookup the output byte from the drive state.
estott 2:4e88faab6988 73 int8_t driveOut = driveTable[driveState & 0x07];
estott 2:4e88faab6988 74
estott 2:4e88faab6988 75 //Turn off first
estott 2:4e88faab6988 76 if (~driveOut & 0x01) L1L = 0;
estott 2:4e88faab6988 77 if (~driveOut & 0x02) L1H = 1;
estott 2:4e88faab6988 78 if (~driveOut & 0x04) L2L = 0;
estott 2:4e88faab6988 79 if (~driveOut & 0x08) L2H = 1;
estott 2:4e88faab6988 80 if (~driveOut & 0x10) L3L = 0;
estott 2:4e88faab6988 81 if (~driveOut & 0x20) L3H = 1;
estott 2:4e88faab6988 82
estott 2:4e88faab6988 83 //Then turn on
estott 2:4e88faab6988 84 if (driveOut & 0x01) L1L = 1;
estott 2:4e88faab6988 85 if (driveOut & 0x02) L1H = 0;
estott 2:4e88faab6988 86 if (driveOut & 0x04) L2L = 1;
estott 2:4e88faab6988 87 if (driveOut & 0x08) L2H = 0;
estott 2:4e88faab6988 88 if (driveOut & 0x10) L3L = 1;
estott 2:4e88faab6988 89 if (driveOut & 0x20) L3H = 0;
estott 0:de4320f74764 90 }
estott 0:de4320f74764 91
estott 2:4e88faab6988 92 //Convert photointerrupter inputs to a rotor state
estott 0:de4320f74764 93 inline int8_t readRotorState(){
estott 2:4e88faab6988 94 return stateMap[I1 + 2*I2 + 4*I3];
estott 0:de4320f74764 95 }
estott 0:de4320f74764 96
estott 0:de4320f74764 97 //Basic synchronisation routine
estott 2:4e88faab6988 98 int8_t motorHome() {
estott 0:de4320f74764 99 //Put the motor in drive state 0 and wait for it to stabilise
estott 0:de4320f74764 100 motorOut(0);
estott 3:569b35e2a602 101 wait(2.0);
estott 0:de4320f74764 102
estott 0:de4320f74764 103 //Get the rotor state
estott 2:4e88faab6988 104 return readRotorState();
estott 0:de4320f74764 105 }
iachinweze1 12:41b3112021a3 106
iachinweze1 12:41b3112021a3 107 void stateUpdate(/*int8_t *currentState, int8_t offset, int8_t lead*/) {
iachinweze1 12:41b3112021a3 108 // TODO: Global fix
iachinweze1 12:41b3112021a3 109 currentState = readRotorState();
iachinweze1 12:41b3112021a3 110 motorOut((currentState - orState + lead + 6) % 6);
iachinweze1 12:41b3112021a3 111 }
iachinweze1 12:41b3112021a3 112
estott 0:de4320f74764 113 //Main
estott 0:de4320f74764 114 int main() {
estott 0:de4320f74764 115 //Initialise the serial port
iachinweze1 12:41b3112021a3 116 // Serial pc(SERIAL_TX, SERIAL_RX);
iachinweze1 12:41b3112021a3 117 // pc.printf("Hello\n\r");
estott 0:de4320f74764 118
estott 0:de4320f74764 119 //Run the motor synchronisation
estott 2:4e88faab6988 120 orState = motorHome();
iachinweze1 12:41b3112021a3 121
iachinweze1 12:41b3112021a3 122 I1.fall(&stateUpdate);
iachinweze1 12:41b3112021a3 123 I2.fall(&stateUpdate);
iachinweze1 12:41b3112021a3 124 I3.fall(&stateUpdate);
iachinweze1 12:41b3112021a3 125
iachinweze1 12:41b3112021a3 126 currentState = readRotorState();
iachinweze1 12:41b3112021a3 127 motorOut((currentState-orState+lead+6)%6); // We push it digitally
estott 0:de4320f74764 128
iachinweze1 12:41b3112021a3 129 // pc.printf("Rotor origin: %x\n\r",orState);
iachinweze1 12:41b3112021a3 130 // orState is subtracted from future rotor state inputs to align rotor and motor states
iachinweze1 12:41b3112021a3 131 // intState = readRotorState();
iachinweze1 12:41b3112021a3 132 //if (intState != intStateOld) {
iachinweze1 12:41b3112021a3 133 // pc.printf("old:%d \t new:%d \t next:%d \n\r",intStateOld, intState, (intState-orState+lead+6)%6);
iachinweze1 12:41b3112021a3 134 // intStateOld = intState;
iachinweze1 12:41b3112021a3 135 // motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive
iachinweze1 12:41b3112021a3 136 // }
iachinweze1 12:41b3112021a3 137
iachinweze1 12:41b3112021a3 138 // Keep the program running indefinitely
iachinweze1 12:41b3112021a3 139 while (1); // {}
iachinweze1 12:41b3112021a3 140 // pc.printf("Current:%d \t Next:%d \n\r", currentState, (currentState-orState+lead+6)%6);}
estott 0:de4320f74764 141 }
estott 0:de4320f74764 142