Rotork Research Team / Mbed 2 deprecated TFM_Encoder

Dependencies:   mbed QEI

Revision:
3:4249dbdf7ed3
Parent:
2:3f95c82c26bb
Child:
4:3aedc9246ae4
--- a/main.cpp	Fri Oct 26 12:58:15 2018 +0000
+++ b/main.cpp	Wed Nov 14 09:58:13 2018 +0000
@@ -1,25 +1,28 @@
 #include "mbed.h"
-void ReportA(void); //These voids are written after the main. They must be listed here too (functional prototypes).
-void ReportB(void);
-void ReportX(void);
-void Startup(void);
+#include "QEI.h"
+
+void Initialisation (void); //These voids are written after the main. They must be listed here too (functional prototypes).
 void StepCW(void);
 void Ph1(void);
+void Ph12 (void);
 void Ph2(void);
+void Ph23 (void);
 void Ph3(void);
+void Ph34 (void);
 void Ph4(void);
+void Ph41 (void);
 
 Serial pc(USBTX, USBRX); // tx, rx - set up the Terraterm input from mbed
 
+QEI wheel(p5, p6, p8, 800, QEI::X4_ENCODING);         //code for quadrature encoder see QEI.h
+
+Timer t;
+
 DigitalOut      Phase1                 (p21);         //Pin and LED set up                   
 DigitalOut      Phase2                 (p22);
 DigitalOut      Phase3                 (p23);
 DigitalOut      Phase4                 (p24);
 
-InterruptIn     ChannelA                (p5);
-InterruptIn     ChannelB                (p6);
-InterruptIn     Index                   (p8);
-
 DigitalIn       Button1                 (p11);
 
 DigitalOut      led1(LED1);
@@ -27,142 +30,229 @@
 DigitalOut      led3(LED3);
 DigitalOut      led4(LED4);
 
-int CountA=0;   //On start up the integer will be zero
-int CountB=0;
-int CountX=0;
+int StateA = 0;
+int StateB  = 0;
+int TimePerClick = 0;  
+int TimePerRev = 0;
+int RPS = 0;
+int RPM = 0;
 
-float x=0.04, y=0.01;   //x=time of square wave when 1 phase energised, y=time of square wave when 2 phases energised
+float x=0.05, y=0.02, z=1;   //x=time of square wave when 1 phase energised, y=time of square wave when 2 phases energised
 
 int main(void) 
