Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
FredM67 committed Jun 14, 2023
1 parent 95e6994 commit 9d2b4e1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
12 changes: 6 additions & 6 deletions Mk2_3phase_RFdatalog_temp/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
// constants which must be set individually for each system
//
inline constexpr uint8_t NO_OF_PHASES{ 3 }; /**< number of phases of the main supply. */
inline constexpr uint8_t NO_OF_DUMPLOADS{ 3 }; /**< number of dump loads connected to the diverter */
inline constexpr uint8_t NO_OF_DUMPLOADS{ 2 }; /**< number of dump loads connected to the diverter */

#ifdef EMONESP
inline constexpr bool EMONESP_CONTROL{ true };
Expand All @@ -51,7 +51,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 -----------
Expand All @@ -72,10 +72,10 @@ inline constexpr bool DUAL_TARIFF{ false }; /**< set it to 'true' if th
// D12 is MISO
// D13 is SCK

inline constexpr uint8_t physicalLoadPin[NO_OF_DUMPLOADS]{ 5, 6, 7 }; /**< for 3-phase PCB, Load #1/#2/#3 (Rev 2 PCB) */
inline uint8_t loadPrioritiesAndState[NO_OF_DUMPLOADS]{ 0, 1, 2 }; /**< load priorities and states at startup */
inline constexpr uint8_t physicalLoadPin[NO_OF_DUMPLOADS]{ 5, 7 }; /**< for 3-phase PCB, Load #1/#2/#3 (Rev 2 PCB) */
inline uint8_t loadPrioritiesAndState[NO_OF_DUMPLOADS]{ 0, 1 }; /**< load priorities and states at startup */

inline constexpr uint8_t relayPin{ 0xff }; /**< for 3-phase PCB, relay trigger */
inline constexpr uint8_t relayPin{ 9 }; /**< for 3-phase PCB, relay trigger */
inline constexpr uint8_t dualTariffPin{ 0xff }; /**< for 3-phase PCB, off-peak trigger */
inline constexpr uint8_t diversionPin{ 0xff }; /**< if LOW, set diversion on standby */
inline constexpr uint8_t rotationPin{ 0xff }; /**< if LOW, trigger a load priority rotation */
Expand All @@ -84,7 +84,7 @@ inline constexpr uint8_t watchDogPin{ 0xff }; /**< watch dog LED */

inline constexpr uint8_t tempSensorPin{ 0xff }; /**< for 3-phase PCB, sensor pin */

inline relayOutput<> relay_Output{ relayPin }; /**< config for relay diversion, see class definition for defaults */
inline relayOutput<> relay_Output{ relayPin, 1000, 200, 1, 1 }; /**< config for relay diversion, see class definition for defaults */

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 */
Expand Down
6 changes: 6 additions & 0 deletions Mk2_3phase_RFdatalog_temp/processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ void initializeOptionalPins()
delay(100); // allow time to settle
}

if constexpr (RELAY_DIVERSION)
{
pinMode(relayPin, OUTPUT);
delay(100);
}

if constexpr (WATCHDOG_PIN_PRESENT)
{
pinMode(watchDogPin, OUTPUT); // set as output
Expand Down
27 changes: 14 additions & 13 deletions Mk2_3phase_RFdatalog_temp/utils_relay.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ template< uint8_t T = 1 > class relayOutput
* @param _importThreshold Import threshold to turn relay OFF
*/
constexpr relayOutput(uint8_t _relay_pin, int16_t _surplusThreshold, int16_t _importThreshold)
: relay_pin(_relay_pin), surplusThreshold(abs(_surplusThreshold)), importThreshold(abs(_importThreshold))
: relay_pin(_relay_pin), surplusThreshold(-abs(_surplusThreshold)), importThreshold(abs(_importThreshold))
{
}

Expand All @@ -60,7 +60,7 @@ template< uint8_t T = 1 > class relayOutput
* @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)
: relay_pin(_relay_pin), surplusThreshold(abs(_surplusThreshold)), importThreshold(abs(_importThreshold)), minON(_minON * 60), minOFF(_minOFF * 60)
: relay_pin(_relay_pin), surplusThreshold(-abs(_surplusThreshold)), importThreshold(abs(_importThreshold)), minON(_minON * 60), minOFF(_minOFF * 60)
{
}

Expand All @@ -81,7 +81,7 @@ template< uint8_t T = 1 > class relayOutput
*/
constexpr auto get_surplusThreshold() const
{
return surplusThreshold;
return -surplusThreshold;
}

/**
Expand Down Expand Up @@ -135,11 +135,12 @@ template< uint8_t T = 1 > class relayOutput
{
const auto currentAvgPower{ sliding_Average.getAverage() };

if (currentAvgPower > surplusThreshold)
// To avoid changing sign, surplus is a negative value
if (currentAvgPower < surplusThreshold)
{
try_turnON();
}
else if (-currentAvgPower > importThreshold)
else if (currentAvgPower > importThreshold)
{
try_turnOFF();
}
Expand Down Expand Up @@ -219,14 +220,14 @@ template< uint8_t T = 1 > class relayOutput
}

private:
const uint8_t relay_pin{ 0xff }; /**< Pin associated with the relay */
const int16_t surplusThreshold{ 1000 }; /**< Surplus threshold to turn relay ON */
const int16_t importThreshold{ 200 }; /**< Import threshold to turn relay OFF */
const uint16_t minON{ 5 * 60 }; /**< Minimum duration in seconds the relay is turned ON */
const 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 */
const uint8_t relay_pin{ 0xff }; /**< Pin associated with the relay */
const int16_t surplusThreshold{ -1000 }; /**< Surplus threshold to turn relay ON */
const int16_t importThreshold{ 200 }; /**< Import threshold to turn relay OFF */
const uint16_t minON{ 5 * 60 }; /**< Minimum duration in seconds the relay is turned ON */
const 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 */

static inline movingAvg< int16_t, T * 60 / DATALOG_PERIOD_IN_SECONDS > sliding_Average;
};
Expand Down

0 comments on commit 9d2b4e1

Please sign in to comment.