From 6fb028da2b2ca901e66f590699eae8e1b0d3d9ee Mon Sep 17 00:00:00 2001 From: Mateusz Lubecki Date: Fri, 23 Apr 2021 11:53:46 +0200 Subject: [PATCH] changed liker script to allocate less space for the stack to fix crashes with sprintf, refactoring of wx_handlers almost done --- Debug/src/subdir.mk | 6 + include/config_data.h | 2 +- include/wx_handler.h | 1 - include/wx_handler_humidity.h | 16 ++ include/wx_handler_pressure.h | 17 ++ include/wx_handler_temperature.h | 2 +- ldscripts/sections.ld | 2 +- src/config_data.c | 2 +- src/wx_handler.c | 372 +++++-------------------- src/wx_handler_humidity.c | 82 ++++++ src/wx_handler_pressure.c | 129 +++++++++ src/wx_handler_temperature.c | 83 ++---- system/include/umb_master/umb_master.h | 2 +- system/src/modbus_rtu/rtu_getters.c | 2 +- system/src/umb_master/umb_master.c | 6 +- 15 files changed, 358 insertions(+), 366 deletions(-) create mode 100644 include/wx_handler_humidity.h create mode 100644 include/wx_handler_pressure.h create mode 100644 src/wx_handler_humidity.c create mode 100644 src/wx_handler_pressure.c diff --git a/Debug/src/subdir.mk b/Debug/src/subdir.mk index 38c76d3..9bfe046 100644 --- a/Debug/src/subdir.mk +++ b/Debug/src/subdir.mk @@ -20,6 +20,8 @@ C_SRCS += \ ../src/rte_rtu.c \ ../src/rte_wx.c \ ../src/wx_handler.c \ +../src/wx_handler_humidity.c \ +../src/wx_handler_pressure.c \ ../src/wx_handler_temperature.c OBJS += \ @@ -39,6 +41,8 @@ OBJS += \ ./src/rte_rtu.o \ ./src/rte_wx.o \ ./src/wx_handler.o \ +./src/wx_handler_humidity.o \ +./src/wx_handler_pressure.o \ ./src/wx_handler_temperature.o C_DEPS += \ @@ -58,6 +62,8 @@ C_DEPS += \ ./src/rte_rtu.d \ ./src/rte_wx.d \ ./src/wx_handler.d \ +./src/wx_handler_humidity.d \ +./src/wx_handler_pressure.d \ ./src/wx_handler_temperature.d diff --git a/include/config_data.h b/include/config_data.h index e46fe20..6c202ee 100644 --- a/include/config_data.h +++ b/include/config_data.h @@ -133,7 +133,7 @@ typedef struct config_data_umb_t { uint16_t channel_temperature; - uint16_t channel_qfe; + uint16_t channel_qnh; /** * #define _UMB_CHANNEL_WINDSPEED 460 #define _UMB_CHANNEL_WINDGUSTS 440 diff --git a/include/wx_handler.h b/include/wx_handler.h index 5470e3c..4c1bc46 100644 --- a/include/wx_handler.h +++ b/include/wx_handler.h @@ -31,7 +31,6 @@ typedef enum wx_pwr_state_t { void wx_get_all_measurements(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); int32_t wx_get_bme280_temperature_pressure_humidity(float * const temperature, float * const pressure, int8_t * const humidity); -int32_t wx_get_ms5611_pressure(float * const pressure); void wx_pool_anemometer(void); void wx_pwr_init(void); void wx_pwr_periodic_handle(void); diff --git a/include/wx_handler_humidity.h b/include/wx_handler_humidity.h new file mode 100644 index 0000000..f45bd38 --- /dev/null +++ b/include/wx_handler_humidity.h @@ -0,0 +1,16 @@ +/* + * wx_handler_humidity.h + * + * Created on: Apr 15, 2021 + * Author: mateusz + */ + +#ifndef WX_HANDLER_HUMIDITY_H_ +#define WX_HANDLER_HUMIDITY_H_ + +#include "config_data.h" + +int32_t wx_get_humidity_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); +int32_t wx_get_humidity_bme280(int8_t * const humidity); + +#endif /* WX_HANDLER_HUMIDITY_H_ */ diff --git a/include/wx_handler_pressure.h b/include/wx_handler_pressure.h new file mode 100644 index 0000000..c9f05e5 --- /dev/null +++ b/include/wx_handler_pressure.h @@ -0,0 +1,17 @@ +/* + * wx_handler_pressure.h + * + * Created on: Apr 15, 2021 + * Author: mateusz + */ + +#ifndef WX_HANDLER_PRESSURE_H_ +#define WX_HANDLER_PRESSURE_H_ + +#include "config_data.h" + +int32_t wx_get_pressure_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); +int32_t wx_get_pressure_ms5611(float * const pressure); +int32_t wx_get_pressure_bme280(float * const pressure); + +#endif /* WX_HANDLER_PRESSURE_H_ */ diff --git a/include/wx_handler_temperature.h b/include/wx_handler_temperature.h index e49d096..1958967 100644 --- a/include/wx_handler_temperature.h +++ b/include/wx_handler_temperature.h @@ -13,6 +13,6 @@ 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); int32_t wx_get_temperature_dallas(void); int32_t wx_get_temperature_ms5611(float * const temperature); -int32_t wx_get_temperature_bme280(); +int32_t wx_get_temperature_bme280(float * const temperature); #endif /* WX_HANDLER_TEMPERATURE_H_ */ diff --git a/ldscripts/sections.ld b/ldscripts/sections.ld index 9ef139f..48086cc 100644 --- a/ldscripts/sections.ld +++ b/ldscripts/sections.ld @@ -18,7 +18,7 @@ _estack = __stack; /* STM specific definition */ * for the different modes. */ -__Main_Stack_Size = 1024 ; +__Main_Stack_Size = 512 ; PROVIDE ( _Main_Stack_Size = __Main_Stack_Size ) ; diff --git a/src/config_data.c b/src/config_data.c index 64aa8e0..580c128 100644 --- a/src/config_data.c +++ b/src/config_data.c @@ -251,7 +251,7 @@ const config_data_umb_t config_data_umb = { .channel_wingsusts = _UMB_CHANNEL_WINDGUSTS, .channel_winddirection = _UMB_CHANNEL_WINDDIRECTION, .channel_temperature = _UMB_CHANNEL_TEMPERATURE, - .channel_qfe = _UMB_CHANNEL_QFE + .channel_qnh = _UMB_CHANNEL_QFE #else .channel_windspeed = 0xFFFF, .channel_wingsusts = 0xFFFF, diff --git a/src/wx_handler.c b/src/wx_handler.c index 57efdf7..9865a9c 100644 --- a/src/wx_handler.c +++ b/src/wx_handler.c @@ -6,18 +6,19 @@ */ #include "wx_handler.h" +#include "wx_handler_humidity.h" +#include "wx_handler_pressure.h" +#include "wx_handler_temperature.h" #include #include #include #include -#include "drivers/ms5611.h" -#include "drivers/bme280.h" + #include "drivers/analog_anemometer.h" #include "station_config.h" - #include "modbus_rtu/rtu_getters.h" #include "modbus_rtu/rtu_return_values.h" @@ -28,14 +29,18 @@ #define WX_WATCHDOG_PERIOD (SYSTICK_TICKS_PER_SECONDS * SYSTICK_TICKS_PERIOD * 90) #define WX_WATCHDOG_RESET_DURATION (SYSTICK_TICKS_PER_SECONDS * SYSTICK_TICKS_PERIOD * 3) -//#define WX_MAX_TEMPERATURE_SLEW_RATE 4.0f - uint32_t wx_last_good_wind_time = 0; uint32_t wx_last_good_temperature_time = 0; wx_pwr_state_t wx_pwr_state; uint32_t wx_wind_pool_call_counter = 0; static const float direction_constant = M_PI/180.0f; +static const config_data_wx_sources_t internal = { + .temperature = WX_SOURCE_INTERNAL, + .pressure = WX_SOURCE_INTERNAL, + .humidity = WX_SOURCE_INTERNAL, + .wind = WX_SOURCE_INTERNAL +}; #define MODBUS_QF_TEMPERATURE_FULL 1 #define MODBUS_QF_TEMPERATURE_DEGR (1 << 1) @@ -45,304 +50,98 @@ static const float direction_constant = M_PI/180.0f; #define MODBUS_QF_PRESSURE_FULL (1 << 5) #define MODBUS_QF_PRESSURE_DEGR (1 << 6) + void wx_get_all_measurements(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) { int32_t return_value = 0; int32_t i = 0, j = 0; - float pressure_average_sum = 0.0f; int32_t function_result = -1; // used for return values from various functions - int8_t parameter_result = 0; // stores which parameters have been retrieved successfully. this is used for failsafe handling + int32_t parameter_result = 0; // stores which parameters have been retrieved successfully. this is used for failsafe handling + int32_t backup_parameter_result = 0; // uses during retrieving backup int8_t modbus_qf = 0; // quality factor for Modbus-RTU communication umb_qf_t umb_quality_factor = UMB_QF_UNITIALIZED; // wuality factor for UMB communication + parameter_result = wx_get_temperature_measurement(config_sources, config_mode, config_umb); + parameter_result = wx_get_pressure_measurement(config_sources, config_mode, config_umb); + parameter_result = wx_get_humidity_measurement(config_sources, config_mode, config_umb); - /** - * - * // add the current pressure into buffer - rte_wx_pressure_history[rte_wx_pressure_it++] = rte_wx_pressure; + // check if all parameters (except wind) were collected successfully + if (parameter_result == (WX_HANDLER_PARAMETER_RESULT_TEMPERATURE | WX_HANDLER_PARAMETER_RESULT_PRESSURE | WX_HANDLER_PARAMETER_RESULT_HUMIDITY | WX_HANDLER_PARAMETER_RESULT_TEMP_INTERNAL)) { + ; // if everything were OK do nothing + } + else { + // if not check what was faulty and backup with an internal sensor + if (parameter_result & WX_HANDLER_PARAMETER_RESULT_TEMPERATURE == 0) { + // if we don't have temperature + // check what is the primary source of temperature + if (config_sources->temperature != WX_SOURCE_INTERNAL) { + // if this is something different than an internal source use the internal sensor + backup_parameter_result |= wx_get_temperature_measurement(&internal, config_mode, config_umb); + } + else { + ; // + } + } - // reseting the average length iterator - j = 0; + if (parameter_result & WX_HANDLER_PARAMETER_RESULT_PRESSURE == 0) { - // check if and end of the buffer was reached - if (rte_wx_pressure_it >= PRESSURE_AVERAGE_LN) { - rte_wx_pressure_it = 0; - } + if (config_sources->pressure != WX_SOURCE_INTERNAL) { + backup_parameter_result |= wx_get_pressure_measurement(&internal, config_mode, config_umb); + } + } - // calculating the average of pressure measuremenets - for (i = 0; i < PRESSURE_AVERAGE_LN; i++) { + if (parameter_result & WX_HANDLER_PARAMETER_RESULT_HUMIDITY == 0) { - // skip empty slots in the history to provide proper value even for first wx packet - if (rte_wx_pressure_history[i] < 10.0f) { - continue; - } - - // add to the average - pressure_average_sum += rte_wx_pressure_history[i]; - - // increase the average lenght iterator - j++; - } - - rte_wx_pressure_valid = pressure_average_sum / (float)j; - * - * - */ - - if (config_mode->wx_umb == 1) { - if (rte_wx_umb_qf == UMB_QF_FULL) { - rte_wx_temperature_average_external_valid = umb_get_temperature(config_umb); - rte_wx_pressure_valid = umb_get_qfe(config_umb); + if (config_sources->pressure != WX_SOURCE_INTERNAL) { + backup_parameter_result |= wx_get_humidity_measurement(&internal, config_mode, config_umb); + } } } -#if !defined(_UMB_MASTER) && !defined(_DAVIS_SERIAL) && defined(_MODBUS_RTU) - #ifdef _RTU_SLAVE_TEMPERATURE_SOURCE - return_value = rtu_get_temperature(&rte_wx_temperature_external_dallas_valid); - - // if temperature has been uploaded by the modbus sensor correctly - if (return_value == MODBUS_RET_OK) { - // update the last measurement timestamp to prevent relay clicking - rte_wx_update_last_measuremenet_timers(RTE_WX_MEASUREMENT_TEMPERATURE); - - // set the first bit to signalize QF_FULL - modbus_qf |= MODBUS_QF_TEMPERATURE_FULL; - } - else if (return_value == MODBUS_RET_DEGRADED) { - // update the last measurement timestamp to prevent relay clicking - rte_wx_update_last_measuremenet_timers(RTE_WX_MEASUREMENT_TEMPERATURE); - - // set the second bit to signalize QF_DEGRADED - modbus_qf |= MODBUS_QF_TEMPERATURE_DEGR; - } - else { - // set third bit if there is something wrong (like not avaliable or - // not configured - modbus_qf |= MODBUS_QF_TEMPERATURE_NAVB; - } - #endif - - // modbus rtu HUMIDITY - #ifdef _RTU_SLAVE_HUMIDITY_SOURCE - return_value = rtu_get_humidity(&rte_wx_humidity_valid); - - // do simmilar things but for humidity - if (return_value == MODBUS_RET_OK) { - modbus_qf |= MODBUS_QF_HUMIDITY_FULL; - } - else if (return_value == MODBUS_RET_DEGRADED) { - modbus_qf |= MODBUS_QF_HUMIDITY_DEGR; - } - else { - ; - } - #endif - - // modbus rtu PRESSURE - #ifdef _RTU_SLAVE_PRESSURE_SOURCE - return_value = rtu_get_pressure(&rte_wx_pressure_valid); - - // do simmilar things but for pressure - if (return_value == MODBUS_RET_OK) { - modbus_qf |= MODBUS_QF_PRESSURE_FULL; - } - else if (return_value == MODBUS_RET_DEGRADED) { - modbus_qf |= MODBUS_QF_PRESSURE_DEGR; - } - else { - ; - } - #endif -#endif - -#if (!defined(_UMB_MASTER) && !defined(_DAVIS_SERIAL) && !defined(_MODBUS_RTU) && defined (_SENSOR_MS5611)) || (defined (_SENSOR_MS5611) && defined (_MODBUS_RTU)) - - wx_get_ms5611_temperature(); - -// // quering MS5611 sensor for temperature -// return_value = ms5611_get_temperature(&rte_wx_temperature_ms, &rte_wx_ms5611_qf); +//#if defined(_MODBUS_RTU) // -// if (return_value == MS5611_OK) { -// rte_wx_temperature_ms_valid = rte_wx_temperature_ms; +// // unify quality factor across Modbus-RTU sensor and embedded +// // ones. // -// } - -#endif - -#if (!defined(_UMB_MASTER) && !defined(_DAVIS_SERIAL) && !defined(_MODBUS_RTU) && defined (_SENSOR_BME280)) || (defined (_SENSOR_BME280) && defined (_MODBUS_RTU)) - - //wx_get_bme280_temperature_pressure_humidity(); - -// // reading raw values from BME280 sensor -// return_value = bme280_read_raw_data(bme280_data_buffer); -// -// if (return_value == BME280_OK) { -// -// // setting back the Quality Factor to FULL to trace any problems with sensor readouts -// rte_wx_bme280_qf = BME280_QF_FULL; -// -// // converting raw values to temperature -// bme280_get_temperature(&rte_wx_temperature_ms, bme280_get_adc_t(), &rte_wx_bme280_qf); -// -// // if modbus RTU is enabled but the quality factor for RTU-pressure is set to NOT_AVALIABLE -// if ((modbus_qf & MODBUS_QF_PRESSURE_FULL) == 0 && (modbus_qf & MODBUS_QF_PRESSURE_DEGR) == 0) { -// // converting raw values to pressure -// bme280_get_pressure(&rte_wx_pressure, bme280_get_adc_p(), &rte_wx_bme280_qf); +// // BME280 (or MS5611) has a prioryty over the Modbus-RTU +// if (rte_wx_bme280_qf == BME280_QF_NOT_AVAILABLE || +// rte_wx_bme280_qf == BME280_QF_UKNOWN) +// { +// // if an internal sensor is not responding or it is not used at all +// // check the result of modbus RTU. this is an a little bit of complicated +// // case as BME280 is a pressure and humidity sensor at once, so changing +// // this QF will also influence the pressure one, but at this point we might +// // agree that we won't use BME280 and external, RTU pressure sensor as it +// // would make no sense to do so +// if ((modbus_qf & MODBUS_QF_HUMIDITY_FULL) > 0) { +// rte_wx_bme280_qf = BME280_QF_FULL; // } // else { -// ; -// } -// -// // if modbus RTU is enabled but the quality factor for RTU-humidity is set to NOT_AVALIABLE -// if ((modbus_qf & MODBUS_QF_HUMIDITY_FULL) == 0 && (modbus_qf & MODBUS_QF_HUMIDITY_DEGR) == 0) { -// // converting raw values to humidity -// bme280_get_humidity(&rte_wx_humidity, bme280_get_adc_h(), &rte_wx_bme280_qf); -// } -// else { -// ; // if the RTU-humidity is set to FULL use that value instead of BME280 -// } -// -// if (rte_wx_bme280_qf == BME280_QF_FULL) { -// -// // always read the temperature as it is used as an internal temperature in 5th telemetry channel -// rte_wx_temperature_ms_valid = rte_wx_temperature_ms; -// -// // if modbus RTU is enabled but the quality factor for RTU-humidity is set to non FULL -// if ((modbus_qf & MODBUS_QF_HUMIDITY_FULL) == 0 && (modbus_qf & MODBUS_QF_HUMIDITY_DEGR) == 0) { -// rte_wx_humidity_valid = rte_wx_humidity; -// } -// else { -// ; // if humidity was obtained from RTU sensor use that value and do not bother with BME280 -// } -// -// if ((modbus_qf & MODBUS_QF_PRESSURE_FULL) == 0 && (modbus_qf & MODBUS_QF_PRESSURE_DEGR) == 0) { -// rte_wx_pressure_valid = rte_wx_pressure; -// -// // add the current pressure into buffer -// rte_wx_pressure_history[rte_wx_pressure_it++] = rte_wx_pressure; -// -// // reseting the average length iterator -// j = 0; -// -// // check if and end of the buffer was reached -// if (rte_wx_pressure_it >= PRESSURE_AVERAGE_LN) { -// rte_wx_pressure_it = 0; -// } -// -// // calculating the average of pressure measuremenets -// for (i = 0; i < PRESSURE_AVERAGE_LN; i++) { -// -// // skip empty slots in the history to provide proper value even for first wx packet -// if (rte_wx_pressure_history[i] < 10.0f) { -// continue; -// } -// -// // add to the average -// pressure_average_sum += rte_wx_pressure_history[i]; -// -// // increase the average lenght iterator -// j++; -// } -// -// rte_wx_pressure_valid = pressure_average_sum / (float)j; -// } +// rte_wx_bme280_qf = BME280_QF_UKNOWN; // } // } -// else { -// // set the quality factor is sensor is not responding on the i2c bus -// rte_wx_bme280_qf = BME280_QF_NOT_AVAILABLE; -// } -#endif - -#if (!defined(_UMB_MASTER) && !defined(_DAVIS_SERIAL) && !defined(_MODBUS_RTU)) || defined (_DALLAS_AS_TELEM) || defined (_MODBUS_RTU) //&& !defined(_RTU_SLAVE_TEMPERATURE_SOURCE) - - wx_get_dallas_temperature(); - - // enabling slew rate checking after first power up - //wx_inhibit_slew_rate_check = 0; -#endif - -#if (!defined(_UMB_MASTER) && !defined(_DAVIS_SERIAL) && !defined(_MODBUS_RTU) && defined (_SENSOR_MS5611)) || (defined (_SENSOR_MS5611) && defined (_MODBUS_RTU)) - wx_get_ms5611_pressure(); -// // quering MS5611 sensor for pressure -// return_value = ms5611_get_pressure(&rte_wx_pressure, &rte_wx_ms5611_qf); // -// if (return_value == MS5611_OK && (modbus_qf & MODBUS_QF_PRESSURE_FULL) == 0 && (modbus_qf & MODBUS_QF_PRESSURE_DEGR) == 0) { -// // add the current pressure into buffer -// rte_wx_pressure_history[rte_wx_pressure_it++] = rte_wx_pressure; +// // Dallas temperature qualiy factor +// if (rte_wx_error_dallas_qf == DALLAS_QF_NOT_AVALIABLE) { // -// // reseting the average length iterator -// j = 0; +// // if an internal sensor is not responding check the result of modbus RTU +// if ((modbus_qf & MODBUS_QF_TEMPERATURE_FULL) > 0) { +// rte_wx_error_dallas_qf = DALLAS_QF_UNKNOWN; +// rte_wx_current_dallas_qf = DALLAS_QF_FULL; // -// // check if and end of the buffer was reached -// if (rte_wx_pressure_it >= PRESSURE_AVERAGE_LN) { -// rte_wx_pressure_it = 0; // } -// -// // calculating the average of pressure measuremenets -// for (i = 0; i < PRESSURE_AVERAGE_LN; i++) { -// -// // skip empty slots in the history to provide proper value even for first wx packet -// if (rte_wx_pressure_history[i] < 10.0f) { -// continue; -// } -// -// // add to the average -// pressure_average_sum += rte_wx_pressure_history[i]; -// -// // increase the average lenght iterator -// j++; +// else if ((modbus_qf & MODBUS_QF_TEMPERATURE_DEGR) > 0) { +// rte_wx_error_dallas_qf = DALLAS_QF_DEGRADATED; +// rte_wx_current_dallas_qf = DALLAS_QF_DEGRADATED; +// } +// else if ((modbus_qf & MODBUS_QF_TEMPERATURE_NAVB) > 0) { +// rte_wx_error_dallas_qf = DALLAS_QF_DEGRADATED; +// rte_wx_current_dallas_qf = DALLAS_QF_NOT_AVALIABLE; // } -// -// rte_wx_pressure_valid = pressure_average_sum / (float)j; // } - -#endif - -#if defined(_MODBUS_RTU) - - // unify quality factor across Modbus-RTU sensor and embedded - // ones. - - // BME280 (or MS5611) has a prioryty over the Modbus-RTU - if (rte_wx_bme280_qf == BME280_QF_NOT_AVAILABLE || - rte_wx_bme280_qf == BME280_QF_UKNOWN) - { - // if an internal sensor is not responding or it is not used at all - // check the result of modbus RTU. this is an a little bit of complicated - // case as BME280 is a pressure and humidity sensor at once, so changing - // this QF will also influence the pressure one, but at this point we might - // agree that we won't use BME280 and external, RTU pressure sensor as it - // would make no sense to do so - if ((modbus_qf & MODBUS_QF_HUMIDITY_FULL) > 0) { - rte_wx_bme280_qf = BME280_QF_FULL; - } - else { - rte_wx_bme280_qf = BME280_QF_UKNOWN; - } - } - - // Dallas temperature qualiy factor - if (rte_wx_error_dallas_qf == DALLAS_QF_NOT_AVALIABLE) { - - // if an internal sensor is not responding check the result of modbus RTU - if ((modbus_qf & MODBUS_QF_TEMPERATURE_FULL) > 0) { - rte_wx_error_dallas_qf = DALLAS_QF_UNKNOWN; - rte_wx_current_dallas_qf = DALLAS_QF_FULL; - - } - else if ((modbus_qf & MODBUS_QF_TEMPERATURE_DEGR) > 0) { - rte_wx_error_dallas_qf = DALLAS_QF_DEGRADATED; - rte_wx_current_dallas_qf = DALLAS_QF_DEGRADATED; - } - else if ((modbus_qf & MODBUS_QF_TEMPERATURE_NAVB) > 0) { - rte_wx_error_dallas_qf = DALLAS_QF_DEGRADATED; - rte_wx_current_dallas_qf = DALLAS_QF_NOT_AVALIABLE; - } - } -#endif +//#endif @@ -354,7 +153,6 @@ int32_t wx_get_bme280_temperature_pressure_humidity(float * const temperature, f int i = 0, j = 0; int32_t return_value = 0; - float pressure_average_sum = 0.0f; // reading raw values from BME280 sensor return_value = bme280_read_raw_data(bme280_data_buffer); @@ -365,17 +163,14 @@ int32_t wx_get_bme280_temperature_pressure_humidity(float * const temperature, f rte_wx_bme280_qf = BME280_QF_FULL; // converting raw values to temperature - bme280_get_temperature(temperature, bme280_get_adc_t(), &rte_wx_bme280_qf); + //bme280_get_temperature(temperature, bme280_get_adc_t(), &rte_wx_bme280_qf); // if modbus RTU is enabled but the quality factor for RTU-pressure is set to NOT_AVALIABLE - bme280_get_pressure(pressure, bme280_get_adc_p(), &rte_wx_bme280_qf); + //bme280_get_pressure(pressure, bme280_get_adc_p(), &rte_wx_bme280_qf); // if modbus RTU is enabled but the quality factor for RTU-humidity is set to NOT_AVALIABLE - bme280_get_humidity(humidity, bme280_get_adc_h(), &rte_wx_bme280_qf); + //bme280_get_humidity(humidity, bme280_get_adc_h(), &rte_wx_bme280_qf); - if (rte_wx_bme280_qf == BME280_QF_FULL) { - ; - } } else { // set the quality factor is sensor is not responding on the i2c bus @@ -386,23 +181,6 @@ int32_t wx_get_bme280_temperature_pressure_humidity(float * const temperature, f } -int32_t wx_get_ms5611_pressure(float * const pressure) { - - int32_t return_value = 0; - int i = 0, j = 0; - float pressure_average_sum = 0.0f; - - // quering MS5611 sensor for pressure - return_value = ms5611_get_pressure(&pressure, &rte_wx_ms5611_qf); - - if (return_value == MS5611_OK) { - ; - } - - return return_value; - -} - void wx_pool_anemometer(void) { // locals diff --git a/src/wx_handler_humidity.c b/src/wx_handler_humidity.c new file mode 100644 index 0000000..1a70c67 --- /dev/null +++ b/src/wx_handler_humidity.c @@ -0,0 +1,82 @@ +/* + * wx_handler_humidity.c + * + * Created on: Apr 15, 2021 + * Author: mateusz + */ + +#include "wx_handler_humidity.h" +#include + +#include "rte_wx.h" + +#include +#include + +int32_t wx_get_humidity_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) { + + int32_t out = 0; + int32_t measurement_result = 0; + + // choose a temperature source from the configuration + switch(config_sources->humidity) { + case WX_SOURCE_INTERNAL: { + + if (config_mode->wx_ms5611_or_bme == 0) { + // MS5611 is chosen + // + + // but MS5611 isn't a humidity sensor + ; + } + else { + // BME280 + measurement_result = wx_get_humidity_bme280(&rte_wx_humidity); + + // if humidity has been retrieved successfully + if (measurement_result == BME280_OK) { + rte_wx_humidity_valid = rte_wx_humidity; + } + } + + break; + } + case WX_SOURCE_UMB: { + break; + } + case WX_SOURCE_RTU: + case WX_SOURCE_FULL_RTU: { + + // get the value read from RTU registers + measurement_result = rtu_get_humidity(&rte_wx_humidity); + + // check + if (measurement_result == MODBUS_RET_OK || measurement_result == MODBUS_RET_DEGRADED) { + + // set the flag that external temperature is available + out |= WX_HANDLER_PARAMETER_RESULT_HUMIDITY; + + if (measurement_result == BME280_OK) { + rte_wx_humidity_valid = rte_wx_humidity; + } + } + + break; + } + case WX_SOURCE_DAVIS_SERIAL: + break; + } + + return out; +} + +int32_t wx_get_humidity_bme280(int8_t * const humidity) { + int32_t output = 0; + + if (rte_wx_bme280_qf == BME280_QF_FULL) { + + output = bme280_get_humidity(humidity, bme280_get_adc_h(), &rte_wx_bme280_qf); + } + + return output; +} diff --git a/src/wx_handler_pressure.c b/src/wx_handler_pressure.c new file mode 100644 index 0000000..42e18a1 --- /dev/null +++ b/src/wx_handler_pressure.c @@ -0,0 +1,129 @@ +/* + * wx_handler_pressure.c + * + * Created on: Apr 15, 2021 + * Author: mateusz + */ + +#include "wx_handler_pressure.h" +#include "wx_handler.h" + +#include "rte_wx.h" +#include "main.h" + +#include +#include + +int32_t wx_get_pressure_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) { + + int32_t output = 0; + int32_t measurement_retval = 0; + int32_t i = 0, j = 0; + float pressure_average_sum = 0.0f; + umb_qf_t umb_quality_factor = UMB_QF_UNITIALIZED; // quality factor for UMB communication + + switch(config_sources->temperature) { + case WX_SOURCE_INTERNAL: { + + // switch between MS5611 and BME280 depends on user configuration + if (config_mode->wx_ms5611_or_bme == 1) { + measurement_retval = wx_get_pressure_bme280(&rte_wx_pressure); + } + else { + measurement_retval = wx_get_pressure_ms5611(&rte_wx_pressure); + } + + // check if pressure has been retrieved correctly + if (measurement_retval == BME280_OK || measurement_retval == MS5611_OK) { + // BME280 measures all three things at one call to the driver + output |= WX_HANDLER_PARAMETER_RESULT_PRESSURE; + + // add the current pressure into buffer for average calculation + rte_wx_pressure_history[rte_wx_pressure_it++] = rte_wx_pressure; + + // reseting the average length iterator + j = 0; + + // check if and end of the buffer was reached + if (rte_wx_pressure_it >= PRESSURE_AVERAGE_LN) { + rte_wx_pressure_it = 0; + } + + // calculating the average of pressure measuremenets + for (i = 0; i < PRESSURE_AVERAGE_LN; i++) { + + // skip empty slots in the history to provide proper value even for first wx packet + if (rte_wx_pressure_history[i] < 10.0f) { + continue; + } + + // add to the average + pressure_average_sum += rte_wx_pressure_history[i]; + + // increase the average lenght iterator + j++; + } + + rte_wx_pressure_valid = pressure_average_sum / (float)j; + + } + break; + } + case WX_SOURCE_UMB: { + // get current UMB bus quality factor + umb_quality_factor = umb_get_current_qf(&rte_wx_umb_context, master_time); + + // if there are any data collected from UMB sensors + if (umb_quality_factor == UMB_QF_FULL || umb_quality_factor == UMB_QF_DEGRADED) { + + // get the average pressure directly, there is no need for any further processing + rte_wx_pressure = umb_get_qnh(config_umb); + + // set the flag that external temperature is available + output |= WX_HANDLER_PARAMETER_RESULT_PRESSURE; + } + else { + // do nothing if no new data was received from UMB sensor in last 10 minutes + ; + } + + break; + } + case WX_SOURCE_RTU: + case WX_SOURCE_FULL_RTU: { + break; + } + case WX_SOURCE_DAVIS_SERIAL: + break; + } + + return output; + +} + +int32_t wx_get_pressure_ms5611(float * const pressure) { + + int32_t return_value = 0; + int i = 0, j = 0; + float pressure_average_sum = 0.0f; + + // quering MS5611 sensor for pressure + return_value = ms5611_get_pressure(pressure, &rte_wx_ms5611_qf); + + return return_value; + +} + +int32_t wx_get_pressure_bme280(float * const pressure) { + + int32_t retval = 0; + + // check if raw data is avaliable + if (rte_wx_bme280_qf == BME280_QF_FULL) { + retval = bme280_get_pressure(pressure, bme280_get_adc_p(), &rte_wx_bme280_qf); + } + + return retval; + +} + diff --git a/src/wx_handler_temperature.c b/src/wx_handler_temperature.c index cb41a9a..47e48b7 100644 --- a/src/wx_handler_temperature.c +++ b/src/wx_handler_temperature.c @@ -25,8 +25,8 @@ 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) { - int32_t function_result = -1; // used for return values from various functions - int8_t parameter_result = 0; // stores which parameters have been retrieved successfully. this is used for failsafe handling + int32_t measurement_result = -1; // used for return values from various functions + int32_t parameter_result = 0; // stores which parameters have been retrieved successfully. this is used for failsafe handling umb_qf_t umb_quality_factor = UMB_QF_UNITIALIZED; // wuality factor for UMB communication int32_t i = 0, j = 0; @@ -45,76 +45,31 @@ int32_t wx_get_temperature_measurement(const config_data_wx_sources_t * const co // the measuremenet won't be retrieved if (config_mode->wx_ms5611_or_bme == 1) { // this will get all three parameters (humidity, pressure, internal temp) in single call - function_result = wx_get_bme280_temperature_pressure_humidity(&rte_wx_temperature_internal, &rte_wx_pressure, &rte_wx_humidity); + measurement_result = wx_get_temperature_bme280(&rte_wx_temperature_internal); } else { // ms5611 is a bit different as the sensor (internal) temperature is collected separately from pressure - function_result = wx_get_ms5611_temperature(&rte_wx_temperature_internal); + measurement_result = wx_get_temperature_ms5611(&rte_wx_temperature_internal); } // check if temperature from pressure sensor has been retrieved w/o errors - if (function_result == BME280_OK || function_result == MS5611_OK) { + if (measurement_result == BME280_OK || measurement_result == MS5611_OK) { // set the flag for internal temperature - parameter_result |= WX_HANDLER_PARAMETER_RESULT_TEMP_INTERNAL; + parameter_result = parameter_result | WX_HANDLER_PARAMETER_RESULT_TEMP_INTERNAL; - // check which sensor is used once again - if (config_mode->wx_ms5611_or_bme == 1) { - // BME280 measures all three things at one call to the driver - parameter_result |= WX_HANDLER_PARAMETER_RESULT_PRESSURE; - parameter_result |= WX_HANDLER_PARAMETER_RESULT_HUMIDITY; - - // always read the temperature as it is used as an internal temperature in 5th telemetry channel - rte_wx_temperature_internal_valid = rte_wx_temperature_external; - - // set humidity - rte_wx_humidity_valid = rte_wx_humidity; - - // add the current pressure into buffer for average calculation - rte_wx_pressure_history[rte_wx_pressure_it++] = rte_wx_pressure; - - // reseting the average length iterator - j = 0; - - // check if and end of the buffer was reached - if (rte_wx_pressure_it >= PRESSURE_AVERAGE_LN) { - rte_wx_pressure_it = 0; - } - - // calculating the average of pressure measuremenets - for (i = 0; i < PRESSURE_AVERAGE_LN; i++) { - - // skip empty slots in the history to provide proper value even for first wx packet - if (rte_wx_pressure_history[i] < 10.0f) { - continue; - } - - // add to the average - pressure_average_sum += rte_wx_pressure_history[i]; - - // increase the average lenght iterator - j++; - } - - rte_wx_pressure_valid = pressure_average_sum / (float)j; - - } - else { - - ; // MS5611 measures pressure and temperature separately - } } // 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 // is triggered. - function_result = wx_get_dallas_temperature(); + measurement_result = wx_get_temperature_dallas(); // check if communication with dallas sensor has successed - if (function_result == 0) { + if (measurement_result == 0) { // if yes set the local variable with flag signalling that we have an external temperature - parameter_result |= WX_HANDLER_PARAMETER_RESULT_TEMPERATURE; + parameter_result = parameter_result | WX_HANDLER_PARAMETER_RESULT_TEMPERATURE; } break; @@ -130,7 +85,7 @@ int32_t wx_get_temperature_measurement(const config_data_wx_sources_t * const co rte_wx_temperature_average_external_valid = umb_get_temperature(config_umb); // set the flag that external temperature is available - parameter_result |= WX_HANDLER_PARAMETER_RESULT_TEMPERATURE; + parameter_result = parameter_result | WX_HANDLER_PARAMETER_RESULT_TEMPERATURE; } else { // do nothing if no new data was received from UMB sensor in last 10 minutes @@ -143,10 +98,10 @@ int32_t wx_get_temperature_measurement(const config_data_wx_sources_t * const co case WX_SOURCE_FULL_RTU: { // get the value read from RTU registers - function_result = rtu_get_temperature(&rte_wx_temperature_external); + measurement_result = rtu_get_temperature(&rte_wx_temperature_external); // check - if (function_result == MODBUS_RET_OK || function_result == MODBUS_RET_DEGRADED) { + if (measurement_result == MODBUS_RET_OK || measurement_result == MODBUS_RET_DEGRADED) { // set the flag that external temperature is available parameter_result |= WX_HANDLER_PARAMETER_RESULT_TEMPERATURE; @@ -159,7 +114,7 @@ int32_t wx_get_temperature_measurement(const config_data_wx_sources_t * const co } - return function_result; + return parameter_result; } int32_t wx_get_temperature_dallas() { @@ -230,9 +185,19 @@ int32_t wx_get_temperature_ms5611(float * const temperature) { int32_t return_value = 0; // quering MS5611 sensor for temperature - return_value = ms5611_get_temperature(&temperature, &rte_wx_ms5611_qf); + return_value = ms5611_get_temperature(temperature, &rte_wx_ms5611_qf); return return_value; } +int32_t wx_get_temperature_bme280(float * const temperature){ + int32_t output = 0; + + if (rte_wx_bme280_qf == BME280_QF_FULL) { + output = bme280_get_temperature(temperature, bme280_get_adc_t(), &rte_wx_bme280_qf); + } + + return output; +} + diff --git a/system/include/umb_master/umb_master.h b/system/include/umb_master/umb_master.h index 344adee..44e097a 100644 --- a/system/include/umb_master/umb_master.h +++ b/system/include/umb_master/umb_master.h @@ -30,7 +30,7 @@ uint16_t umb_get_windspeed(const config_data_umb_t * const config_umb); uint16_t umb_get_windgusts(const config_data_umb_t * const config_umb); int16_t umb_get_winddirection(const config_data_umb_t * const config_umb); float umb_get_temperature(const config_data_umb_t * const config_umb); -float umb_get_qfe(const config_data_umb_t * const config_umb); +float umb_get_qnh(const config_data_umb_t * const config_umb); #endif /* INCLUDE_UMB_MASTER_UMB_MASTER_H_ */ diff --git a/system/src/modbus_rtu/rtu_getters.c b/system/src/modbus_rtu/rtu_getters.c index f6fa7c5..ddd5ee9 100644 --- a/system/src/modbus_rtu/rtu_getters.c +++ b/system/src/modbus_rtu/rtu_getters.c @@ -576,7 +576,7 @@ int32_t rtu_get_humidity(int8_t* out) { // check when the value has been updated if (main_get_master_time() - last_update_timestam > RTU_MAXIMUM_VALUE_AGE || - physical_register_value > 99 || physical_register_value < 0) { + physical_register_value > 100 || physical_register_value < 0) { retval = MODBUS_RET_NOT_AVALIABLE; } else { diff --git a/system/src/umb_master/umb_master.c b/system/src/umb_master/umb_master.c index 005fa53..9a993e4 100644 --- a/system/src/umb_master/umb_master.c +++ b/system/src/umb_master/umb_master.c @@ -54,7 +54,7 @@ void umb_master_init(umb_context_t* ctx, srl_context_t* serial_ctx, const config ctx->channel_numbers[3] = config_umb->channel_temperature; - ctx->channel_numbers[4] = config_umb->channel_qfe; + ctx->channel_numbers[4] = config_umb->channel_qnh; } @@ -475,11 +475,11 @@ float umb_get_temperature(const config_data_umb_t * const config_umb) { return out; } -float umb_get_qfe(const config_data_umb_t * const config_umb) { +float umb_get_qnh(const config_data_umb_t * const config_umb) { float out = 0; for (int i = 0; i < UMB_CHANNELS_STORAGE_CAPAC; i++) { - if (rte_wx_umb_channel_values[i][0] == (int16_t)config_umb->channel_qfe) { + if (rte_wx_umb_channel_values[i][0] == (int16_t)config_umb->channel_qnh) { out = (float)rte_wx_umb_channel_values[i][1] * 0.1f; break; }