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

Implements the processing engine. More...

#include <Arduino.h>
#include "config_derived.h"
#include "dualtariff.h"
#include "processing.h"
#include "utils_pins.h"
#include "shared_var.h"
#include "mult_asm.h"
#include "ct_filter.h"
#include "energy_bucket.h"
#include "load_priorities.h"
#include "adc_chain.h"
#include "datalog_math.h"
Include dependency graph for processing.cpp:

Go to the source code of this file.

Classes

struct  adc_ctx_t
 Interrupt Service Routine - Interrupt-Driven Analog Conversion. More...
 

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 getLoadPins ()
 The pins the loads drive: the local TRIACs, plus the status LED of each remote load.
 
constexpr uint16_t getOutputPins ()
 Retrieves the output pins configuration.
 
template<typename T, size_t N>
constexpr void initializeArray (T(&array)[N], 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 int32_t initThreshold (const bool lower)
 set default threshold at compile time so the variable can be read-only
 
 ISR (ADC_vect)
 
void printParamsForSelectedOutputMode ()
 Print the settings used for the selected output mode.
 
void processCurrentRawSample (const uint8_t phase, const uint16_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 uint16_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 uint16_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

static adc_ctx_t _channels [AdcChain::NO_OF_CONVERSIONS]
 
static adc_ctx_t * _ctx = &_channels[0]
 
uint32_t absenceOfDivertedEnergyCountInMC { 0 }
 
bool b_diversionStarted { false }
 
bool beyondStartUpPeriod { false }
 
constexpr LoadPriorities::Bucket bucketLevels
 
uint16_t countLoadON [NO_OF_DUMPLOADS] {}
 
constexpr float f_offsetOfEnergyThresholdsInAFmode { 0.1F }
 
constexpr uint32_t FULL_SCALE_PER_SAMPLE { 1UL << 18 }
 
uint16_t i_DCoffset_V [NO_OF_PHASES] {}
 
constexpr uint16_t i_DCoffset_V_nom { 511U << 6 }
 
uint16_t i_sampleSetsDuringThisDatalogPeriod { 0 }
 
int16_t i_sampleVminusDC [NO_OF_PHASES] {}
 
constexpr int32_t l_capacityOfEnergyBucket_main { Energy::fromWatts(WORKING_ZONE_IN_JOULES * SUPPLY_FREQUENCY) }
 
int32_t l_energyInBucket_main { 0 }
 
uint32_t l_filterDC_V [NO_OF_PHASES] {}
 
constexpr int32_t l_lowerThreshold_default { initThreshold(true) }
 
constexpr int32_t l_midPointOfEnergyBucket_main { l_capacityOfEnergyBucket_main / 2 }
 
uint32_t l_sum_Vsquared [NO_OF_PHASES] {}
 
int32_t l_sumP [NO_OF_PHASES] {}
 
int32_t l_sumP_atSupplyPoint [NO_OF_PHASES] {}
 
constexpr int32_t l_upperThreshold_default { initThreshold(false) }
 
LoadPriorities::Switching< NO_OF_DUMPLOADS > loadSwitching
 
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 }
 
uint8_t perSecondCounter { 0 }
 
LoadStates physicalLoadState [NO_OF_DUMPLOADS] {}
 
Polarities polarityConfirmed [NO_OF_PHASES] {}
 
Polarities polarityConfirmedOfLastSampleV [NO_OF_PHASES] {}
 
Polarities polarityOfMostRecentSampleV [NO_OF_PHASES] {}
 
constexpr uint8_t powerCalPerSampleShift { Energy::toFixedPerSample< N_MIN, N_MAX >(f_powerCal).shift }
 
const Energy::PerSampleCalibration< N_MIN, N_MAX, NO_OF_PHASES > powerCalPerSample PROGMEM { Energy::toFixedPerSample< N_MIN, N_MAX >(f_powerCal) }
 

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
2026-11-24

Definition in file processing.cpp.

Function Documentation

◆ initThreshold()

int32_t 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 72 of file processing.cpp.

◆ ISR()

ISR ( ADC_vect )

Definition at line 1146 of file processing.cpp.

Here is the call graph for this function:

Variable Documentation

◆ _channels

Initial value:
= {
{ .next = &_channels[1], .index = static_cast< uint8_t >(AdcChain::Channel::V1), .admux = AdcChain::admuxWrittenAfter(PCB_VERSION, AdcChain::Channel::V1) },
{ .next = &_channels[2], .index = static_cast< uint8_t >(AdcChain::Channel::I1), .admux = AdcChain::admuxWrittenAfter(PCB_VERSION, AdcChain::Channel::I1) },
{ .next = &_channels[3], .index = static_cast< uint8_t >(AdcChain::Channel::V2), .admux = AdcChain::admuxWrittenAfter(PCB_VERSION, AdcChain::Channel::V2) },
{ .next = &_channels[4], .index = static_cast< uint8_t >(AdcChain::Channel::I2), .admux = AdcChain::admuxWrittenAfter(PCB_VERSION, AdcChain::Channel::I2) },
{ .next = &_channels[5], .index = static_cast< uint8_t >(AdcChain::Channel::V3), .admux = AdcChain::admuxWrittenAfter(PCB_VERSION, AdcChain::Channel::V3) },
{ .next = &_channels[0], .index = static_cast< uint8_t >(AdcChain::Channel::I3), .admux = AdcChain::admuxWrittenAfter(PCB_VERSION, AdcChain::Channel::I3) }
}
constexpr PcbVersion PCB_VERSION
Definition config.h:21
constexpr uint8_t admuxWrittenAfter(const PcbVersion board, const Channel converted)
The ADMUX value the interrupt writes after the conversion of a channel.
Definition adc_chain.h:107
static adc_ctx_t _channels[AdcChain::NO_OF_CONVERSIONS]
uint8_t index

Definition at line 1135 of file processing.cpp.

◆ _ctx

adc_ctx_t* _ctx = &_channels[0]
static

Definition at line 1144 of file processing.cpp.

◆ absenceOfDivertedEnergyCountInMC

uint32_t absenceOfDivertedEnergyCountInMC { 0 }

number of main cycles without diverted energy

Definition at line 112 of file processing.cpp.

◆ b_diversionStarted

bool b_diversionStarted { false }

Tracks whether diversion has started

Definition at line 64 of file processing.cpp.

◆ beyondStartUpPeriod

bool beyondStartUpPeriod { false }

start-up delay, allows things to settle

Definition at line 116 of file processing.cpp.

◆ bucketLevels

LoadPriorities::Bucket bucketLevels
constexpr
Initial value:
constexpr int32_t l_capacityOfEnergyBucket_main
constexpr int32_t l_upperThreshold_default
constexpr int32_t l_midPointOfEnergyBucket_main
constexpr int32_t l_lowerThreshold_default

the fixed levels of the main energy bucket

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

◆ f_offsetOfEnergyThresholdsInAFmode

float f_offsetOfEnergyThresholdsInAFmode { 0.1F }
constexpr

Definition at line 43 of file processing.cpp.

◆ FULL_SCALE_PER_SAMPLE

uint32_t FULL_SCALE_PER_SAMPLE { 1UL << 18 }
constexpr

Definition at line 47 of file processing.cpp.

◆ i_DCoffset_V

uint16_t i_DCoffset_V[NO_OF_PHASES] {}

<— for LPF main energy bucket for 3-phase use, with units of Joules * SUPPLY_FREQUENCY * 2^Energy::FRACTION_BITS

Definition at line 36 of file processing.cpp.

◆ i_DCoffset_V_nom

uint16_t i_DCoffset_V_nom { 511U << 6 }
constexpr

nominal mid-point value of ADC @ x64 scale

Definition at line 35 of file processing.cpp.

◆ i_sampleSetsDuringThisDatalogPeriod

uint16_t i_sampleSetsDuringThisDatalogPeriod { 0 }

number of sample sets during each datalogging period

Definition at line 98 of file processing.cpp.

◆ i_sampleVminusDC

int16_t i_sampleVminusDC[NO_OF_PHASES] {}

current raw voltage sample filtered (left-aligned ADC)

Definition at line 91 of file processing.cpp.

◆ l_capacityOfEnergyBucket_main

int32_t l_capacityOfEnergyBucket_main { Energy::fromWatts(WORKING_ZONE_IN_JOULES * SUPPLY_FREQUENCY) }
constexpr

for resetting flexible thresholds

Definition at line 39 of file processing.cpp.

◆ l_energyInBucket_main

int32_t l_energyInBucket_main { 0 }

main energy bucket (over all phases)

Definition at line 82 of file processing.cpp.

◆ l_filterDC_V

uint32_t l_filterDC_V[NO_OF_PHASES] {}

for the LPF which determines DC offset (voltage)

Definition at line 92 of file processing.cpp.

◆ l_lowerThreshold_default

int32_t l_lowerThreshold_default { initThreshold(true) }
constexpr

lower default threshold set accordingly to the output mode

Definition at line 79 of file processing.cpp.

◆ l_midPointOfEnergyBucket_main

int32_t l_midPointOfEnergyBucket_main { l_capacityOfEnergyBucket_main / 2 }
constexpr

threshold in anti-flicker mode - must not exceed 0.4

Definition at line 41 of file processing.cpp.

◆ l_sum_Vsquared

uint32_t l_sum_Vsquared[NO_OF_PHASES] {}

for summation of V^2 values during datalog period

Definition at line 95 of file processing.cpp.

◆ l_sumP

int32_t l_sumP[NO_OF_PHASES] {}

cumulative power per phase

Definition at line 90 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 94 of file processing.cpp.

◆ l_upperThreshold_default

int32_t l_upperThreshold_default { initThreshold(false) }
constexpr

upper default threshold set accordingly to the output mode

Definition at line 80 of file processing.cpp.

◆ loadSwitching

for improved control of multiple loads

Definition at line 88 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 100 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 102 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 97 of file processing.cpp.

◆ outputMode

OutputModes outputMode { OutputModes::NORMAL }
constexpr

Output mode to be used

Definition at line 62 of file processing.cpp.

◆ perSecondCounter

uint8_t perSecondCounter { 0 }

for counting every second inside the ISR

Definition at line 114 of file processing.cpp.

◆ physicalLoadState

LoadStates physicalLoadState[NO_OF_DUMPLOADS] {}

Physical state of the loads

Definition at line 109 of file processing.cpp.

◆ polarityConfirmed

Polarities polarityConfirmed[NO_OF_PHASES] {}

for zero-crossing detection

Definition at line 106 of file processing.cpp.

◆ polarityConfirmedOfLastSampleV

Polarities polarityConfirmedOfLastSampleV[NO_OF_PHASES] {}

for zero-crossing detection

Definition at line 107 of file processing.cpp.

◆ polarityOfMostRecentSampleV

Polarities polarityOfMostRecentSampleV[NO_OF_PHASES] {}

for zero-crossing detection

Definition at line 105 of file processing.cpp.

◆ powerCalPerSampleShift

uint8_t powerCalPerSampleShift { Energy::toFixedPerSample< N_MIN, N_MAX >(f_powerCal).shift }
constexpr

Definition at line 57 of file processing.cpp.

◆ PROGMEM

power calibration over n, in fixed point, for every expected n: no division in the ISR

override bit of each load: the ISR tests a mask instead of shifting by the pin

Definition at line 56 of file processing.cpp.