diff --git a/Mk2_3phase_RFdatalog_temp/config.h b/Mk2_3phase_RFdatalog_temp/config.h index 2d3c1493..4adb915c 100644 --- a/Mk2_3phase_RFdatalog_temp/config.h +++ b/Mk2_3phase_RFdatalog_temp/config.h @@ -27,6 +27,9 @@ #include "debug.h" #include "types.h" +#include "utils_relay.h" +#include "utils_temp.h" + //-------------------------------------------------------------------------------------------------- // constants which must be set individually for each system // diff --git a/Mk2_3phase_RFdatalog_temp/types.h b/Mk2_3phase_RFdatalog_temp/types.h index 845d5401..f9a232d5 100644 --- a/Mk2_3phase_RFdatalog_temp/types.h +++ b/Mk2_3phase_RFdatalog_temp/types.h @@ -67,150 +67,6 @@ template< uint8_t N = 3, uint8_t S = 0 > class PayloadTx_struct int16_t temperature_x100[S]; /**< temperature in 100th of °C */ }; -/** - * @brief Config parameters for relay diversion - * - */ -class relayConfig -{ -public: - constexpr relayConfig() = default; - - /** - * @brief Construct a new relay Config object - * - * @param _surplusThreshold Surplus threshold to turn relay ON - * @param _importThreshold Import threshold to turn relay OFF - */ - constexpr relayConfig(int16_t _surplusThreshold, int16_t _importThreshold) - : surplusThreshold(abs(_surplusThreshold)), importThreshold(abs(_importThreshold)) - { - } - - /** - * @brief Construct a new relay Config object - * - * @param _surplusThreshold Surplus threshold to turn relay ON - * @param _importThreshold Import threshold to turn relay OFF - * @param _minON Minimum duration to leave relay ON - * @param _minOFF Minimum duration in minutes to l - */ - constexpr relayConfig(int16_t _surplusThreshold, int16_t _importThreshold, uint16_t _minON, uint16_t _minOFF) - : surplusThreshold(abs(_surplusThreshold)), importThreshold(abs(_importThreshold)), minON(_minON * 60), minOFF(_minOFF * 60) - { - } - - /** - * @brief Get the surplus threshold which will turns ON the relay - * - * @return constexpr auto - */ - constexpr auto get_surplusThreshold() const - { - return surplusThreshold; - } - - /** - * @brief Get the import threshold which will turns OFF the relay - * - * @return constexpr auto - */ - constexpr auto get_importThreshold() const - { - return importThreshold; - } - - /** - * @brief Get the minimum ON-time - * - * @return constexpr auto - */ - constexpr auto get_minON() const - { - return minON; - } - - /** - * @brief Get the minimum OFF-time - * - * @return constexpr auto - */ - constexpr auto get_minOFF() const - { - return minOFF; - } - - /** - * @brief Increment the duration of the current state - * - */ - void inc_duration() - { - ++duration; - } - - /** - * @brief Proceed with the relay - * - * @param currentAvgPower Current sliding average power - */ - void proceed_relay(int16_t currentAvgPower) - { - if (currentAvgPower > surplusThreshold) - { - try_turnON(); - } - else if (-currentAvgPower > importThreshold) - { - try_turnOFF(); - } - } - -private: - /** - * @brief Turn ON the relay if the 'time' condition is met - * - */ - void try_turnON() - { - if (relayState) - { - return; - } - if (duration > minOFF) - { - relayState = true; - duration = 0; - } - } - - /** - * @brief Turn OFF the relay if the 'time' condition is met - * - */ - void try_turnOFF() - { - if (!relayState) - { - return; - } - if (duration > minON) - { - relayState = false; - duration = 0; - } - } - -private: - int16_t surplusThreshold{ 1000 }; /**< Surplus threshold to turn relay ON */ - int16_t importThreshold{ 200 }; /**< Import threshold to turn relay OFF */ - uint16_t minON{ 5 * 60 }; /**< Minimum duration in seconds the relay is turned ON */ - uint16_t minOFF{ 5 * 60 }; /**< Minimum duration in seconds the relay is turned OFF */ - - uint16_t duration{ 0 }; /**< Duration of the current state */ - bool relayState{ false }; /**< State of the relay */ -}; - /** @brief Config parameters for overriding a load * @details This class allows the user to define when and how long a load will be forced at * full power during off-peak period. @@ -248,9 +104,6 @@ class pairForceLoad uint16_t uiDuration{ UINT16_MAX }; /**< the duration for overriding the load in hours or minutes */ }; -using ScratchPad = uint8_t[9]; -using DeviceAddress = uint8_t[8]; - /** * @brief Helper function to retrieve the dimension of a C-array * diff --git a/Mk2_3phase_RFdatalog_temp/utils_relay.h b/Mk2_3phase_RFdatalog_temp/utils_relay.h index f6bb0e45..14fc49d5 100644 --- a/Mk2_3phase_RFdatalog_temp/utils_relay.h +++ b/Mk2_3phase_RFdatalog_temp/utils_relay.h @@ -14,4 +14,148 @@ #include "types.h" +/** + * @brief Config parameters for relay diversion + * + */ +class relayConfig +{ +public: + constexpr relayConfig() = default; + + /** + * @brief Construct a new relay Config object + * + * @param _surplusThreshold Surplus threshold to turn relay ON + * @param _importThreshold Import threshold to turn relay OFF + */ + constexpr relayConfig(int16_t _surplusThreshold, int16_t _importThreshold) + : surplusThreshold(abs(_surplusThreshold)), importThreshold(abs(_importThreshold)) + { + } + + /** + * @brief Construct a new relay Config object + * + * @param _surplusThreshold Surplus threshold to turn relay ON + * @param _importThreshold Import threshold to turn relay OFF + * @param _minON Minimum duration to leave relay ON + * @param _minOFF Minimum duration in minutes to l + */ + constexpr relayConfig(int16_t _surplusThreshold, int16_t _importThreshold, uint16_t _minON, uint16_t _minOFF) + : surplusThreshold(abs(_surplusThreshold)), importThreshold(abs(_importThreshold)), minON(_minON * 60), minOFF(_minOFF * 60) + { + } + + /** + * @brief Get the surplus threshold which will turns ON the relay + * + * @return constexpr auto + */ + constexpr auto get_surplusThreshold() const + { + return surplusThreshold; + } + + /** + * @brief Get the import threshold which will turns OFF the relay + * + * @return constexpr auto + */ + constexpr auto get_importThreshold() const + { + return importThreshold; + } + + /** + * @brief Get the minimum ON-time + * + * @return constexpr auto + */ + constexpr auto get_minON() const + { + return minON; + } + + /** + * @brief Get the minimum OFF-time + * + * @return constexpr auto + */ + constexpr auto get_minOFF() const + { + return minOFF; + } + + /** + * @brief Increment the duration of the current state + * + */ + void inc_duration() + { + ++duration; + } + + /** + * @brief Proceed with the relay + * + * @param currentAvgPower Current sliding average power + */ + void proceed_relay(int16_t currentAvgPower) + { + if (currentAvgPower > surplusThreshold) + { + try_turnON(); + } + else if (-currentAvgPower > importThreshold) + { + try_turnOFF(); + } + } + +private: + /** + * @brief Turn ON the relay if the 'time' condition is met + * + */ + void try_turnON() + { + if (relayState) + { + return; + } + if (duration > minOFF) + { + relayState = true; + duration = 0; + } + } + + /** + * @brief Turn OFF the relay if the 'time' condition is met + * + */ + void try_turnOFF() + { + if (!relayState) + { + return; + } + if (duration > minON) + { + relayState = false; + duration = 0; + } + } + +private: + int16_t surplusThreshold{ 1000 }; /**< Surplus threshold to turn relay ON */ + int16_t importThreshold{ 200 }; /**< Import threshold to turn relay OFF */ + uint16_t minON{ 5 * 60 }; /**< Minimum duration in seconds the relay is turned ON */ + uint16_t minOFF{ 5 * 60 }; /**< Minimum duration in seconds the relay is turned OFF */ + + uint16_t duration{ 0 }; /**< Duration of the current state */ + bool relayState{ false }; /**< State of the relay */ +}; + #endif // _UTILS_RELAY_H \ No newline at end of file diff --git a/Mk2_3phase_RFdatalog_temp/utils_temp.h b/Mk2_3phase_RFdatalog_temp/utils_temp.h index b5453ab2..7750aa5b 100644 --- a/Mk2_3phase_RFdatalog_temp/utils_temp.h +++ b/Mk2_3phase_RFdatalog_temp/utils_temp.h @@ -14,8 +14,9 @@ #include "config.h" #include "constants.h" -#include "dualtariff.h" -#include "processing.h" + +using ScratchPad = uint8_t[9]; +using DeviceAddress = uint8_t[8]; inline int16_t readTemperature(const DeviceAddress &deviceAddress); inline void requestTemperatures();