Commit a8639c6c authored by Clément Foucher's avatar Clément Foucher
Browse files

Use the new Data Acquisition API.

Proceed to modules configuration here so that the specific needs of this library are fully controlled.
Simplify data conversion as only two channels are required.
parent dbbfda61
{
"name": "opalib_pid_voltage",
"version": "2.0.0",
"version": "2.1.0",
"description": "OwnTech Power API Library for PID in voltage mode.",
"keywords": "OwnTech Power API"
}
......@@ -19,6 +19,7 @@
/**
* @author Antoine Boche <antoine.boche@laas.fr>
* @author Clément Foucher <clement.foucher@laas.fr>
*/
/////
......@@ -42,91 +43,25 @@
// ADC convertions variable
// ADC1
static float32_t raw_values_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_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};
/////
// 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()
float32_t data_conversion_convert_v1_low(uint16_t raw_value)
{
data_conversion_adc1();
data_conversion_adc2();
return (raw_value*gain_adc2[0])-offset_adc2[0];
}
/**
* 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)
float32_t data_conversion_convert_v_high(uint16_t raw_value)
{
if (adc_number == 1)
{
return converted_values_adc1[adc_channel];
}
else if (adc_number == 2)
{
return converted_values_adc2[adc_channel];
}
else
{
return 0;
}
return (raw_value*gain_adc2[2])+offset_adc2[2];
}
/**
......@@ -172,4 +107,4 @@ void temp_parameters_set(float32_t gain, float32_t offset)
{
gain_adc1[3] = gain;
offset_adc1[3] = offset;
}
\ No newline at end of file
}
......@@ -19,6 +19,7 @@
/**
* @author Antoine Boche <antoine.boche@laas.fr>
* @author Clément Foucher <clement.foucher@laas.fr>
*/
#ifndef DATA_CONVERSION_H_
......@@ -30,20 +31,6 @@
extern "C" {
#endif
/**
* @brief Convert the raw data from the adc
* into the proper value it represents
*/
void data_conversion();
/**
* @brief Extract le value converted anywhere in the code
*
* @param[in] adc_number number of the adc
* @param[in] adc_channel channel of the adc
*/
float32_t get_value_converted(uint8_t adc_number, uint8_t adc_channel);
/**
* @brief Change the parameters for the data conversion of V1_low
*
......@@ -101,6 +88,9 @@ void i_high_parameters_set(float32_t gain, float32_t offset);
void temp_parameters_set(float32_t gain, float32_t offset);
float32_t data_conversion_convert_v1_low(uint16_t raw_value);
float32_t data_conversion_convert_v_high(uint16_t raw_value);
#ifdef __cplusplus
}
......
......@@ -34,6 +34,9 @@
// Drivers
#include "leg.h" // PWM management layer by inverter leg interface
#include "hrtim.h"
#include "data_acquisition.h"
#include "dac.h"
// Application
#include "data_conversion.h"
......@@ -60,7 +63,7 @@ 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 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
......@@ -73,6 +76,35 @@ static uint16_t pwm_pulse_width;
static uint16_t pwm_period, pwm_phase_shift, pwm_low_pulse_width, pwm_high_pulse_width;
/////
// Private functions
static void _opalib_pid_init_modules()
{
uint8_t init_status;
// Initialize data acquisition
data_acquisition_init();
char* adc1_channels[] =
{
"V1_LOW",
"V_HIGH"
};
init_status = data_acquisition_configure_adc_channels(1, adc1_channels, 2);
__ASSERT( (init_status == 0), "ERROR: ADC 1 channel configuration went wrong!");
data_acquisition_configure_adc_trigger_source(1, LL_ADC_REG_TRIG_EXT_HRTIM_TRG1);
// HRTIM
hrtim_init_voltage();
// DAC
owntech_dac_dac2_constant_init();
}
/////
// Public Functions
......@@ -82,6 +114,8 @@ static uint16_t pwm_period, pwm_phase_shift, pwm_low_pulse_width, pwm_high_pulse
*/
void opalib_pid_voltage_init_buck(float32_t vref, float32_t kp, float32_t ki, float32_t kd, uint32_t task_period_us)
{
_opalib_pid_init_modules();
pwm_period = leg_period();
pwm_phase_shift = pwm_period / 2;
pwm_low_pulse_width = pwm_period * LOW_DUTY;
......@@ -96,6 +130,9 @@ void opalib_pid_voltage_init_buck(float32_t vref, float32_t kp, float32_t ki, fl
PID_variables.Kd = kd;
arm_pid_init_f32(&PID_variables, 1);
// Begin acquisition
data_acquisition_start();
}
/**
......@@ -104,6 +141,8 @@ void opalib_pid_voltage_init_buck(float32_t vref, float32_t kp, float32_t ki, fl
*/
void opalib_pid_voltage_init_boost(float32_t vref, float32_t kp, float32_t ki, float32_t kd, uint32_t task_period_us)
{
_opalib_pid_init_modules();
pwm_phase_shift = pwm_period / 2;
pwm_low_pulse_width = pwm_period * LOW_DUTY;
pwm_high_pulse_width = pwm_period * HIGH_DUTY;
......@@ -117,28 +156,53 @@ void opalib_pid_voltage_init_boost(float32_t vref, float32_t kp, float32_t ki, f
PID_variables.Kd = kd;
arm_pid_init_f32(&PID_variables, 1);
// Begin acquisition
data_acquisition_start();
}
/**
* This function calculation of the PID for the chosen
* This function calculation of the PID for the chosen
* topology and set the new duty cycle in the hrtim PWM
*/
void opalib_pid_voltage_calculation_and_pwm_update()
{
// Conversion of the raw data from measures
data_conversion();
// Acquire and convert data
uint32_t v1_low_count;
uint32_t v_high_count;
uint16_t* v1_low_buffer = data_acquisition_get_v1_low_values(&v1_low_count);
uint16_t* v_high_buffer = data_acquisition_get_v_high_values(&v_high_count);
if (v1_low_count > 0)
{
uint32_t raw_value = v1_low_buffer[v1_low_count-1];
V1_value = data_conversion_convert_v1_low(raw_value);
}
else
{
V1_value = 0;
}
if (v_high_count > 0)
{
uint32_t raw_value = v_high_buffer[v_high_count-1];
Vhigh_value = data_conversion_convert_v_high(raw_value);
}
else
{
Vhigh_value = 0;
}
// PID CALCUALTIONS
V1_value = get_value_converted(2 , 0);
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 > Vsat)
{
pid_out = arm_pid_f32(&PID_variables, error_pid); // PID CALCULATIONS
//PID anti WindUp saturation
//PID anti WindUp saturation
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);
......@@ -165,18 +229,18 @@ void opalib_pid_voltage_calculation_and_pwm_update()
}
else
{
{
pwm_pulse_width = (pwm_duty_cycle * pwm_period);
leg_set(TIMA, pwm_pulse_width, 0);
leg_set(TIMB, pwm_pulse_width, pwm_phase_shift);
}
leg_set(TIMB, pwm_pulse_width, pwm_phase_shift);
}
Count_pid_reset = 0;
}
else
{
// Prevent from intempestive reset
// Prevent from intempestive reset
if (Count_pid_reset > 100)
{
arm_pid_reset_f32(&PID_variables);
......@@ -185,16 +249,7 @@ void opalib_pid_voltage_calculation_and_pwm_update()
pwm_duty_cycle = LOW_DUTY;
leg_set(TIMA, pwm_low_pulse_width, 0);
leg_set(TIMB, pwm_low_pulse_width, pwm_phase_shift);
leg_set(TIMB, pwm_low_pulse_width, pwm_phase_shift);
Count_pid_reset++;
}
}
void test()
{
data_conversion();
pwm_pulse_width = (0.5 * pwm_period);
leg_set(TIMA, pwm_pulse_width, 0);
leg_set(TIMB, pwm_pulse_width, pwm_phase_shift);
}
}
......@@ -54,12 +54,11 @@ void opalib_pid_voltage_init_buck(float32_t vref, float32_t kp, float32_t ki, fl
void opalib_pid_voltage_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
* @brief This function calculation of the PID for the chosen
* topology and set the new duty cycle in the hrtim PWM
*/
void opalib_pid_voltage_calculation_and_pwm_update();
void test();
#ifdef __cplusplus
}
......
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