this code is for inverted pendulum balancer. it is based on two-loop PID controller. One controller is for Angle and another one is for velocity.

Dependencies:   HCSR04 L298HBridge mbed

Revision:
0:489498e8dae5
diff -r 000000000000 -r 489498e8dae5 PID/PID.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PID/PID.h	Fri Aug 25 21:10:23 2017 +0000
@@ -0,0 +1,61 @@
+//////////////////////////////////////////////////////////////////////////////////
+// Company: edinburgh of university
+// Engineer: ZEjun DU
+// 
+// Create Date: 2017/08/20 13:06:52
+// Design Name: Inverted Pendulum Balancer
+// Module Name: basic PID algorithm
+// Tool Versions: “Keil 5” or “Mbed Complie Online”
+// Description: this part is to build the basic PID control theroy
+//                           
+// 
+//////////////////////////////////////////////////////////////////////////////////
+
+#include "mbed.h"
+
+////////////////////////////////////////////////////////////
+//the D term is very sensitive, this part is to filter the D term 
+//but the effect of this function is not obvious
+////////////////////////////////////////////////////////////
+
+// Examples for _filter:
+// f_cut = 10 Hz -> _filter = 15.9155e-3
+// f_cut = 15 Hz -> _filter = 10.6103e-3
+// f_cut = 20 Hz -> _filter =  7.9577e-3
+// f_cut = 25 Hz -> _filter =  6.3662e-3
+// f_cut = 30 Hz -> _filter =  5.3052e-3
+#define PID_D_TERM_FILTER 0.00795770f    // 20hz filter on D term
+//#define PID_D_TERM_FILTER 0.0063662f    // 30hz filter on D term
+//#define PID_D_TERM_FILTER 0.0f    // no filter on D term
+
+typedef struct
+{
+    float kp;
+    float ki;
+    float kd;
+    float desired;
+    float prevError;
+    float error;
+    float integ;
+    float deri; 
+    float prevDeri;
+    float outP;
+    float outI;
+    float outD;
+    float iLimit;
+    float iLimitLow;
+    float dt;
+}PidObject;
+
+float show_I_term(void);
+void pidInit(PidObject* pid, const float desired, const float kp,const float ki, const float kd, const float iLimit, const float dt);
+float get_p(PidObject* pid);
+float get_i(PidObject* pid, float distance);
+float get_d(PidObject* pid);
+
+float get_pid_vel(PidObject* pid, float distance);
+
+float get_pid(PidObject* pid);
+void pidReset(PidObject* pid);
+void pidSetError(PidObject* pid, const float error);
+void pidSetDesired(PidObject* pid, const float desired);
\ No newline at end of file