3-phase PV router
Loading...
Searching...
No Matches
RelayLogic::Relay< Output > Class Template Reference

The state of one relay, and its decisions. More...

#include <relay_engine.h>

Inheritance diagram for RelayLogic::Relay< Output >:

Public Member Functions

constexpr Relay ()=delete
 
constexpr Relay (const uint8_t _relay_pin)
 Construct a new relay Config object with default parameters.
 
constexpr Relay (uint8_t _relay_pin, int16_t _surplusThreshold, int16_t _importThreshold)
 Construct a new relay Config object with default/custom parameters.
 
constexpr Relay (uint8_t _relay_pin, int16_t _surplusThreshold, int16_t _importThreshold, uint16_t _minON, uint16_t _minOFF)
 Construct a new relay Config object with custom parameters.
 
constexpr auto get_importThreshold () const
 Get the import threshold which will turns OFF the relay.
 
constexpr auto get_minOFF () const
 Get the minimum OFF-time in seconds.
 
constexpr auto get_minON () const
 Get the minimum ON-time in seconds.
 
constexpr auto get_pin () const
 Get the control pin of the relay.
 
constexpr auto get_surplusThreshold () const
 Get the surplus threshold which will turns ON the relay.
 
constexpr bool hasValidMinimumTimes () const
 Whether both minimum times fit in 16 bits of seconds (1092 minutes at most).
 
void inc_duration () const
 Increment the duration of the current state.
 
auto isRelayON () const
 Return the state.
 
bool proceed_forced_only (const uint16_t overrideBitmask) const
 Proceed with the relay while diversion is disabled: ON only while forced.
 
bool proceed_relay (const int32_t currentAvgPower, const uint16_t overrideBitmask, const bool onAllowed=true) const
 Proceed with the relay.
 

Private Member Functions

bool isForced (const uint16_t overrideBitmask) const
 
bool try_switch (const bool on) const
 Turn the relay ON or OFF if the 'time' condition is met.
 

Static Private Member Functions

static constexpr int16_t negativeOf (const int16_t value)
 -abs(value), without the abs() macro of Arduino.h.
 
static constexpr uint16_t toSeconds (const uint16_t minutes)
 Minutes to seconds, in 16 bits.
 

Private Attributes

uint16_t duration { 0 }
 
const int16_t importThreshold { 200 }
 
const uint16_t minOFF { 5 * 60 }
 
const uint16_t minON { 5 * 60 }
 
const uint8_t relay_pin { 0xff }
 
bool relayIsON { false }
 
const int16_t surplusThreshold { -1000 }
 

Detailed Description

template<typename Output = NoOutput>
class RelayLogic::Relay< Output >

The state of one relay, and its decisions.

  • Relay Control: The relay is turned ON when the surplus threshold is exceeded and turned OFF when the import threshold is crossed.
  • Time Constraints: Minimum ON and OFF durations ensure stable operation.
  • State Management: The class tracks the relay's current state and the duration of its current state.

On each change, Output::switched(pin, on) drives the pin (relayOutput, utils_relay.h); then the state changes, and the methods return true.

Template Parameters
OutputA type with a static switched(uint8_t pin, bool on), called on each change.

Definition at line 70 of file relay_engine.h.

Constructor & Destructor Documentation

◆ Relay() [1/4]

template<typename Output = NoOutput>
RelayLogic::Relay< Output >::Relay ( )
constexprdelete

◆ Relay() [2/4]

template<typename Output = NoOutput>
RelayLogic::Relay< Output >::Relay ( const uint8_t _relay_pin)
inlineexplicitconstexpr

Construct a new relay Config object with default parameters.

Parameters
_relay_pinControl pin for the relay

Definition at line 80 of file relay_engine.h.

◆ Relay() [3/4]

template<typename Output = NoOutput>
RelayLogic::Relay< Output >::Relay ( uint8_t _relay_pin,
int16_t _surplusThreshold,
int16_t _importThreshold )
inlineconstexpr

Construct a new relay Config object with default/custom parameters.

