Commit 504530fe authored by Antoine Boche's avatar Antoine Boche
Browse files

Addition of antiwindup and negative error

Addition of an anti-windup at the end of the pid calculation to
avoid PID divergence.
Correction of the negative error calculation
Small optimization of the pid calculation time

Tested in V1.1.2 board 2 and board 4
Buck mode
Converged in 20ms
12V step response
24V Vhigh
parent 3bfc7923
......@@ -43,27 +43,94 @@
// ADC1
static float32_t raw_values_adc1[4]={0};
static float32_t raw_values_offset_removed_adc1[4]={0};
static float32_t raw_values_mult_by_gain_adc1[4]={0};
static float32_t converted_values_adc1[4]={0};
static float32_t gain_adc1[4] = {0.0125, 0.0107, 0.0125, 0};
static float32_t offset_adc1[4] = {2030.88, 2029.065, 2017.68, 0};
// ADC2
static float32_t raw_values_adc2[3]={0};
static float32_t raw_values_offset_removed_adc2[3]={0};
static float32_t raw_values_mult_by_gain_adc2[3]={0};
static float32_t converted_values_adc2[3]={0};
static float32_t gain_adc2[3] = {0.0462, 0.0355, 0.0668};
static float32_t offset_adc2[3] = {94.04, 2011.74, -4.1708};
static float32_t gain_adc1_triple_linearization[3] = {0.0439, 0.0261, 0.0453};
static float32_t offset_adc2_triple_linearization[3] = {2036.583, 1834.98, 2160.97};
/////
// Private Functions
void data_conversion_adc1()
{
for (uint16_t i=0; i<3; i++)
{
if(data_dispatch_get_values_available_in_adc1_channel(i)!=0)
{
// Data gathering from the buffer
raw_values_adc1[i] = (float32_t)data_dispatch_get_next_value_from_adc1_channel(i);
}
else
{
raw_values_adc1[i] = 0;
}
}
//Conversion to float for PID calculation
arm_mult_f32(&raw_values_adc1, &gain_adc1, &raw_values_mult_by_gain_adc1, 4);
arm_sub_f32(&raw_values_mult_by_gain_adc1, &offset_adc1, &converted_values_adc1, 4);
}
void data_conversion_adc2()
{
for (uint16_t i=0; i<3; i++)
{
if(data_dispatch_get_values_available_in_adc2_channel(i)!=0)
{
// Data gathering from the buffer
raw_values_adc2[i] = (float32_t)data_dispatch_get_next_value_from_adc2_channel(i);
}
else
{
raw_values_adc2[i] = 0;
}
}
//Conversion to float for PID calculation
arm_mult_f32(&raw_values_adc2, &gain_adc2, &raw_values_mult_by_gain_adc2, 3);
arm_sub_f32(&raw_values_mult_by_gain_adc2, &offset_adc2, &converted_values_adc2, 3);
}
/////
// Public Functions
/**
* This function allows to convert the raw data from the adc
* into the proper value it represents
*/
void data_conversion()
{
data_conversion_adc1();
data_conversion_adc2();
}
/**
* This function allow to get le value converted anywhere in the code
*/
float32_t get_value_converted(uint8_t adc_number, uint8_t adc_channel)
{
if (adc_number == 1)
{
return converted_values_adc1[adc_channel];
}
else if (adc_number == 2)
{
return converted_values_adc2[adc_channel];
}
else
{
return 0;
}
}
/**
* These functions update the paramters of the variables
* These functions update the parameters of the variables
*/
void v1_low_parameters_set(float32_t gain, float32_t offset)
{
......@@ -73,8 +140,8 @@ void v1_low_parameters_set(float32_t gain, float32_t offset)
void v2_low_parameters_set(float32_t gain, float32_t offset)
{
gain_adc2[1] = gain;
offset_adc2[1] = offset;
gain_adc1[0] = gain;
offset_adc1[0] = offset;
}
void v_high_parameters_set(float32_t gain, float32_t offset)
......@@ -85,8 +152,8 @@ void v_high_parameters_set(float32_t gain, float32_t offset)
void i1_low_parameters_set(float32_t gain, float32_t offset)
{
gain_adc1[0] = gain;
offset_adc1[0] = offset;
gain_adc2[1] = gain;
offset_adc2[1] = offset;
}
void i2_low_parameters_set(float32_t gain, float32_t offset)
......@@ -105,109 +172,4 @@ void temp_parameters_set(float32_t gain, float32_t offset)
{
gain_adc1[3] = gain;
offset_adc1[3] = offset;
}
/**
* This function select the right conversion equation
* depending of the voltage measured to solve the non linear response
* of V1 and V2 measurment component
*/
void triple_linerarization(float32_t value_adc, int8_t adc_channel)
{
if (value_adc < 2350)
{
gain_adc2[adc_channel] = gain_adc1_triple_linearization[0];
offset_adc2[adc_channel] = offset_adc2_triple_linearization[0];
}
else if ((value_adc >= 2350) && (value_adc <= 2609))
{
gain_adc2[adc_channel] = gain_adc1_triple_linearization[1];
offset_adc2[adc_channel] = offset_adc2_triple_linearization[1];
}
else if (value_adc > 2609)
{
gain_adc2[adc_channel] = gain_adc1_triple_linearization[2];
offset_adc2[adc_channel] = offset_adc2_triple_linearization[2];
}
}
/////
// Public Functions
/**
* This function allows to convert the raw data from the adc
* into the proper value it represents
*/
void data_conversion()
{
if(
// (data_dispatch_get_values_available_in_adc1_channel(0)!=0) &&
// (data_dispatch_get_values_available_in_adc1_channel(1)!=0) &&
// (data_dispatch_get_values_available_in_adc1_channel(2)!=0) &&
(data_dispatch_get_values_available_in_adc2_channel(0)!=0) //&&
// (data_dispatch_get_values_available_in_adc2_channel(1)!=0) &&
// (data_dispatch_get_values_available_in_adc2_channel(2)!=0)
)
{
// Data gathering from the buffer and conversion to float for PID calculation
// raw_values_adc1[0] = (float32_t)data_dispatch_get_next_value_from_adc1_channel(0);
// raw_values_adc1[1] = (float32_t)data_dispatch_get_next_value_from_adc1_channel(1);
// raw_values_adc1[2] = (float32_t)data_dispatch_get_next_value_from_adc1_channel(2);
raw_values_adc2[0] = (float32_t)data_dispatch_get_next_value_from_adc2_channel(0);
// raw_values_adc2[1] = (float32_t)data_dispatch_get_next_value_from_adc2_channel(1);
// raw_values_adc2[2] = (float32_t)data_dispatch_get_next_value_from_adc2_channel(2);
arm_mult_f32(&raw_values_adc2[0], &gain_adc2[0], &raw_values_offset_removed_adc2[0], 1);
arm_sub_f32(&raw_values_offset_removed_adc2[0], &offset_adc2[0], &converted_values_adc2[0], 1);
}
if(
// (data_dispatch_get_values_available_in_adc1_channel(0)!=0) &&
// (data_dispatch_get_values_available_in_adc1_channel(1)!=0) &&
// (data_dispatch_get_values_available_in_adc1_channel(2)!=0) &&
//(data_dispatch_get_values_available_in_adc2_channel(0)!=0) //&&
// (data_dispatch_get_values_available_in_adc2_channel(1)!=0) &&
(data_dispatch_get_values_available_in_adc2_channel(2)!=0)
)
{
// Data gathering from the buffer and conversion to float for PID calculation
raw_values_adc2[2] = (float32_t)data_dispatch_get_next_value_from_adc2_channel(2);
// Update of the linearization depending of the value
//triple_linerarization(raw_values_adc2[0], 0);
//triple_linerarization(raw_values_adc2[1], 1);
// Conversion of the raw ADC value in volt and ampere
// arm_sub_f32(raw_values_adc1, offset_adc1, raw_values_offset_removed_adc1, 4);
//arm_sub_f32(&raw_values_adc2[0], &offset_adc2[0], &raw_values_offset_removed_adc2[0], 1);
// arm_mult_f32(raw_values_offset_removed_adc1, gain_adc1, converted_values_adc1, 4);
//arm_mult_f32(&raw_values_offset_removed_adc2[0], &gain_adc2[0], &converted_values_adc2[0], 1);
arm_mult_f32(&raw_values_adc2[2], &gain_adc2[2], &raw_values_offset_removed_adc2[2], 1);
arm_sub_f32(&raw_values_offset_removed_adc2[2], &offset_adc2[2], &converted_values_adc2[2], 1);
}
}
/**
* This function allow to get le value converted anywhere in the code
*/
float32_t get_value_converted(uint8_t adc_number, uint8_t adc_channel)
{
if (adc_number == 1)
{
return converted_values_adc1[adc_channel];
}
else if (adc_number == 2)
{
return converted_values_adc2[adc_channel];
}
else
{
return 0;
}
}
}
\ No newline at end of file
......@@ -44,20 +44,60 @@ void data_conversion();
*/
float32_t get_value_converted(uint8_t adc_number, uint8_t adc_channel);
/**
* @brief Change the parameters for the data conversion of V1_low
*
* @param[in] gain gain of th sensor
* @param[in] offset offset of the sensor
*/
void v1_low_parameters_set(float32_t gain, float32_t offset);
/**
* @brief Change the parameters for the data conversion of V2_low
*
* @param[in] gain gain of th sensor
* @param[in] offset offset of the sensor
*/
void v2_low_parameters_set(float32_t gain, float32_t offset);
/**
* @brief Change the parameters for the data conversion of V_high
*
* @param[in] gain gain of th sensor
* @param[in] offset offset of the sensor
*/
void v_high_parameters_set(float32_t gain, float32_t offset);
/**
* @brief Change the parameters for the data conversion of I1_low
*
* @param[in] gain gain of th sensor
* @param[in] offset offset of the sensor
*/
void i1_low_parameters_set(float32_t gain, float32_t offset);
/**
* @brief Change the parameters for the data conversion of I2_low
*
* @param[in] gain gain of th sensor
* @param[in] offset offset of the sensor
*/
void i2_low_parameters_set(float32_t gain, float32_t offset);
/**
* @brief Change the parameters for the data conversion of I_high
*
* @param[in] gain gain of th sensor
* @param[in] offset offset of the sensor
*/
void i_high_parameters_set(float32_t gain, float32_t offset);
/**
* @brief Change the parameters for the data conversion of temp
*
* @param[in] gain gain of th sensor
* @param[in] offset offset of the sensor
*/
void temp_parameters_set(float32_t gain, float32_t offset);
......
......@@ -178,4 +178,4 @@ void test()
pwm_pulse_width = (0.5 * pwm_period);
leg_set(TIMA, pwm_pulse_width, 0);
leg_set(TIMB, pwm_pulse_width, pwm_phase_shift);
}
\ No newline at end of file
}
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