ES2017 coursework 2

Dependencies:   PID

Fork of ES_CW2_Starter by Edward Stott

Revision:
8:77627657da80
Parent:
7:5932ed0bad6d
Child:
9:575b29cbf5e4
--- a/main.cpp	Thu Mar 02 19:05:49 2017 +0000
+++ b/main.cpp	Thu Mar 02 20:27:10 2017 +0000
@@ -53,6 +53,9 @@
 DigitalIn I2(I2pin);
 DigitalIn I3(I3pin);
 
+InterruptIn qA(CHA);
+InterruptIn qB(CHB);
+
 //Motor Drive outputs
 DigitalOut L1L(L1Lpin);
 DigitalOut L1H(L1Hpin);
@@ -61,6 +64,8 @@
 DigitalOut L3L(L3Lpin);
 DigitalOut L3H(L3Hpin);
 DigitalOut clk(LED1);
+DigitalOut Direction(LED2);
+//DigitalOut testpin(D13);
 
 //Timeout function for rotating at set speed
 Timeout spinTimer;
@@ -79,6 +84,8 @@
 int8_t intStateOld = 0;
 
 int i=0;
+int pos=0;
+bool DIR=0;
 
 //Set a given drive state
 void motorOut(int8_t driveState)
@@ -149,6 +156,29 @@
     printSpeed.attach(&speedo, 1.0);
 }
 
+void edgeRiseA(){
+    pos++;
+    if(pos>=468){
+//        Direction=!Direction;
+        pos=pos%468;
+//        testpin=!testpin;
+    }
+    if(qB) DIR = 0;
+    else DIR = 1;
+    clk=DIR;
+//CLOCKWISE:        A rises before B -> On A edge, B low -> DIR = 1
+//ANTICLOCKWISE:    B rises before A -> On A edge, B high-> DIR = 0
+}
+
+void edgeIncr(){
+    pos++;
+    if(pos>=468){
+//        Direction=!Direction;
+        pos=pos%468;
+//        testpin=!testpin;
+    }
+}
+
 //Main function
 int main()
 {
@@ -164,13 +194,19 @@
     int index=0;
     int units = 0, tens = 0, decimals = 0;
     char ch;
+//    testpin=0;
 
     speedTimer.start();
     I1.rise(&rps);
+    
+    qA.rise(&edgeRiseA);
+    qB.rise(&edgeIncr);
+    qA.fall(&edgeIncr);
+    qB.fall(&edgeIncr);
 
     while(1) {
         //Toggle LED so we know something's happening
-        clk = !clk;
+//        clk = !clk;
 
         //If there's a character to read from the serial port
         if (pc.readable()) {
@@ -234,6 +270,9 @@
                 case 's':
                     pc.printf("Revs / sec: %2.2f\r", revs);
                     break;
+                case 't':
+                    pc.printf("%d\n\r", pos);
+                    break;
                 default:
                     //Set speed variables to zero to stop motor spinning
                     revsec=0;