Commit c691b42d authored by Antoine Boche's avatar Antoine Boche Committed by Clément Foucher
Browse files

Addition automatic ki calculation and small improvement

- Addition of automatic calculation of ki with the task period time since
the arm_pid function need ki*t and not ki for the initialisation.
- Small improvement for the anti WindUp calculation with arm functions
- Addition of a counter for the reset of the pid when the electrical source
is off to avoid inopportune reset with an error of measure.
- Addition of Vsat verification which is the minimum voltage required for the entry
to suit the Vref
parent 504530fe
......@@ -58,6 +58,10 @@ static float32_t Vref;
static float32_t V1_value, Vhigh_value;
static float32_t error_pid;
static arm_pid_instance_f32 PID_variables;
static uint32_t pid_period_us; //The period where the pid is calculated (used for Ki calculation)
static float32_t WindUp_sub, WindUp_mult; // Transition variables for arm calculation of the WindUp
static uint32_t Count_pid_reset; // Counter to reset the PID when calculation is off
static float32_t Vsat; // Represent the minimum voltage required for the entry to suit the Vref
static float32_t pid_out_windUp = 0.1; // stores the current pid output after anti windup and before saturation
static float32_t prev_pid_out = 0.1; // stores the previous unsaturated output
static float32_t pid_out; // stores the current pid_output after saturation and anti windUp (the effective duty cycle)
......@@ -76,17 +80,19 @@ static uint16_t pwm_period, pwm_phase_shift, pwm_low_pulse_width, pwm_high_pulse
* This function initializes all the parameters
* needed for the PID calculation for the buck topoligy
*/
void pid_init_buck(float32_t vref, float32_t kp, float32_t ki, float32_t kd)
void pid_init_buck(float32_t vref, float32_t kp, float32_t ki, float32_t kd, uint32_t task_period_us)
{
pwm_period = leg_period();
pwm_phase_shift = pwm_period / 2;
pwm_low_pulse_width = pwm_period * LOW_DUTY;
pwm_high_pulse_width = pwm_period * HIGH_DUTY;
pid_period_us = task_period_us;
Vref = vref;
Vsat = Vref/0.9; // Calculation of the minimum voltage required for the entry to suit the Vref
PID_variables.Kp = kp;
PID_variables.Ki = ki;
PID_variables.Ki = ki*pid_period_us/1000000.;
PID_variables.Kd = kd;
arm_pid_init_f32(&PID_variables, 1);
......@@ -96,16 +102,18 @@ void pid_init_buck(float32_t vref, float32_t kp, float32_t ki, float32_t kd)
* This function initializes all the parameters
* needed for the PID calculation for the boost topoligy
*/
void pid_init_boost(float32_t vref, float32_t kp, float32_t ki, float32_t kd)
void pid_init_boost(float32_t vref, float32_t kp, float32_t ki, float32_t kd, uint32_t task_period_us)
{
pwm_phase_shift = pwm_period / 2;
pwm_low_pulse_width = pwm_period * LOW_DUTY;
pwm_high_pulse_width = pwm_period * HIGH_DUTY;
pid_period_us = task_period_us;
Vref = vref;
Vsat = Vref/(1-0.9); // Calculation of the minimum voltage required for the entry to suit the Vref
PID_variables.Kp = kp;
PID_variables.Ki = ki;
PID_variables.Ki = ki*pid_period_us/1000000.;
PID_variables.Kd = kd;
arm_pid_init_f32(&PID_variables, 1);
......@@ -126,12 +134,14 @@ void pid_calculation_and_pwm_update()
Vhigh_value = get_value_converted(2 , 2);
arm_sub_f32(&Vref, &V1_value, &error_pid, 1); // CALCULATING THE ERROR BASED ON THE REFERENCE
if (Vhigh_value > Vref)
if (Vhigh_value > Vsat)
{
pid_out = arm_pid_f32(&PID_variables, error_pid); // PID CALCULATIONS
//PID anti WindUp saturation
pid_out_windUp = pid_out + Kw * (pwm_duty_cycle - prev_pid_out);
arm_sub_f32(&pwm_duty_cycle, &prev_pid_out, &WindUp_sub, 1);
arm_mult_f32(&WindUp_sub, &Kw, &WindUp_mult, 1);
arm_add_f32(&WindUp_mult, &pid_out, &pid_out_windUp, 1);
PID_variables.state[2] = pid_out_windUp;
pwm_duty_cycle = pid_out_windUp;
prev_pid_out = pid_out;
......@@ -160,14 +170,23 @@ void pid_calculation_and_pwm_update()
leg_set(TIMA, pwm_pulse_width, 0);
leg_set(TIMB, pwm_pulse_width, pwm_phase_shift);
}
Count_pid_reset = 0;
}
else
{
arm_pid_reset_q31(&PID_variables);
// Prevent from intempestive reset
if (Count_pid_reset > 100)
{
arm_pid_reset_f32(&PID_variables);
Count_pid_reset = 100;
}
pwm_duty_cycle = LOW_DUTY;
leg_set(TIMA, pwm_low_pulse_width, 0);
leg_set(TIMB, pwm_low_pulse_width, pwm_phase_shift);
Count_pid_reset++;
}
}
......
......@@ -40,7 +40,7 @@ extern "C" {
* @param[in] ki ki for PID
* @param[in] kd kd for PID
*/
void pid_init_buck(float32_t vref, float32_t kp, float32_t ki, float32_t kd);
void pid_init_buck(float32_t vref, float32_t kp, float32_t ki, float32_t kd, uint32_t task_period_us);
/**
* @brief This function Initialize all the parameters
......@@ -51,7 +51,7 @@ void pid_init_buck(float32_t vref, float32_t kp, float32_t ki, float32_t kd);
* @param[in] ki ki for PID
* @param[in] kd kd for PID
*/
void pid_init_boost(float32_t vref, float32_t kp, float32_t ki, float32_t kd);
void pid_init_boost(float32_t vref, float32_t kp, float32_t ki, float32_t kd, uint32_t task_period_us);
/**
* @brief This function calculation of the PID for the chosen
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment