vinayakdabholkar
Advanced Member level 4
Code:
#define BOOL unsigned char
#define TRUE 1
#define FALSE 0
float PID_Kp, PID_Ki, PID_Kd;
float PID_Integrated;
float PID_Prev_Input;
float PID_MinOutput, PID_MaxOutput;
BOOL PID_First_Time;
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;
// --- calculate proportional value ---
ErrValue = Err * PID_Kp;
// --- Calculate integrated value ---
PID_Integrated = PID_Integrated + (Err * PID_Ki);
// limit it to output minimum and maximum
if (PID_Integrated < PID_MinOutput)
PID_Integrated = PID_MinOutput;
if (PID_Integrated > PID_MaxOutput)
PID_Integrated = PID_MaxOutput;
// --- calculate derivative value ---
if (PID_First_Time)
{
// to avoid a huge DiffValue the first time (PID_Prev_Input = 0)
PID_First_Time = FALSE;
PID_Prev_Input = InputValue;
}
DiffValue = (InputValue - PID_Prev_Input) * PID_Kd;
PID_Prev_Input = InputValue;
// --- calculate total ---
Result = ErrValue + PID_Integrated - DiffValue; // mind the minus sign!!!
// limit it to output minimum and maximum
if (Result < PID_MinOutput)
Result = PID_MinOutput;
if (Result > PID_MaxOutput)
Result = PID_MaxOutput;
return (Result);
}
This is a PID algorithm which i am using in my digital compensator to control a buck converter. What i do is convert the analog output to digital value using the PIC18F4520. This digital value is passed to the PID routine along with a setpoint value. I have used 0-5V reference for ADC and my setpoint voltage is 1.2V. so if 5V is 1023 then 1.2V is 246. Based on this my output varies from 225-275. So the PID routine returns a value in the range of 225-275. This value i then map as per my requirements to set the duty cycle of the PWM module.
So for 275 duty should be zero, for 225 it should be 100% and in between for other values based on the calculations.Now i wanted to ask is how do i set the values of kp, ki and kd ? I know there are tuning methods that show steps for settings these values.I want to see how the output tracks the setpoint , the changes that occur when values of Kp, ki and kd are changed, as happens in MATLAB. I my self am not sure how to go about it . Can someone guide me here