3-phase PV router
Loading...
Searching...
No Matches
load_priorities.h
Go to the documentation of this file.
1
37
38#ifndef LOAD_PRIORITIES_H
39#define LOAD_PRIORITIES_H
40
41#include <stdint.h>
42
43#include "load_map.h" // LoadStates
44
45inline constexpr uint8_t loadStateMask{ 0x7FU };
46inline constexpr uint8_t loadStateOnBit{ (uint8_t)(~loadStateMask) };
47
49{
55template< uint8_t N >
56__attribute__((always_inline)) inline uint8_t nextToAdd(const uint8_t (&priorities)[N])
57{
58 for (uint8_t index = 0; index < N; ++index)
59 {
60 if (0x00 == (priorities[index] & loadStateOnBit))
61 {
62 return index;
63 }
64 }
65
66 return N;
67}
68
74template< uint8_t N >
75__attribute__((always_inline)) inline uint8_t nextToRemove(const uint8_t (&priorities)[N])
76{
77 uint8_t index{ N };
78 do
79 {
80 if (priorities[--index] & loadStateOnBit)
81 {
82 return index;
83 }
84 } while (index);
85
86 return N;
87}
88
96template< uint8_t N >
97__attribute__((always_inline)) inline void rotate(uint8_t (&priorities)[N])
98{
99 if constexpr (N > 1)
100 {
101 uint8_t i{ static_cast< uint8_t >(N - 1) };
102 const auto temp{ priorities[i] };
103 do
104 {
105 priorities[i] = priorities[i - 1];
106 --i;
107 } while (i);
108 priorities[0] = temp;
109 }
110}
111
122template< bool CALIBRATION, uint8_t N, typename IsOverridden >
123__attribute__((always_inline)) inline void toPhysical(const uint8_t (&priorities)[N], LoadStates (&physical)[N],
124 const bool diversionEnabled, IsOverridden isOverridden)
125{
126 uint8_t idx{ N };
127 do
128 {
129 --idx;
130 const auto iLoad{ priorities[idx] & loadStateMask };
131
132 const bool bOn{ isOverridden(iLoad) || (diversionEnabled && (priorities[idx] & loadStateOnBit)) };
133 physical[iLoad] = (!CALIBRATION && bOn) ? LoadStates::LOAD_ON : LoadStates::LOAD_OFF;
134 } while (idx);
135}
136
138struct Bucket
139{
140 int32_t capacity;
141 int32_t midPoint;
142 int32_t lowerDefault;
143 int32_t upperDefault;
144};
145
151template< uint8_t N >
153{
154 static constexpr uint8_t POST_TRANSITION_MAX_COUNT{ 3 };
155
156 int32_t lowerThreshold{ 0 };
157 int32_t upperThreshold{ 0 };
158 bool recentTransition{ false };
159 uint8_t postTransitionCount{ 0 };
160 uint8_t activeLoad{ N };
161
173 __attribute__((noinline)) void onNewCycle(uint8_t (&priorities)[N], const int32_t& energy, const Bucket& bucket)
174 {
175 // Restrictions apply for the period immediately after a load has been switched.
176 // if (recentTransition)
177 // recentTransition = (++postTransitionCount < POST_TRANSITION_MAX_COUNT);
178 // for optimization, the next line is equivalent to the two lines above
180
181 if (energy > bucket.midPoint)
182 {
183 // the energy state is in the upper half of the working range
184 lowerThreshold = bucket.lowerDefault; // reset the "opposite" threshold
185 if (energy > upperThreshold)
186 {
187 // Because the energy level is high, some action may be required
188 onHighEnergy(priorities, energy, bucket.capacity);
189 }
190 }
191 else
192 {
193 // the energy state is in the lower half of the working range
194 upperThreshold = bucket.upperDefault; // reset the "opposite" threshold
195 if (energy < lowerThreshold)
196 {
197 // Because the energy level is low, some action may be required
198 onLowEnergy(priorities, energy);
199 }
200 }
201 }
202
209 __attribute__((always_inline)) inline void onHighEnergy(uint8_t (&priorities)[N], const int32_t& energy, const int32_t capacity)
210 {
211 const auto tempLoad{ nextToAdd(priorities) };
212
213 if (tempLoad == N)
214 {
215 return;
216 }
217
218 bool bOK_toAddLoad{ true };
219 // a load which is now OFF has been identified for potentially being switched ON
221 {
222 // During the post-transition period, any increase in the energy level is noted.
223 upperThreshold = energy;
224
225 // the energy thresholds must remain within range
226 if (upperThreshold > capacity)
227 {
228 upperThreshold = capacity;
229 }
230
231 // Only the active load may be switched during this period. All other loads must
232 // wait until the recent transition has had sufficient opportunity to take effect.
233 bOK_toAddLoad = (tempLoad == activeLoad);
234 }
235
236 if (bOK_toAddLoad)
237 {
238 priorities[tempLoad] |= loadStateOnBit;
239 activeLoad = tempLoad;
241 recentTransition = true;
242 }
243 }
244
251 __attribute__((always_inline)) inline void onLowEnergy(uint8_t (&priorities)[N], const int32_t& energy)
252 {
253 const auto tempLoad{ nextToRemove(priorities) };
254
255 if (tempLoad == N)
256 {
257 return;
258 }
259
260 bool bOK_toRemoveLoad{ true };
261 // a load which is now ON has been identified for potentially being switched OFF
263 {
264 // During the post-transition period, any decrease in the energy level is noted.
265 lowerThreshold = energy;
266
267 // the energy thresholds must remain within range
268 if (lowerThreshold < 0)
269 {
270 lowerThreshold = 0;
271 }
272
273 // Only the active load may be switched during this period. All other loads must
274 // wait until the recent transition has had sufficient opportunity to take effect.
275 bOK_toRemoveLoad = (tempLoad == activeLoad);
276 }
277
278 if (bOK_toRemoveLoad)
279 {
280 priorities[tempLoad] &= loadStateMask;
281 activeLoad = tempLoad;
283 recentTransition = true;
284 }
285 }
286};
287} // namespace LoadPriorities
288
289#endif /* LOAD_PRIORITIES_H */
uint8_t i
Encoding of the load map: one packed byte per dump load.
LoadStates
Definition load_map.h:32
constexpr uint8_t loadStateMask
constexpr uint8_t loadStateOnBit
uint8_t nextToRemove(const uint8_t(&priorities)[N])
The next logical load to switch OFF: the last one ON, in priority order.
uint8_t nextToAdd(const uint8_t(&priorities)[N])
The next logical load to switch ON: the first one OFF, in priority order.
void toPhysical(const uint8_t(&priorities)[N], LoadStates(&physical)[N], const bool diversionEnabled, IsOverridden isOverridden)
Maps the logical load states to the physical ones.
void rotate(uint8_t(&priorities)[N])
Rotates the priorities by one: the last load becomes the first, the others move down one place....
The fixed levels of the energy bucket, in bucket units.
The switching decision, once per mains cycle, and its state.
static constexpr uint8_t POST_TRANSITION_MAX_COUNT
void onNewCycle(uint8_t(&priorities)[N], const int32_t &energy, const Bucket &bucket)
The decision of one mains cycle: may switch one load ON or OFF.
void onLowEnergy(uint8_t(&priorities)[N], const int32_t &energy)
The energy level is low: may switch the last load ON to OFF.
void onHighEnergy(uint8_t(&priorities)[N], const int32_t &energy, const int32_t capacity)
The energy level is high: may switch the next load ON.