3-phase PV router
Loading...
Searching...
No Matches
Functions | Variables
processing.cpp File Reference

Implements the processing engine. More...

#include <Arduino.h>
#include "calibration.h"
#include "dualtariff.h"
#include "processing.h"
#include "utils_pins.h"
Include dependency graph for processing.cpp:

Go to the source code of this file.

Functions

void confirmPolarity (const uint8_t phase)
 This routine prevents a zero-crossing point from being declared until a certain number of consecutive samples in the 'other' half of the waveform have been encountered.
 
void initializeOptionalPins ()
 Initializes the optional pins.
 
void initializeProcessing ()
 Initializes the ports and load states for processing.
 
constexpr auto initThreshold (const bool lower)
 set default threshold at compile time so the variable can be read-only
 
uint8_t nextLogicalLoadToBeAdded ()
 Retrieve the next load that could be added (be aware of the order)
 
uint8_t nextLogicalLoadToBeRemoved ()
 Retrieve the next load that could be removed (be aware of the reverse-order)
 
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 processCurrentRawSample (const uint8_t phase, const int16_t rawSample)
 Process the calculation for the actual current raw sample for the specific phase.
 
void processDataLogging ()
 Process with data logging.
 
void processLatestContribution (const uint8_t phase)
 Process the latest contribution after each phase specific new cycle additional processing is performed after each main cycle based on phase 0.
 
void processMinusHalfCycle (const uint8_t phase)
 Process the start of a new -ve half cycle, for this phase, just after the zero-crossing point.
 
void processPlusHalfCycle (const uint8_t phase)
 Process the start of a new +ve half cycle, for this phase, just after the zero-crossing point.
 
void processPolarity (const uint8_t phase, const int16_t rawSample)
 Process with the polarity for the actual voltage sample for the specific phase.
 
void processRawSamples (const uint8_t phase)
 This routine is called by the ISR when a pair of V & I sample 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 (const uint8_t phase)
 Process the startup period for the router.
 
void processVoltage (const uint8_t phase)
 Process the calculation for the current voltage sample for the specific phase.
 
void processVoltageRawSample (const uint8_t phase, const int16_t rawSample)
 Process the current voltage raw sample for the specific phase.
 
void updatePhysicalLoadStates ()
 This function provides the link between the logical and physical loads.
 
void updatePortsStates ()
 update the control ports for each of the physical loads
 

Variables

uint8_t activeLoad { NO_OF_DUMPLOADS }
 
bool b_recentTransition { false }
 
bool beyondStartUpPeriod { false }
 
uint16_t countLoadON [NO_OF_DUMPLOADS]
 
constexpr float f_capacityOfEnergyBucket_main { static_cast< float >(WORKING_ZONE_IN_JOULES * SUPPLY_FREQUENCY) }
 
float f_energyInBucket_main { 0.0F }
 
float f_lowerEnergyThreshold
 
constexpr float f_lowerThreshold_default { initThreshold(true) }
 
constexpr float f_midPointOfEnergyBucket_main { f_capacityOfEnergyBucket_main * 0.5F }
 
constexpr float f_offsetOfEnergyThresholdsInAFmode { 0.1F }
 
float f_upperEnergyThreshold
 
constexpr float f_upperThreshold_default { initThreshold(false) }
 
constexpr int16_t i_DCoffset_I_nom { 512L }
 
uint16_t i_sampleSetsDuringThisDatalogPeriod
 
int32_t l_cumVdeltasThisCycle [NO_OF_PHASES]
 
int32_t l_DCoffset_V [NO_OF_PHASES]
 
constexpr int32_t l_DCoffset_V_max { (512L + 100L) * 256L }
 
constexpr int32_t l_DCoffset_V_min { (512L - 100L) * 256L }
 
int32_t l_sampleVminusDC [NO_OF_PHASES]
 
int32_t l_sum_Vsquared [NO_OF_PHASES]
 
int32_t l_sumP [NO_OF_PHASES]
 
int32_t l_sumP_atSupplyPoint [NO_OF_PHASES]
 
