3-phase PV router
Loading...
Searching...
No Matches
Datalog Namespace Reference

Classes

struct  FixedCalibration
 Calibration values of all phases in 16-bit fixed point, with their shared shift. More...
 

Typedefs

using uint24_t = uint32_t
 

Functions

template<uint8_t SHIFT, typename Count>
int32_t averagePower (const int32_t sum, const Count sampleSets)
 The average power of a datalog period, back at the x1 scale of V_ADC x I_ADC.
 
uint16_t isqrt (const uint32_t value)
 Integer square root, rounded to the nearest integer.
 
template<uint8_t N>
constexpr bool isValidVoltageCalibration (const uint16_t factor, const float(&cal)[N])
 Check that the voltage calibration values can be represented.
 
template<uint8_t N>
constexpr FixedCalibration< N > powerCalibration (const float(&cal)[N])
 The power calibration in fixed point, for watts(), built at compile time.
 
constexpr uint8_t powerShift (const uint32_t maxSampleSets)
 The shift by which the ISR scales the power down before adding it to the datalog sum.
 
uint16_t sqrtScaled (const uint32_t meanSquare, uint8_t &fractionBits)
 The square root of a mean square, with as many fractional bits as fit.
 
template<bool Normalise>
uint16_t squareRoot (uint32_t value, uint8_t &fractionBits)
 Square root, rounded to the nearest integer, of the value or of the value normalised.
 
template<uint8_t N>
constexpr FixedCalibration< N > voltageCalibration (const uint16_t factor, const float(&cal)[N])
 The voltage calibration in fixed point, for vrms(), built at compile time.
 
template<uint8_t N>
constexpr uint8_t voltageShift (const uint16_t factor, const float(&cal)[N])
 Largest shift that keeps every (factor x calibration value) within 16 bits.
 
uint16_t vrms (const uint32_t meanSquare, const uint16_t calFixed, const uint8_t shift)
 The RMS voltage of a phase.
 
constexpr uint16_t vrmsFactor (const uint8_t datalogPeriodInSeconds)
 Scaling of the datalog voltage, Vrms_L_x100.
 
int16_t watts (const int32_t averagePower, const uint16_t calFixed, const uint8_t shift)
 The average power of a phase, in watts.
 

Typedef Documentation

◆ uint24_t

using Datalog::uint24_t = uint32_t

Definition at line 114 of file datalog_math.h.

Function Documentation

◆ averagePower()

template<uint8_t SHIFT, typename Count>
int32_t Datalog::averagePower ( const int32_t sum,
const Count sampleSets )
inline

The average power of a datalog period, back at the x1 scale of V_ADC x I_ADC.

sum x 2^SHIFT / sampleSets, truncated toward zero, in 32 bits: the quotient and the remainder are scaled up separately, so the division loses nothing more. The rounding of each sample in the ISR keeps the average within 8 (half the shift), and averages out with the noise of real samples.

Template Parameters
SHIFTThe shift of the sum in the ISR (powerShift()).
CountThe type of the sample set counter (16 or 32 bits).
Parameters
sumThe power summed over the period, scaled down by 2^SHIFT.
sampleSetsThe sample sets of the period (not 0, below 2^27).
Returns
The average, at most +-2^18.

Definition at line 96 of file datalog_math.h.

Here is the caller graph for this function:

◆ isqrt()

uint16_t Datalog::isqrt ( const uint32_t value)
inline

Integer square root, rounded to the nearest integer.

Parameters
valueThe value to take the square root of.
Returns
round(sqrt(value)), at most 65535.

Definition at line 287 of file datalog_math.h.

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

◆ isValidVoltageCalibration()

template<uint8_t N>
bool Datalog::isValidVoltageCalibration ( const uint16_t factor,
const float(&) cal[N] )
constexpr

Check that the voltage calibration values can be represented.

Every value must be positive, and the shift small enough for vrms(): with up to 15 fractional bits from sqrtScaled(), the total shift must stay below 32.

Definition at line 338 of file datalog_math.h.

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

◆ powerCalibration()

template<uint8_t N>
FixedCalibration< N > Datalog::powerCalibration ( const float(&) cal[N])
constexpr

The power calibration in fixed point, for watts(), built at compile time.

Parameters
calf_powerCal, one value per phase.

Definition at line 403 of file datalog_math.h.

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

◆ powerShift()

uint8_t Datalog::powerShift ( const uint32_t maxSampleSets)
constexpr

The shift by which the ISR scales the power down before adding it to the datalog sum.

In ADC steps: one sample adds V_ADC x I_ADC, at most 2^18; a sine over the whole span of the ADC averages 2^17. While a datalog period of full-span sines fits in the signed 32-bit sum, no shift; beyond, a shift by 4 (at most 2^14 per sample: 30 s fit with any signal). The same idea as the V^2 scaling, but from the sample count: a firmware that samples faster needs it for shorter periods.

