3-phase PV router
Loading...
Searching...
No Matches
energy_bucket.h
Go to the documentation of this file.
1
23
24#ifndef ENERGY_BUCKET_H
25#define ENERGY_BUCKET_H
26
27#include <stdint.h>
28
29#include "mult_asm.h"
30
31namespace Energy
32{
33inline constexpr uint8_t FRACTION_BITS{ 4 };
34
38constexpr uint8_t shiftFor(const float largest)
39{
40 uint8_t shift{ 0 };
41 while ((shift < 31) && (largest * static_cast< float >(1UL << (shift + 1)) <= 65535.0F))
42 {
43 ++shift;
44 }
45 return shift;
46}
47
51template< uint8_t N >
52constexpr float largestOf(const float (&cal)[N])
53{
54 float largest{ 0.0F };
55 for (const auto value : cal)
56 {
57 if (value > largest)
58 {
59 largest = value;
60 }
61 }
62 return largest;
63}
64
71template< uint8_t N >
72constexpr uint8_t calibrationShift(const float (&cal)[N])
73{
74 return shiftFor(largestOf(cal));
75}
76
84constexpr uint16_t toFixed(const float cal, const uint8_t shift)
85{
86 // scaling by a power of two is exact in float, so only the final rounding matters
87 return static_cast< uint16_t >(cal * static_cast< float >(1UL << shift) + 0.5F);
88}
89
102template< uint8_t NMin, uint8_t NMax, uint8_t N >
104{
105 uint16_t value[N][NMax - NMin + 1];
106 uint8_t shift;
107};
108
118template< uint8_t NMin, uint8_t NMax, uint8_t SumShift = 0, uint8_t N >
120{
121 static_assert((NMin >= 1) && (NMin <= NMax), "empty range of sample counts");
122 static_assert(NMax <= (64U << SumShift), "sumP must stay within 24 bits: at most 64 sample sets per cycle, or shift sumP");
123
124 constexpr float sumScale{ static_cast< float >(1UL << SumShift) }; // exact in float
125
127 table.shift = shiftFor(largestOf(cal) * sumScale / NMin); // the largest entry: largest cal, smallest n
128 for (uint8_t phase = 0; phase < N; ++phase)
129 {
130 for (uint8_t n = NMin; n <= NMax; ++n)
131 {
132 table.value[phase][n - NMin] = toFixed(cal[phase] * sumScale / n, table.shift);
133 }
134 }
135 return table;
136}
137
146template< uint8_t N >
147constexpr bool isValidCalibration(const float (&cal)[N])
148{
149 for (const auto value : cal)
150 {
151 if (!(value > 0.0F))
152 {
153 return false;
154 }
155 }
156 return calibrationShift(cal) >= FRACTION_BITS + 9;
157}
158
168inline __attribute__((always_inline)) int32_t contribution(const int32_t averagePower, const uint16_t calFixed, const uint8_t shift)
169{
170 // Work on the magnitude and apply the sign last: rounding is then half away from
171 // zero, and import and export weigh exactly the same.
172 const bool negative{ averagePower < 0 };
173 const uint32_t magnitude{ static_cast< uint32_t >(negative ? -averagePower : averagePower) };
174
175 uint32_t product; // (magnitude x calFixed) >> 8
176 multU24x16_to32_hi8(product, magnitude, calFixed);
177
178 // remaining shift down to bucket units; >= 1 for any calibration value below 8
179 const uint8_t remaining{ static_cast< uint8_t >(shift - FRACTION_BITS - 8) };
180
181 // Rounded: (product + 2^(remaining-1)) >> remaining. From remaining = 9 on, the rounding
182 // constant is a multiple of 256, so the same value is ((product >> 8) + 2^(remaining-9))
183 // >> (remaining-8): '>> 8' is a byte move, and the add in between keeps the compiler from
184 // merging both shifts back into one loop (remaining = 11 for the new board with 50 A CTs:
185 // a 3-turn loop instead of 11, ~52 cycles less). It also cannot overflow, unlike the
186 // 32-bit add of the general form. shift is a constant at the call site in the ISR, so
187 // only one branch is compiled there.
188 int32_t rounded;
189 if (remaining >= 9)
190 {
191 rounded = static_cast< int32_t >(((product >> 8) + (1UL << (remaining - 9))) >> (remaining - 8));
192 }
193 else
194 {
195 rounded = static_cast< int32_t >((product + (1UL << (remaining - 1))) >> remaining);
196 }
197
198 return negative ? -rounded : rounded;
199}
200
204constexpr int32_t fromWatts(const int32_t watts)
205{
206 return watts * (1L << FRACTION_BITS);
207}
208
212constexpr uint16_t gcd(const uint16_t a, const uint16_t b)
213{
214 return b ? gcd(b, a % b) : a;
215}
216
226template< uint8_t SUPPLY_FREQUENCY >
227constexpr int32_t toHundredthsOfJoule(const int32_t units)
228{
229 constexpr uint16_t unitsPerJoule{ SUPPLY_FREQUENCY << FRACTION_BITS };
230 constexpr int32_t numerator{ 100 / gcd(100, unitsPerJoule) };
231 constexpr int32_t denominator{ unitsPerJoule / gcd(100, unitsPerJoule) };
232
233 return (units * numerator + (units < 0 ? -denominator / 2 : denominator / 2)) / denominator;
234}
235} // namespace Energy
236
237#endif // ENERGY_BUCKET_H
constexpr uint8_t SUPPLY_FREQUENCY
static void multU24x16_to32_hi8(uint32_t &result, A a, B b)
Optimized 24×16→40 unsigned multiplication, returning bits 8..39.
Definition mult_asm.h:144
Assembly-optimized multiplication functions for AVR microcontrollers.
constexpr uint8_t calibrationShift(const float(&cal)[N])
Largest shift that keeps every calibration value within 16 bits.
constexpr uint16_t toFixed(const float cal, const uint8_t shift)
A calibration value as a 16-bit fixed-point constant.
constexpr uint8_t FRACTION_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 uint16_t gcd(const uint16_t a, const uint16_t b)
Greatest common divisor, to reduce fractions at compile time.
constexpr PerSampleCalibration< NMin, NMax, N > toFixedPerSample(const float(&cal)[N])
Build the per-sample calibration table at compile time.
constexpr uint8_t shiftFor(const float largest)
Largest shift that keeps a value within 16 bits once scaled by 2^shift.
constexpr int32_t toHundredthsOfJoule(const int32_t units)
An energy in bucket units, in hundredths of a joule (for the datalog).
constexpr bool isValidCalibration(const float(&cal)[N])
Check that the calibration values can be represented.
constexpr float largestOf(const float(&cal)[N])
Largest of the calibration values.
constexpr int32_t fromWatts(const int32_t watts)
A power in watts, in bucket units per mains cycle.
cal / n for every phase and every expected sample count n, in fixed point.
uint16_t value[N][NMax - NMin+1]