#include "PID_Lib.h"
void Reset_PID()
{
PID_Integrated = 0.0;
PID_Prev_Input = 0.0;
PID_First_Time = TRUE;
}
void Init_PID(float Kp, float Ki, float Kd, float MinOutput, float MaxOutput)
{
PID_Kp = Kp;
PID_Ki = Ki;
PID_Kd = Kd;
PID_MinOutput = MinOutput;
PID_MaxOutput = MaxOutput;
PID_Integrated = 0.0;
PID_Prev_Input = 0.0;
PID_First_Time = TRUE;
}
float PID_Calculate(float Setpoint, float InputValue)
{
float Err, ErrValue, DiffValue, Result;
Err = SetPoint - InputValue;
ErrValue = Err * PID_Kp;
PID_Integrated = PID_Integrated + (Err * PID_Ki);
if (PID_Integrated < PID_MinOutput)
PID_Integrated = PID_MinOutput;
if (PID_Integrated > PID_MaxOutput)
PID_Integrated = PID_MaxOutput;
if (PID_First_Time)
{
PID_First_Time = FALSE;
PID_Prev_Input = InputValue;
}
DiffValue = (InputValue - PID_Prev_Input) * PID_Kd;
PID_Prev_Input = InputValue;
if (Result < PID_MinOutput)
Result = PID_MinOutput;
if (Result > PID_MaxOutput)
Result = PID_MaxOutput;
return (Result);
}