diff --git a/runtime/Mk2_3phase_RFdatalog_temp/Mk2_3phase_RFdatalog_temp.ino b/runtime/Mk2_3phase_RFdatalog_temp/Mk2_3phase_RFdatalog_temp.ino index c9398a7..c8b9a84 100644 --- a/runtime/Mk2_3phase_RFdatalog_temp/Mk2_3phase_RFdatalog_temp.ino +++ b/runtime/Mk2_3phase_RFdatalog_temp/Mk2_3phase_RFdatalog_temp.ino @@ -184,6 +184,6 @@ */ static_assert(__cplusplus >= 201703L, "**** Please define 'gnu++17' in 'platform.txt' ! ****"); -static_assert(__cplusplus >= 201703L, "See also : https://github.com/FredM67/PVRouter-3-phase/blob/main/Mk2_3phase_RFdatalog_temp/Readme.md"); +static_assert(__cplusplus >= 201703L, "See also : https://github.com/FredM67/PVRouter-3-phase/blob/main/runtime/Mk2_3phase_RFdatalog_temp/Readme.md"); // The active code can be found in the other cpp/h files diff --git a/runtime/Mk2_3phase_RFdatalog_temp/config.h b/runtime/Mk2_3phase_RFdatalog_temp/config.h index 7bea63d..54a4b86 100644 --- a/runtime/Mk2_3phase_RFdatalog_temp/config.h +++ b/runtime/Mk2_3phase_RFdatalog_temp/config.h @@ -50,7 +50,7 @@ inline constexpr bool OVERRIDE_PIN_PRESENT{ false }; /**< set #endif inline constexpr bool WATCHDOG_PIN_PRESENT{ false }; /**< set it to 'true' if there's a watch led */ -inline constexpr bool RELAY_DIVERSION{ false }; /**< set it to 'true' if a relay is used for diversion */ +inline constexpr bool RELAY_DIVERSION{ true }; /**< set it to 'true' if a relay is used for diversion */ inline constexpr bool DUAL_TARIFF{ false }; /**< set it to 'true' if there's a dual tariff each day AND the router is connected to the billing meter */ // ----------- Pinout assignments ----------- @@ -83,7 +83,7 @@ inline constexpr uint8_t watchDogPin{ 0xff }; /**< watch dog LED */ inline constexpr uint8_t tempSensorPin{ 0xff }; /**< for 3-phase PCB, sensor pin */ -inline constexpr RelayEngine relays{ { { 0xff, 1000, 200, 1, 1 } } }; /**< config for relay diversion, see class definition for defaults and advanced options */ +inline constexpr RelayEngine relays{ 15_i, { { 6, 1000, 200, 1, 1 } } }; /**< config for relay diversion, see class definition for defaults and advanced options */ inline constexpr uint8_t ul_OFF_PEAK_DURATION{ 8 }; /**< Duration of the off-peak period in hours */ inline constexpr pairForceLoad rg_ForceLoad[NO_OF_DUMPLOADS]{ { -3, 2 } }; /**< force config for load #1 ONLY for dual tariff */ diff --git a/runtime/Mk2_3phase_RFdatalog_temp/types.h b/runtime/Mk2_3phase_RFdatalog_temp/types.h index 4847994..1423eba 100644 --- a/runtime/Mk2_3phase_RFdatalog_temp/types.h +++ b/runtime/Mk2_3phase_RFdatalog_temp/types.h @@ -15,6 +15,7 @@ #include #include "constants.h" +#include "type_traits.hpp" // ------------------------------- // definitions of enumerated types diff --git a/runtime/Mk2_3phase_RFdatalog_temp/utils_relay.h b/runtime/Mk2_3phase_RFdatalog_temp/utils_relay.h index cc9e995..86db719 100644 --- a/runtime/Mk2_3phase_RFdatalog_temp/utils_relay.h +++ b/runtime/Mk2_3phase_RFdatalog_temp/utils_relay.h @@ -20,22 +20,24 @@ #include "ewma_avg.hpp" #include "utils_pins.h" +#include "debug.h" + /** * @brief Relay diversion config and engine * * @ingroup RelayDiversion */ -class relayOutput +class RelayOutput { public: - constexpr relayOutput() = delete; + constexpr RelayOutput() = delete; /** * @brief Construct a new relay Config object with default parameters * * @param _relay_pin Control pin for the relay */ - explicit constexpr relayOutput(const uint8_t _relay_pin) + explicit constexpr RelayOutput(const uint8_t _relay_pin) : relay_pin{_relay_pin} { } @@ -47,7 +49,7 @@ class relayOutput * @param _surplusThreshold Surplus threshold to turn relay ON * @param _importThreshold Import threshold to turn relay OFF */ - constexpr relayOutput(uint8_t _relay_pin, int16_t _surplusThreshold, int16_t _importThreshold) + constexpr RelayOutput(uint8_t _relay_pin, int16_t _surplusThreshold, int16_t _importThreshold) : relay_pin{_relay_pin}, surplusThreshold{-abs(_surplusThreshold)}, importThreshold{abs(_importThreshold)} { } @@ -61,7 +63,7 @@ class relayOutput * @param _minON Minimum duration in minutes to leave relay ON * @param _minOFF Minimum duration in minutes to leave relay OFF */ - constexpr relayOutput(uint8_t _relay_pin, int16_t _surplusThreshold, int16_t _importThreshold, uint16_t _minON, uint16_t _minOFF) + constexpr RelayOutput(uint8_t _relay_pin, int16_t _surplusThreshold, int16_t _importThreshold, uint16_t _minON, uint16_t _minOFF) : relay_pin{_relay_pin}, surplusThreshold{-abs(_surplusThreshold)}, importThreshold{abs(_importThreshold)}, minON{_minON * 60}, minOFF{_minOFF * 60} { } @@ -255,7 +257,7 @@ class RelayEngine * @brief Construct a list of relays * */ - explicit constexpr RelayEngine(const relayOutput (&ref)[N]) + explicit constexpr RelayEngine(const RelayOutput (&ref)[N]) : relay(ref) { } @@ -264,7 +266,7 @@ class RelayEngine * @brief Construct a list of relays with a custom sliding average * */ - constexpr RelayEngine(integral_constant< uint8_t, D > ic, const relayOutput (&ref)[N]) + constexpr RelayEngine(integral_constant< uint8_t, D > ic, const RelayOutput (&ref)[N]) : relay(ref) { } @@ -391,7 +393,7 @@ class RelayEngine } private: - const relayOutput relay[N]; /**< Array of relays */ + const RelayOutput relay[N]; /**< Array of relays */ mutable uint8_t settle_change{ 60 }; /**< Delay in seconds until next change occurs */ diff --git a/runtime/test/test_RelayEngine/test_main.cpp b/runtime/test/test_RelayEngine/test_main.cpp new file mode 100644 index 0000000..303e29e --- /dev/null +++ b/runtime/test/test_RelayEngine/test_main.cpp @@ -0,0 +1,63 @@ +#include +#include + +#include "utils_relay.h" + +// Assuming you have a RelayOutput array defined somewhere +const RelayOutput relayArray[2]{ { 5, 1000, 200, 5, 5 }, { 6, 1000, 200, 5, 5 } }; + +RelayEngine< 2 >* relayEngineTest; + +void setUp(void) +{ + relayEngineTest = new RelayEngine< 2 >(relayArray); +} + +void tearDown(void) +{ + delete relayEngineTest; +} + +void test_get_size(void) +{ + TEST_ASSERT_EQUAL(2, relayEngineTest->get_size()); +} + +void test_get_relay(void) +{ + for(uint8_t i = 0; i < relayEngineTest->get_size(); ++i) { + TEST_ASSERT_EQUAL(true, relayArray[i].get_pin() == relayEngineTest->get_relay(i).get_pin()); + TEST_ASSERT_EQUAL(true, relayArray[i].get_minOFF() == relayEngineTest->get_relay(i).get_minOFF()); + TEST_ASSERT_EQUAL(true, relayArray[i].get_minON() == relayEngineTest->get_relay(i).get_minON()); + TEST_ASSERT_EQUAL(true, relayArray[i].get_surplusThreshold() == relayEngineTest->get_relay(i).get_surplusThreshold()); + TEST_ASSERT_EQUAL(true, relayArray[i].get_importThreshold() == relayEngineTest->get_relay(i).get_importThreshold()); + } +} + +void test_get_average(void) +{ + // Assuming initial average is 0 + TEST_ASSERT_EQUAL(0, RelayEngine< 2 >::get_average()); +} + +void test_update_average(void) +{ + RelayEngine< 2 >::update_average(100); + // Assuming the average changes after update + TEST_ASSERT_NOT_EQUAL(0, RelayEngine< 2 >::get_average()); +} + +void setup() +{ + UNITY_BEGIN(); + RUN_TEST(test_get_size); + RUN_TEST(test_get_relay); + RUN_TEST(test_get_average); + RUN_TEST(test_update_average); + UNITY_END(); +} + +void loop() +{ + // Do nothing here +} \ No newline at end of file diff --git a/runtime/test/test_RelayOutput/test_,main.cpp b/runtime/test/test_RelayOutput/test_,main.cpp new file mode 100644 index 0000000..96c21ef --- /dev/null +++ b/runtime/test/test_RelayOutput/test_,main.cpp @@ -0,0 +1,75 @@ +#include +#include + +#include "utils_relay.h" + +static_assert(__cplusplus >= 201703L, "See also : https://github.com/FredM67/PVRouter-3-phase/blob/main/runtime/Mk2_3phase_RFdatalog_temp/Readme.md"); + +RelayOutput* RelayOutputTest; + +void setUp(void) { + RelayOutputTest = new RelayOutput(5, 1000, 200, 5, 5); +} + +void tearDown(void) { + delete RelayOutputTest; +} + +void test_get_pin(void) { + TEST_ASSERT_EQUAL(5, RelayOutputTest->get_pin()); +} + +void test_get_surplusThreshold(void) { + TEST_ASSERT_EQUAL(1000, RelayOutputTest->get_surplusThreshold()); +} + +void test_get_importThreshold(void) { + TEST_ASSERT_EQUAL(200, RelayOutputTest->get_importThreshold()); +} + +void test_get_minON(void) { + TEST_ASSERT_EQUAL(5 * 60, RelayOutputTest->get_minON()); +} + +void test_get_minOFF(void) { + TEST_ASSERT_EQUAL(5 * 60, RelayOutputTest->get_minOFF()); +} + +void test_isRelayON(void) { + TEST_ASSERT_EQUAL(false, RelayOutputTest->isRelayON()); +} + +void test_proceed_relay(void) { + TEST_ASSERT_EQUAL(false, RelayOutputTest->proceed_relay(150)); + TEST_ASSERT_EQUAL(false, RelayOutputTest->isRelayON()); + + for(uint16_t _time = 0; _time < RelayOutputTest->get_minOFF(); ++_time) { + RelayOutputTest->inc_duration(); + } + + TEST_ASSERT_EQUAL(true, RelayOutputTest->proceed_relay(-1500)); + TEST_ASSERT_EQUAL(true, RelayOutputTest->isRelayON()); + + for(uint16_t _time = 0; _time < RelayOutputTest->get_minON(); ++_time) { + RelayOutputTest->inc_duration(); + } + + TEST_ASSERT_EQUAL(true, RelayOutputTest->proceed_relay(250)); + TEST_ASSERT_EQUAL(false, RelayOutputTest->isRelayON()); +} + +void setup() { + UNITY_BEGIN(); + RUN_TEST(test_get_pin); + RUN_TEST(test_get_surplusThreshold); + RUN_TEST(test_get_importThreshold); + RUN_TEST(test_get_minON); + RUN_TEST(test_get_minOFF); + RUN_TEST(test_isRelayON); + RUN_TEST(test_proceed_relay); + UNITY_END(); +} + +void loop() { + // Do nothing here +} \ No newline at end of file