remove_cv< remove_reference< decltype(DATALOG_PERIOD_IN_MAINS_CYCLES)>::type >::type n_cycleCountForDatalogging { 0 }
 
uint8_t n_lowestNoOfSampleSetsPerMainsCycle
 
uint8_t n_samplesDuringThisMainsCycle [NO_OF_PHASES]
 
constexpr OutputModes outputMode { OutputModes::NORMAL }
 
LoadStates physicalLoadState [NO_OF_DUMPLOADS]
 
Polarities polarityConfirmed [NO_OF_PHASES]
 
Polarities polarityConfirmedOfLastSampleV [NO_OF_PHASES]
 
Polarities polarityOfMostRecentSampleV [NO_OF_PHASES]
 
constexpr uint8_t POST_TRANSITION_MAX_COUNT { 3 }
 
uint8_t postTransitionCount
 

Detailed Description

Implements the processing engine.

Author
Frédéric Metrich (frede.nosp@m.ric..nosp@m.metri.nosp@m.ch@l.nosp@m.ive.f.nosp@m.r)
Version
0.1
Date
2021-10-04

Definition in file processing.cpp.

Function Documentation

◆ initializeOptionalPins()

void initializeOptionalPins ( )

Initializes the optional pins.

Definition at line 143 of file processing.cpp.

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

◆ initializeProcessing()

void initializeProcessing ( )

Initializes the ports and load states for processing.

Definition at line 91 of file processing.cpp.

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

◆ initThreshold()

constexpr auto initThreshold ( const bool  lower)
constexpr

set default threshold at compile time so the variable can be read-only

Parameters
lowerTrue to set the lower threshold, false for higher
Returns
the corresponding threshold

Definition at line 43 of file processing.cpp.

◆ printParamsForSelectedOutputMode()

void printParamsForSelectedOutputMode ( )

Print the settings used for the selected output mode.

Definition at line 799 of file processing.cpp.

Here is the caller graph for this function:

◆ updatePortsStates()

void updatePortsStates ( )

update the control ports for each of the physical loads

Definition at line 190 of file processing.cpp.

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

Variable Documentation

◆ activeLoad

uint8_t activeLoad { NO_OF_DUMPLOADS }

current active load

Definition at line 62 of file processing.cpp.

◆ b_recentTransition

bool b_recentTransition { false }

a load state has been recently toggled

Definition at line 58 of file processing.cpp.

◆ beyondStartUpPeriod

bool beyondStartUpPeriod { false }

start-up delay, allows things to settle

Definition at line 85 of file processing.cpp.

◆ countLoadON

uint16_t countLoadON[NO_OF_DUMPLOADS]

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

Definition at line 83 of file processing.cpp.

◆ f_capacityOfEnergyBucket_main

constexpr float f_capacityOfEnergyBucket_main { static_cast< float >(WORKING_ZONE_IN_JOULES * SUPPLY_FREQUENCY) }
constexpr

for resetting flexible thresholds

Definition at line 29 of file processing.cpp.

◆ f_energyInBucket_main

float f_energyInBucket_main { 0.0F }

main energy bucket (over all phases)

Definition at line 53 of file processing.cpp.

◆ f_lowerEnergyThreshold

float f_lowerEnergyThreshold

dynamic lower threshold

Definition at line 54 of file processing.cpp.

◆ f_lowerThreshold_default

constexpr float f_lowerThreshold_default { initThreshold(true) }
constexpr

lower default threshold set accordingly to the output mode

Definition at line 50 of file processing.cpp.

◆ f_midPointOfEnergyBucket_main

constexpr float f_midPointOfEnergyBucket_main { f_capacityOfEnergyBucket_main * 0.5F }
constexpr

threshold in anti-flicker mode - must not exceed 0.4

Definition at line 31 of file processing.cpp.

◆ f_offsetOfEnergyThresholdsInAFmode

constexpr float f_offsetOfEnergyThresholdsInAFmode { 0.1F }
constexpr

