1-phase PV router
|
Functions used by the ISR. More...
Functions | |
void | confirmPolarity () |
Confirms the polarity of the current voltage sample. | |
ISR (ADC_vect) | |
Interrupt Service Routine - Interrupt-Driven Analog Conversion. | |
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 | 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. | |
Functions used by the ISR.
This group contains functions that are executed within the Interrupt Service Routine (ISR) or are closely tied to time-sensitive operations. These functions are optimized for speed and efficiency to ensure the system operates without delays or interruptions.
|
inline |
Confirms the polarity of the current voltage sample.
This function ensures that a zero-crossing point is not declared until a specified number of consecutive samples in the opposite half of the waveform have been encountered. It helps to stabilize polarity detection and avoid false zero-crossing detections.
PERSISTENCE_FOR_POLARITY_CHANGE
), the polarity is confirmed and updated. Definition at line 483 of file processing.cpp.
References PERSISTENCE_FOR_POLARITY_CHANGE, polarityConfirmed, polarityConfirmedOfLastSampleV, and polarityOfMostRecentVsample.
Referenced by processVoltageRawSample().
ISR | ( | ADC_vect | ) |
Interrupt Service Routine - Interrupt-Driven Analog Conversion.
This ISR is triggered whenever an ADC conversion is completed, approximately every 104 µs in free-running mode. It processes the results of the completed conversion and sets up the next conversion in sequence. The ISR handles voltage and current samples for the grid and diverted connections, ensuring real-time processing of energy data.
An Interrupt Service Routine is now defined which instructs the ADC to perform a conversion for each of the voltage and current sensors in turn.
This Interrupt Service Routine is for use when the ADC is in the free-running mode. It is executed whenever an ADC conversion has finished, approx every 104 µs. In free-running mode, the ADC has already started its next conversion by the time that the ISR is executed. The ISR therefore needs to "look ahead".
At the end of conversion Type N, conversion Type N+1 will start automatically. The ISR which runs at this point therefore needs to capture the results of conversion Type N, and set up the conditions for conversion Type N+2, and so on.
By means of various helper functions, all of the time-critical activities are processed within the ISR.
The main code is notified by means of a flag when fresh copies of loggable data are available.
volatile
for shared variables.Definition at line 1221 of file processing.cpp.
References currentSensor_diverted, currentSensor_grid, processDivertedCurrentRawSample(), processGridCurrentRawSample(), processVoltageRawSample(), and voltageSensor.
|
inline |
Retrieve the next load that could be removed (be aware of the reverse-order).
This function identifies the next logical load that can be switched OFF based on the reverse order of load priorities. It ensures that the highest-priority load remains active while deactivating lower-priority loads as needed.
Definition at line 1068 of file processing.cpp.
References loadPrioritiesAndState, loadStateOnBit, and NO_OF_DUMPLOADS.
Referenced by proceedHighEnergyLevel().
|
inline |
Process with data logging.
At the end of each datalogging period, this function copies the relevant variables for use by the main code. These variables are then reset for the next datalogging period.
Definition at line 1095 of file processing.cpp.
References loadPrioritiesAndState, loadStateOnBit, and NO_OF_DUMPLOADS.
Referenced by proceedLowEnergyLevel().
|
inline |
Process the case of high energy level, some action may be required.
This function determines whether additional loads can be switched ON when the energy level in the bucket exceeds the upper threshold. It ensures that the energy thresholds remain within valid limits and handles load activation during the post-transition period.
Definition at line 963 of file processing.cpp.
References activeLoad, capacityOfEnergyBucket_long, energyInBucket_prediction, loadPrioritiesAndState, loadStateOnBit, nextLogicalLoadToBeAdded(), NO_OF_DUMPLOADS, postTransitionCount, recentTransition, and upperEnergyThreshold.
Referenced by processRawSamples().
|
inline |
Process the case of low energy level, some action may be required.
This function determines whether loads can be switched OFF when the energy level in the bucket falls below the lower threshold. It ensures that the energy thresholds remain within valid limits and handles load deactivation during the post-transition period.
Definition at line 1015 of file processing.cpp.
References activeLoad, energyInBucket_prediction, loadPrioritiesAndState, loadStateMask, lowerEnergyThreshold, nextLogicalLoadToBeRemoved(), NO_OF_DUMPLOADS, postTransitionCount, and recentTransition.
Referenced by processRawSamples().
|
inline |
Process with data logging.
At the end of each datalogging period, this function copies the relevant variables for use by the main code. These variables are then reset for the next datalogging period.
Definition at line 1124 of file processing.cpp.
References Shared::b_datalogEventPending, beyondStartUpPeriod, Shared::copyOf_countLoadON, Shared::copyOf_divertedEnergyTotal_Wh_forDL, Shared::copyOf_energyInBucket_long, Shared::copyOf_lowestNoOfSampleSetsPerMainsCycle, Shared::copyOf_sampleSetsDuringThisDatalogPeriod, Shared::copyOf_sum_Vsquared, Shared::copyOf_sumP_diverted_overDL_Period, Shared::copyOf_sumP_grid_overDL_Period, countLoadON, DATALOG_PERIOD_IN_MAINS_CYCLES, divertedEnergyTotal_Wh, energyInBucket_long, i, l_sum_Vsquared, lowestNoOfSampleSetsPerMainsCycle, n_cycleCountForDatalogging, NO_OF_DUMPLOADS, sampleSetsDuringThisDatalogPeriod, sumP_diverted_overDL_Period, and sumP_grid_overDL_Period.
Referenced by processPlusHalfCycle().
|
inline |
Processes the raw current sample for the diverted connection point.
This function calculates the real power at the diverted connection point using the raw current sample from the ADC. It applies a low-pass filter (LPF) to offset the high-pass filter (HPF) effect of the current transformer (CT). The filtered current sample is then used to compute the instantaneous power, which is accumulated for further processing.
rawSample | The raw current sample from the ADC. |
Definition at line 447 of file processing.cpp.
References Shared::b_diversionEnabled, Shared::b_overrideLoadOn, DCoffset_I, sampleVminusDC_long, sumP_diverted, and sumP_diverted_overDL_Period.
Referenced by ISR().
|
inline |
Processes the raw current sample for the grid connection point.
This function calculates the real power at the grid connection point using the raw current sample from the ADC. It applies a low-pass filter (LPF) to offset the high-pass filter (HPF) effect of the current transformer (CT). The filtered current sample is then used to compute the instantaneous power, which is accumulated for further processing.
rawSample | The raw current sample from the ADC. |
Definition at line 406 of file processing.cpp.
References alpha, DCoffset_I, lpf_gain, sampleVminusDC_long, sumP_grid, and sumP_grid_overDL_Period.
Referenced by ISR().
|
inline |
Process the latest contribution after each new cycle.
This function calculates the real power and energy during the last whole mains cycle. It updates the energy bucket and prepares the system for the next cycle.
Definition at line 856 of file processing.cpp.
References absenceOfDivertedEnergyCountInMC, Shared::absenceOfDivertedEnergyCountInSeconds, antiCreepLimit_inIEUperMainsCycle, Shared::b_newCycle, Shared::copyOf_divertedEnergyTotal_Wh, displayShutdown_inMainsCycles, divertedEnergyRecent_IEU, divertedEnergyTotal_Wh, Shared::EDD_isActive, energyInBucket_long, IEU_per_Wh_diverted, perSecondCounter, realEnergy_diverted, realEnergy_grid, requiredExportPerMainsCycle_inIEU, sampleSetsDuringThisMainsCycle, sumP_diverted, sumP_grid, and SUPPLY_FREQUENCY.
Referenced by processPlusHalfCycle().
|
inline |
Process the start of a new -ve half cycle, just after the zero-crossing point.
This function handles the processing required at the start of a new negative half-cycle of the mains waveform. It updates the low-pass filter (LPF) for DC offset removal, ensures the LPF output remains within valid limits, and predicts the energy state at the end of the mains cycle.
Definition at line 801 of file processing.cpp.
References cumVdeltasThisCycle_long, DCoffset_V_long, DCoffset_V_max, DCoffset_V_min, energyInBucket_long, energyInBucket_prediction, sampleSetsDuringThisMainsCycle, and sumP_grid.
Referenced by processRawSamples().
|
inline |
Process the start of a new +ve half cycle, just after the zero-crossing point.
This function handles the processing required at the start of a new positive half-cycle of the mains waveform. It updates performance metrics, processes the latest energy contributions, and resets accumulators for the new cycle.
processLatestContribution()
.processDataLogging()
.Definition at line 772 of file processing.cpp.
References lowestNoOfSampleSetsPerMainsCycle, processDataLogging(), processLatestContribution(), and sampleSetsDuringThisMainsCycle.
Referenced by processRawSamples().
|
inline |
Processes the polarity of the current voltage sample.
This function determines the polarity of the voltage sample by removing the DC offset and comparing the adjusted value to zero. The polarity is then stored for use in zero-crossing detection and other processing steps.
polarityOfMostRecentVsample
variable with the determined polarity.rawSample | The raw voltage sample from the ADC. |
Definition at line 379 of file processing.cpp.
References DCoffset_V_long, NEGATIVE, polarityOfMostRecentVsample, POSITIVE, and sampleVminusDC_long.
Referenced by processVoltageRawSample().
|
inline |
This routine is called by the ISR when a pair of V & I samples becomes available.
This function processes raw voltage and current samples to determine the energy state and manage load control. It handles zero-crossing detection, energy bucket updates, and load state adjustments based on the energy level.
Definition at line 520 of file processing.cpp.
References absenceOfDivertedEnergyCountInMC, beyondStartUpPeriod, capacityOfEnergyBucket_long, Shared::EDD_isActive, energyInBucket_long, energyInBucket_prediction, loadPrioritiesAndState, loadStateOnBit, lowerEnergyThreshold, lowerThreshold_default, midPointOfEnergyBucket_long, NEGATIVE, polarityConfirmed, polarityConfirmedOfLastSampleV, POSITIVE, POST_TRANSITION_MAX_COUNT, POST_ZERO_CROSSING_MAX_COUNT, postTransitionCount, proceedHighEnergyLevel(), proceedLowEnergyLevel(), processMinusHalfCycle(), processPlusHalfCycle(), processStartNewCycle(), processStartUp(), recentTransition, refresh7SegDisplay(), sampleSetsDuringNegativeHalfOfMainsCycle, updatePhysicalLoadStates(), updatePortsStates(), upperEnergyThreshold, and upperThreshold_default.
Referenced by processVoltageRawSample().
|
inline |
This code is executed once per 20ms, shortly after the start of each new mains cycle on phase 0.
This function manages the energy bucket and ensures that its level remains within the defined limits. It is responsible for maintaining system stability when conditions change, such as when energy import switches to export or vice versa.
Definition at line 736 of file processing.cpp.
References capacityOfEnergyBucket_long, energyInBucket_long, sampleSetsDuringNegativeHalfOfMainsCycle, sampleSetsDuringThisMainsCycle, sumP_diverted, and sumP_grid.
Referenced by processRawSamples().
|
inline |
Process the startup period for the router.
This function ensures that the system remains in a stable state during the startup period. It waits for the DC-blocking filters to settle and initializes key variables for energy and load management. Once the startup period is complete, the system transitions to normal operation.
beyondStartUpPeriod
to true. Definition at line 704 of file processing.cpp.
References beyondStartUpPeriod, delayBeforeSerialStarts, lowestNoOfSampleSetsPerMainsCycle, sampleSetsDuringThisMainsCycle, startUpPeriod, sumP_diverted, sumP_diverted_overDL_Period, sumP_grid, and sumP_grid_overDL_Period.
Referenced by processRawSamples().
|
inline |
Process the calculation for the current voltage sample.
This function calculates the squared voltage value for the current sample and accumulates it for further processing. It also updates the DC offset and polarity information for the next cycle.
Definition at line 643 of file processing.cpp.
References cumVdeltasThisCycle_long, l_sum_Vsquared, polarityConfirmed, polarityConfirmedOfLastSampleV, and sampleVminusDC_long.
Referenced by processVoltageRawSample().
|
inline |
Process the current voltage raw sample.
This function processes the raw voltage sample by performing the following steps:
processPolarity()
.confirmPolarity()
to ensure stability in zero-crossing detection.processRawSamples()
.processVoltage()
.rawSample | The raw voltage sample from the ADC. |
Definition at line 673 of file processing.cpp.
References confirmPolarity(), processPolarity(), processRawSamples(), processVoltage(), sampleSetsDuringThisDatalogPeriod, and sampleSetsDuringThisMainsCycle.
Referenced by ISR().
void updatePhysicalLoadStates | ( | ) |
This function provides the link between the logical and physical loads.
This function maps the logical load states to the physical load states. It ensures that the physical loads are updated according to the logical priorities and states. The function also handles priority rotation if enabled.
The array, logicalLoadState[], contains the on/off state of all logical loads, with element 0 being for the one with the highest priority. The array, physicalLoadState[], contains the on/off state of all physical loads.
The lowest 7 bits of element is the load number as defined in 'physicalLoadState'. The highest bit of each 'loadPrioritiesAndState' determines if the load is ON or OFF. The order of each element in 'loadPrioritiesAndState' determines the load priority. 'loadPrioritiesAndState[i] & loadStateMask' will extract the load number at position 'i' 'loadPrioritiesAndState[i] & loadStateOnBit' will extract the load state at position 'i'
Any other mapping relationships could be configured here.
Definition at line 334 of file processing.cpp.
References Shared::b_diversionEnabled, Shared::b_overrideLoadOn, Shared::b_reOrderLoads, i, LOAD_OFF, LOAD_ON, loadPrioritiesAndState, loadStateMask, loadStateOnBit, NO_OF_DUMPLOADS, OFF, and physicalLoadState.
Referenced by initializeOldPCBPins(), and processRawSamples().
void updatePortsStates | ( | ) |
Updates the control ports for each of the physical loads.
This function determines the ON/OFF state of each physical load and updates the corresponding control ports. It ensures that the correct pins are set to their respective states based on the load's current status.
pinsOFF
mask.pinsON
mask.setPinsOFF
and setPinsON
functions. Definition at line 285 of file processing.cpp.
References countLoadON, i, LOAD_OFF, NO_OF_DUMPLOADS, physicalLoadPin, physicalLoadState, setPinsOFF(), and setPinsON().
Referenced by initializeOldPCBPins(), and processRawSamples().