Parameters
maxSampleSetsThe most sample sets in a datalog period (MAX_SAMPLE_SETS_PER_DATALOG).
Returns
0, or 4.

Definition at line 76 of file datalog_math.h.

Here is the caller graph for this function:

◆ sqrtScaled()

uint16_t Datalog::sqrtScaled ( const uint32_t meanSquare,
uint8_t & fractionBits )
inline

The square root of a mean square, with as many fractional bits as fit.

Parameters
meanSquareThe mean of V_ADC^2 (in the scaling of the ISR).
[out]fractionBitsThe number of fractional bits of the result.
Returns
sqrt(meanSquare) x 2^fractionBits, rounded; >= 2^15 unless meanSquare is 0.

Definition at line 300 of file datalog_math.h.

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

◆ squareRoot()

template<bool Normalise>
uint16_t Datalog::squareRoot ( uint32_t value,
uint8_t & fractionBits )
inline

Square root, rounded to the nearest integer, of the value or of the value normalised.

Digit by digit, two bits of the value per bit of the root, most significant first. The remainder (value - root^2 so far, < 2^19 before each trial) and twice the root (< 2^17) fit in 3 bytes; keeping twice the root makes each trial a single compare: remainder >= 2 x (2 x root) + 1 <=> remainder > 2 x (2 x root).

On AVR, this is assembly: the value is consumed a byte at a time, each bit of the root costs ~21 cycles, and the normalisation ~6 cycles per byte and ~12 per pair of bits (avr-gcc's code for the same needs about twice as much; see test_datalog_math_avr).

Template Parameters
Normalisefalse: round(sqrt(value)). true: the value is first shifted left by pairs of bits up to >= 2^30, so that the root has 15 to 16 significant bits.
Parameters
valueThe value to take the square root of.
[out]fractionBitsThe number of fractional bits of the result (0 if not normalised).
Returns
round(sqrt(value) x 2^fractionBits), at most 65535.

Definition at line 213 of file datalog_math.h.

Here is the caller graph for this function:

◆ voltageCalibration()

template<uint8_t N>
FixedCalibration< N > Datalog::voltageCalibration ( const uint16_t factor,
const float(&) cal[N] )
constexpr

The voltage calibration in fixed point, for vrms(), built at compile time.

Parameters
factorThe scaling of the result (100 for hundredths of a volt, x4 when the ISR scales V^2 down by 16).
calf_voltageCal, one value per phase.

Definition at line 422 of file datalog_math.h.

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

◆ voltageShift()

template<uint8_t N>
uint8_t Datalog::voltageShift ( const uint16_t factor,
const float(&) cal[N] )
constexpr

Largest shift that keeps every (factor x calibration value) within 16 bits.

Parameters
factorThe scaling of the result, e.g. 100 for hundredths of a volt.
calThe voltage calibration values, one per phase.

Definition at line 326 of file datalog_math.h.

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

◆ vrms()

uint16_t Datalog::vrms ( const uint32_t meanSquare,
const uint16_t calFixed,
const uint8_t shift )
inline

The RMS voltage of a phase.

Parameters
meanSquareThe mean of V_ADC^2 over the datalog period (sum_Vsquared / sample sets).
calFixedThe phase's factor x f_voltageCal, from Energy::toFixed().
shiftThe shift returned by voltageShift() (<= 16).
Returns
factor x f_voltageCal x sqrt(meanSquare), rounded, saturated to 65535.

Definition at line 358 of file datalog_math.h.

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

◆ vrmsFactor()

uint16_t Datalog::vrmsFactor ( const uint8_t datalogPeriodInSeconds)
constexpr

Scaling of the datalog voltage, Vrms_L_x100.

Hundredths of a volt. For datalog periods over 10 s, processVoltage() scales V^2 down by 16 so that the sum cannot overflow: the root is then 4 times smaller, hence x4.

Parameters
datalogPeriodInSecondsDATALOG_PERIOD_IN_SECONDS.
Returns
The factor for voltageCalibration(): 100, or 400 for periods over 10 s.

Definition at line 314 of file datalog_math.h.

Here is the caller graph for this function:

◆ watts()

int16_t Datalog::watts ( const int32_t averagePower,
const uint16_t calFixed,
const uint8_t shift )
inline

The average power of a phase, in watts.

Parameters
averagePowerThe phase's average power over the datalog period, in V_ADC x I_ADC units (sumP / sample sets, at most +-2^18).
calFixedThe phase's power calibration, from Energy::toFixed().
shiftThe shift returned by Energy::calibrationShift() (>= 13 for any valid calibration, see Energy::isValidCalibration()).
Returns
averagePower x f_powerCal, rounded half away from zero, saturated to +-32767 W.

Definition at line 45 of file datalog_math.h.

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