1-phase PV router
Loading...
Searching...
No Matches
processing.cpp File Reference
#include <Arduino.h>
#include "calibration.h"
#include "dualtariff.h"
#include "processing.h"
#include "utils_pins.h"
#include "utils_display.h"
Include dependency graph for processing.cpp:

Go to the source code of this file.

Functions

void confirmPolarity ()
 Confirms the polarity of the current voltage sample.
 
constexpr uint16_t getInputPins ()
 Retrieves the input pins configuration.
 
constexpr uint16_t getOutputPins ()
 Retrieves the output pins configuration.
 
void initializeOldPCBPins ()
 Initializes optional pins for the old PCB 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 logLoadPriorities ()
 Prints the load priorities to the Serial output.
 
uint8_t nextLogicalLoadToBeAdded ()
 Retrieve the next load that could be removed (be aware of the reverse-order).
 
uint8_t nextLogicalLoadToBeRemoved ()
 Process with data logging.
 
void printParamsForSelectedOutputMode ()
 Print the settings used for the selected output mode.
 
void proceedHighEnergyLevel ()
 Process the case of high energy level, some action may be required.
 
void proceedLowEnergyLevel ()
 Process the case of low energy level, some action may be required.
 
void processDataLogging ()
 Process with data logging.
 
void processDivertedCurrentRawSample (const int16_t rawSample)
 Processes the raw current sample for the diverted connection point.
 
void processGridCurrentRawSample (const int16_t rawSample)
 Processes the raw current sample for the grid connection point.
 
void processLatestContribution ()
 Process the latest contribution after each new cycle.
 
void processMinusHalfCycle ()
 Process the start of a new -ve half cycle, just after the zero-crossing point.
 
void processPlusHalfCycle ()
 Process the start of a new +ve half cycle, just after the zero-crossing point.
 
void processPolarity (const int16_t rawSample)
 Processes 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 processStartNewCycle ()
 This code is executed once per 20ms, shortly after the start of each new mains cycle on phase 0.
 
void processStartUp ()
 Process the startup period for the router.
 
void processVoltage ()
 Process the calculation for the current voltage sample.
 
void processVoltageRawSample (const int16_t rawSample)
 Process the current voltage raw sample.
 
void updatePhysicalLoadStates ()
 This function provides the link between the logical and physical loads.
 
void updatePortsStates ()
 Updates the control ports for each of the physical loads.
 

Variables

uint32_t absenceOfDivertedEnergyCountInMC { 0 }
 
uint8_t activeLoad { NO_OF_DUMPLOADS }
 
constexpr int32_t antiCreepLimit_inIEUperMainsCycle { static_cast< int32_t >(ANTI_CREEP_LIMIT * (1.0f / powerCal_diverted)) }
 
bool beyondStartUpPeriod { false }
 
constexpr int32_t capacityOfEnergyBucket_long { static_cast< int32_t >(WORKING_ZONE_IN_JOULES * SUPPLY_FREQUENCY * (1.0f / powerCal_grid)) }
 
uint16_t countLoadON [NO_OF_DUMPLOADS] {}
 
int32_t cumVdeltasThisCycle_long { 0 }
 
constexpr uint8_t currentSensor_diverted { OLD_PCB ? 4 : 3 }
 
constexpr uint8_t currentSensor_grid { OLD_PCB ? 5 : 1 }
 
constexpr int16_t DCoffset_I { 512 }
 
int32_t DCoffset_V_long { 512L * 256 }
 
constexpr int32_t DCoffset_V_max { (512L + 100) * 256 }
 
constexpr int32_t DCoffset_V_min { (512L - 100) * 256 }
 
int32_t divertedEnergyRecent_IEU { 0 }
 
uint16_t divertedEnergyTotal_Wh { 0 }
 
int32_t energyInBucket_long { 0 }
 
int32_t energyInBucket_prediction { 0 }
 
constexpr int32_t IEU_per_Wh_diverted { static_cast< int32_t >(JOULES_PER_WATT_HOUR * SUPPLY_FREQUENCY * (1.0f / powerCal_diverted)) }
 
