|
| 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.
|
| |
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
-
| SHIFT | The shift of the sum in the ISR (powerShift()). |
| Count | The type of the sample set counter (16 or 32 bits). |
- Parameters
-
| sum | The power summed over the period, scaled down by 2^SHIFT. |
| sampleSets | The 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.
| 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
-
| maxSampleSets | The 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.
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
-
| Normalise | false: 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
-
| value | The value to take the square root of. |
| [out] | fractionBits | The 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.