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 };
287 uint16_t pinsON{ 0 };
288 uint16_t pinsOFF{ 0 };
409 static int32_t lpf_long{};
413 int32_t sampleIminusDC_grid = ((int32_t)(rawSample -
DCoffset_I)) << 8;
416 const int32_t last_lpf_long = lpf_long;
417 lpf_long +=
alpha * (sampleIminusDC_grid - last_lpf_long);
418 sampleIminusDC_grid += (
lpf_gain * lpf_long);
422 const int32_t filtI_div4 = sampleIminusDC_grid >> 2;
423 int32_t instP = filtV_div4 * filtI_div4;
456 int32_t sampleIminusDC_diverted = ((int32_t)(rawSample -
DCoffset_I)) << 8;
460 const int32_t filtI_div4 = sampleIminusDC_diverted >> 2;
461 int32_t instP = filtV_div4 * filtI_div4;
489 static uint8_t count{ 0 };
646 int32_t inst_Vsquared{ filtV_div4 * filtV_div4 };
648 inst_Vsquared >>= 12;
965 bool bOK_toAddLoad{
true };
1017 bool bOK_toRemoveLoad{
true };
1042 if (bOK_toRemoveLoad)
1178 DBUG(
"\tzero-crossing persistence (sample sets) = ");
1181 DBUG(
"\tcapacityOfEnergyBucket_long = ");
1223 static uint8_t sample_index{ 0 };
1226 switch (sample_index)
1316 for (uint8_t idx = 0; idx <
relays.get_size(); ++idx)
1318 const auto relayPin =
relays.get_relay(idx).get_pin();
1320 pinMode(relayPin, OUTPUT);
1349 DBUGLN(F(
"Load Priorities: "));
1353 DBUGLN(loadPrioAndState);
1359static_assert(
IEU_per_Wh_diverted > 4000000,
"IEU_per_Wh_diverted calculation is incorrect");
Calibration values definition.
constexpr float powerCal_grid
constexpr float powerCal_diverted
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 uint16_t startUpPeriod
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 uint32_t JOULES_PER_WATT_HOUR
constexpr uint8_t ANTI_CREEP_LIMIT
constexpr uint16_t delayBeforeSerialStarts
constexpr uint8_t SUPPLY_FREQUENCY
Classes/types needed for dual-tariff support.
void logLoadPriorities()
Prints the load priorities to the Serial output.
constexpr uint16_t getInputPins()
Retrieves the input pins configuration.
constexpr uint16_t getOutputPins()
Retrieves the output pins configuration.
void initializeProcessing()
Initializes the processing engine, including ports, load states, and ADC setup.
void initializeOldPCBPins()
Initializes optional pins for the old PCB configuration.
void refresh7SegDisplay()
Refreshes the display by updating the active digit and its segments.
void processGridCurrentRawSample(const int16_t rawSample)
Processes the raw current sample for the grid connection point.
ISR(ADC_vect)
Interrupt Service Routine - Interrupt-Driven Analog Conversion.
void confirmPolarity()
Confirms the polarity of the current voltage sample.
void processRawSamples()
This routine is called by the ISR when a pair of V & I samples becomes available.
void updatePhysicalLoadStates()
This function provides the link between the logical and physical loads.
void processDivertedCurrentRawSample(const int16_t rawSample)
Processes the raw current sample for the diverted connection point.
void updatePortsStates()
Updates the control ports for each of the physical loads.
void processMinusHalfCycle()
Process the start of a new -ve half cycle, just after the zero-crossing point.
void processStartNewCycle()
This code is executed once per 20ms, shortly after the start of each new mains cycle on phase 0.
void processPolarity(const int16_t rawSample)
Processes the polarity of the current voltage sample.
void proceedLowEnergyLevel()
Process the case of low energy level, some action may be required.
uint8_t nextLogicalLoadToBeAdded()
Retrieve the next load that could be removed (be aware of the reverse-order).
void processVoltage()
Process the calculation for the current voltage sample.
void processDataLogging()
Process with data logging.
void processPlusHalfCycle()
Process the start of a new +ve half cycle, just after the zero-crossing point.
uint8_t nextLogicalLoadToBeRemoved()
Process with data logging.
void processLatestContribution()
Process the latest contribution after each new cycle.
void processVoltageRawSample(const int16_t rawSample)
Process the current voltage raw sample.
void processStartUp()
Process the startup period for the router.
void proceedHighEnergyLevel()
Process the case of high energy level, some action may be required.
volatile uint16_t copyOf_divertedEnergyTotal_Wh_forDL
volatile int32_t copyOf_energyInBucket_long
volatile int32_t copyOf_sum_Vsquared
volatile int32_t copyOf_sumP_grid_overDL_Period
volatile bool b_datalogEventPending
volatile bool b_overrideLoadOn[NO_OF_DUMPLOADS]
volatile bool b_reOrderLoads
volatile uint16_t copyOf_sampleSetsDuringThisDatalogPeriod
volatile int32_t copyOf_sumP_diverted_overDL_Period
volatile uint8_t copyOf_lowestNoOfSampleSetsPerMainsCycle
volatile uint16_t copyOf_divertedEnergyTotal_Wh
volatile uint16_t copyOf_countLoadON[NO_OF_DUMPLOADS]
volatile bool EDD_isActive
volatile uint16_t absenceOfDivertedEnergyCountInSeconds
volatile bool b_diversionEnabled
constexpr int16_t DCoffset_I
uint32_t absenceOfDivertedEnergyCountInMC
int32_t sumP_grid_overDL_Period
constexpr int32_t requiredExportPerMainsCycle_inIEU
int32_t cumVdeltasThisCycle_long
constexpr int32_t IEU_per_Wh_diverted
constexpr uint8_t PERSISTENCE_FOR_POLARITY_CHANGE
constexpr int32_t DCoffset_V_min
constexpr uint8_t currentSensor_grid
uint16_t countLoadON[NO_OF_DUMPLOADS]
int32_t divertedEnergyRecent_IEU
int32_t sampleVminusDC_long
uint8_t loadPrioritiesAndState[NO_OF_DUMPLOADS]
constexpr uint8_t voltageSensor
remove_cv< remove_reference< decltype(DATALOG_PERIOD_IN_MAINS_CYCLES)>::type >::type n_cycleCountForDatalogging
constexpr uint8_t POST_TRANSITION_MAX_COUNT
constexpr int32_t antiCreepLimit_inIEUperMainsCycle
int32_t energyInBucket_long
LoadStates physicalLoadState[NO_OF_DUMPLOADS]
Polarities polarityConfirmedOfLastSampleV
constexpr int32_t upperThreshold_default
constexpr int32_t capacityOfEnergyBucket_long
Polarities polarityConfirmed
constexpr int32_t lowerThreshold_default
int32_t upperEnergyThreshold
uint16_t sampleSetsDuringNegativeHalfOfMainsCycle
void printParamsForSelectedOutputMode()
Print the settings used for the selected output mode.
int32_t sumP_diverted_overDL_Period
constexpr int32_t DCoffset_V_max
int32_t realEnergy_diverted
int32_t lowerEnergyThreshold
uint8_t lowestNoOfSampleSetsPerMainsCycle
constexpr uint8_t POST_ZERO_CROSSING_MAX_COUNT
uint16_t sampleSetsDuringThisDatalogPeriod
constexpr int32_t midPointOfEnergyBucket_long
uint8_t sampleSetsDuringThisMainsCycle
constexpr uint8_t currentSensor_diverted
uint16_t divertedEnergyTotal_Wh
Polarities polarityOfMostRecentVsample
uint8_t postTransitionCount
int32_t energyInBucket_prediction
Public functions/variables of processing engine.
constexpr uint8_t loadStateMask
constexpr uint8_t loadStateOnBit
7-segments display functions
constexpr uint32_t displayShutdown_inMainsCycles
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.
constexpr void setPinOFF(uint8_t pin)
Set the Pin state to OFF for the specified pin.
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.