From a1ede74749c5b4bf25ba3a58866838ee02da4d43 Mon Sep 17 00:00:00 2001 From: Mateusz Lubecki Date: Thu, 20 Oct 2022 09:06:54 +0200 Subject: [PATCH] temperature handling refactoring --- STM32L476_ParaMETEO/makefile | 1 + STM32L476_ParaMETEO/sources.mk | 1 + STM32L476_ParaMETEO/src/subdir.mk | 3 -- include/config_data.h | 13 +++++- include/rte_wx.h | 7 ++- src/config_data_default.c | 2 + src/main.c | 3 +- src/pwr_save.c | 2 +- src/rte_wx.c | 5 +- src/wx_handler.c | 5 ++ src/wx_handler_temperature.c | 58 ++++++----------------- {include => system/include}/crc_.h | 0 system/include/drivers/dallas.h | 13 +----- system/include/drivers/max31865.h | 4 +- system/include/float_average.h | 25 ++++++++++ {src => system/src}/crc_.c | 0 system/src/drivers/dallas.c | 68 ++------------------------- system/src/drivers/max31865.c | 74 ++++++++++++++++++++++++++---- system/src/float_average.c | 73 +++++++++++++++++++++++++++++ 19 files changed, 213 insertions(+), 144 deletions(-) rename {include => system/include}/crc_.h (100%) create mode 100644 system/include/float_average.h rename {src => system/src}/crc_.c (100%) create mode 100644 system/src/float_average.c diff --git a/STM32L476_ParaMETEO/makefile b/STM32L476_ParaMETEO/makefile index 3e02206..480ef49 100644 --- a/STM32L476_ParaMETEO/makefile +++ b/STM32L476_ParaMETEO/makefile @@ -23,6 +23,7 @@ RM := rm -rf -include system/src/davis_vantage/subdir.mk -include system/src/cmsis/stm32l4xx/subdir.mk -include system/src/aprs/subdir.mk +-include system/src/subdir.mk -include src/subdir.mk -include subdir.mk -include objects.mk diff --git a/STM32L476_ParaMETEO/sources.mk b/STM32L476_ParaMETEO/sources.mk index d56fa66..30e9ba2 100644 --- a/STM32L476_ParaMETEO/sources.mk +++ b/STM32L476_ParaMETEO/sources.mk @@ -31,6 +31,7 @@ SUBDIRS := \ src \ system/src/aprs \ system/src/cmsis/stm32l4xx \ +system/src \ system/src/davis_vantage \ system/src/diag \ system/src/drivers \ diff --git a/STM32L476_ParaMETEO/src/subdir.mk b/STM32L476_ParaMETEO/src/subdir.mk index 3b2318a..1b4d136 100644 --- a/STM32L476_ParaMETEO/src/subdir.mk +++ b/STM32L476_ParaMETEO/src/subdir.mk @@ -14,7 +14,6 @@ C_SRCS += \ ../src/config_data_first.c \ ../src/config_data_second.c \ ../src/configuration_handler.c \ -../src/crc_.c \ ../src/delay.c \ ../src/dummy.c \ ../src/float_to_string.c \ @@ -47,7 +46,6 @@ OBJS += \ ./src/config_data_first.o \ ./src/config_data_second.o \ ./src/configuration_handler.o \ -./src/crc_.o \ ./src/delay.o \ ./src/dummy.o \ ./src/float_to_string.o \ @@ -80,7 +78,6 @@ C_DEPS += \ ./src/config_data_first.d \ ./src/config_data_second.d \ ./src/configuration_handler.d \ -./src/crc_.d \ ./src/delay.d \ ./src/dummy.d \ ./src/float_to_string.d \ diff --git a/include/config_data.h b/include/config_data.h index 9f01868..3b5cbb7 100644 --- a/include/config_data.h +++ b/include/config_data.h @@ -62,6 +62,12 @@ typedef struct __attribute__((aligned (4))) config_data_mode_t { uint8_t wx_dust_sensor; + /** + * 0x00 or 0xFF - PT sensor disabled + * bit0 - enabled / disabled + * bit1 - PT100 (0) or PT1000 (1) + * bit2 through bit7 - resistor value from lookup table + * */ uint8_t wx_pt_sensor; uint8_t victron; @@ -171,7 +177,12 @@ typedef enum config_data_wx_sources_enum_t { * the controller queries for average and maximum wind speed. */ WX_SOURCE_FULL_RTU = 4, - WX_SOURCE_DAVIS_SERIAL = 5 + WX_SOURCE_DAVIS_SERIAL = 5, + + /** + * This position is valid only for temperature + */ + WX_SOURCE_INTERNAL_PT100 = 6, } config_data_wx_sources_enum_t; typedef struct __attribute__((aligned (4))) config_data_wx_sources_t { diff --git a/include/rte_wx.h b/include/rte_wx.h index b56b41f..2b61000 100644 --- a/include/rte_wx.h +++ b/include/rte_wx.h @@ -21,6 +21,8 @@ #include "drivers/ms5611.h" #include "drivers/bme280.h" +#include "float_average.h" + #define WIND_AVERAGE_LEN 18 @@ -30,10 +32,7 @@ #define RTE_WX_MEASUREMENT_TEMPERATURE 100 #define RTE_WX_MEASUREMENT_PRESSUERE 300 -extern float rte_wx_temperature_external, rte_wx_temperature_external_valid; -extern float rte_wx_temperature_external_slew_rate; extern float rte_wx_temperature_average_external_valid; -extern float rte_wx_temperature_min_external_valid, rte_wx_temperature_max_external_valid; extern float rte_wx_temperature_internal, rte_wx_temperature_internal_valid; extern float rte_wx_pressure, rte_wx_pressure_valid; extern float rte_wx_pressure_history[PRESSURE_AVERAGE_LN]; @@ -77,7 +76,7 @@ extern int8_t rte_wx_humidity, rte_wx_humidity_valid; extern uint8_t rte_wx_tx20_excessive_slew_rate; extern dallas_qf_t rte_wx_current_dallas_qf, rte_wx_error_dallas_qf; -extern dallas_average_t rte_wx_dallas_average; +extern float_average_t rte_wx_dallas_average; extern ms5611_qf_t rte_wx_ms5611_qf; extern bme280_qf_t rte_wx_bme280_qf; extern analog_wind_qf_t rte_wx_wind_qf; diff --git a/src/config_data_default.c b/src/config_data_default.c index ac4c107..b4bb288 100644 --- a/src/config_data_default.c +++ b/src/config_data_default.c @@ -73,6 +73,8 @@ const config_data_mode_t __attribute__((section(".config_section_default.mode")) .wx_umb = 0, #endif + .wx_pt_sensor = 0x77, // TODO: + #ifdef _DUST_SDS011_SERIAL .wx_dust_sensor = WX_DUST_SDS011_SERIAL, #endif diff --git a/src/main.c b/src/main.c index d057ac8..6486791 100644 --- a/src/main.c +++ b/src/main.c @@ -789,7 +789,8 @@ int main(int argc, char* argv[]){ spi_init_full_duplex_pio(SPI_MASTER_MOTOROLA, CLOCK_REVERSED_RISING, SPI_SPEED_DIV256, SPI_ENDIAN_MSB); // initialize MAX RDT amplifier - max31865_init(MAX_3WIRE); + max31865_init(main_config_data_mode->wx_pt_sensor & 0x3, (main_config_data_mode->wx_pt_sensor & 0xFC) >> 2); + #endif // initialize GPIO pins leds are connecting to diff --git a/src/pwr_save.c b/src/pwr_save.c index c92b6f2..8808432 100644 --- a/src/pwr_save.c +++ b/src/pwr_save.c @@ -659,7 +659,7 @@ void pwr_save_pooling_handler(const config_data_mode_t * config, const config_da } #ifdef INHIBIT_CUTOFF - vbatt = 0xFFFFu; + vbatt = 0xFFFFu; // TODO:: THis shall not be uncommented on production!!! #endif if (vbatt > PWR_SAVE_STARTUP_RESTORE_VOLTAGE_DEF) { diff --git a/src/rte_wx.c b/src/rte_wx.c index c97267e..9a33eb4 100644 --- a/src/rte_wx.c +++ b/src/rte_wx.c @@ -21,10 +21,7 @@ * */ -float rte_wx_temperature_external = 0.0f, rte_wx_temperature_external_valid = 0.0f; -float rte_wx_temperature_external_slew_rate = 0.0f; float rte_wx_temperature_average_external_valid = 0.0f; -float rte_wx_temperature_min_external_valid = 0.0f, rte_wx_temperature_max_external_valid = 0.0f; float rte_wx_temperature_internal = 0.0f, rte_wx_temperature_internal_valid = 0.0f; float rte_wx_pressure = 0.0f, rte_wx_pressure_valid = 0.0f; float rte_wx_pressure_history[PRESSURE_AVERAGE_LN]; @@ -64,7 +61,7 @@ uint16_t rte_wx_winddirection_last = 0; int8_t rte_wx_humidity = 0, rte_wx_humidity_valid = 0; dallas_qf_t rte_wx_current_dallas_qf, rte_wx_error_dallas_qf = DALLAS_QF_UNKNOWN; -dallas_average_t rte_wx_dallas_average; +float_average_t rte_wx_dallas_average; ms5611_qf_t rte_wx_ms5611_qf = MS5611_QF_UNKNOWN; bme280_qf_t rte_wx_bme280_qf = BME280_QF_UKNOWN; analog_wind_qf_t rte_wx_wind_qf = AN_WIND_QF_UNKNOWN; diff --git a/src/wx_handler.c b/src/wx_handler.c index 119dd1a..5619f8f 100644 --- a/src/wx_handler.c +++ b/src/wx_handler.c @@ -22,6 +22,7 @@ #ifdef STM32L471xx #include #include +#include "drivers/max31865.h" #endif #include "drivers/analog_anemometer.h" @@ -65,6 +66,10 @@ void wx_check_force_i2c_reset(void) { if (wx_force_i2c_sensor_reset == 1) { wx_force_i2c_sensor_reset = 0; +#ifdef STM32L471xx + max31865_init(main_config_data_mode->wx_pt_sensor & 0x3, (main_config_data_mode->wx_pt_sensor & 0xFC) >> 2); +#endif + if (main_config_data_mode->wx_ms5611_or_bme == 0) { ms5611_reset(&rte_wx_ms5611_qf); ms5611_read_calibration(SensorCalData, &rte_wx_ms5611_qf); diff --git a/src/wx_handler_temperature.c b/src/wx_handler_temperature.c index 017c230..3fccbf2 100644 --- a/src/wx_handler_temperature.c +++ b/src/wx_handler_temperature.c @@ -20,8 +20,6 @@ #define WX_MAX_TEMPERATURE_SLEW_RATE 4.0f -uint8_t wx_inhibit_slew_rate_check = 1; - int32_t wx_get_temperature_measurement(const config_data_wx_sources_t * const config_sources, const config_data_mode_t * const config_mode, const config_data_umb_t * const config_umb, const config_data_rtu_t * const config_rtu) { @@ -62,6 +60,7 @@ int32_t wx_get_temperature_measurement(const config_data_wx_sources_t * const co } + // measure an external temperature using Dallas one wire sensor. // this function has blockin I/O which also adds a delay required by MS5611 // sensor to finish data acquisition after the pressure measurement @@ -102,7 +101,7 @@ int32_t wx_get_temperature_measurement(const config_data_wx_sources_t * const co // get the value read from RTU registers measurement_result = rtu_get_temperature(&temp, config_rtu); - rte_wx_temperature_external = (float)temp / 10.0f; + rte_wx_temperature_average_external_valid = (float)temp / 10.0f; // check if (measurement_result == MODBUS_RET_OK || measurement_result == MODBUS_RET_DEGRADED) { @@ -139,54 +138,27 @@ int32_t wx_get_temperature_dallas() { int32_t output = 0; + float temperature = 0.0f; + // get the value from dallas one-wire sensor - rte_wx_temperature_external = dallas_query(&rte_wx_current_dallas_qf); + temperature = dallas_query(&rte_wx_current_dallas_qf); // checking if communication was successfull - if (rte_wx_temperature_external != -128.0f) { - - // calculate the slew rate - rte_wx_temperature_external_slew_rate = rte_wx_temperature_external - rte_wx_temperature_external_valid; - - // chcecking the positive (ascending) slew rate of the temperature measuremenets - if (rte_wx_temperature_external_slew_rate > WX_MAX_TEMPERATURE_SLEW_RATE && wx_inhibit_slew_rate_check == 0) { - - // if temeperature measuremenet has changed more than maximum allowed slew rate set degradadet QF - rte_wx_error_dallas_qf = DALLAS_QF_DEGRADATED; - - // and increase the temperature only by 1.0f to decrease slew rate - rte_wx_temperature_external += 1.0f; - - } - - // chcecking the negaive (descending) slew rate of the temperature measuremenets - if (rte_wx_temperature_external_slew_rate < -WX_MAX_TEMPERATURE_SLEW_RATE && wx_inhibit_slew_rate_check == 0) { - - // if temeperature measuremenet has changed more than maximum allowed slew rate set degradadet QF - rte_wx_error_dallas_qf = DALLAS_QF_DEGRADATED; - - // and decrease the temperature only by 1.0f to decrease slew rate - rte_wx_temperature_external -= 1.0f; - - } - - // store current value - rte_wx_temperature_external_valid = rte_wx_temperature_external; + if (temperature != -128.0f) { // include current temperature into the average - dallas_average(rte_wx_temperature_external, &rte_wx_dallas_average); + float_average(temperature, &rte_wx_dallas_average); // update the current temperature with current average - rte_wx_temperature_average_external_valid = dallas_get_average(&rte_wx_dallas_average); - - // update current minimal temperature - rte_wx_temperature_min_external_valid = dallas_get_min(&rte_wx_dallas_average); - - // and update maximum also - rte_wx_temperature_max_external_valid = dallas_get_max(&rte_wx_dallas_average); + rte_wx_temperature_average_external_valid = float_get_average(&rte_wx_dallas_average); // updating last good measurement time wx_last_good_temperature_time = master_time; + +#if defined(STM32L471xx) + rte_wx_temperature_average_dallas = (int16_t)(rte_wx_temperature_average_external_valid * 10.0f); +#endif + } else { // if there were a communication error set the error to unavaliable @@ -196,10 +168,6 @@ int32_t wx_get_temperature_dallas() { output = -1; } -#if defined(STM32L471xx) - rte_wx_temperature_average_dallas = (int16_t)(rte_wx_temperature_average_external_valid * 10.0f); -#endif - return output; } diff --git a/include/crc_.h b/system/include/crc_.h similarity index 100% rename from include/crc_.h rename to system/include/crc_.h diff --git a/system/include/drivers/dallas.h b/system/include/drivers/dallas.h index 7b9f35b..54456bd 100644 --- a/system/include/drivers/dallas.h +++ b/system/include/drivers/dallas.h @@ -25,9 +25,9 @@ extern "C" { #include #endif +#include "float_average.h" #include "station_config.h" -#define DALLAS_AVERAGE_LN 9 #define DALLAS_INIT_VALUE -128.0f extern volatile int delay_5us; @@ -45,11 +45,6 @@ typedef struct dallas_struct_t { uint8_t shift; }dallas_struct_t; -typedef struct dallas_average_t { - float values[DALLAS_AVERAGE_LN]; - float *begin, *end, *current; -}dallas_average_t; - typedef enum dallas_qf_t { DALLAS_QF_UNKNOWN = 0, DALLAS_QF_FULL, @@ -57,7 +52,7 @@ typedef enum dallas_qf_t { DALLAS_QF_NOT_AVALIABLE }dallas_qf_t; -void dallas_init(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, uint16_t GPIO_PinSource, dallas_average_t* average); +void dallas_init(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, uint16_t GPIO_PinSource, float_average_t* average); void dallas_config_timer(void); void dallas_deconfig_timer(void); char dallas_reset(void); @@ -65,10 +60,6 @@ float dallas_query(dallas_qf_t *qf); void dallas_send_byte(char data); char dallas_receive_byte(void); uint8_t dallas_calculate_crc8(uint8_t *addr, uint8_t len); -void dallas_average(float in, dallas_average_t* average); -float dallas_get_average(const dallas_average_t* average); -float dallas_get_min(const dallas_average_t* average); -float dallas_get_max(const dallas_average_t* average); diff --git a/system/include/drivers/max31865.h b/system/include/drivers/max31865.h index da84066..0688174 100644 --- a/system/include/drivers/max31865.h +++ b/system/include/drivers/max31865.h @@ -13,13 +13,13 @@ #include #define MAX_3WIRE 3 -#define MAX_4WIRE 4 +#define MAX_4WIRE 1 typedef enum max31865_qf_t { MAX_QF_FULL }max31865_qf_t; -void max31865_init(uint8_t rdt_type); +void max31865_init(uint8_t rdt_type, uint8_t reference_resistor); void max31865_pool(void); int32_t max31865_get_pt100_result(max31865_qf_t * quality_factor); int32_t max31865_get_result(uint32_t RTDnominal); diff --git a/system/include/float_average.h b/system/include/float_average.h new file mode 100644 index 0000000..8bc79c8 --- /dev/null +++ b/system/include/float_average.h @@ -0,0 +1,25 @@ +/* + * float_average.h + * + * Created on: Oct 17, 2022 + * Author: mateusz + */ + +#ifndef INCLUDE_FLOAT_AVERAGE_H_ +#define INCLUDE_FLOAT_AVERAGE_H_ + +#define FLOAT_AVERAGE_LN 9 +#define FLOAT_INIT_VALUE -128.0f + + +typedef struct float_average_t { + float values[FLOAT_AVERAGE_LN]; + float *begin, *end, *current; +}float_average_t; + +void float_average(float in, float_average_t* average); +float float_get_average(const float_average_t* average); +float float_get_min(const float_average_t* average); +float float_get_max(const float_average_t* average); + +#endif /* INCLUDE_FLOAT_AVERAGE_H_ */ diff --git a/src/crc_.c b/system/src/crc_.c similarity index 100% rename from src/crc_.c rename to system/src/crc_.c diff --git a/system/src/drivers/dallas.c b/system/src/drivers/dallas.c index 98cdd59..18a1ffd 100644 --- a/system/src/drivers/dallas.c +++ b/system/src/drivers/dallas.c @@ -44,7 +44,7 @@ static void dallas_delay_start(void) { NVIC_EnableIRQ( TIM2_IRQn ); // enabling in case that it wasn't been enabled earlier } -void dallas_init(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, uint16_t GPIO_PinSource, dallas_average_t* average) { +void dallas_init(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, uint16_t GPIO_PinSource, float_average_t* average) { #ifdef STM32F10X_MD_VL GPIO_InitTypeDef GPIO_input; @@ -92,12 +92,12 @@ void dallas_init(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, uint16_t GPIO_PinSource #endif - for (int i = 0; i < DALLAS_AVERAGE_LN; i++) { - average->values[i] = DALLAS_INIT_VALUE; + for (int i = 0; i < FLOAT_AVERAGE_LN; i++) { + average->values[i] = FLOAT_INIT_VALUE; } average->current = average->values; average->begin = average->values; - average->end = average->values + DALLAS_AVERAGE_LN - 1; + average->end = average->values + FLOAT_AVERAGE_LN - 1; } void dallas_config_timer(void) { @@ -270,63 +270,3 @@ uint8_t dallas_calculate_crc8(uint8_t *addr, uint8_t len) { /* Return calculated CRC */ return crc; } - -void dallas_average(float in, dallas_average_t* average) { - *average->current = in; - - if (average->current == average->end) { - average->current = average->begin; - } - else { - average->current++; - } - -} - -float dallas_get_average(const dallas_average_t* average) { - float out = 0.0f; - uint8_t j = 0; - - for (int i = 0; i < DALLAS_AVERAGE_LN; i++) { - if (average->values[i] == DALLAS_INIT_VALUE) - continue; - - out += average->values[i]; - j++; - } - if (j > 0) { - out /= j; - return out; - } - else { - return DALLAS_INIT_VALUE; - } -} - -float dallas_get_min(const dallas_average_t* average) { - float out = 128.0f; - - for (int i = 0; i < DALLAS_AVERAGE_LN; i++) { - if (average->values[i] == DALLAS_INIT_VALUE) - continue; - - if (average->values[i] < out) - out = average->values[i]; - } - - return out; -} - -float dallas_get_max(const dallas_average_t* average) { - float out = -128.0f; - - for (int i = 0; i < DALLAS_AVERAGE_LN; i++) { - if (average->values[i] == DALLAS_INIT_VALUE) - continue; - - if (average->values[i] > out) - out = average->values[i]; - } - - return out; -} diff --git a/system/src/drivers/max31865.c b/system/src/drivers/max31865.c index ff69996..95dec93 100644 --- a/system/src/drivers/max31865.c +++ b/system/src/drivers/max31865.c @@ -16,6 +16,7 @@ int32_t test; typedef enum max31865_pool_state_t { + MAX_UNINITIALIZED, MAX_IDLE, MAX_INITIALIZED, MAX_ERROR, @@ -25,7 +26,49 @@ typedef enum max31865_pool_state_t { MAX_POWER_OFF }max31865_pool_state_t; -max31865_pool_state_t max31865_current_state = MAX_IDLE; +static const float max31865_rref_lookup_table[32] = +{ + 430.0f, + 432.0f, + 442.0f, + 470.0f, + 499.0f, + 510.0f, + 560.0f, + 620.0f, + 680.0f, + 768.0f, // 9 + 1000.0f, + 1100.0f, + 1200.0f, + 1300.0f, + 1400.0f, + 1500.0f, + 1600.0f, + 1740.0f, + 1800.0f, + 1910.0f, // 19 + 2000.0f, // 20 + 2100.0f, + 2400.0f, + 2700.0f, + 3000.0f, + 3090.0f, + 3400.0f, + 3900.0f, + 4300.0f, // 28 + 4700.0f, // 29 + 4990.0f, // 30 + 5600.0f // 31 + +}; + +max31865_pool_state_t max31865_current_state = MAX_UNINITIALIZED; + +/** + * reference resistor value + */ +float max31865_rref = 0; /** * This variable is incremented from 0 up to 9 to pause measurement @@ -154,16 +197,30 @@ static void max31865_send_config_register(void) { } } -void max31865_init(uint8_t rdt_type) { +void max31865_init(uint8_t rdt_type, uint8_t reference_resistor) { uint8_t * rx_data; if (rdt_type == MAX_3WIRE) { max31865_rdt_sensor_type = 1; } - else { + else if (rdt_type == MAX_4WIRE) { max31865_rdt_sensor_type = 0; } + else { + max31865_current_state = MAX_UNINITIALIZED; + + return; + } + + if (reference_resistor > 31) { + max31865_current_state = MAX_UNINITIALIZED; + + return; + } + else { + max31865_rref = max31865_rref_lookup_table[reference_resistor]; + } // set filter to 50Hz max31865_filter_select = 1; @@ -204,10 +261,10 @@ void max31865_pool(void) { case MAX_IDLE: // MAX31865 is powered up but not initialized if (max31865_rdt_sensor_type == 1) { - max31865_init(MAX_3WIRE); + max31865_init(MAX_3WIRE, max31865_rref); } else { - max31865_init(MAX_4WIRE); + max31865_init(MAX_4WIRE, max31865_rref); } if (max31865_ok == 1) { @@ -264,7 +321,7 @@ void max31865_pool(void) { max31865_raw_result = max31865_raw_result >> 1; //test = max31865_get_pt100_result(0); - test = max31865_get_result(100); +// test = max31865_get_result(100); } else { max31865_current_state = MAX_ERROR; @@ -291,6 +348,7 @@ void max31865_pool(void) { } break; + case MAX_UNINITIALIZED: case MAX_POWER_OFF: // supply voltage for MAX31865 is powered off and no communication // is currently possible @@ -302,7 +360,7 @@ int32_t max31865_get_pt100_result(max31865_qf_t * quality_factor) { int32_t temperature_scaled = 0; - float R_ohms = (max31865_raw_result * REFERENCE_RESISTOR) / 32768.0f; + float R_ohms = (max31865_raw_result * max31865_rref) / 32768.0f; float num, denom, T ; @@ -332,7 +390,7 @@ int32_t max31865_get_result(uint32_t RTDnominal) { Rt = max31865_raw_result; Rt /= 32768.0f; - Rt *= REFERENCE_RESISTOR; + Rt *= max31865_rref; // Serial.print("\nResistance: "); Serial.println(Rt, 8); diff --git a/system/src/float_average.c b/system/src/float_average.c new file mode 100644 index 0000000..3966331 --- /dev/null +++ b/system/src/float_average.c @@ -0,0 +1,73 @@ +/* + * float_average.c + * + * Created on: Oct 17, 2022 + * Author: mateusz + */ + +#include "float_average.h" +#include + +void float_average(float in, float_average_t* average) { + *average->current = in; + + if (average->current == average->end) { + average->current = average->begin; + } + else { + average->current++; + } + +} + +float float_get_average(const float_average_t* average) { + float out = 0.0f; + uint8_t j = 0; + + for (int i = 0; i < FLOAT_AVERAGE_LN; i++) { + if (average->values[i] == FLOAT_INIT_VALUE) { + continue; + } + + out += average->values[i]; + j++; + } + if (j > 0) { + out /= j; + return out; + } + else { + return FLOAT_INIT_VALUE; + } +} + +float float_get_min(const float_average_t* average) { + float out = 128.0f; + + for (int i = 0; i < FLOAT_AVERAGE_LN; i++) { + if (average->values[i] == FLOAT_INIT_VALUE) { + continue; + } + + if (average->values[i] < out) + out = average->values[i]; + } + + return out; +} + +float dallas_get_max(const float_average_t* average) { + float out = -128.0f; + + for (int i = 0; i < FLOAT_AVERAGE_LN; i++) { + if (average->values[i] == FLOAT_INIT_VALUE) { + continue; + } + + if (average->values[i] > out) + out = average->values[i]; + } + + return out; +} +