Rotork Research Team / Mbed 2 deprecated TFM_Encoder

Dependencies:   mbed QEI

Revision:
13:da9d3fbbe407
Parent:
12:cbea987a3ec4
Child:
14:1eb49362a607
--- a/main.cpp	Thu Jan 03 15:35:32 2019 +0000
+++ b/main.cpp	Thu Jan 10 15:07:19 2019 +0000
@@ -3,6 +3,7 @@
 
 void Initialisation (void);                             //These voids are written after the main. They must be listed here too (functional prototypes).
 void StepCW(void);
+void Ph0(void);
 void Ph1(void);
 void Ph12 (void);
 void Ph2(void);
@@ -51,13 +52,13 @@
 int TimePerRev = 0;                                     //for calc of RPM
 int RPS = 0;                                            //for calc of RPM
 int rpm = 0;                                            //for calc of RPM
-int SetPoint = 1000;                                     //for adjusting the speed
+int SetPoint = 700;                                     //for adjusting the speed
 int z = 800;                                             //TimePerRev = TimePerClick * (800/z); 800 pulses per rev, PulseCount2_==800 for wheel.getwhoop_ flag. i.e. 10 points per reoluition for RPM calc.
 int T = 80;                                             //Motor temp limit
      
 char c;                                                 //keyboard cotrol GetChar
 
-float duty   =   0.5;                                     //velocity loop: 1 = fastest, 0.96 = slowest. Below 0.96 the motor will not operate.
+float duty   =   1;                                     //velocity loop: 1 = fastest, 0.96 = slowest. Below 0.96 the motor will not operate.
 float diff = 0.0;                                       //Velocity loop: diff = SetPoint - rpm;
 float x=0.1;                                            //x=time of square wave when 1 phase energised, 
 float y=0.04;                                           //y=time of square wave when 2 phases energised
@@ -139,20 +140,20 @@
             {
                 case 0:Ph1();break;
                 case 1:Ph1();break;
-                case 2:Ph12();break;
-                case 3:Ph12();break;
+                case 2:Ph1();break;
+                case 3:Ph1();break;
                 case 4:Ph2();break;
                 case 5:Ph2();break;
-                case 6:Ph23();break;
-                case 7:Ph23();break;
+                case 6:Ph2();break;
+                case 7:Ph2();break;
                 case 8:Ph3();break;
                 case 9:Ph3();break;
-                case 10:Ph34();break;
-                case 11:Ph34();break;
+                case 10:Ph3();break;
+                case 11:Ph3();break;
                 case 12:Ph4();break;
                 case 13:Ph4();break;
-                case 14:Ph41();break;
-                case 15:Ph41();break;
+                case 14:Ph4();break;
+                case 15:Ph4();break;
                 default:break; 
             } 
         
@@ -174,20 +175,20 @@
             {
                 case 0:Ph1();break;
                 case 1:Ph1();break;
-                case 2:Ph12();break;
-                case 3:Ph12();break;
+                case 2:Ph1();break;
+                case 3:Ph1();break;
                 case 4:Ph2();break;
                 case 5:Ph2();break;
-                case 6:Ph23();break;
-                case 7:Ph23();break;
+                case 6:Ph2();break;
+                case 7:Ph2();break;
                 case 8:Ph3();break;
                 case 9:Ph3();break;
-                case 10:Ph34();break;
-                case 11:Ph34();break;
+                case 10:Ph3();break;
+                case 11:Ph3();break;
                 case 12:Ph4();break;
                 case 13:Ph4();break;
-                case 14:Ph41();break;
-                case 15:Ph41();break;
+                case 14:Ph4();break;
+                case 15:Ph4();break;
                 default:break; 
             } 
         
