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

Dependencies:   Crypto

Files at this revision

API Documentation at this revision

Comitter:
iachinweze1
Date:
Tue Mar 05 13:37:55 2019 +0000
Parent:
11:37801818b10f
Child:
13:d08bbfae7197
Commit message:
Interrupt science

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Mar 04 19:22:33 2019 +0000
+++ b/main.cpp	Tue Mar 05 13:37:55 2019 +0000
@@ -45,13 +45,18 @@
 //Phase lead to make motor spin
 const int8_t lead = 2;  //2 for forwards, -2 for backwards
 
+// Global States
+// TODO: Can we not use globals?
+int8_t orState = 0;    //Rotot offset at motor state 0
+int8_t currentState = 0;    //Rotot offset at motor state 0 
+
 //Status LED
 DigitalOut led1(LED1);
 
 //Photointerrupter inputs
-DigitalIn I1(I1pin);
-DigitalIn I2(I2pin);
-DigitalIn I3(I3pin);
+InterruptIn I1(I1pin);
+InterruptIn I2(I2pin);
+InterruptIn I3(I3pin);
 
 //Motor Drive outputs
 DigitalOut L1L(L1Lpin);
@@ -98,30 +103,40 @@
     //Get the rotor state
     return readRotorState();
 }
-    
+
+void stateUpdate(/*int8_t *currentState, int8_t offset, int8_t lead*/) {
+    // TODO: Global fix
+    currentState = readRotorState(); 
+    motorOut((currentState - orState + lead + 6) % 6);
+}
+
 //Main
 int main() {
-    int8_t orState = 0;    //Rotot offset at motor state 0
-    int8_t intState = 0;
-    int8_t intStateOld = 0;
-    
     //Initialise the serial port
-    Serial pc(SERIAL_TX, SERIAL_RX);
-    pc.printf("Hello\n\r");
+    // Serial pc(SERIAL_TX, SERIAL_RX);
+    // pc.printf("Hello\n\r");
     
     //Run the motor synchronisation
     orState = motorHome();
-    pc.printf("Rotor origin: %x\n\r",orState);
-    //orState is subtracted from future rotor state inputs to align rotor and motor states
+    
+    I1.fall(&stateUpdate);
+    I2.fall(&stateUpdate);
+    I3.fall(&stateUpdate);
+    
+    currentState = readRotorState();
+    motorOut((currentState-orState+lead+6)%6); // We push it digitally
     
-    //Poll the rotor state and set the motor outputs accordingly to spin the motor
-    while (1) {
-        intState = readRotorState();
-        if (intState != intStateOld) {
-            pc.printf("old:%d \t new:%d \t next:%d \n\r",intStateOld, intState, (intState-orState+lead+6)%6);
-            intStateOld = intState;
-            motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive
-        }
-    }
+    // pc.printf("Rotor origin: %x\n\r",orState);
+    // orState is subtracted from future rotor state inputs to align rotor and motor states
+    // intState = readRotorState();
+    //if (intState != intStateOld) {
+//        pc.printf("old:%d \t new:%d \t next:%d \n\r",intStateOld, intState, (intState-orState+lead+6)%6);
+//        intStateOld = intState;
+//        motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive
+//    }
+
+    // Keep the program running indefinitely
+    while (1); // {}
+        // pc.printf("Current:%d \t Next:%d \n\r", currentState, (currentState-orState+lead+6)%6);}
 }