3-phase PV router
Loading...
Searching...
No Matches
load_priorities.h File Reference

The load priority state machine: which load to switch at each mains cycle. More...

#include <stdint.h>
#include "load_map.h"
Include dependency graph for load_priorities.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  LoadPriorities::Bucket
 The fixed levels of the energy bucket, in bucket units. More...
 
struct  LoadPriorities::Switching< N >
 The switching decision, once per mains cycle, and its state. More...
 

Namespaces

namespace  LoadPriorities
 

Functions

template<uint8_t N>
uint8_t LoadPriorities::nextToAdd (const uint8_t(&priorities)[N])
 The next logical load to switch ON: the first one OFF, in priority order.
 
template<uint8_t N>
uint8_t LoadPriorities::nextToRemove (const uint8_t(&priorities)[N])
 The next logical load to switch OFF: the last one ON, in priority order.
 
template<uint8_t N>
void LoadPriorities::rotate (uint8_t(&priorities)[N])
 Rotates the priorities by one: the last load becomes the first, the others move down one place. Each load keeps its state.
 
template<bool CALIBRATION, uint8_t N, typename IsOverridden>
void LoadPriorities::toPhysical (const uint8_t(&priorities)[N], LoadStates(&physical)[N], const bool diversionEnabled, IsOverridden isOverridden)
 Maps the logical load states to the physical ones.
 

Variables

constexpr uint8_t loadStateMask { 0x7FU }
 
constexpr uint8_t loadStateOnBit { (uint8_t)(~loadStateMask) }
 

Detailed Description

The load priority state machine: which load to switch at each mains cycle.

Author
Frédéric Metrich (frede.nosp@m.ric..nosp@m.metri.nosp@m.ch@l.nosp@m.ive.f.nosp@m.r)
Version
1.0
Date
2026-11-21

Each element of the priority array is one logical load, the highest priority first: its low 7 bits are the physical load number (an index of physicalLoadPin[]), its high bit says whether the load is ON.

Once per mains cycle, the ISR compares the energy bucket with two thresholds: above the upper one, the first load OFF in priority order comes ON; below the lower one, the last load ON goes OFF. After a switch, no other load may switch for POST_TRANSITION_MAX_COUNT cycles, so that the switch has time to show in the bucket: only the load just switched may switch back. The threshold follows the bucket meanwhile.

A threshold goes back to its default only when the bucket is in the other half: while the bucket stays in the upper half, the upper threshold keeps the level the bucket reached during the last post-transition period, and a load comes ON only if the bucket rises above it. The same for the lower threshold in the lower half.

Then the logical states give the physical ones: a load is ON if its override is active, or if diversion is enabled and its logical state is ON. Calibration mode keeps every load OFF.

Note
This header has no Arduino dependency, so it can be exercised by the native test suite. Everything runs in the ADC ISR: the functions are forced inline, except Switching::onNewCycle() (see there).

Definition in file load_priorities.h.

Variable Documentation

◆ loadStateMask

uint8_t loadStateMask { 0x7FU }
inlineconstexpr

bit mask for masking load state

Definition at line 45 of file load_priorities.h.

◆ loadStateOnBit

uint8_t loadStateOnBit { (uint8_t)(~loadStateMask) }
inlineconstexpr

bit mask for load state ON

Definition at line 46 of file load_priorities.h.