@@ -206,20 +207,20 @@
             //pc.printf("3 StateB= %i, Pulses= %i, Revs= %i \r", StateB,wheel.getPulses(),wheel.getRevolutions());
             switch(StateB)
             {   
-                case 15:Ph41();break;
-                case 14:Ph41();break;
+                case 15:Ph4();break;
+                case 14:Ph4();break;
                 case 13:Ph4();break;
                 case 12:Ph4();break;
-                case 11:Ph34();break;
-                case 10:Ph34();break;
+                case 11:Ph3();break;
+                case 10:Ph3();break;
                 case 9:Ph3();break;
                 case 8:Ph3();break;
-                case 7:Ph23();break;
-                case 6:Ph23();break;
+                case 7:Ph2();break;
+                case 6:Ph2();break;
                 case 5:Ph2();break;
                 case 4:Ph2();break;
-                case 3:Ph12();break;
-                case 2:Ph12();break;
+                case 3:Ph1();break;
+                case 2:Ph1();break;
                 case 1:Ph1();break;
                 case 0:Ph1();break;
                 default:break; 
@@ -239,20 +240,20 @@
             //pc.printf("4 StateB= %i, Pulses= %i, Revs= %i \r", StateB,wheel.getPulses(),wheel.getRevolutions());
             switch(StateB)
             {
-                case 15:Ph41();break;
-                case 14:Ph41();break;
+                case 15:Ph4();break;
+                case 14:Ph4();break;
                 case 13:Ph4();break;
                 case 12:Ph4();break;
-                case 11:Ph34();break;
-                case 10:Ph34();break;
+                case 11:Ph3();break;
+                case 10:Ph3();break;
                 case 9:Ph3();break;
                 case 8:Ph3();break;
-                case 7:Ph23();break;
-                case 6:Ph23();break;
+                case 7:Ph2();break;
+                case 6:Ph2();break;
                 case 5:Ph2();break;
                 case 4:Ph2();break;
-                case 3:Ph12();break;
-                case 2:Ph12();break;
+                case 3:Ph1();break;
+                case 2:Ph1();break;
                 case 1:Ph1();break;
                 case 0:Ph1();break;
                 default:break; 
@@ -285,20 +286,30 @@
 {
         Ph1();
         wait(x);
-        Ph12(); 
-        wait(y);
+        //Ph12(); 
+        //wait(y);
         Ph2();
         wait(x);
-        Ph23();
-        wait(y);
+        //Ph23();
+        //wait(y);
         Ph3();
         wait(x);
-        Ph34();
-        wait(y);
+        //Ph34();
+        //wait(y);
         Ph4();
         wait(x);
-        Ph41();
-        wait(y);        
+        //Ph41();
+        //wait(y);        
+}
+
+void Ph0(void)
+{
+        Phase1.write(0);
+        Phase2.write(0);
+        Phase3.write(0);
+        Phase4.write(0);                         
+        //wait(x);
+        //pc.printf("Phase 1 = %i\n\r", wheel.getPulses());   
 }
 
 void Ph1(void)
@@ -397,12 +408,16 @@
             c = pc.getc();
             if(c == 'z')                            //turn on led1 causes CW operation
             {
+                AdjCW = 0;
+                AdjACW = 10;
                 led1 = !led1;
                 led2 = 0;
-                pc.printf("%i, %.5f, %i, %i, %i  \n\r", SetPoint, duty, AdjCW, AdjACW, rpm);
+                pc.printf("%i, %.5f, %i, %i, %i  \n\r", SetPoint, duty, AdjCW, AdjACW, rpm);                
             }
             if(c == 'x')                            //turn on led2 causes ACW operation
             {
+                AdjCW = 0;
+                AdjACW = 10;
                 led1 = 0;
                 led2 = !led2 ;
                 pc.printf("%i, %.5f, %i, %i, %i  \n\r", SetPoint, duty, AdjCW, AdjACW, rpm);
@@ -410,7 +425,7 @@
             if(c == 'q')                            //Increases setpoint used in Velocity loop
             {
                 //duty = duty + 0.0001;
-                SetPoint=SetPoint+10;                
+                SetPoint=SetPoint+5;                
                 if (SetPoint >2200)
                {
                    SetPoint = 2200;
@@ -420,7 +435,7 @@
             if(c == 'a')                            //Decreases setpoint used in Velocity loop
             {
                 //duty = duty - 0.0001;
-                SetPoint=SetPoint-10;                
+                SetPoint=SetPoint-5;                
                 if (SetPoint <50)
                {
                    SetPoint = 50;
@@ -485,22 +500,32 @@
     rpm = (RPS * 60)/10000;
     //Aout=((0.30303*rpm)/1000);               // for 500 rpm (0.30303*500/1000)*3.3V = 0.500V   
     ReadKType();
-    //if(rpm < 800)
-    //{
-    //    AdjCW = 0;
-    //    AdjACW = 3;
-    //}
-    //if(rpm > 799 and rpm < 2000)
-    //{
-    //    AdjCW = 1;
-    //    AdjACW = 2;
-    //}
-    //if(rpm >1999)
-    //{
-    //    AdjCW = 2;
-    //    AdjACW = 1;
-    //}
-            
+    if(rpm < 10)
+    {
+        AdjCW = 0;
+        AdjACW = 10;
+    }
+    if(rpm > 9 and rpm < 124)
+    {
+        AdjCW = 3;
+        AdjACW = 7;
+    }
+    if(rpm > 125 and rpm < 319)
+    {
+        AdjCW = 4;
+        AdjACW = 6;
+    }
+    if(rpm > 320 and rpm < 399)
+    {
+        AdjCW = 5;
+        AdjACW = 6;
+    }
+    if(rpm > 400)
+    {
+        AdjCW = 5;
+        AdjACW = 5;
+    }
+                  
     //pc.printf("rpm = %i\r", rpm);
     //pc.printf("StateA= %i, StateB= %i, StateC= %i, Pulses = %i\n\r", StateA, StateB, StateC, wheel.getPulses());
 }
@@ -508,14 +533,14 @@
 void VelocityLoop (void)
 {
    diff = SetPoint - rpm;                   //difference between setpoint and the RPM measurement
-    duty = duty + (diff*0.0001);           //duty is adjusted to speed up or slow down until difference = 0     
+    duty = duty + (diff*0.001);           //0.00001 when change required at fast rpm. 0.001 when changes required at slow rpm. duty is adjusted to speed up or slow down until difference = 0     
    if (duty > 1)                            //limits for duty. Motor will not operate below 0.96. 1 = max
    {
        duty = 1;
     }
-    if (duty <0.01)                          //3A min duty 0.96, 6.5A min duty 0.4
+    if (duty <0.32490)                          //3A min duty 0.96, 6.5A min duty 0.4
     {
-        duty = 0.01;
+        duty = 0.32490;
     }
     //pc.printf("%i, %.5f, %i, %i, %i  \r", SetPoint, duty, AdjCW, AdjACW, rpm);   //SetPoint = %i, rpm = %i\n\r", duty, SetPoint, rpm);
 }