Parameters
_relay_pinControl pin for the relay
_surplusThresholdSurplus threshold to turn relay ON (positive value, e.g., 1000)
_importThresholdImport threshold to turn relay OFF
  • Positive values: turn OFF when importing > threshold (normal installations)
  • Negative values: turn OFF when surplus < abs(threshold) (battery systems)
  • Zero: turn OFF when importing > 0W (problematic with batteries)

Definition at line 95 of file relay_engine.h.

Here is the call graph for this function:

◆ Relay() [4/4]

template<typename Output = NoOutput>
RelayLogic::Relay< Output >::Relay ( uint8_t _relay_pin,
int16_t _surplusThreshold,
int16_t _importThreshold,
uint16_t _minON,
uint16_t _minOFF )
inlineconstexpr

Construct a new relay Config object with custom parameters.

Parameters
_relay_pinControl pin for the relay
_surplusThresholdSurplus threshold to turn relay ON (positive value, e.g., 1000)
_importThresholdImport threshold to turn relay OFF
  • Positive values: turn OFF when importing > threshold (normal installations)
  • Negative values: turn OFF when surplus < abs(threshold) (battery systems)
  • Zero: turn OFF when importing > 0W (problematic with batteries)
_minONMinimum duration in minutes to leave relay ON
_minOFFMinimum duration in minutes to leave relay OFF

Definition at line 112 of file relay_engine.h.

Here is the call graph for this function:

Member Function Documentation

◆ get_importThreshold()

template<typename Output = NoOutput>
auto RelayLogic::Relay< Output >::get_importThreshold ( ) const
inlineconstexpr

Get the import threshold which will turns OFF the relay.

Returns
constexpr auto

Definition at line 142 of file relay_engine.h.

Here is the caller graph for this function:

◆ get_minOFF()

template<typename Output = NoOutput>
auto RelayLogic::Relay< Output >::get_minOFF ( ) const
inlineconstexpr

Get the minimum OFF-time in seconds.

Returns
constexpr auto

Definition at line 162 of file relay_engine.h.

◆ get_minON()

template<typename Output = NoOutput>
auto RelayLogic::Relay< Output >::get_minON ( ) const
inlineconstexpr

Get the minimum ON-time in seconds.

Returns
constexpr auto

Definition at line 152 of file relay_engine.h.

◆ get_pin()

template<typename Output = NoOutput>
auto RelayLogic::Relay< Output >::get_pin ( ) const
inlineconstexpr

Get the control pin of the relay.

Returns
constexpr auto

Definition at line 122 of file relay_engine.h.

Here is the caller graph for this function:

◆ get_surplusThreshold()

template<typename Output = NoOutput>
auto RelayLogic::Relay< Output >::get_surplusThreshold ( ) const
inlineconstexpr

Get the surplus threshold which will turns ON the relay.

Returns
constexpr auto

Definition at line 132 of file relay_engine.h.

Here is the caller graph for this function:

◆ hasValidMinimumTimes()

template<typename Output = NoOutput>
bool RelayLogic::Relay< Output >::hasValidMinimumTimes ( ) const
inlineconstexpr

Whether both minimum times fit in 16 bits of seconds (1092 minutes at most).

validation.h refuses a relay for which it is false.

Definition at line 172 of file relay_engine.h.

◆ inc_duration()

template<typename Output = NoOutput>
void RelayLogic::Relay< Output >::inc_duration ( ) const
inline

Increment the duration of the current state.

This function must be called every second.

Definition at line 192 of file relay_engine.h.

◆ isForced()

template<typename Output = NoOutput>
bool RelayLogic::Relay< Output >::isForced ( const uint16_t overrideBitmask) const
inlineprivate

Definition at line 260 of file relay_engine.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isRelayON()

template<typename Output = NoOutput>
auto RelayLogic::Relay< Output >::isRelayON ( ) const
inline

Return the state.

Returns
auto

Definition at line 182 of file relay_engine.h.

◆ negativeOf()

