Ultimate Gain method

Dependents:   2016_slave_MD_rorikon 2016_slave_MD_rorikon WRS_mechanamu_test

Files at this revision

API Documentation at this revision

Comitter:
sgrsn
Date:
Wed Nov 14 02:20:07 2018 +0000
Parent:
4:3ca1603fbcda
Commit message:
...

Changed in this revision

PID.cpp Show annotated file Show diff for this revision Revisions of this file
PID.h Show annotated file Show diff for this revision Revisions of this file
diff -r 3ca1603fbcda -r 6949e401a9ad PID.cpp
--- a/PID.cpp	Fri Sep 30 12:31:16 2016 +0000
+++ b/PID.cpp	Wed Nov 14 02:20:07 2018 +0000
@@ -2,17 +2,17 @@
 
 PID::PID(Timer *T)
 {
-    timer = *T;
+    timer = T;
     integral    = 0;
     prev_hensa  = 0;
     nowtime     = 0;
     prev_time   = 0;
     lateD      = 0;
-    timer.start();
+    timer->start();
 }
-float PID::control(float target, float nowrpm)
+float PID::controlPID(float target, float nowrpm)
 {
-    nowtime = timer.read();
+    nowtime = timer->read();
     float hensa = target - nowrpm;
     float dt = nowtime - prev_time;
     integral += (hensa + prev_hensa) / 2 * dt;
@@ -20,43 +20,46 @@
     float sousaryou = Kp*hensa + Ki*integral + Kd*differential;
     //printf("%f %f %f\r\n", Kp*hensa, Ki*integral, Kd*differential);
     prev_hensa = hensa;
-    prev_time = timer.read();
+    prev_time = nowtime;
     return sousaryou;
 }
 float PID::PI_lateD(float target, float nowrpm)
 {
-    nowtime = timer.read();
+    nowtime = timer->read();
     float hensa = target - nowrpm;
     float dt = nowtime - prev_time;
     integral += (hensa + prev_hensa) / 2 * dt;
     float sousaryou = Kp*hensa + Ki*integral + Kd*lateD;
     prev_hensa = hensa;
-    prev_time = timer.read();
+    prev_time = timer->read();
     lateD = (hensa - prev_hensa) / dt;
     return sousaryou;
 }
-float PID::control_P(float target, float nowrpm, float new_Kp)
+float PID::controlP(float target, float nowrpm, float new_Kp)
 {
     float hensa = target - nowrpm;
     float sousaryou = new_Kp*hensa;
     return sousaryou;
 }
-float PID::control_PI(float target, float nowrpm)
+float PID::controlPI(float target, float nowrpm)
 {
-    Kp = 0.45 * Ku;
-    Ti = 0.83 * Pu;
-    Ki = (1 / Ti) * Kp;
-    nowtime = timer.read();
+    nowtime = timer->read();
     float hensa = target - nowrpm;
     float dt = nowtime - prev_time;
     integral += (hensa + prev_hensa) / 2 * dt;
     float sousaryou = Kp*hensa + Ki*integral;
     //printf("%f %f %f\r\n", Kp*hensa, Ki*integral, Kd*differential);
     prev_hensa = hensa;
-    prev_time = timer.read();
+    prev_time = timer->read();
     return sousaryou;
 }
-void PID::set_parameter(float new_Ku, float new_Pu)
+void PID::setParameter(float new_Kp, float new_Ki, float new_Kd)
+{
+    Kp = new_Kp;
+    Ki = new_Ki;
+    Kd = new_Kd;
+}
+void PID::setParameter(float new_Ku, float new_Pu)
 {  
     Ku = new_Ku;
     Pu = new_Pu;
@@ -67,9 +70,15 @@
     Ki = (1 / Ti) * Kp;
     Kd = Td * Kp;
 }
+void PID::setParameterPI(float new_Ku, float new_Pu)
+{
+    Kp = 0.45 * Ku;
+    Ti = 0.83 * Pu;
+    Ki = (1 / Ti) * Kp;
+}
 void PID::reset()
 {
     integral    = 0;
     prev_hensa  = 0;
-    prev_time = timer.read();
+    prev_time = timer->read();
 }
diff -r 3ca1603fbcda -r 6949e401a9ad PID.h
--- a/PID.h	Fri Sep 30 12:31:16 2016 +0000
+++ b/PID.h	Wed Nov 14 02:20:07 2018 +0000
@@ -6,11 +6,13 @@
 {
     public:
         PID(Timer *T);
-        float control(float target, float nowrpm);
+        float controlPID(float target, float nowrpm);
         float PI_lateD(float target, float nowrpm);
-        float control_P(float target, float nowrpm, float new_Kp);
-        float control_PI(float target, float nowrpm);
-        void set_parameter(float new_Ku, float new_Pu);
+        float controlP(float target, float nowrpm, float new_Kp);
+        float controlPI(float target, float nowrpm);
+        void setParameter(float new_Kp, float new_Ki, float new_Kd);
+        void setParameter(float new_Ku, float new_Pu);
+        void setParameterPI(float new_Ku, float new_Pu);
         void reset();
 
         float Ku;
@@ -22,7 +24,7 @@
         float Kd;
         
         private:
-        Timer timer;
+        Timer *timer;
         float integral;
         float prev_hensa;
         float nowtime;