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

Classes

struct  PerSampleCalibration
 cal / n for every phase and every expected sample count n, in fixed point. More...
 

Functions

template<uint8_t N>
constexpr uint8_t calibrationShift (const float(&cal)[N])
 Largest shift that keeps every calibration value within 16 bits.
 
int32_t contribution (const int32_t averagePower, const uint16_t calFixed, const uint8_t shift)
 Contribution of one mains cycle of one phase to the bucket.
 
constexpr int32_t fromWatts (const int32_t watts)
 A power in watts, in bucket units per mains cycle.
 
constexpr uint16_t gcd (const uint16_t a, const uint16_t b)
 Greatest common divisor, to reduce fractions at compile time.
 
template<uint8_t N>
constexpr bool isValidCalibration (const float(&cal)[N])
 Check that the calibration values can be represented.
 
template<uint8_t N>
constexpr float largestOf (const float(&cal)[N])
 Largest of the calibration values.
 
constexpr uint8_t shiftFor (const float largest)
 Largest shift that keeps a value within 16 bits once scaled by 2^shift.
 
constexpr uint16_t toFixed (const float cal, const uint8_t shift)
 A calibration value as a 16-bit fixed-point constant.
 
template<uint8_t NMin, uint8_t NMax, uint8_t SumShift = 0, uint8_t N>
constexpr PerSampleCalibration< NMin, NMax, N > toFixedPerSample (const float(&cal)[N])
 Build the per-sample calibration table at compile time.
 
template<uint8_t SUPPLY_FREQUENCY>
constexpr int32_t toHundredthsOfJoule (const int32_t units)
 An energy in bucket units, in hundredths of a joule (for the datalog).
 

Variables

constexpr uint8_t FRACTION_BITS { 4 }
 

Function Documentation

◆ calibrationShift()

template<uint8_t N>
uint8_t Energy::calibrationShift ( const float(&) cal[N])
constexpr

Largest shift that keeps every calibration value within 16 bits.

Parameters
calThe power calibration values, one per phase.
Returns
The shift, i.e. the number of fractional bits of the fixed-point values.

Definition at line 72 of file energy_bucket.h.

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

◆ contribution()

int32_t Energy::contribution ( const int32_t averagePower,
const uint16_t calFixed,
const uint8_t shift )
inline

Contribution of one mains cycle of one phase to the bucket.

Parameters
averagePowerThe phase's average power over the cycle, in V_ADC x I_ADC units (at most +-2^18).
calFixedThe phase's calibration value, from toFixed().
shiftThe shift returned by calibrationShift().
Returns
averagePower x cal, in bucket units, rounded to the nearest unit.

Definition at line 168 of file energy_bucket.h.

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

◆ fromWatts()

int32_t Energy::fromWatts ( const int32_t watts)
constexpr

A power in watts, in bucket units per mains cycle.

Definition at line 204 of file energy_bucket.h.

Here is the caller graph for this function:

◆ gcd()

uint16_t Energy::gcd ( const uint16_t a,
const uint16_t b )
constexpr

Greatest common divisor, to reduce fractions at compile time.

Definition at line 212 of file energy_bucket.h.

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

◆ isValidCalibration()

template<uint8_t N>
bool Energy::isValidCalibration ( const float(&) cal[N])
constexpr

Check that the calibration values can be represented.

Every value must be positive, and the largest must leave enough fractional bits for contribution(): shift >= FRACTION_BITS + 9, i.e. values below ~8.

Parameters
calThe power calibration values, one per phase.

Definition at line 147 of file energy_bucket.h.

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

◆ largestOf()

template<uint8_t N>
float Energy::largestOf ( const float(&) cal[N])
constexpr

Largest of the calibration values.

Definition at line 52 of file energy_bucket.h.

Here is the caller graph for this function:

◆ shiftFor()

uint8_t Energy::shiftFor ( const float largest)
constexpr

Largest shift that keeps a value within 16 bits once scaled by 2^shift.

Definition at line 38 of file energy_bucket.h.

Here is the caller graph for this function:

◆ toFixed()

uint16_t Energy::toFixed ( const float cal,
const uint8_t shift )
constexpr

A calibration value as a 16-bit fixed-point constant.

Parameters
calThe calibration value.
shiftThe shift returned by calibrationShift().
Returns
round(cal x 2^shift).

Definition at line 84 of file energy_bucket.h.

Here is the caller graph for this function:

◆ toFixedPerSample()

template<uint8_t NMin, uint8_t NMax, uint8_t SumShift = 0, uint8_t N>
PerSampleCalibration< NMin, NMax, N > Energy::toFixedPerSample ( const float(&) cal[N])
constexpr

Build the per-sample calibration table at compile time.

Beyond 64 sample sets per cycle, sumP may exceed 24 bits: the ISR then hands over sumP >> SumShift, and the table holds cal x 2^SumShift / n to make up for it.

Template Parameters
SumShiftThe shift the ISR applies to sumP before contribution() (0: none).
Parameters
calThe power calibration values, one per phase.

Definition at line 119 of file energy_bucket.h.

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

◆ toHundredthsOfJoule()

template<uint8_t SUPPLY_FREQUENCY>
int32_t Energy::toHundredthsOfJoule ( const int32_t units)
constexpr

An energy in bucket units, in hundredths of a joule (for the datalog).

units * 100 / (SUPPLY_FREQUENCY * 2^FRACTION_BITS), with the fraction reduced at compile time (1/8 at 50 Hz, 5/48 at 60 Hz) so that it cannot overflow, rounded half away from zero as Print::print(float) did. No float code.

Template Parameters
SUPPLY_FREQUENCYmains frequency in Hz

Definition at line 227 of file energy_bucket.h.

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

Variable Documentation

◆ FRACTION_BITS

uint8_t Energy::FRACTION_BITS { 4 }
inlineconstexpr

fractional bits of a bucket unit

Definition at line 33 of file energy_bucket.h.