int32_t l_sum_Vsquared { 0 }
 
uint8_t loadPrioritiesAndState [NO_OF_DUMPLOADS]
 
int32_t lowerEnergyThreshold { 0 }
 
constexpr int32_t lowerThreshold_default { midPointOfEnergyBucket_long }
 
uint8_t lowestNoOfSampleSetsPerMainsCycle { 0 }
 
constexpr int32_t midPointOfEnergyBucket_long { capacityOfEnergyBucket_long >> 1 }
 
remove_cv< remove_reference< decltype(DATALOG_PERIOD_IN_MAINS_CYCLES)>::type >::type n_cycleCountForDatalogging { 0 }
 
uint8_t perSecondCounter { 0 }
 
constexpr uint8_t PERSISTENCE_FOR_POLARITY_CHANGE { 1 }
 
LoadStates physicalLoadState [NO_OF_DUMPLOADS]
 
Polarities polarityConfirmed
 
Polarities polarityConfirmedOfLastSampleV
 
Polarities polarityOfMostRecentVsample
 
constexpr uint8_t POST_TRANSITION_MAX_COUNT { 3 }
 
constexpr uint8_t POST_ZERO_CROSSING_MAX_COUNT { 3 }
 
uint8_t postTransitionCount { 0 }
 
int32_t realEnergy_diverted { 0 }
 
int32_t realEnergy_grid { 0 }
 
bool recentTransition { false }
 
constexpr int32_t requiredExportPerMainsCycle_inIEU { static_cast< int32_t >(REQUIRED_EXPORT_IN_WATTS * (1.0f / powerCal_grid)) }
 
uint16_t sampleSetsDuringNegativeHalfOfMainsCycle { 0 }
 
uint16_t sampleSetsDuringThisDatalogPeriod { 0 }
 
uint8_t sampleSetsDuringThisMainsCycle { 0 }
 
int32_t sampleVminusDC_long { 0 }
 
int32_t sumP_diverted { 0 }
 
int32_t sumP_diverted_overDL_Period { 0 }
 
int32_t sumP_grid { 0 }
 
int32_t sumP_grid_overDL_Period { 0 }
 
int32_t upperEnergyThreshold { 0 }
 
constexpr int32_t upperThreshold_default { midPointOfEnergyBucket_long }
 
constexpr uint8_t voltageSensor { OLD_PCB ? 3 : 0 }
 

Function Documentation

◆ printParamsForSelectedOutputMode()

void printParamsForSelectedOutputMode ( )

Print the settings used for the selected output mode.

This function outputs the configuration parameters related to the selected output mode. It is primarily used for debugging and verifying the system's configuration.

  • Prints the zero-crossing persistence value in sample sets.
  • Prints the capacity of the energy bucket in integer energy units.

Definition at line 1176 of file processing.cpp.

References capacityOfEnergyBucket_long, DBUG, DBUGLN, and PERSISTENCE_FOR_POLARITY_CHANGE.

Referenced by printConfiguration().

Here is the caller graph for this function:

Variable Documentation

◆ absenceOfDivertedEnergyCountInMC

uint32_t absenceOfDivertedEnergyCountInMC { 0 }

number of main cycles without diverted energy

Definition at line 104 of file processing.cpp.

Referenced by processLatestContribution(), and processRawSamples().

◆ activeLoad

uint8_t activeLoad { NO_OF_DUMPLOADS }

current active load

Definition at line 75 of file processing.cpp.

Referenced by proceedHighEnergyLevel(), and proceedLowEnergyLevel().

◆ antiCreepLimit_inIEUperMainsCycle

int32_t antiCreepLimit_inIEUperMainsCycle { static_cast< int32_t >(ANTI_CREEP_LIMIT * (1.0f / powerCal_diverted)) }
constexpr

threshold value in Integer Energy Units (IEU) that prevents small measurement noise from being incorrectly registered as diverted energy

Definition at line 40 of file processing.cpp.

Referenced by processLatestContribution().