template<typename Output = NoOutput>
static constexpr int16_t RelayLogic::Relay< Output >::negativeOf ( const int16_t value)
inlinestaticconstexprprivate

-abs(value), without the abs() macro of Arduino.h.

Definition at line 255 of file relay_engine.h.

Here is the caller graph for this function:

◆ proceed_forced_only()

template<typename Output = NoOutput>
bool RelayLogic::Relay< Output >::proceed_forced_only ( const uint16_t overrideBitmask) const
inline

Proceed with the relay while diversion is disabled: ON only while forced.

Parameters
overrideBitmaskOverride bitmask (local pins)
Returns
bool True if state has changed

The minimum ON and OFF times still apply.

Definition at line 236 of file relay_engine.h.

Here is the call graph for this function:

◆ proceed_relay()

template<typename Output = NoOutput>
bool RelayLogic::Relay< Output >::proceed_relay ( const int32_t currentAvgPower,
const uint16_t overrideBitmask,
const bool onAllowed = true ) const
inline

Proceed with the relay.

Parameters
currentAvgPowerCurrent average power
overrideBitmaskOverride bitmask (local pins): the relay is forced ON while its bit is set
onAllowedFalse to turn the relay OFF only (a forced relay is never turned OFF)
Returns
bool True if state has changed

Definition at line 208 of file relay_engine.h.

Here is the call graph for this function:

◆ toSeconds()

template<typename Output = NoOutput>
static constexpr uint16_t RelayLogic::Relay< Output >::toSeconds ( const uint16_t minutes)
inlinestaticconstexprprivate

Minutes to seconds, in 16 bits.

Returns
the seconds, or UINT16_MAX beyond 1092 minutes: no number of minutes gives it (it is no multiple of 60), so hasValidMinimumTimes() can tell. On AVR, minutes * 60 would wrap silently (1093 minutes gave 44 seconds).

Definition at line 249 of file relay_engine.h.

Here is the caller graph for this function:

◆ try_switch()

template<typename Output = NoOutput>
bool RelayLogic::Relay< Output >::try_switch ( const bool on) const
inlineprivate

Turn the relay ON or OFF if the 'time' condition is met.

Parameters
onTrue to turn the relay ON, false to turn it OFF
Returns
bool True if state has changed

Definition at line 271 of file relay_engine.h.

Here is the caller graph for this function:

Member Data Documentation

◆ duration

template<typename Output = NoOutput>
uint16_t RelayLogic::Relay< Output >::duration { 0 }
mutableprivate

Duration of the current state

Definition at line 293 of file relay_engine.h.

◆ importThreshold

template<typename Output = NoOutput>
const int16_t RelayLogic::Relay< Output >::importThreshold { 200 }
private

Import threshold to turn relay OFF (positive = import mode, negative = surplus mode)

Definition at line 289 of file relay_engine.h.

◆ minOFF

template<typename Output = NoOutput>
const uint16_t RelayLogic::Relay< Output >::minOFF { 5 * 60 }
private

Minimum duration in seconds the relay is turned OFF

Definition at line 291 of file relay_engine.h.

◆ minON

template<typename Output = NoOutput>
const uint16_t RelayLogic::Relay< Output >::minON { 5 * 60 }
private

Minimum duration in seconds the relay is turned ON

Definition at line 290 of file relay_engine.h.

◆ relay_pin

template<typename Output = NoOutput>
const uint8_t RelayLogic::Relay< Output >::relay_pin { 0xff }
private

Pin associated with the relay (unused_pin)

Definition at line 287 of file relay_engine.h.

◆ relayIsON

template<typename Output = NoOutput>
bool RelayLogic::Relay< Output >::relayIsON { false }
mutableprivate

True if the relay is ON

Definition at line 294 of file relay_engine.h.

◆ surplusThreshold

template<typename Output = NoOutput>
const int16_t RelayLogic::Relay< Output >::surplusThreshold { -1000 }
private

Surplus threshold to turn relay ON

Definition at line 288 of file relay_engine.h.


The documentation for this class was generated from the following file: