From 4c1797aa24128ab4eb029728e10ed4edce4741d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Tue, 26 Jan 2021 21:59:41 +0100 Subject: [PATCH] Issue #24: Make UNSTABLE flag only go away, if the PT1000 value stays within the stable region for a given amount of samples --- stm-firmware/adc-meas.c | 11 ++++++++--- stm-firmware/include/reflow-controller/adc-meas.h | 6 ++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/stm-firmware/adc-meas.c b/stm-firmware/adc-meas.c index 3aef6b9..c598e42 100644 --- a/stm-firmware/adc-meas.c +++ b/stm-firmware/adc-meas.c @@ -214,7 +214,7 @@ void adc_pt1000_get_resistance_calibration(float *offset, float *sensitivity_dev static inline float adc_pt1000_apply_calibration(float raw_resistance) { if (calibration_active) - return (raw_resistance - pt1000_offset) * (1.0f + pt1000_sens_dev); + return (raw_resistance - pt1000_offset) * (1.0f + pt1000_sens_dev); else return raw_resistance; @@ -304,9 +304,11 @@ static inline __attribute__((optimize("O3"))) void adc_pt1000_filter(float adc_p float alpha; float res; static uint8_t old_state = 0; + static uint32_t stable_sample_counter = 0; res = ADC_TO_RES(adc_prefiltered_value); if (ABS(res - pt1000_res_raw_lf) >= ADC_PT1000_FILTER_UNSTABLE_DIFF) { + stable_sample_counter = 0; alpha = ADC_PT1000_FILTER_WEIGHT_FAST; if (old_state != 1) { safety_controller_report_error_with_key(ERR_FLAG_MEAS_ADC_UNSTABLE, MEAS_ADC_SAFETY_FLAG_KEY); @@ -315,8 +317,11 @@ static inline __attribute__((optimize("O3"))) void adc_pt1000_filter(float adc_p } else { alpha = filter_alpha; if (old_state != 2) { - safety_controller_ack_flag_with_key(ERR_FLAG_MEAS_ADC_UNSTABLE, MEAS_ADC_SAFETY_FLAG_KEY); - old_state = 2; + stable_sample_counter++; + if (stable_sample_counter >= ADC_PT1000_FILTER_STABLE_SAMPLE_COUNT) { + safety_controller_ack_flag_with_key(ERR_FLAG_MEAS_ADC_UNSTABLE, MEAS_ADC_SAFETY_FLAG_KEY); + old_state = 2; + } } } diff --git a/stm-firmware/include/reflow-controller/adc-meas.h b/stm-firmware/include/reflow-controller/adc-meas.h index 9e96b81..b7e8b35 100644 --- a/stm-firmware/include/reflow-controller/adc-meas.h +++ b/stm-firmware/include/reflow-controller/adc-meas.h @@ -49,6 +49,12 @@ */ #define ADC_PT1000_FILTER_UNSTABLE_DIFF 10 +/** + * @brief Sample count, the moving average filter has to be within @ref ADC_PT1000_FILTER_UNSTABLE_DIFF for the filter + * to be considered stable + */ +#define ADC_PT1000_FILTER_STABLE_SAMPLE_COUNT 200 + /** * @brief ADC channel number of PT1000 sensor input */