◆ beyondStartUpPeriod

bool beyondStartUpPeriod { false }

start-up delay, allows things to settle

Definition at line 110 of file processing.cpp.

Referenced by processDataLogging(), processRawSamples(), and processStartUp().

◆ capacityOfEnergyBucket_long

int32_t capacityOfEnergyBucket_long { static_cast< int32_t >(WORKING_ZONE_IN_JOULES * SUPPLY_FREQUENCY * (1.0f / powerCal_grid)) }
constexpr

main energy bucket for single-phase use, with units of Joules * SUPPLY_FREQUENCY

Definition at line 33 of file processing.cpp.

Referenced by printParamsForSelectedOutputMode(), proceedHighEnergyLevel(), processRawSamples(), and processStartNewCycle().

◆ countLoadON

uint16_t countLoadON[NO_OF_DUMPLOADS] {}

Number of cycle the load was ON (over 1 datalog period)

Definition at line 102 of file processing.cpp.

Referenced by processDataLogging(), and updatePortsStates().

◆ cumVdeltasThisCycle_long

int32_t cumVdeltasThisCycle_long { 0 }

for the LPF which determines DC offset (voltage)

Definition at line 81 of file processing.cpp.

Referenced by processMinusHalfCycle(), and processVoltage().

◆ currentSensor_diverted

uint8_t currentSensor_diverted { OLD_PCB ? 4 : 3 }
inlineconstexpr

A3 is for CT2 which measures diverted current (A4 for the old PCB)

Definition at line 14 of file processing.cpp.

Referenced by ISR().

◆ currentSensor_grid

uint8_t currentSensor_grid { OLD_PCB ? 5 : 1 }
inlineconstexpr

A1 is for CT1 which measures grid current (A5 for the old PCB)

Definition at line 13 of file processing.cpp.

Referenced by ISR().

◆ DCoffset_I

int16_t DCoffset_I { 512 }
constexpr

nominal mid-point value of ADC @ x1 scale

Definition at line 31 of file processing.cpp.

Referenced by processDivertedCurrentRawSample(), and processGridCurrentRawSample().

◆ DCoffset_V_long

int32_t DCoffset_V_long { 512L * 256 }

<— for LPF

Definition at line 27 of file processing.cpp.

Referenced by processMinusHalfCycle(), and processPolarity().

◆ DCoffset_V_max

int32_t DCoffset_V_max { (512L + 100) * 256 }
constexpr

mid-point of ADC plus a working margin

Definition at line 29 of file processing.cpp.

Referenced by processMinusHalfCycle().

◆ DCoffset_V_min

int32_t DCoffset_V_min { (512L - 100) * 256 }
constexpr

mid-point of ADC minus a working margin

Definition at line 28 of file processing.cpp.

Referenced by processMinusHalfCycle().

◆ divertedEnergyRecent_IEU

int32_t divertedEnergyRecent_IEU { 0 }

Hi-res accumulator of limited range

Definition at line 60 of file processing.cpp.

Referenced by processLatestContribution().

◆ divertedEnergyTotal_Wh

uint16_t divertedEnergyTotal_Wh { 0 }

WattHour register of 63K range

Definition at line 61 of file processing.cpp.

Referenced by processDataLogging(), and processLatestContribution().

◆ energyInBucket_long

int32_t energyInBucket_long { 0 }

◆ energyInBucket_prediction

int32_t energyInBucket_prediction { 0 }

predicted energy level at the end of the current mains cycle

Definition at line 86 of file processing.cpp.

Referenced by proceedHighEnergyLevel(), proceedLowEnergyLevel(), processMinusHalfCycle(), and processRawSamples().

◆ IEU_per_Wh_diverted

int32_t IEU_per_Wh_diverted { static_cast< int32_t >(JOULES_PER_WATT_HOUR * SUPPLY_FREQUENCY * (1.0f / powerCal_diverted)) }
constexpr

Definition at line 70 of file processing.cpp.

Referenced by processLatestContribution().

◆ l_sum_Vsquared

