3-phase PV router
Loading...
Searching...
No Matches
energy_bucket.h File Reference

Integer arithmetic of the energy bucket. More...

#include <stdint.h>
#include "mult_asm.h"
Include dependency graph for energy_bucket.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  Energy::PerSampleCalibration< NMin, NMax, N >
 cal / n for every phase and every expected sample count n, in fixed point. More...
 

Namespaces

namespace  Energy
 

Functions

template<uint8_t N>
constexpr uint8_t Energy::calibrationShift (const float(&cal)[N])
 Largest shift that keeps every calibration value within 16 bits.
 
int32_t Energy::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 Energy::fromWatts (const int32_t watts)
 A power in watts, in bucket units per mains cycle.
 
constexpr uint16_t Energy::gcd (const uint16_t a, const uint16_t b)
 Greatest common divisor, to reduce fractions at compile time.
 
template<uint8_t N>
constexpr bool Energy::isValidCalibration (const float(&cal)[N])
 Check that the calibration values can be represented.
 
template<uint8_t N>
constexpr float Energy::largestOf (const float(&cal)[N])
 Largest of the calibration values.
 
constexpr uint8_t Energy::shiftFor (const float largest)
 Largest shift that keeps a value within 16 bits once scaled by 2^shift.
 
constexpr uint16_t Energy::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 > Energy::toFixedPerSample (const float(&cal)[N])
 Build the per-sample calibration table at compile time.
 
template<uint8_t SUPPLY_FREQUENCY>
constexpr int32_t Energy::toHundredthsOfJoule (const int32_t units)
 An energy in bucket units, in hundredths of a joule (for the datalog).
 

Variables

constexpr uint8_t Energy::FRACTION_BITS { 4 }
 

Detailed Description

Integer arithmetic of the energy bucket.

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

The energy bucket used to be a float, which put libgcc floating-point calls in the ADC ISR. It is now an int32_t in fixed-point units: 1 unit = 1 / 2^FRACTION_BITS W x mains cycle.

The power calibration values stay floats in calibration.h. They are turned into 16-bit fixed-point constants at compile time, with a shift chosen so that the largest of them fills the 16 bits - the quantisation error then stays far below the accuracy of the calibration itself.

Note
This header has no Arduino dependency, so it can be exercised by the native test suite.

Definition in file energy_bucket.h.