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

Integer arithmetic of the datalog: power and RMS voltage per phase. More...

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

Go to the source code of this file.

Classes

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

Namespaces

namespace  Datalog
 

Typedefs

using Datalog::uint24_t = uint32_t
 

Functions

template<uint8_t SHIFT, typename Count>
int32_t Datalog::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 Datalog::isqrt (const uint32_t value)
 Integer square root, rounded to the nearest integer.
 
template<uint8_t N>
constexpr bool Datalog::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 > Datalog::powerCalibration (const float(&cal)[N])
 The power calibration in fixed point, for watts(), built at compile time.
 
constexpr uint8_t Datalog::powerShift (const uint32_t maxSampleSets)
 The shift by which the ISR scales the power down before adding it to the datalog sum.
 
uint16_t Datalog::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 Datalog::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 > Datalog::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 Datalog::voltageShift (const uint16_t factor, const float(&cal)[N])
 Largest shift that keeps every (factor x calibration value) within 16 bits.
 
uint16_t Datalog::vrms (const uint32_t meanSquare, const uint16_t calFixed, const uint8_t shift)
 The RMS voltage of a phase.
 
constexpr uint16_t Datalog::vrmsFactor (const uint8_t datalogPeriodInSeconds)
 Scaling of the datalog voltage, Vrms_L_x100.
 
int16_t Datalog::watts (const int32_t averagePower, const uint16_t calFixed, const uint8_t shift)
 The average power of a phase, in watts.
 

Detailed Description

Integer arithmetic of the datalog: power and RMS voltage per phase.

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

At the end of each datalog period, the main loop turns the sums of the ISR into watts and volts. This used to be float arithmetic (a multiplication by f_powerCal, sqrt() and a multiplication by f_voltageCal), which linked about 750 bytes of libgcc/libm float code and kept the calibration arrays in RAM (#120).

The calibration values stay floats in calibration.h. They are turned into 16-bit fixed-point constants at compile time (Energy::toFixed()), and the square root is an integer one, computed on a normalised value so that it keeps 15 to 16 significant bits.

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

Definition in file datalog_math.h.