ADC filtering in separate function + unit-tests
This commit is contained in:
+22
-18
@@ -76,6 +76,27 @@ void calibrate_current_sensors()
|
||||
load_current_offset = -(float)(((adc_filtered[ADC_POS_I_LOAD] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) * ADC_GAIN_I_LOAD / 1000.0;
|
||||
}
|
||||
|
||||
void adc_update_value(unsigned int pos)
|
||||
{
|
||||
// low pass filter with filter constant c = 1/16
|
||||
// y(n) = c * x(n) + (c - 1) * y(n-1)
|
||||
|
||||
#if FEATURE_PWM_SWITCH == 1
|
||||
if (pos == ADC_POS_V_SOLAR || pos == ADC_POS_I_SOLAR) {
|
||||
// only read input voltage and current when switch is on or permanently off
|
||||
if (pwm_switch.signal_high() || pwm_switch.active() == false) {
|
||||
adc_filtered[pos] += (uint32_t)adc_readings[pos] - (adc_filtered[pos] >> ADC_FILTER_CONST);
|
||||
}
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
// adc_readings: 12-bit ADC values left-aligned in uint16_t
|
||||
adc_filtered[pos] += (uint32_t)adc_readings[pos] - (adc_filtered[pos] >> ADC_FILTER_CONST);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
void update_measurements()
|
||||
{
|
||||
@@ -231,26 +252,9 @@ extern "C" void DMA1_Channel1_IRQHandler(void)
|
||||
{
|
||||
if ((DMA1->ISR & DMA_ISR_TCIF1) != 0) // Test if transfer completed on DMA channel 1
|
||||
{
|
||||
// low pass filter with filter constant c = 1/16
|
||||
// y(n) = c * x(n) + (c - 1) * y(n-1)
|
||||
#ifdef CHARGER_TYPE_PWM
|
||||
for (unsigned int i = 0; i < NUM_ADC_CH; i++) {
|
||||
if (i == ADC_POS_V_SOLAR || i == ADC_POS_I_SOLAR) {
|
||||
// only read input voltage and current when switch is on or permanently off
|
||||
if (GPIOB->IDR & GPIO_PIN_1 || pwm_switch_enabled() == false) {
|
||||
adc_filtered[i] += (uint32_t)adc_readings[i] - (adc_filtered[i] >> ADC_FILTER_CONST);
|
||||
}
|
||||
}
|
||||
else {
|
||||
adc_filtered[i] += (uint32_t)adc_readings[i] - (adc_filtered[i] >> ADC_FILTER_CONST);
|
||||
}
|
||||
adc_update_value(i);
|
||||
}
|
||||
#else
|
||||
for (unsigned int i = 0; i < NUM_ADC_CH; i++) {
|
||||
// adc_readings: 12-bit ADC values left-aligned in uint16_t
|
||||
adc_filtered[i] += (uint32_t)adc_readings[i] - (adc_filtered[i] >> ADC_FILTER_CONST);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
DMA1->IFCR |= 0x0FFFFFFF; // clear all interrupt registers
|
||||
}
|
||||
|
||||
@@ -50,4 +50,8 @@ void adc_setup(void);
|
||||
*/
|
||||
void dma_setup(void);
|
||||
|
||||
/** Read, filter and check raw ADC readings stored by DMA controller
|
||||
*/
|
||||
void adc_update_value(unsigned int pos);
|
||||
|
||||
#endif /* ADC_DMA */
|
||||
|
||||
@@ -108,6 +108,11 @@ void pwm_signal_stop()
|
||||
_pwm_active = false;
|
||||
}
|
||||
|
||||
bool pwm_signal_high()
|
||||
{
|
||||
return (GPIOB->IDR & GPIO_PIN_1);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
// dummy functions for unit tests
|
||||
@@ -117,6 +122,7 @@ void pwm_signal_duty_cycle_step(int delta) {;}
|
||||
void pwm_signal_init_registers(int freq_Hz) {;}
|
||||
void pwm_signal_start(float pwm_duty) {;}
|
||||
void pwm_signal_stop() {;}
|
||||
bool pwm_signal_high() { return false; }
|
||||
|
||||
#endif
|
||||
|
||||
@@ -125,6 +131,11 @@ bool PwmSwitch::active()
|
||||
return _pwm_active;
|
||||
}
|
||||
|
||||
bool PwmSwitch::signal_high()
|
||||
{
|
||||
return pwm_signal_high();
|
||||
}
|
||||
|
||||
PwmSwitch::PwmSwitch(PowerPort *pwm_terminal, PowerPort *pwm_port_int)
|
||||
{
|
||||
terminal = pwm_terminal;
|
||||
|
||||
+7
-1
@@ -41,12 +41,18 @@ public:
|
||||
*/
|
||||
void control();
|
||||
|
||||
/** Read the on/off status of the PWM switch
|
||||
/** Read the general on/off status of PWM switching
|
||||
*
|
||||
* @returns true if on
|
||||
*/
|
||||
bool active();
|
||||
|
||||
/** Read the current high or low state of the PWM signal
|
||||
*
|
||||
* @returns true if high, false if low
|
||||
*/
|
||||
bool signal_high();
|
||||
|
||||
/** Read the currently set duty cycle
|
||||
*
|
||||
* @returns Duty cycle between 0.0 and 1.0
|
||||
|
||||
+15
-7
@@ -22,14 +22,22 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
extern uint16_t adc_readings[NUM_ADC_CH];
|
||||
extern uint32_t adc_filtered[NUM_ADC_CH];
|
||||
|
||||
|
||||
void prepare_adc_readings(adc_values_t values)
|
||||
void prepare_adc_readings(AdcValues values)
|
||||
{
|
||||
adc_filtered[ADC_POS_VREF_MCU] = (uint32_t)(1.224 / 3.3 * 4096) << (4 + ADC_FILTER_CONST);
|
||||
adc_filtered[ADC_POS_V_SOLAR] = (uint32_t)((values.solar_voltage / (ADC_GAIN_V_SOLAR)) / 3.3 * 4096) << (4 + ADC_FILTER_CONST);
|
||||
adc_filtered[ADC_POS_V_BAT] = (uint32_t)((values.battery_voltage / (ADC_GAIN_V_BAT)) / 3.3 * 4096) << (4 + ADC_FILTER_CONST);
|
||||
adc_filtered[ADC_POS_I_DCDC] = (uint32_t)((values.dcdc_current / (ADC_GAIN_I_DCDC)) / 3.3 * 4096) << (4 + ADC_FILTER_CONST);
|
||||
adc_filtered[ADC_POS_I_LOAD] = (uint32_t)((values.load_current / (ADC_GAIN_I_LOAD)) / 3.3 * 4096) << (4 + ADC_FILTER_CONST);
|
||||
adc_readings[ADC_POS_VREF_MCU] = (uint16_t)(1.224 / 3.3 * 4096) << 4;
|
||||
adc_readings[ADC_POS_V_SOLAR] = (uint16_t)((values.solar_voltage / (ADC_GAIN_V_SOLAR)) / 3.3 * 4096) << 4;
|
||||
adc_readings[ADC_POS_V_BAT] = (uint16_t)((values.battery_voltage / (ADC_GAIN_V_BAT)) / 3.3 * 4096) << 4;
|
||||
adc_readings[ADC_POS_I_DCDC] = (uint16_t)((values.dcdc_current / (ADC_GAIN_I_DCDC)) / 3.3 * 4096) << 4;
|
||||
adc_readings[ADC_POS_I_LOAD] = (uint16_t)((values.load_current / (ADC_GAIN_I_LOAD)) / 3.3 * 4096) << 4;
|
||||
}
|
||||
|
||||
void prepare_adc_filtered()
|
||||
{
|
||||
// initialize also filtered values
|
||||
for (int i = 0; i < NUM_ADC_CH; i++) {
|
||||
adc_filtered[i] = adc_readings[i] << ADC_FILTER_CONST;
|
||||
}
|
||||
}
|
||||
|
||||
+4
-2
@@ -25,6 +25,8 @@ typedef struct {
|
||||
float load_current;
|
||||
float bat_temperature;
|
||||
float internal_temperature;
|
||||
} adc_values_t;
|
||||
} AdcValues;
|
||||
|
||||
void prepare_adc_readings(adc_values_t values);
|
||||
void prepare_adc_readings(AdcValues values);
|
||||
|
||||
void prepare_adc_filtered();
|
||||
|
||||
@@ -5,7 +5,34 @@
|
||||
|
||||
#include "main.h"
|
||||
|
||||
static adc_values_t adcval;
|
||||
static AdcValues adcval;
|
||||
|
||||
extern uint32_t adc_filtered[NUM_ADC_CH];
|
||||
|
||||
// testing only for 2 values
|
||||
void check_filtering()
|
||||
{
|
||||
// reset values
|
||||
adc_filtered[ADC_POS_VREF_MCU] = 0;
|
||||
adc_filtered[ADC_POS_V_SOLAR] = 0;
|
||||
|
||||
// collect 1000 samples to update filtered values
|
||||
for (int s = 0; s < 1000; s++) {
|
||||
for (int i = 0; i < NUM_ADC_CH; i++) {
|
||||
adc_update_value(i);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t adc_filtered_bak[NUM_ADC_CH];
|
||||
adc_filtered_bak[ADC_POS_VREF_MCU] = adc_filtered[ADC_POS_VREF_MCU];
|
||||
adc_filtered_bak[ADC_POS_V_SOLAR] = adc_filtered[ADC_POS_V_SOLAR];
|
||||
|
||||
// overwrite filtered values
|
||||
prepare_adc_filtered();
|
||||
|
||||
TEST_ASSERT_EQUAL(adc_filtered[ADC_POS_VREF_MCU], adc_filtered_bak[ADC_POS_VREF_MCU]);
|
||||
TEST_ASSERT_EQUAL(adc_filtered[ADC_POS_V_SOLAR], adc_filtered_bak[ADC_POS_V_SOLAR]);
|
||||
}
|
||||
|
||||
void check_solar_terminal_readings()
|
||||
{
|
||||
@@ -53,11 +80,13 @@ void adc_tests()
|
||||
adcval.solar_voltage = 30;
|
||||
prepare_adc_readings(adcval);
|
||||
|
||||
UNITY_BEGIN();
|
||||
|
||||
RUN_TEST(check_filtering);
|
||||
|
||||
// call original update_measurements function
|
||||
update_measurements();
|
||||
|
||||
UNITY_BEGIN();
|
||||
|
||||
RUN_TEST(check_solar_terminal_readings);
|
||||
RUN_TEST(check_bat_terminal_readings);
|
||||
RUN_TEST(check_load_terminal_readings);
|
||||
@@ -108,6 +108,7 @@ void dev_stat_new_bat_temp_max()
|
||||
|
||||
void dev_stat_new_int_temp_max()
|
||||
{
|
||||
dev_stat.int_temp_max = 20;
|
||||
dev_stat.internal_temp = 22;
|
||||
dev_stat.update_min_max_values();
|
||||
TEST_ASSERT_EQUAL(22, dev_stat.int_temp_max);
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
#include "main.h"
|
||||
|
||||
static adc_values_t adcval;
|
||||
static AdcValues adcval;
|
||||
|
||||
const float dcdc_current_sun = 3;
|
||||
const float load_current = 1;
|
||||
@@ -38,6 +38,7 @@ void energy_calculation_init()
|
||||
|
||||
// insert values into ADC functions
|
||||
prepare_adc_readings(adcval);
|
||||
prepare_adc_filtered();
|
||||
update_measurements();
|
||||
|
||||
for (int i = 0; i < 60*60*sun_hours; i++) {
|
||||
@@ -49,6 +50,7 @@ void energy_calculation_init()
|
||||
// disable DC/DC = solar charging
|
||||
adcval.dcdc_current = 0;
|
||||
prepare_adc_readings(adcval);
|
||||
prepare_adc_filtered();
|
||||
update_measurements();
|
||||
|
||||
for (int i = 0; i < 60*60*night_hours; i++) {
|
||||
|
||||
Reference in New Issue
Block a user