Casper Maas / Mbed 2 deprecated PID_controller

Dependencies:   HIDScope MODSERIAL QEI biquadFilter mbed

Revision:
1:e10ae03926e6
Parent:
0:447a347725bb
Child:
2:2c32825bb327
--- a/main.cpp	Fri Oct 26 17:46:54 2018 +0000
+++ b/main.cpp	Mon Oct 29 07:24:56 2018 +0000
@@ -6,7 +6,9 @@
 #include "QEI.h"
 #include "MODSERIAL.h"
 #include "HIDScope.h"
+#include "Math.h"
 
+AnalogIn button2(A4);
 PwmOut pwmpin1(D6);
 DigitalOut directionpin1(D7);
 QEI encoder1 (D9, D8, NC, 8400, QEI::X4_ENCODING);
@@ -21,7 +23,7 @@
 double Ki = 0.5;   //1
 double Kd = 10; //200
 double Ts = 0.1; // Sample time in seconds
-volatile double reference_rotation = 8400;
+volatile double reference_rotation = 0; //define as radians
 double motor_position;
 bool AlwaysTrue;
 
@@ -64,8 +66,8 @@
 // CONTROLLING THE MOTOR
 void Motor_mover()
 {
-    double motor_position = encoder1.getPulses();
-    double error = reference_rotation - motor_position;
+    double motor_position = encoder1.getPulses(); //output in counts
+    double error = reference_rotation - motor_position*(2*PI)/8400;
     double u = PID_controller(error);
     moter_control(u);
 }
@@ -88,12 +90,37 @@
 
 int main()
 {
+  // INITIALIZING
+  
   pwmpin1.period_us(60);
   pc.printf("test");
   pc.baud(9600);
   //show_counts.attach(PrintFlag, 0.2);
   ref_rot.attach(Motor_mover, 0.01);
   Scope_Data.attach(ScopeData, 0.1);
+ 
+ // DEFININING VARIABLES
+            //float reference;
+            float foo[17];
+            float length = 9.0f;
+            float b = 8.0f;
+            int a;
+ 
+ 
+  
+  // EXECTURION  IN MAIN
+        for (a=0; a<10; a++){
+            foo[a] = 3.14f/length*(a);
+            printf("%f\n\r", foo[a]);
+            }
+        for(a=10; a<18; a++){
+            foo[a] = 3.14f/length*b;
+            printf("%f\n\r", foo[a]);
+            reference = foo[a];
+            b = b-1.0f;
+            }        
+  
+  
     
     while (true) {
         /*
@@ -105,6 +132,19 @@
             wait(0.2f);
             }
             */
+           // sin input 
+           /*
+           if (button2 == false){
+            wait (0.2f);
+            for (b=0; b<4; b++){
+                  for(a=0; a<18; a++){
+                    reference_rotation = foo[a];
+                    wait(0.1f);
+                     }
+                    }
+                }
+                */
+            }
+            
             
     }
-}
\ No newline at end of file