3-phase PV router
Loading...
Searching...
No Matches
test_main.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2#include <Arduino.h>
3
4#include <unity.h>
5
7
8#include "types.h"
9#include "utils_pins.h"
10#include "utils_relay.h"
11
12constexpr RelayDiverter testRelays{ { { 2, 1000, 200, 1, 1 },
13 { 3, 100, 20, 2, 3 } } };
14
15void setUp(void)
16{
17 // Setup for each test
18}
19
20void tearDown(void)
21{
22 // clean stuff up here
23}
24
26{
27 relayOutput relay(4, 500, 100);
28 TEST_ASSERT_EQUAL(4, relay.get_pin());
29 TEST_ASSERT_EQUAL(500, relay.get_surplusThreshold());
30 TEST_ASSERT_EQUAL(100, relay.get_importThreshold());
31}
32
34{
35 relayOutput relay(4, 500, 100);
36 TEST_ASSERT_EQUAL(4, relay.get_pin());
37 TEST_ASSERT_EQUAL(500, relay.get_surplusThreshold());
38 TEST_ASSERT_EQUAL(100, relay.get_importThreshold());
39}
40
42{
43 relayOutput relay(4, -500, -100);
44 TEST_ASSERT_EQUAL(4, relay.get_pin());
45 TEST_ASSERT_EQUAL(500, relay.get_surplusThreshold());
46 TEST_ASSERT_EQUAL(-100, relay.get_importThreshold());
47}
48
49void test_get_pin(void)
50{
51 TEST_ASSERT_EQUAL(2, testRelays.get_relay(0).get_pin());
52 TEST_ASSERT_EQUAL(3, testRelays.get_relay(1).get_pin());
53}
54
56{
57 TEST_ASSERT_EQUAL(1000, testRelays.get_relay(0).get_surplusThreshold());
58 TEST_ASSERT_EQUAL(100, testRelays.get_relay(1).get_surplusThreshold());
59}
60
62{
63 TEST_ASSERT_EQUAL(200, testRelays.get_relay(0).get_importThreshold());
64 TEST_ASSERT_EQUAL(20, testRelays.get_relay(1).get_importThreshold());
65}
66
68{
69 TEST_ASSERT_EQUAL(1 * 60, testRelays.get_relay(0).get_minON());
70 TEST_ASSERT_EQUAL(2 * 60, testRelays.get_relay(1).get_minON());
71}
72
74{
75 TEST_ASSERT_EQUAL(1 * 60, testRelays.get_relay(0).get_minOFF());
76 TEST_ASSERT_EQUAL(3 * 60, testRelays.get_relay(1).get_minOFF());
77}
78
80{
81 TEST_ASSERT_FALSE(testRelays.get_relay(1).isRelayON());
82}
83
85{
86 const auto& my_relay{ testRelays.get_relay(1) };
87
88 TEST_ASSERT_FALSE(my_relay.isRelayON());
89
90 /* The relay is OFF, test the "TurnON" case */
91 const auto surplus{ -my_relay.get_surplusThreshold() - 1 };
92 uint16_t overrideBitmask = 0; // No override active
93
94 TEST_ASSERT_FALSE(my_relay.proceed_relay(surplus, overrideBitmask));
95 delay(100);
96 TEST_ASSERT_FALSE(my_relay.isRelayON());
97
98 for (uint8_t timer = 0; timer < my_relay.get_minOFF() - 1; ++timer)
99 {
100 my_relay.inc_duration();
101 }
102 overrideBitmask = 0; // Reset bitmask
103 TEST_ASSERT_FALSE(my_relay.proceed_relay(surplus, overrideBitmask));
104 delay(100);
105
106 my_relay.inc_duration();
107
108 overrideBitmask = 0; // Reset bitmask
109 TEST_ASSERT_TRUE(my_relay.proceed_relay(surplus, overrideBitmask));
110 TEST_ASSERT_TRUE(my_relay.isRelayON());
111}
112
114{
115 const auto& my_relay{ testRelays.get_relay(1) };
116
117 TEST_ASSERT_TRUE(my_relay.isRelayON());
118
119 /* The relay is ON, test the "TurnOFF" case */
120 const auto consum{ my_relay.get_importThreshold() + 1 };
121 uint16_t overrideBitmask = 0; // No override active
122
123 TEST_ASSERT_FALSE(my_relay.proceed_relay(consum, overrideBitmask));
124 delay(100);
125 TEST_ASSERT_TRUE(my_relay.isRelayON());
126
127 for (uint8_t timer = 0; timer < my_relay.get_minON() - 1; ++timer)
128 {
129 my_relay.inc_duration();
130 }
131 overrideBitmask = 0; // Reset bitmask
132 TEST_ASSERT_FALSE(my_relay.proceed_relay(consum, overrideBitmask));
133 delay(100);
134
135 my_relay.inc_duration();
136
137 overrideBitmask = 0; // Reset bitmask
138 TEST_ASSERT_TRUE(my_relay.proceed_relay(consum, overrideBitmask));
139 TEST_ASSERT_FALSE(my_relay.isRelayON());
140}
141
143{
144 const auto& my_relay{ testRelays.get_relay(1) };
145
146 // Ensure relay is OFF initially
147 TEST_ASSERT_FALSE(my_relay.isRelayON());
148
149 // Test override with insufficient surplus (would normally not turn ON)
150 const auto insufficient_surplus{ -my_relay.get_surplusThreshold() + 100 };
151
152 // Test at half the minimum OFF time - should still be blocked
153 for (uint8_t timer = 0; timer < my_relay.get_minOFF() / 2; ++timer)
154 {
155 my_relay.inc_duration();
156 }
157
158 uint16_t overrideBitmask = (1U << my_relay.get_pin());
159 TEST_ASSERT_FALSE(my_relay.proceed_relay(insufficient_surplus, overrideBitmask));
160 TEST_ASSERT_FALSE(my_relay.isRelayON());
161
162 // Increment to just before minimum OFF time is reached
163 for (uint8_t timer = my_relay.get_minOFF() / 2; timer < my_relay.get_minOFF() - 1; ++timer)
164 {
165 my_relay.inc_duration();
166 }
167
168 overrideBitmask = (1U << my_relay.get_pin());
169 TEST_ASSERT_FALSE(my_relay.proceed_relay(insufficient_surplus, overrideBitmask));
170 TEST_ASSERT_FALSE(my_relay.isRelayON());
171
172 // Increment one more time - now minimum OFF time is reached
173 my_relay.inc_duration();
174
175 overrideBitmask = (1U << my_relay.get_pin());
176 TEST_ASSERT_TRUE(my_relay.proceed_relay(insufficient_surplus, overrideBitmask));
177 TEST_ASSERT_TRUE(my_relay.isRelayON());
178}
179
181{
182 const auto& my_relay{ testRelays.get_relay(1) };
183
184 // Relay should be ON from previous test
185 TEST_ASSERT_TRUE(my_relay.isRelayON());
186
187 // Test that relay respects minimum ON time even after override is released
188 const auto high_import{ my_relay.get_importThreshold() + 100 }; // High import should turn OFF relay
189 uint16_t overrideBitmask = 0; // No override active anymore
190
191 // Test at half the minimum ON time - should NOT turn OFF yet
192 for (uint8_t timer = 0; timer < my_relay.get_minON() / 2; ++timer)
193 {
194 my_relay.inc_duration();
195 }
196
197 TEST_ASSERT_FALSE(my_relay.proceed_relay(high_import, overrideBitmask));
198 TEST_ASSERT_TRUE(my_relay.isRelayON()); // Should stay ON
199
200 // Test just before minimum ON time is reached
201 for (uint8_t timer = my_relay.get_minON() / 2; timer < my_relay.get_minON() - 1; ++timer)
202 {
203 my_relay.inc_duration();
204 }
205
206 TEST_ASSERT_FALSE(my_relay.proceed_relay(high_import, overrideBitmask));
207 TEST_ASSERT_TRUE(my_relay.isRelayON()); // Should still stay ON
208
209 // Increment one more time - now minimum ON time is reached
210 my_relay.inc_duration();
211
212 TEST_ASSERT_TRUE(my_relay.proceed_relay(high_import, overrideBitmask));
213 TEST_ASSERT_FALSE(my_relay.isRelayON()); // Now it can turn OFF
214}
215
217{
218 RUN_TEST(test_relay_turnON);
219 delay(100);
220 RUN_TEST(test_relay_turnOFF);
221 delay(100);
223 delay(100);
225}
226
228{
229 TEST_ASSERT_EQUAL(2, testRelays.size());
230}
231
232// ============================================================================
233// RelayDiverter settle_change tests
234// ============================================================================
235
236// Use a different D parameter (1 minute instead of 10) to:
237// 1. Create a separate EWMA instance (different template instantiation)
238// 2. Have faster EWMA convergence (fewer samples needed)
239// EWMA window = D * 60 / DATALOG_PERIOD_IN_SECONDS = 1 * 60 / 5 = 12 samples
241 { { 8, 500, 100, 1, 1 },
242 { 9, 800, 150, 1, 1 } } };
243
245{
246 // RelayDiverter initializes settle_change to 60 seconds
247 // proceed_relays() should return early and not change any relay state
248
249 // Ensure relays are OFF initially
250 TEST_ASSERT_FALSE(settleTestRelays.get_relay(0).isRelayON());
251 TEST_ASSERT_FALSE(settleTestRelays.get_relay(1).isRelayON());
252
253 // Feed surplus power - need enough samples for EWMA to converge
254 // With D=1, window is 12 samples, so ~50 samples should be enough
255 for (uint8_t i = 0; i < 50; ++i)
256 {
257 settleTestRelays.update_average(-600); // 600W surplus
258 }
259
260 // Try to proceed relays - should be blocked by settle_change
261 uint16_t overrideBitmask = 0;
262 settleTestRelays.proceed_relays(overrideBitmask);
263
264 // Relays should still be OFF because settle_change blocks changes
265 TEST_ASSERT_FALSE(settleTestRelays.get_relay(0).isRelayON());
266 TEST_ASSERT_FALSE(settleTestRelays.get_relay(1).isRelayON());
267}
268
270{
271 // After 60 calls to inc_duration(), settle_change should reach 0
272 // and allow relay state changes
273
274 // Wait for minOFF to elapse (60 seconds) AND settle_change to reach 0
275 for (uint8_t i = 0; i < 60; ++i)
276 {
277 settleTestRelays.inc_duration();
278 }
279
280 // Feed more surplus to ensure EWMA is well below threshold
281 for (uint8_t i = 0; i < 50; ++i)
282 {
283 settleTestRelays.update_average(-600); // 600W surplus > 500W threshold
284 }
285
286 // Now proceed_relays should be able to turn ON relay 0
287 uint16_t overrideBitmask = 0;
288 settleTestRelays.proceed_relays(overrideBitmask);
289
290 // Relay 0 should now be ON (it has the lower threshold)
291 TEST_ASSERT_TRUE(settleTestRelays.get_relay(0).isRelayON());
292 TEST_ASSERT_FALSE(settleTestRelays.get_relay(1).isRelayON());
293}
294
296{
297 // After a relay changes state, settle_change is reset to 60
298 // This should block the next relay from changing immediately
299
300 // Relay 0 is ON from previous test
301 TEST_ASSERT_TRUE(settleTestRelays.get_relay(0).isRelayON());
302
303 // Feed more surplus to try to turn ON relay 1
304 for (uint8_t i = 0; i < 50; ++i)
305 {
306 settleTestRelays.update_average(-1000); // 1000W surplus > 800W threshold
307 }
308
309 // Try to proceed relays immediately - should be blocked by settle_change
310 uint16_t overrideBitmask = 0;
311 settleTestRelays.proceed_relays(overrideBitmask);
312
313 // Relay 1 should still be OFF because settle_change was reset to 60
314 TEST_ASSERT_FALSE(settleTestRelays.get_relay(1).isRelayON());
315
316 // Wait for settle_change to elapse (60 seconds)
317 for (uint8_t i = 0; i < 60; ++i)
318 {
319 settleTestRelays.inc_duration();
320 }
321
322 // Keep feeding surplus
323 for (uint8_t i = 0; i < 50; ++i)
324 {
325 settleTestRelays.update_average(-1000);
326 }
327
328 // Now relay 1 should be able to turn ON
329 overrideBitmask = 0;
330 settleTestRelays.proceed_relays(overrideBitmask);
331
332 TEST_ASSERT_TRUE(settleTestRelays.get_relay(0).isRelayON());
333 TEST_ASSERT_TRUE(settleTestRelays.get_relay(1).isRelayON());
334}
335
337{
338 // Both relays are ON from previous test
339 TEST_ASSERT_TRUE(settleTestRelays.get_relay(0).isRelayON());
340 TEST_ASSERT_TRUE(settleTestRelays.get_relay(1).isRelayON());
341
342 // Feed import power to try to turn OFF relays
343 for (uint8_t i = 0; i < 50; ++i)
344 {
345 settleTestRelays.update_average(300); // 300W import > both import thresholds
346 }
347
348 // Try to proceed relays immediately - should be blocked
349 uint16_t overrideBitmask = 0;
350 settleTestRelays.proceed_relays(overrideBitmask);
351
352 // Both relays should still be ON (blocked by settle_change)
353 TEST_ASSERT_TRUE(settleTestRelays.get_relay(0).isRelayON());
354 TEST_ASSERT_TRUE(settleTestRelays.get_relay(1).isRelayON());
355
356 // Wait for settle_change AND minON to elapse
357 for (uint8_t i = 0; i < 60; ++i)
358 {
359 settleTestRelays.inc_duration();
360 }
361
362 // Keep feeding import
363 for (uint8_t i = 0; i < 50; ++i)
364 {
365 settleTestRelays.update_average(300);
366 }
367
368 // Now relay 1 should turn OFF first (reverse order for import)
369 overrideBitmask = 0;
370 settleTestRelays.proceed_relays(overrideBitmask);
371
372 TEST_ASSERT_TRUE(settleTestRelays.get_relay(0).isRelayON()); // Still ON
373 TEST_ASSERT_FALSE(settleTestRelays.get_relay(1).isRelayON()); // Turned OFF
374}
375
386
387// ============================================================================
388// Relay ordering tests
389// ============================================================================
390
391// Use 3 relays to clearly demonstrate ordering behavior
392// D=3 for separate EWMA instance
393// All relays have same thresholds to focus on ordering, not threshold differences
395 { { 11, 500, 100, 1, 1 },
396 { 12, 500, 100, 1, 1 },
397 { 13, 500, 100, 1, 1 } } };
398
400{
401 // Feed enough samples for EWMA to converge to surplus
402 for (uint8_t i = 0; i < 50; ++i)
403 {
404 orderingTestRelays.update_average(-600); // 600W surplus
405 }
406}
407
409{
410 // Feed enough samples for EWMA to converge to import
411 for (uint8_t i = 0; i < 50; ++i)
412 {
413 orderingTestRelays.update_average(200); // 200W import
414 }
415}
416
418{
419 // Wait for settle_change (60s) and minON/minOFF (60s) to elapse
420 for (uint8_t i = 0; i < 60; ++i)
421 {
422 orderingTestRelays.inc_duration();
423 }
424}
425
427{
428 // With surplus power, relays should turn ON in ascending order: 0 → 1 → 2
429 // This ensures lower-priority loads are activated first
430
431 // Verify all relays start OFF
432 TEST_ASSERT_FALSE(orderingTestRelays.get_relay(0).isRelayON());
433 TEST_ASSERT_FALSE(orderingTestRelays.get_relay(1).isRelayON());
434 TEST_ASSERT_FALSE(orderingTestRelays.get_relay(2).isRelayON());
435
436 // Wait for initial settle_change to expire
438
439 // Feed surplus power
441
442 // First proceed_relays call - relay 0 should turn ON
443 uint16_t overrideBitmask = 0;
444 orderingTestRelays.proceed_relays(overrideBitmask);
445
446 TEST_ASSERT_TRUE(orderingTestRelays.get_relay(0).isRelayON()); // ON first
447 TEST_ASSERT_FALSE(orderingTestRelays.get_relay(1).isRelayON());
448 TEST_ASSERT_FALSE(orderingTestRelays.get_relay(2).isRelayON());
449
450 // Wait for settle_change to expire
453
454 // Second proceed_relays call - relay 1 should turn ON
455 overrideBitmask = 0;
456 orderingTestRelays.proceed_relays(overrideBitmask);
457
458 TEST_ASSERT_TRUE(orderingTestRelays.get_relay(0).isRelayON());
459 TEST_ASSERT_TRUE(orderingTestRelays.get_relay(1).isRelayON()); // ON second
460 TEST_ASSERT_FALSE(orderingTestRelays.get_relay(2).isRelayON());
461
462 // Wait for settle_change to expire
465
466 // Third proceed_relays call - relay 2 should turn ON
467 overrideBitmask = 0;
468 orderingTestRelays.proceed_relays(overrideBitmask);
469
470 TEST_ASSERT_TRUE(orderingTestRelays.get_relay(0).isRelayON());
471 TEST_ASSERT_TRUE(orderingTestRelays.get_relay(1).isRelayON());
472 TEST_ASSERT_TRUE(orderingTestRelays.get_relay(2).isRelayON()); // ON third
473}
474
476{
477 // With import power, relays should turn OFF in descending order: 2 → 1 → 0
478 // This ensures higher-priority loads stay on longer
479
480 // All relays are ON from previous test
481 TEST_ASSERT_TRUE(orderingTestRelays.get_relay(0).isRelayON());
482 TEST_ASSERT_TRUE(orderingTestRelays.get_relay(1).isRelayON());
483 TEST_ASSERT_TRUE(orderingTestRelays.get_relay(2).isRelayON());
484
485 // Wait for settle_change to expire
487
488 // Feed import power
490
491 // First proceed_relays call - relay 2 should turn OFF (last ON, first OFF)
492 uint16_t overrideBitmask = 0;
493 orderingTestRelays.proceed_relays(overrideBitmask);
494
495 TEST_ASSERT_TRUE(orderingTestRelays.get_relay(0).isRelayON());
496 TEST_ASSERT_TRUE(orderingTestRelays.get_relay(1).isRelayON());
497 TEST_ASSERT_FALSE(orderingTestRelays.get_relay(2).isRelayON()); // OFF first
498
499 // Wait for settle_change to expire
502
503 // Second proceed_relays call - relay 1 should turn OFF
504 overrideBitmask = 0;
505 orderingTestRelays.proceed_relays(overrideBitmask);
506
507 TEST_ASSERT_TRUE(orderingTestRelays.get_relay(0).isRelayON());
508 TEST_ASSERT_FALSE(orderingTestRelays.get_relay(1).isRelayON()); // OFF second
509 TEST_ASSERT_FALSE(orderingTestRelays.get_relay(2).isRelayON());
510
511 // Wait for settle_change to expire
514
515 // Third proceed_relays call - relay 0 should turn OFF
516 overrideBitmask = 0;
517 orderingTestRelays.proceed_relays(overrideBitmask);
518
519 TEST_ASSERT_FALSE(orderingTestRelays.get_relay(0).isRelayON()); // OFF third
520 TEST_ASSERT_FALSE(orderingTestRelays.get_relay(1).isRelayON());
521 TEST_ASSERT_FALSE(orderingTestRelays.get_relay(2).isRelayON());
522}
523
530
531// ============================================================================
532// Duration overflow tests
533// ============================================================================
534
535// Use D=2 to create a separate EWMA instance from other tests
536// Using pin 10 to avoid conflicts with other tests
538 { { 10, 500, 100, 1, 1 } } };
539
541{
542 // Test that duration saturates at UINT16_MAX and doesn't wrap to 0.
543 // If it wrapped, the relay would be incorrectly blocked after overflow.
544 //
545 // The inc_duration() function has this protection:
546 // if (duration < UINT16_MAX) { ++duration; }
547
548 const auto& relay = overflowTestRelays.get_relay(0);
549
550 // Ensure relay is OFF initially
551 TEST_ASSERT_FALSE(relay.isRelayON());
552
553 // Increment duration to UINT16_MAX (65535 iterations)
554 // This takes only a few seconds on Arduino - no actual waiting involved
555 for (uint32_t i = 0; i < UINT16_MAX; ++i)
556 {
557 relay.inc_duration();
558 }
559
560 // At this point duration == UINT16_MAX (65535)
561 // Relay should be able to turn ON (duration >> minOFF which is 60)
562 uint16_t overrideBitmask = 0;
563 const int32_t surplus = -600; // 600W surplus > 500W threshold
564 TEST_ASSERT_TRUE(relay.proceed_relay(surplus, overrideBitmask));
565 TEST_ASSERT_TRUE(relay.isRelayON());
566}
567
569{
570 // After reaching UINT16_MAX, further increments should not wrap to 0
571 // The relay should remain functional
572
573 const auto& relay = overflowTestRelays.get_relay(0);
574
575 // Relay is ON from previous test
576 TEST_ASSERT_TRUE(relay.isRelayON());
577
578 // Increment many more times beyond UINT16_MAX
579 // If there was no saturation check, this would wrap duration to ~1000
580 // and block the relay from turning OFF (duration < minON)
581 for (uint16_t i = 0; i < 1000; ++i)
582 {
583 relay.inc_duration();
584 }
585
586 // Relay should still be able to turn OFF
587 // If duration wrapped to ~1000, this would still work (1000 > 60)
588 // So we need a more precise test...
589 const int32_t import = 200; // 200W import > 100W threshold
590 uint16_t overrideBitmask = 0;
591 TEST_ASSERT_TRUE(relay.proceed_relay(import, overrideBitmask));
592 TEST_ASSERT_FALSE(relay.isRelayON());
593}
594
596{
597 // This test proves that without saturation, wrapping WOULD cause issues.
598 // We increment exactly to where a wrap would put duration below minOFF,
599 // then verify the relay still works (proving no wrap occurred).
600
601 const auto& relay = overflowTestRelays.get_relay(0);
602
603 // Relay is OFF from previous test
604 TEST_ASSERT_FALSE(relay.isRelayON());
605
606 // Increment to UINT16_MAX
607 for (uint32_t i = 0; i < UINT16_MAX; ++i)
608 {
609 relay.inc_duration();
610 }
611
612 // Now increment 50 more times
613 // If duration wrapped: (65535 + 50) % 65536 = 49, which is < minOFF (60)
614 // If duration saturates: it stays at 65535, which is > minOFF (60)
615 for (uint8_t i = 0; i < 50; ++i)
616 {
617 relay.inc_duration();
618 }
619
620 // Try to turn ON the relay
621 // If wrapped to 49: would FAIL (49 < 60, blocked by minOFF)
622 // If saturated at 65535: should SUCCEED (65535 > 60)
623 uint16_t overrideBitmask = 0;
624 const int32_t surplus = -600;
625 TEST_ASSERT_TRUE(relay.proceed_relay(surplus, overrideBitmask));
626 TEST_ASSERT_TRUE(relay.isRelayON());
627
628 // Similarly test turn OFF
629 // Increment to UINT16_MAX again
630 for (uint32_t i = 0; i < UINT16_MAX; ++i)
631 {
632 relay.inc_duration();
633 }
634
635 // Increment 50 more times (would wrap to 49 if not saturating)
636 for (uint8_t i = 0; i < 50; ++i)
637 {
638 relay.inc_duration();
639 }
640
641 // Try to turn OFF
642 // If wrapped to 49: would FAIL (49 < 60, blocked by minON)
643 // If saturated: should SUCCEED
644 const int32_t import = 200;
645 overrideBitmask = 0;
646 TEST_ASSERT_TRUE(relay.proceed_relay(import, overrideBitmask));
647 TEST_ASSERT_FALSE(relay.isRelayON());
648}
649
658
659// ============================================================================
660// proceed_forced_only tests (diversion disabled)
661// ============================================================================
662
663// Use D=4 to create a separate EWMA instance
665 { { 4, 500, 100, 1, 1 } } };
666
668{
669 // not forced, on a relay that's already OFF: no change
670 const auto& relay = forceOffTestRelays.get_relay(0);
671
672 TEST_ASSERT_FALSE(relay.isRelayON());
673 TEST_ASSERT_FALSE(relay.proceed_forced_only(0));
674 TEST_ASSERT_FALSE(relay.isRelayON());
675}
676
678{
679 // First turn the relay ON
680 const auto& relay = forceOffTestRelays.get_relay(0);
681
682 // Wait for minOFF to elapse
683 for (uint8_t i = 0; i < 60; ++i)
684 {
685 relay.inc_duration();
686 }
687
688 // Turn ON the relay
689 uint16_t overrideBitmask = 0;
690 const int32_t surplus = -600;
691 TEST_ASSERT_TRUE(relay.proceed_relay(surplus, overrideBitmask));
692 TEST_ASSERT_TRUE(relay.isRelayON());
693
694 // Not forced any more: OFF fails at once (minON not met)
695 TEST_ASSERT_FALSE(relay.proceed_forced_only(0));
696 TEST_ASSERT_TRUE(relay.isRelayON());
697
698 // Wait half of minON
699 for (uint8_t i = 0; i < 30; ++i)
700 {
701 relay.inc_duration();
702 }
703
704 // Still should fail
705 TEST_ASSERT_FALSE(relay.proceed_forced_only(0));
706 TEST_ASSERT_TRUE(relay.isRelayON());
707}
708
710{
711 const auto& relay = forceOffTestRelays.get_relay(0);
712
713 // Relay is ON from previous test
714 TEST_ASSERT_TRUE(relay.isRelayON());
715
716 // Wait for remaining minON time (30 more seconds)
717 for (uint8_t i = 0; i < 30; ++i)
718 {
719 relay.inc_duration();
720 }
721
722 // Now turning OFF should succeed
723 TEST_ASSERT_TRUE(relay.proceed_forced_only(0));
724 TEST_ASSERT_FALSE(relay.isRelayON());
725}
726
728{
729 // Diversion disabled, but the relay is forced: it turns ON once its minOFF is over
730 const auto& relay = forceOffTestRelays.get_relay(0);
731 const uint16_t forced{ static_cast< uint16_t >(1U << relay.get_pin()) };
732
733 TEST_ASSERT_FALSE(relay.isRelayON());
734 TEST_ASSERT_FALSE(relay.proceed_forced_only(forced)); // minOFF not met yet
735 TEST_ASSERT_FALSE(relay.isRelayON());
736
737 for (uint8_t i = 0; i < 60; ++i)
738 {
739 relay.inc_duration();
740 }
741
742 TEST_ASSERT_TRUE(relay.proceed_forced_only(forced));
743 TEST_ASSERT_TRUE(relay.isRelayON());
744 TEST_ASSERT_FALSE(relay.proceed_forced_only(forced)); // stays ON while forced
745}
746
748{
750 delay(100);
752 delay(100);
754 delay(100);
756}
757
758// ============================================================================
759// Diversion disabled tests (proceed_relays with diversionEnabled=false)
760// ============================================================================
761
762// Use D=5 to create a separate EWMA instance
764 { { 5, 500, 100, 1, 1 },
765 { 6, 800, 150, 1, 1 } } };
766
768{
769 // First turn on both relays
770 const auto& relay0 = diversionTestRelays.get_relay(0);
771 const auto& relay1 = diversionTestRelays.get_relay(1);
772
773 // Wait for initial settle_change and minOFF to elapse
774 for (uint8_t i = 0; i < 60; ++i)
775 {
776 diversionTestRelays.inc_duration();
777 }
778
779 // Feed surplus to EWMA
780 for (uint8_t i = 0; i < 50; ++i)
781 {
782 diversionTestRelays.update_average(-600);
783 }
784
785 // Turn on relay 0
786 uint16_t overrideBitmask = 0;
787 diversionTestRelays.proceed_relays(overrideBitmask);
788 TEST_ASSERT_TRUE(relay0.isRelayON());
789
790 // Wait for settle_change
791 for (uint8_t i = 0; i < 60; ++i)
792 {
793 diversionTestRelays.inc_duration();
794 }
795
796 // Feed more surplus
797 for (uint8_t i = 0; i < 50; ++i)
798 {
799 diversionTestRelays.update_average(-1000);
800 }
801
802 // Turn on relay 1
803 overrideBitmask = 0;
804 diversionTestRelays.proceed_relays(overrideBitmask);
805 TEST_ASSERT_TRUE(relay0.isRelayON());
806 TEST_ASSERT_TRUE(relay1.isRelayON());
807
808 // Wait for settle_change and minON to elapse
809 for (uint8_t i = 0; i < 60; ++i)
810 {
811 diversionTestRelays.inc_duration();
812 }
813
814 // Now disable diversion - both relays should turn OFF
815 overrideBitmask = 0;
816 diversionTestRelays.proceed_relays(overrideBitmask, false);
817
818 TEST_ASSERT_FALSE(relay0.isRelayON());
819 TEST_ASSERT_FALSE(relay1.isRelayON());
820}
821
823{
824 // Turn relays back on, then try to force off before minON
825 const auto& relay0 = diversionTestRelays.get_relay(0);
826
827 // Wait for settle_change and minOFF
828 for (uint8_t i = 0; i < 60; ++i)
829 {
830 diversionTestRelays.inc_duration();
831 }
832
833 // Feed surplus
834 for (uint8_t i = 0; i < 50; ++i)
835 {
836 diversionTestRelays.update_average(-600);
837 }
838
839 // Turn on relay 0
840 uint16_t overrideBitmask = 0;
841 diversionTestRelays.proceed_relays(overrideBitmask);
842 TEST_ASSERT_TRUE(relay0.isRelayON());
843
844 // Immediately try to disable diversion (settle_change is 60, so it will be blocked)
845 // First wait for settle_change but NOT minON
846 // Actually settle_change blocks proceed_relays entirely, so we need to wait
847 // Try immediately - should be blocked by settle_change
848 overrideBitmask = 0;
849 diversionTestRelays.proceed_relays(overrideBitmask, false);
850 TEST_ASSERT_TRUE(relay0.isRelayON()); // Still ON due to settle_change
851
852 // Wait for settle_change but only half of minON
853 for (uint8_t i = 0; i < 30; ++i)
854 {
855 diversionTestRelays.inc_duration();
856 }
857
858 // Still blocked by settle_change (60s total)
859 overrideBitmask = 0;
860 diversionTestRelays.proceed_relays(overrideBitmask, false);
861 TEST_ASSERT_TRUE(relay0.isRelayON());
862
863 // Wait remaining settle_change time (30 more = 60 total = minON)
864 for (uint8_t i = 0; i < 30; ++i)
865 {
866 diversionTestRelays.inc_duration();
867 }
868
869 // Now settle_change is 0 and minON is met, should turn off
870 overrideBitmask = 0;
871 diversionTestRelays.proceed_relays(overrideBitmask, false);
872 TEST_ASSERT_FALSE(relay0.isRelayON());
873}
874
876{
877 // After forcing relays off, settle_change should be set to 60
878 const auto& relay0 = diversionTestRelays.get_relay(0);
879
880 // Turn relay back on
881 for (uint8_t i = 0; i < 60; ++i)
882 {
883 diversionTestRelays.inc_duration();
884 }
885
886 for (uint8_t i = 0; i < 50; ++i)
887 {
888 diversionTestRelays.update_average(-600);
889 }
890
891 uint16_t overrideBitmask = 0;
892 diversionTestRelays.proceed_relays(overrideBitmask);
893 TEST_ASSERT_TRUE(relay0.isRelayON());
894
895 // Wait for settle_change and minON
896 for (uint8_t i = 0; i < 60; ++i)
897 {
898 diversionTestRelays.inc_duration();
899 }
900
901 // Disable diversion
902 overrideBitmask = 0;
903 diversionTestRelays.proceed_relays(overrideBitmask, false);
904 TEST_ASSERT_FALSE(relay0.isRelayON());
905
906 // Re-enable diversion immediately - should be blocked by settle_change
907 for (uint8_t i = 0; i < 50; ++i)
908 {
909 diversionTestRelays.update_average(-600);
910 }
911
912 overrideBitmask = 0;
913 diversionTestRelays.proceed_relays(overrideBitmask, true);
914 TEST_ASSERT_FALSE(relay0.isRelayON()); // Still OFF due to settle_change
915}
916
918{
919 // With diversion disabled, a forced relay turns ON while the other one stays OFF
920 const auto& relay0 = diversionTestRelays.get_relay(0);
921 const auto& relay1 = diversionTestRelays.get_relay(1);
922
923 // Wait for settle_change and minOFF
924 for (uint8_t i = 0; i < 60; ++i)
925 {
926 diversionTestRelays.inc_duration();
927 }
928
929 TEST_ASSERT_FALSE(relay0.isRelayON());
930 TEST_ASSERT_FALSE(relay1.isRelayON());
931
932 const uint16_t forceRelay1{ static_cast< uint16_t >(1U << relay1.get_pin()) };
933 diversionTestRelays.proceed_relays(forceRelay1, false);
934
935 TEST_ASSERT_FALSE(relay0.isRelayON());
936 TEST_ASSERT_TRUE(relay1.isRelayON());
937}
938
940{
942 delay(100);
944 delay(100);
946 delay(100);
948}
949
950void setup()
951{
952 delay(1000);
953
954 UNITY_BEGIN(); // IMPORTANT LINE!
955}
956
957void loop()
958{
962
963 RUN_TEST(test_get_size);
964
965 RUN_TEST(test_get_pin);
966
968 RUN_TEST(test_get_importThreshold);
969
970 RUN_TEST(test_get_minON);
971 RUN_TEST(test_get_minOFF);
972
973 RUN_TEST(test_isRelayON);
974
975 RUN_TEST(test_proceed_relay);
976
978
979 RUN_TEST(test_relay_ordering);
980
981 RUN_TEST(test_duration_overflow);
982
983 RUN_TEST(test_forced_only);
984
985 RUN_TEST(test_diversion_disabled);
986
987 UNITY_END(); // stop unit testing
989}
Manages a collection of relays and their behavior based on surplus and import thresholds.
constexpr auto get_surplusThreshold() const
Get the surplus threshold which will turns ON the relay.
constexpr auto get_importThreshold() const
Get the import threshold which will turns OFF the relay.
constexpr auto get_pin() const
Get the control pin of the relay.
Represents a single relay configuration and its behavior.
Definition utils_relay.h:57
void setup()
void setUp(void)
Definition test_main.cpp:27
void tearDown(void)
Definition test_main.cpp:28
void loop()
uint8_t i
void test_duration_overflow_wrap_would_block_relay(void)
constexpr RelayDiverter< 1, 2 > overflowTestRelays
void test_relay_ordering(void)
void test_forced_only_when_already_off(void)
void feed_surplus_to_ordering_relays(void)
void test_duration_overflow(void)
void test_get_pin(void)
Definition test_main.cpp:49
void test_forced_only_succeeds_after_minON(void)
void test_get_minOFF(void)
Definition test_main.cpp:73
void test_relay_override_turnON(void)
void test_proceed_relay(void)
void test_relay_ordering_import_turns_off_descending(void)
void test_diversion_disabled(void)
constexpr RelayDiverter< 2, 5 > diversionTestRelays
void test_settle_change_blocks_initial_relay_changes(void)
void test_isRelayON(void)
Definition test_main.cpp:79
void wait_for_relay_change_allowed(void)
void test_forced_only(void)
constexpr RelayDiverter testRelays
Definition test_main.cpp:12
void test_relay_turnOFF(void)
void test_get_size(void)
void test_get_minON(void)
Definition test_main.cpp:67
constexpr RelayDiverter< 2, 1 > settleTestRelays
void test_duration_overflow_stays_at_max_after_more_increments(void)
void test_proceed_settle_change(void)
void test_forced_only_respects_minON_time(void)
void feed_import_to_ordering_relays(void)
void test_diversion_disabled_sets_settle_change(void)
void test_get_surplusThreshold(void)
Definition test_main.cpp:55
void test_relay_override_minimum_ON_time(void)
void test_relay_initialization_with_positive_thresholds(void)
Definition test_main.cpp:33
void test_settle_change_blocks_turn_off_as_well(void)
void test_get_importThreshold(void)
Definition test_main.cpp:61
void test_settle_change_allows_changes_after_60_seconds(void)
void test_relay_initialization(void)
Definition test_main.cpp:25
void test_diversion_disabled_keeps_forced_relay_on(void)
void test_diversion_disabled_forces_relays_off(void)
void test_relay_ordering_surplus_turns_on_ascending(void)
void test_diversion_disabled_respects_minON(void)
void test_settle_change_resets_after_relay_change(void)
void test_relay_turnON(void)
Definition test_main.cpp:84
constexpr RelayDiverter< 1, 4 > forceOffTestRelays
void test_forced_only_turns_on_when_forced(void)
constexpr RelayDiverter< 3, 3 > orderingTestRelays
void test_relay_initialization_with_negative_thresholds(void)
Definition test_main.cpp:41
void test_duration_overflow_saturates_at_uint16_max(void)
Stops the MCU once an embedded test suite has run.
void haltAfterTests()
Some basics classes/types.
Some utility functions for pins manipulation.
Some utility functions for the relay output feature.