-{  
-    ChannelA.rise(&ReportA);  // Interrupt - When channel A goes from 0 to 1 it is reported       
-    ChannelB.rise(&ReportB);  // Interrupt - When channel B goes from 0 to 1 it is reported
-    Index.rise(&ReportX);  // Interrupt - When Index goes from 0 to 1 it is reported             
+{   
     pc.baud(230400);        //Set fastest baud rate
-    Startup();
-  
-   
-    while(CountX>0)
-    {
-        wait(1);
-        pc.printf("A = %d\n\r",CountA);
-        pc.printf("B = %d\n\r",CountB);
-        while(Button1);
-        wait(0.5);
-        Ph1();
-        led1=!led1;
-        wait(1);
-        pc.printf("A = %d\n\r",CountA);
-        pc.printf("B = %d\n\r",CountB);
-        while(Button1);
-        wait(0.5);
-        Ph2();
-        led2=!led2;
-        wait(1);
-        pc.printf("A = %d\n\r",CountA);
-        pc.printf("B = %d\n\r",CountB);
-        while(Button1);
-        wait(0.5);
-        Ph3();
-        led3=!led3;
-        wait(1);
-        pc.printf("A = %d\n\r",CountA);
-        pc.printf("B = %d\n\r",CountB);
-        while(Button1);
-        wait(0.5);
-        Ph4();
-        led4=!led4;
+    StepCW();
+    Initialisation();
+    wait(0.1);
+    t.start();
+    
+while(wheel.getRevolutions()==0)
+    {    
+    switch(StateA)
+        {
+        case 0:Ph1();pc.printf("Ph1= %i\n\r", wheel.getPulses());break; 
+        case 1:Ph12();pc.printf("Ph12= %i\n\r", wheel.getPulses());break; 
+        case 2:Ph2();pc.printf("Ph2= %i\n\r", wheel.getPulses());break; 
+        case 3:Ph23();pc.printf("Ph23= %i\n\r", wheel.getPulses());break; 
+        case 4:Ph3();pc.printf("Ph3= %i\n\r", wheel.getPulses());break; 
+        case 5:Ph34();pc.printf("Ph34= %i\n\r", wheel.getPulses());break; 
+        case 6:Ph4();pc.printf("Ph4= %i\n\r", wheel.getPulses());break;         
+        case 7:Ph41();pc.printf("Ph41= %i\n\r", wheel.getPulses());break;   
+        default:break; 
+        } 
+        
+        if(wheel.getYay()==1)
+        {
+        StateA++;
+        wheel.ResetYay();
+        if (StateA>7)
+            {
+            StateA=0;
+            }
+        } 
     }        
-}
-     
-
-void Startup(void)
-{
-    while(CountX==0)
-    {
-        StepCW();    
+        
+ while(1)
+ {       
+        while(wheel.getRevolutions()>0 && wheel.getPulses()>0)
+        {
+        StateB = (wheel.getPulses())%16;
+        //pc.printf("StateA= %i\n\r", StateA);
+        pc.printf("1 StateB= %i, Pulses= %i, Revs= %i\r", StateB,wheel.getPulses(),wheel.getRevolutions());
+        //pc.printf("",);
+       // pc.printf("",);
+            
+            switch(StateB)
+            {
+            case 0:Ph3();break;
+            case 1:Ph3();break;
+            case 2:Ph34();break;
+            case 3:Ph34();break;
+            case 4:Ph4();break;
+            case 5:Ph4();break;
+            case 6:Ph41();break;
+            case 7:Ph41();break;
+            case 8:Ph1();break;
+            case 9:Ph1();break;
+            case 10:Ph12();break;
+            case 11:Ph12();break;
+            case 12:Ph2();break;
+            case 13:Ph2();break;
+            case 14:Ph23();break;
+            case 15:Ph23();break;
+            default:break; 
+            } 
+        
+        if(wheel.getYay()==1)
+            {
+            wheel.ResetYay();
+            TimePerClick = (t.read_us());  
+            t.reset();
+            TimePerRev = TimePerClick * 400;
+            TimePerRev = TimePerRev / 1000;
+            RPS = 10000000 / TimePerRev;
+            RPM = (RPS * 60)/10000;
+            //pc.printf("RPM = %d\n\r", RPM);
+            }    
+        }
+    
+    
+    while(wheel.getRevolutions()>0 && wheel.getPulses()<1)
+        {
+        StateB = (800+wheel.getPulses())%16;
+        //pc.printf("StateA= %i\n\r", StateA);
+        pc.printf("2 StateB= %i, Pulses= %i, Revs= %i\r", StateB,wheel.getPulses(),wheel.getRevolutions());
+        //pc.printf("",);
+       // pc.printf("",);
+            
+            switch(StateB)
+            {
+            case 0:Ph3();break;
+            case 1:Ph3();break;
+            case 2:Ph34();break;
+            case 3:Ph34();break;
+            case 4:Ph4();break;
+            case 5:Ph4();break;
+            case 6:Ph41();break;
+            case 7:Ph41();break;
+            case 8:Ph1();break;
+            case 9:Ph1();break;
+            case 10:Ph12();break;
+            case 11:Ph12();break;
+            case 12:Ph2();break;
+            case 13:Ph2();break;
+            case 14:Ph23();break;
+            case 15:Ph23();break;
+            default:break; 
+            } 
+        
+        if(wheel.getYay()==1)
+            {
+            wheel.ResetYay();
+            TimePerClick = (t.read_us());  
+            t.reset();
+            TimePerRev = TimePerClick * 400;
+            TimePerRev = TimePerRev / 1000;
+            RPS = 10000000 / TimePerRev;
+            RPM = (RPS * 60)/10000;
+            //pc.printf("RPM = %d\n\r", RPM);
+              }    
+        }
+    
     }
 }
 
-
-
-
-void ReportA(void)                      
-{ 
-CountA++;
-//led1 = !led1;                               //Counts A
-//pc.printf("A = %d\n\r",CountA);       //Prints cumulative counts to Terraterm
-}
-
-
-void ReportB(void)                      
-{
-CountB++;
-//led2 = !led2;                               //Counts B
-//pc.printf("B = %d\n\r",CountB);       //Prints cumulative counts to Terraterm
-
-}
-    
-    
-void ReportX(void) 
-{
-        CountX++;
-        pc.printf("X = %d\n\r",CountX);   //Prints cumulative counts to Terraterm
-        //led4 = !led4;
-        CountA = CountB = 0;
-    }
-
 void StepCW(void)                           //Square wave switching
 {
         Ph1();
-          
-        Phase1 = Phase2 = 1;
-        Phase3 = Phase4 = 0;
+        wait(x);
+        Ph12(); 
         wait(y);
-    
         Ph2();
-     
-        Phase2 = Phase3 = 1;
-        Phase1 = Phase4 = 0;
+        wait(x);
+        Ph23();
         wait(y);
-    
         Ph3();
-        
-        Phase3 = Phase4 = 1;
-        Phase1 = Phase2 = 0;
+        wait(x);
+        Ph34();
         wait(y);
-    
         Ph4();
-    
-        Phase4 = Phase1 = 1;
-        Phase2 = Phase3 = 0;
-        wait(y);   
+        wait(x);
+        Ph41();
+        wait(y);        
 }
 
 void Ph1(void)
 {
-    Phase1 = 1;                         
-    Phase2 = Phase3 = Phase4 = 0;
-    wait(x);
+        Phase1 = 1;                         
+        Phase2 = Phase3 = Phase4 = 0;
+        //wait(x);
+        //pc.printf("Phase 1 = %i\n\r", wheel.getPulses());   
+}
+
+void Ph12 (void)
+{
+        Phase1 = Phase2 = 1;
+        Phase3 = Phase4 = 0;
+        //wait(y);
 }
 
 void Ph2(void)
 {
         Phase2 = 1;
         Phase1 = Phase3 = Phase4 = 0;
-        wait(x);
+        //wait(x);
+        //pc.printf("Phase 2 = %i\n\r", wheel.getPulses());
+}
+
+void Ph23 (void)
+{
+        Phase2 = Phase3 = 1;
+        Phase4 = Phase1 = 0;
+        //wait(y);
 }
 
 void Ph3(void)
 {
         Phase3 = 1;
         Phase1 = Phase2 = Phase4 = 0;
-        wait(x);
+        //wait(x);
+        //pc.printf("Phase 3 = %i\n\r", wheel.getPulses());
+}
+void Ph34 (void)
+{
+        Phase3 = Phase4 = 1;
+        Phase1 = Phase2 = 0;
+        //wait(y);
 }
 
 void Ph4(void)
 {
         Phase4 = 1;
         Phase1 = Phase2 = Phase3 = 0;
-        wait(x);
-}   
\ No newline at end of file
+        //wait(x);
+        //pc.printf("Phase 4 = %i\n\r", wheel.getPulses());
+}
+
+void Ph41 (void)
+{
+        Phase4 = Phase1 = 1;
+        Phase2 = Phase3 = 0;
+        //wait(y);
+}
+
+void Initialisation (void)
+{
+    Phase1 = 0;                              
+    Phase2 = 0;                 
+    Phase3 = 0;                   
+    Phase4 = 0;  
+    led1 = 0;
+    led2 = 0;
+    led3 = 0;
+    led4 = 0;
+    wheel.ResetYay();
+    
+
+}
\ No newline at end of file