3-phase PV router
Loading...
Searching...
No Matches
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)
 Confirms the polarity of the current voltage sample for a specific phase.
 
constexpr uint16_t getInputPins ()
 Retrieves the input pins configuration.
 
constexpr uint16_t getOutputPins ()
 Retrieves the output pins configuration.
 
template<size_t N>
constexpr void initializeArray (int32_t(&array)[N], int32_t value)
 Initializes all elements of a given array to a specified value.
 
void initializeProcessing ()
 Initializes the processing engine, including ports, load states, and ADC setup.
 
constexpr auto initThreshold (const bool lower)
 set default threshold at compile time so the variable can be read-only
 
 ISR (ADC_vect)
 Interrupt Service Routine - Interrupt-Driven Analog Conversion.
 
uint8_t nextLogicalLoadToBeAdded ()
 Retrieve the next logical load that could be added.
 
uint8_t nextLogicalLoadToBeRemoved ()
 Retrieve the next logical load that could be removed (in reverse order).
 
void printParamsForSelectedOutputMode ()
 Print the settings used for the selected output mode.
 
void proceedHighEnergyLevel ()
 Handles the case when the energy level is high, potentially adding a load.
 
void proceedLowEnergyLevel ()
 Handles the case when the energy level is low, potentially removing a load.
 
void processCurrentRawSample (const uint8_t phase, const int16_t rawSample)
 Processes the current raw sample for the specified phase.
 
void processDataLogging ()
 Process data logging at the end of each logging period.
 
void processLatestContribution (const uint8_t phase)
 Process the latest contribution after each phase-specific new cycle.
 
void processMinusHalfCycle (const uint8_t phase)
 Processes the start of a new negative half cycle for the specified phase.
 
void processPlusHalfCycle (const uint8_t phase)
 Process the start of a new positive half cycle for the specified phase.
 
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.
 
void processStartNewCycle ()
 Processes the start of a new mains cycle on phase 0.
 
void processStartUp (const uint8_t phase)
 Processes the startup period for the router.
 
void processVoltage (const uint8_t phase)
 Processes the current voltage sample 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 updatePhysicalLoadStates ()
 Updates the physical load states based on logical load priorities and states.
 
void updatePortsStates ()
 Updates 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 { 0.0F }
 
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 { 0.0F }
 
constexpr float f_upperThreshold_default { initThreshold(false) }
 
constexpr int16_t i_DCoffset_I_nom { 512L }
 
uint16_t i_sampleSetsDuringThisDatalogPeriod { 0 }
 
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 { 0 }
 
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 { 0 }
 

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

◆ initThreshold()

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.

This function displays the relevant configuration parameters for the currently selected output mode. It provides details about the energy bucket capacity, thresholds, and mode-specific settings.

  • For the "normal" mode, it displays the energy bucket capacity and thresholds.
  • For the "anti-flicker" mode, it also displays the offset of energy thresholds.

Definition at line 1027 of file processing.cpp.

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

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 { 0.0F }

dynamic lower threshold

Definition at line 54 of file processing.cpp.

◆ f_lowerThreshold_default

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

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

float f_offsetOfEnergyThresholdsInAFmode { 0.1F }
constexpr

Definition at line 33 of file processing.cpp.

◆ f_upperEnergyThreshold

float f_upperEnergyThreshold { 0.0F }

dynamic upper threshold

Definition at line 55 of file processing.cpp.

◆ f_upperThreshold_default

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

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 { 0 }

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

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

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 { 0 }

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

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

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 { 0 }

counts the number of cycle since last transition

Definition at line 59 of file processing.cpp.