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
Diff: PID/PID.cpp
- Revision:
- 1:ce626b794a6c
- Parent:
- 0:489498e8dae5
--- a/PID/PID.cpp Fri Aug 25 21:10:23 2017 +0000 +++ b/PID/PID.cpp Fri Aug 25 21:25:46 2017 +0000 @@ -95,30 +95,31 @@ { float output; - pid->outP = pid->kp * pid->error; //P + pid->outP = pid->kp * pid->error; //calculate the output of P_term according to PID control theroy //pid->outI += pid->ki * pid->error * pid->dt; //I - pid->outI = -pid->ki * distance * 10; + pid->outI = -pid->ki * distance * 10;//calculate the output of I_term according to PID control theroy + //this code is the difference between Velocity PID and Angle PID - if (pid->outI > pid->iLimit) + if (pid->outI > pid->iLimit)//this part is to limit the output of I_term { pid->outI = pid->iLimit; } - else if (pid->outI < pid->iLimitLow) + else if (pid->outI < pid->iLimitLow)//this part is to limit the output of I_term { pid->outI = pid->iLimitLow; } I_term_parameter = pid->outI; - pid->deri = (pid->error - pid->prevError) / pid->dt; //D + pid->deri = (pid->error - pid->prevError) / pid->dt; //calculate the output of D_term according to PID control theroy pid->prevError = pid->error; - pid->outD = pid->kd * pid->deri; + pid->outD = pid->kd * pid->deri;//calculate the output of D_term according to PID control theroy - output = pid->outP + pid->outI + pid->outD; + output = pid->outP + pid->outI + pid->outD;//calculate the P I D terms' output and returen the OUTPUT - return output; + return output;//returen the total OUTPUT }