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

Committer:
dudu941014
Date:
Fri Aug 25 21:25:46 2017 +0000
Revision:
1:ce626b794a6c
Parent:
0:489498e8dae5
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.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dudu941014 0:489498e8dae5 1 //////////////////////////////////////////////////////////////////////////////////
dudu941014 0:489498e8dae5 2 // Company: edinburgh of university
dudu941014 0:489498e8dae5 3 // Engineer: ZEjun DU
dudu941014 0:489498e8dae5 4 //
dudu941014 0:489498e8dae5 5 // Create Date: 2017/08/20 13:06:52
dudu941014 0:489498e8dae5 6 // Design Name: Inverted Pendulum Balancer
dudu941014 0:489498e8dae5 7 // Module Name: basic PID algorithm
dudu941014 0:489498e8dae5 8 // Tool Versions: “Keil 5” or “Mbed Complie Online”
dudu941014 0:489498e8dae5 9 // Description: this part is to build the basic PID control theroy
dudu941014 0:489498e8dae5 10 //
dudu941014 0:489498e8dae5 11 //
dudu941014 0:489498e8dae5 12 //////////////////////////////////////////////////////////////////////////////////
dudu941014 0:489498e8dae5 13
dudu941014 0:489498e8dae5 14 #include "mbed.h"
dudu941014 0:489498e8dae5 15
dudu941014 0:489498e8dae5 16 ////////////////////////////////////////////////////////////
dudu941014 0:489498e8dae5 17 //the D term is very sensitive, this part is to filter the D term
dudu941014 0:489498e8dae5 18 //but the effect of this function is not obvious
dudu941014 0:489498e8dae5 19 ////////////////////////////////////////////////////////////
dudu941014 0:489498e8dae5 20
dudu941014 0:489498e8dae5 21 // Examples for _filter:
dudu941014 0:489498e8dae5 22 // f_cut = 10 Hz -> _filter = 15.9155e-3
dudu941014 0:489498e8dae5 23 // f_cut = 15 Hz -> _filter = 10.6103e-3
dudu941014 0:489498e8dae5 24 // f_cut = 20 Hz -> _filter = 7.9577e-3
dudu941014 0:489498e8dae5 25 // f_cut = 25 Hz -> _filter = 6.3662e-3
dudu941014 0:489498e8dae5 26 // f_cut = 30 Hz -> _filter = 5.3052e-3
dudu941014 0:489498e8dae5 27 #define PID_D_TERM_FILTER 0.00795770f // 20hz filter on D term
dudu941014 0:489498e8dae5 28 //#define PID_D_TERM_FILTER 0.0063662f // 30hz filter on D term
dudu941014 0:489498e8dae5 29 //#define PID_D_TERM_FILTER 0.0f // no filter on D term
dudu941014 0:489498e8dae5 30
dudu941014 0:489498e8dae5 31 typedef struct
dudu941014 0:489498e8dae5 32 {
dudu941014 0:489498e8dae5 33 float kp;
dudu941014 0:489498e8dae5 34 float ki;
dudu941014 0:489498e8dae5 35 float kd;
dudu941014 0:489498e8dae5 36 float desired;
dudu941014 0:489498e8dae5 37 float prevError;
dudu941014 0:489498e8dae5 38 float error;
dudu941014 0:489498e8dae5 39 float integ;
dudu941014 0:489498e8dae5 40 float deri;
dudu941014 0:489498e8dae5 41 float prevDeri;
dudu941014 0:489498e8dae5 42 float outP;
dudu941014 0:489498e8dae5 43 float outI;
dudu941014 0:489498e8dae5 44 float outD;
dudu941014 0:489498e8dae5 45 float iLimit;
dudu941014 0:489498e8dae5 46 float iLimitLow;
dudu941014 0:489498e8dae5 47 float dt;
dudu941014 0:489498e8dae5 48 }PidObject;
dudu941014 0:489498e8dae5 49
dudu941014 0:489498e8dae5 50 float show_I_term(void);
dudu941014 0:489498e8dae5 51 void pidInit(PidObject* pid, const float desired, const float kp,const float ki, const float kd, const float iLimit, const float dt);
dudu941014 0:489498e8dae5 52 float get_p(PidObject* pid);
dudu941014 0:489498e8dae5 53 float get_i(PidObject* pid, float distance);
dudu941014 0:489498e8dae5 54 float get_d(PidObject* pid);
dudu941014 0:489498e8dae5 55
dudu941014 0:489498e8dae5 56 float get_pid_vel(PidObject* pid, float distance);
dudu941014 0:489498e8dae5 57
dudu941014 0:489498e8dae5 58 float get_pid(PidObject* pid);
dudu941014 0:489498e8dae5 59 void pidReset(PidObject* pid);
dudu941014 0:489498e8dae5 60 void pidSetError(PidObject* pid, const float error);
dudu941014 0:489498e8dae5 61 void pidSetDesired(PidObject* pid, const float desired);