106 for (
size_t i = 0;
i < N; ++
i)
128 uint16_t output_pins{ 0 };
148 for (uint8_t idx = 0; idx <
relays.get_size(); ++idx)
150 const auto relayPin =
relays.get_relay(idx).get_pin();
152 if (
bit_read(output_pins, relayPin))
155 bit_set(output_pins, relayPin);
179 uint16_t input_pins{ 0 };
284 uint16_t pinsON{ 0 };
285 uint16_t pinsOFF{ 0 };
424 int32_t sampleIminusDC = (
static_cast< int32_t
>(rawSample -
i_DCoffset_I_nom)) << 8;
427 const int32_t last_lpf_long{ lpf_long[phase] };
428 lpf_long[phase] += alpha * (sampleIminusDC - last_lpf_long);
429 sampleIminusDC += (lpf_gain * lpf_long[phase]);
433 const int32_t filtI_div4 = sampleIminusDC >> 2;
434 int32_t instP = filtV_div4 * filtI_div4;
495 int32_t inst_Vsquared{ filtV_div4 * filtV_div4 };
499 inst_Vsquared >>= 16;
503 inst_Vsquared >>= 12;
564 bool bOK_toAddLoad{
true };
613 bool bOK_toRemoveLoad{
true };
638 if (bOK_toRemoveLoad)
1030 DBUG(F(
"Output mode: "));
1037 DBUGLN(F(
"anti-flicker"));
1038 DBUG(F(
"\toffsetOfEnergyThresholds = "));
1041 DBUG(F(
"\tf_capacityOfEnergyBucket_main = "));
1043 DBUG(F(
"\tf_lowerEnergyThreshold = "));
1045 DBUG(F(
"\tf_upperEnergyThreshold = "));
1081 static uint8_t sample_index{ 0 };
1084 switch (sample_index)
1088 ADMUX = bit(REFS0) +
sensorV[1];
1095 ADMUX = bit(REFS0) +
sensorI[1];
1102 ADMUX = bit(REFS0) +
sensorV[2];
1109 ADMUX = bit(REFS0) +
sensorI[2];
1116 ADMUX = bit(REFS0) +
sensorV[0];
1123 ADMUX = bit(REFS0) +
sensorI[0];
Calibration values definition.
constexpr uint8_t loadPrioritiesAtStartup[NO_OF_DUMPLOADS]
constexpr bool RELAY_DIVERSION
constexpr bool OVERRIDE_PIN_PRESENT
constexpr uint8_t dualTariffPin
constexpr uint8_t forcePin
constexpr RelayEngine relays
constexpr uint8_t physicalLoadPin[NO_OF_DUMPLOADS]
constexpr bool DUAL_TARIFF
constexpr uint8_t diversionPin
constexpr uint8_t NO_OF_DUMPLOADS
constexpr bool WATCHDOG_PIN_PRESENT
constexpr bool DIVERSION_PIN_PRESENT
constexpr uint8_t rotationPin
constexpr uint8_t watchDogPin
constexpr int16_t REQUIRED_EXPORT_IN_WATTS
constexpr uint32_t WORKING_ZONE_IN_JOULES
constexpr conditional< DATALOG_PERIOD_IN_SECONDS *SUPPLY_FREQUENCY >=UINT8_MAX, uint16_t, uint8_t >::type DATALOG_PERIOD_IN_MAINS_CYCLES
constexpr uint8_t DATALOG_PERIOD_IN_SECONDS
constexpr uint8_t NO_OF_PHASES
constexpr uint8_t SUPPLY_FREQUENCY
Classes/types needed for dual-tariff support.
constexpr uint16_t getInputPins()
Retrieves the input pins configuration.
constexpr void initializeArray(int32_t(&array)[N], int32_t value)
Initializes all elements of a given array to a specified value.
constexpr uint16_t getOutputPins()
Retrieves the output pins configuration.
void initializeProcessing()
Initializes the processing engine, including ports, load states, and ADC setup.
ISR(ADC_vect)
Interrupt Service Routine - Interrupt-Driven Analog Conversion.
void updatePhysicalLoadStates()
Updates the physical load states based on logical load priorities and states.
void processLatestContribution(const uint8_t phase)
Process the latest contribution after each phase-specific new cycle.
void updatePortsStates()
Updates the control ports for each of the physical loads.
void processStartUp(const uint8_t phase)
Processes the startup period for the router.
void processStartNewCycle()
Processes the start of a new mains cycle on phase 0.
void processMinusHalfCycle(const uint8_t phase)
Processes the start of a new negative half cycle for the specified phase.
void processVoltageRawSample(const uint8_t phase, const int16_t rawSample)
Processes the current voltage raw sample for the specified phase.
void proceedLowEnergyLevel()
Handles the case when the energy level is low, potentially removing a load.
void processPolarity(const uint8_t phase, const int16_t rawSample)
Processes the polarity of the current voltage sample for a specific phase.
void processRawSamples(const uint8_t phase)
Processes raw voltage and current samples for the specified phase.
uint8_t nextLogicalLoadToBeAdded()
Retrieve the next logical load that could be added.
void processPlusHalfCycle(const uint8_t phase)
Process the start of a new positive half cycle for the specified phase.
void processDataLogging()
Process data logging at the end of each logging period.
uint8_t nextLogicalLoadToBeRemoved()
Retrieve the next logical load that could be removed (in reverse order).
void confirmPolarity(const uint8_t phase)
Confirms the polarity of the current voltage sample for a specific phase.
void processVoltage(const uint8_t phase)
Processes the current voltage sample for the specified phase.
void proceedHighEnergyLevel()
Handles the case when the energy level is high, potentially adding a load.
void processCurrentRawSample(const uint8_t phase, const int16_t rawSample)
Processes the current raw sample for the specified phase.
Polarities polarityConfirmedOfLastSampleV[NO_OF_PHASES]
uint8_t n_lowestNoOfSampleSetsPerMainsCycle
float f_energyInBucket_main
constexpr OutputModes outputMode
constexpr int16_t i_DCoffset_I_nom
constexpr int32_t l_DCoffset_V_min
constexpr int32_t l_DCoffset_V_max
float f_upperEnergyThreshold
uint16_t countLoadON[NO_OF_DUMPLOADS]
constexpr float f_offsetOfEnergyThresholdsInAFmode
remove_cv< remove_reference< decltype(DATALOG_PERIOD_IN_MAINS_CYCLES)>::type >::type n_cycleCountForDatalogging
constexpr uint8_t POST_TRANSITION_MAX_COUNT
constexpr auto initThreshold(const bool lower)
set default threshold at compile time so the variable can be read-only
constexpr float f_capacityOfEnergyBucket_main
LoadStates physicalLoadState[NO_OF_DUMPLOADS]
int32_t l_sum_Vsquared[NO_OF_PHASES]
int32_t l_sumP[NO_OF_PHASES]
Polarities polarityConfirmed[NO_OF_PHASES]
uint8_t n_samplesDuringThisMainsCycle[NO_OF_PHASES]
constexpr float f_lowerThreshold_default
void printParamsForSelectedOutputMode()
Print the settings used for the selected output mode.
constexpr float f_midPointOfEnergyBucket_main
int32_t l_cumVdeltasThisCycle[NO_OF_PHASES]
int32_t l_sampleVminusDC[NO_OF_PHASES]
Polarities polarityOfMostRecentSampleV[NO_OF_PHASES]
uint16_t i_sampleSetsDuringThisDatalogPeriod
constexpr float f_upperThreshold_default
float f_lowerEnergyThreshold
int32_t l_DCoffset_V[NO_OF_PHASES]
int32_t l_sumP_atSupplyPoint[NO_OF_PHASES]
uint8_t postTransitionCount
Public functions/variables of processing engine.
volatile uint8_t copyOf_lowestNoOfSampleSetsPerMainsCycle
constexpr uint16_t startUpPeriod
constexpr uint8_t PERSISTENCE_FOR_POLARITY_CHANGE
uint8_t loadPrioritiesAndState[NO_OF_DUMPLOADS]
volatile bool b_reOrderLoads
constexpr uint8_t sensorV[NO_OF_PHASES]
constexpr uint8_t sensorI[NO_OF_PHASES]
volatile float copyOf_energyInBucket_main
volatile int32_t copyOf_sumP_atSupplyPoint[NO_OF_PHASES]
volatile int32_t copyOf_sum_Vsquared[NO_OF_PHASES]
volatile uint16_t copyOf_sampleSetsDuringThisDatalogPeriod
volatile bool b_overrideLoadOn[NO_OF_DUMPLOADS]
volatile bool b_newMainsCycle
volatile bool b_datalogEventPending
volatile bool b_diversionOff
constexpr uint16_t initialDelay
volatile uint16_t absenceOfDivertedEnergyCount
volatile uint16_t copyOf_countLoadON[NO_OF_DUMPLOADS]
constexpr uint8_t loadStateMask
constexpr uint8_t loadStateOnBit
Some utility functions for pins manipulation.
constexpr void bit_set(T &_dest, const uint8_t bit)
Set the specified bit to 1.
void setPinsOFF(uint16_t pins)
Set the Pins state to OFF.
constexpr uint8_t bit_read(const T &_src, const uint8_t bit)
Read the specified bit.
void setPinsAsInputPullup(uint16_t pins)
Set the pins as INPUT_PULLUP.
void setPinsAsOutput(uint16_t pins)
Set the pins as OUTPUT.
void setPinsON(uint16_t pins)
Set the Pins state to ON.
constexpr uint8_t bit_clear(T &_dest, const uint8_t bit)
Clear the specified bit.