int32_t l_sum_Vsquared { 0 }

for summation of V^2 values during datalog period

Definition at line 82 of file processing.cpp.

Referenced by processDataLogging(), and processVoltage().

◆ loadPrioritiesAndState

◆ lowerEnergyThreshold

int32_t lowerEnergyThreshold { 0 }

dynamic lower threshold

Definition at line 57 of file processing.cpp.

Referenced by proceedLowEnergyLevel(), and processRawSamples().

◆ lowerThreshold_default

int32_t lowerThreshold_default { midPointOfEnergyBucket_long }
constexpr

default lower threshold for the energy bucket (50% of capacity)

Definition at line 37 of file processing.cpp.

Referenced by processRawSamples().

◆ lowestNoOfSampleSetsPerMainsCycle

uint8_t lowestNoOfSampleSetsPerMainsCycle { 0 }

For a mechanism to check the integrity of this code structure

Definition at line 97 of file processing.cpp.

Referenced by processDataLogging(), processPlusHalfCycle(), and processStartUp().

◆ midPointOfEnergyBucket_long

int32_t midPointOfEnergyBucket_long { capacityOfEnergyBucket_long >> 1 }
constexpr

for resetting flexible thresholds

Definition at line 35 of file processing.cpp.

Referenced by processRawSamples().

◆ n_cycleCountForDatalogging

remove_cv<remove_reference<decltype(DATALOG_PERIOD_IN_MAINS_CYCLES)>::type>::type n_cycleCountForDatalogging { 0 }

for counting how often datalog is updated

Definition at line 106 of file processing.cpp.

Referenced by processDataLogging().

◆ perSecondCounter

uint8_t perSecondCounter { 0 }

for counting every second inside the ISR

Definition at line 108 of file processing.cpp.

Referenced by processLatestContribution().

◆ PERSISTENCE_FOR_POLARITY_CHANGE

uint8_t PERSISTENCE_FOR_POLARITY_CHANGE { 1 }
inlineconstexpr

allows polarity changes to be confirmed

Definition at line 20 of file processing.cpp.

Referenced by confirmPolarity(), and printParamsForSelectedOutputMode().

◆ physicalLoadState

LoadStates physicalLoadState[NO_OF_DUMPLOADS]

Physical state of the loads

Definition at line 101 of file processing.cpp.

Referenced by updatePhysicalLoadStates(), and updatePortsStates().

◆ polarityConfirmed

Polarities polarityConfirmed

for zero-crossing detection

Definition at line 90 of file processing.cpp.

Referenced by confirmPolarity(), processRawSamples(), and processVoltage().

◆ polarityConfirmedOfLastSampleV

Polarities polarityConfirmedOfLastSampleV

for zero-crossing detection

Definition at line 91 of file processing.cpp.

Referenced by confirmPolarity(), processRawSamples(), and processVoltage().

◆ polarityOfMostRecentVsample

Polarities polarityOfMostRecentVsample

for zero-crossing detection

Definition at line 89 of file processing.cpp.

Referenced by confirmPolarity(), and processPolarity().

◆ POST_TRANSITION_MAX_COUNT

uint8_t POST_TRANSITION_MAX_COUNT { 3 }
constexpr

allows each transition to take effect

Definition at line 74 of file processing.cpp.

Referenced by processRawSamples().

◆ POST_ZERO_CROSSING_MAX_COUNT

uint8_t POST_ZERO_CROSSING_MAX_COUNT { 3 }
inlineconstexpr

allows trigger device to be reliably armed

Definition at line 22 of file processing.cpp.

Referenced by processRawSamples().

◆ postTransitionCount

uint8_t postTransitionCount { 0 }

counts the number of cycle since last transition

Definition at line 73 of file processing.cpp.

Referenced by proceedHighEnergyLevel(), proceedLowEnergyLevel(), and processRawSamples().

◆ realEnergy_diverted

int32_t realEnergy_diverted { 0 }

stores the calculated real energy diverted to controlled loads (CT2) for the current mains cycle

Definition at line 85 of file processing.cpp.