Definition at line 33 of file processing.cpp.

◆ f_upperEnergyThreshold

float f_upperEnergyThreshold

dynamic upper threshold

Definition at line 55 of file processing.cpp.

◆ f_upperThreshold_default

constexpr float f_upperThreshold_default { initThreshold(false) }
constexpr

upper default threshold set accordingly to the output mode

Definition at line 51 of file processing.cpp.

◆ i_DCoffset_I_nom

constexpr int16_t i_DCoffset_I_nom { 512L }
constexpr

nominal mid-point value of ADC @ x1 scale

Definition at line 24 of file processing.cpp.

◆ i_sampleSetsDuringThisDatalogPeriod

uint16_t i_sampleSetsDuringThisDatalogPeriod

number of sample sets during each datalogging period

Definition at line 71 of file processing.cpp.

◆ l_cumVdeltasThisCycle

int32_t l_cumVdeltasThisCycle[NO_OF_PHASES]

for the LPF which determines DC offset (voltage)

Definition at line 66 of file processing.cpp.

◆ l_DCoffset_V

int32_t l_DCoffset_V[NO_OF_PHASES]

<— for LPF main energy bucket for 3-phase use, with units of Joules * SUPPLY_FREQUENCY

Definition at line 26 of file processing.cpp.

◆ l_DCoffset_V_max

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

mid-point of ADC plus a working margin

Definition at line 23 of file processing.cpp.

◆ l_DCoffset_V_min

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

mid-point of ADC minus a working margin

Definition at line 22 of file processing.cpp.

◆ l_sampleVminusDC

int32_t l_sampleVminusDC[NO_OF_PHASES]

current raw voltage sample filtered

Definition at line 65 of file processing.cpp.

◆ l_sum_Vsquared

int32_t l_sum_Vsquared[NO_OF_PHASES]

for summation of V^2 values during datalog period

Definition at line 68 of file processing.cpp.

◆ l_sumP

int32_t l_sumP[NO_OF_PHASES]

cumulative power per phase

Definition at line 64 of file processing.cpp.

◆ l_sumP_atSupplyPoint

int32_t l_sumP_atSupplyPoint[NO_OF_PHASES]

for summation of 'real power' values during datalog period

Definition at line 67 of file processing.cpp.

◆ 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 73 of file processing.cpp.

◆ n_lowestNoOfSampleSetsPerMainsCycle

uint8_t n_lowestNoOfSampleSetsPerMainsCycle

For a mechanism to check the integrity of this code structure

Definition at line 75 of file processing.cpp.

◆ n_samplesDuringThisMainsCycle

uint8_t n_samplesDuringThisMainsCycle[NO_OF_PHASES]

number of sample sets for each phase during each mains cycle

Definition at line 70 of file processing.cpp.

◆ outputMode

constexpr OutputModes outputMode { OutputModes::NORMAL }
constexpr

Output mode to be used

Definition at line 35 of file processing.cpp.

◆ physicalLoadState

LoadStates physicalLoadState[NO_OF_DUMPLOADS]

Physical state of the loads

Definition at line 82 of file processing.cpp.

◆ polarityConfirmed

Polarities polarityConfirmed[NO_OF_PHASES]

for zero-crossing detection

Definition at line 79 of file processing.cpp.

◆ polarityConfirmedOfLastSampleV

Polarities polarityConfirmedOfLastSampleV[NO_OF_PHASES]

for zero-crossing detection

Definition at line 80 of file processing.cpp.

◆ polarityOfMostRecentSampleV

Polarities polarityOfMostRecentSampleV[NO_OF_PHASES]

for zero-crossing detection

Definition at line 78 of file processing.cpp.

◆ POST_TRANSITION_MAX_COUNT

constexpr uint8_t POST_TRANSITION_MAX_COUNT { 3 }
constexpr

allows each transition to take effect

Definition at line 60 of file processing.cpp.

◆ postTransitionCount

uint8_t postTransitionCount

counts the number of cycle since last transition

Definition at line 59 of file processing.cpp.