ADC filtering in separate function + unit-tests

This commit is contained in:
Martin Jaeger
2019-11-04 18:02:43 +01:00
parent bc8f74aa45
commit 7412b11002
9 changed files with 99 additions and 32 deletions
+22 -18
View File
@@ -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
}
+4
View File
@@ -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 */
+11
View File
@@ -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
View File
@@ -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