Maik Overmars / Robot_Software

Dependencies:   HIDScope MODSERIAL QEI biquadFilter mbed

Fork of Robot_Software by Bram Jonkheer

Revision:
0:ef81b9f14f58
Child:
1:ce487c9929dd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/help_functions/PID_controller.h	Fri Oct 19 07:50:39 2018 +0000
@@ -0,0 +1,21 @@
+#include "mbed.h"
+
+double PID_controller(double error)
+{
+    Kp = potmeter2.read()*3;
+    
+    double u_k = Kp * error;
+    
+    static double error_integral = 0;
+    static double error_prev = error; // initialization with this value only done once!
+    static BiQuad LowPassFilter(0.0640, 0.1279, 0.0640, -1.1683, 0.4241);
+    
+    error_integral = error_integral + error * 1/samplingfreq;
+    double u_i = Ki * error_integral;
+    
+    double error_derivative = (error - error_prev)*samplingfreq;
+    double filtered_error_derivative = LowPassFilter.step(error_derivative);
+    double u_d = Kd * filtered_error_derivative;
+    error_prev = error;
+    return u_k+u_i+u_d;
+}
\ No newline at end of file