Using HIDScope for P(I)D controller

Dependencies:   FastPWM HIDScope MODSERIAL QEI biquadFilter mbed

Fork of PES_tutorial_5 by BMT Module 9 Group 4

Revision:
15:c2cfab737a4c
Parent:
14:29236a33b5e4
Child:
16:0a8d4d746454
Child:
17:4a0912c93771
--- a/main.cpp	Fri Oct 19 09:02:56 2018 +0000
+++ b/main.cpp	Fri Oct 19 09:30:44 2018 +0000
@@ -20,7 +20,11 @@
 volatile double measuredPosition = 0.0;
 volatile double referencePosition = 0.0; 
 volatile double motorValue= 0.01;
-volatile double Kp = 0.0;
+volatile double Kp = 0.0; //dit maken we variabel, dit zorgt voor een grote of kleine overshoot
+volatile double Ki = 1.0; //dit moeten we bepalen met een plot bijvoorbeeld
+volatile double Ts = 0.01;
+volatile double Error_integral = 0.0;
+
 
 //------------------------------------------------------------------------------
 // Functions
@@ -41,11 +45,14 @@
 
 double FeedbackControl(double Error)
 {  
-    double Kp = 20.0*potMeter2.read(); // Scale 0 to 20
     // Proportional part: 
+    Kp = 20*potMeter2.read(); //van 0 tot 20, waardes rond de 5 zijn het beste (minder overshoot + minder trilling motor beste combinatie hiervan)
     double u_k = Kp * Error;
+    // Integral part:
+    Error_integral = Error_integral + Error * Ts; 
+    double u_i = Ki * Error_integral;
     // Sum all parts and return it 
-    return u_k; //motorValue
+    return u_k + u_i; //motorValue
  }
  
 void SetMotor1(double motorValue)
@@ -89,7 +96,10 @@
     pc.baud (115200);
     pc.printf("Referenceposition %f \r\n", referencePosition);
     pc.printf("Measured position %f \r\n", measuredPosition);
-    pc.printf("Motorvalue %f \r\n", motorValue);
+    pc.printf("Motorvalue/Error %f \r\n", motorValue);
+    pc.printf("Proportional gain %f \r\n", Kp);
+    pc.printf("Integral gain %f \r\n", Ki);
+    pc.printf("Error integral %f \r\n", Error_integral);
 }
 //-----------------------------------------------------------------------------
 int main()