Referenced by processLatestContribution().

◆ realEnergy_grid

int32_t realEnergy_grid { 0 }

stores the calculated real energy from the grid connection point (CT1) for the current mains cycle

Definition at line 84 of file processing.cpp.

Referenced by processLatestContribution().

◆ recentTransition

bool recentTransition { false }

a load state has been recently toggled

Definition at line 72 of file processing.cpp.

Referenced by proceedHighEnergyLevel(), proceedLowEnergyLevel(), and processRawSamples().

◆ requiredExportPerMainsCycle_inIEU

int32_t requiredExportPerMainsCycle_inIEU { static_cast< int32_t >(REQUIRED_EXPORT_IN_WATTS * (1.0f / powerCal_grid)) }
constexpr

target amount of energy to be exported to the grid during each mains cycle, expressed in Integer Energy Units (IEU)

Definition at line 41 of file processing.cpp.

Referenced by processLatestContribution().

◆ sampleSetsDuringNegativeHalfOfMainsCycle

uint16_t sampleSetsDuringNegativeHalfOfMainsCycle { 0 }

for arming the triac/trigger

Definition at line 99 of file processing.cpp.

Referenced by processRawSamples(), and processStartNewCycle().

◆ sampleSetsDuringThisDatalogPeriod

uint16_t sampleSetsDuringThisDatalogPeriod { 0 }

number of sample sets during each datalogging period

Definition at line 95 of file processing.cpp.

Referenced by processDataLogging(), and processVoltageRawSample().

◆ sampleSetsDuringThisMainsCycle

uint8_t sampleSetsDuringThisMainsCycle { 0 }

number of sample sets during each mains cycle

Definition at line 94 of file processing.cpp.

Referenced by processLatestContribution(), processMinusHalfCycle(), processPlusHalfCycle(), processStartNewCycle(), processStartUp(), and processVoltageRawSample().

◆ sampleVminusDC_long

int32_t sampleVminusDC_long { 0 }

voltage sample with DC offset removed

Definition at line 87 of file processing.cpp.

Referenced by processDivertedCurrentRawSample(), processGridCurrentRawSample(), processPolarity(), and processVoltage().

◆ sumP_diverted

int32_t sumP_diverted { 0 }

for per-cycle summation of 'real power'

Definition at line 79 of file processing.cpp.

Referenced by processDivertedCurrentRawSample(), processLatestContribution(), processStartNewCycle(), and processStartUp().

◆ sumP_diverted_overDL_Period

int32_t sumP_diverted_overDL_Period { 0 }

for per-cycle summation of 'real power' during datalog period

Definition at line 80 of file processing.cpp.

Referenced by processDataLogging(), processDivertedCurrentRawSample(), and processStartUp().

◆ sumP_grid

int32_t sumP_grid { 0 }

for per-cycle summation of 'real power'

Definition at line 77 of file processing.cpp.

Referenced by processGridCurrentRawSample(), processLatestContribution(), processMinusHalfCycle(), processStartNewCycle(), and processStartUp().

◆ sumP_grid_overDL_Period

int32_t sumP_grid_overDL_Period { 0 }

for per-cycle summation of 'real power' during datalog period

Definition at line 78 of file processing.cpp.

Referenced by processDataLogging(), processGridCurrentRawSample(), and processStartUp().

◆ upperEnergyThreshold

int32_t upperEnergyThreshold { 0 }

dynamic upper threshold

Definition at line 58 of file processing.cpp.

Referenced by proceedHighEnergyLevel(), and processRawSamples().

◆ upperThreshold_default

int32_t upperThreshold_default { midPointOfEnergyBucket_long }
constexpr

default upper threshold for the energy bucket (50% of capacity)

Definition at line 38 of file processing.cpp.

Referenced by processRawSamples().

◆ voltageSensor

uint8_t voltageSensor { OLD_PCB ? 3 : 0 }
inlineconstexpr

A0 is for the voltage sensor (A3 for the old PCB)

Definition at line 12 of file processing.cpp.

Referenced by ISR().