28constexpr float PI = 3.14159265358979f;
70 int32_t sampleIminusDC = (
static_cast< int32_t
>(rawSample - 512)) << 8;
74 const int32_t filtI_div4 = sampleIminusDC >> 2;
75 int32_t instP = filtV_div4 * filtI_div4;
88 int32_t inst_Vsquared = filtV_div4 * filtV_div4;
105 constexpr int32_t l_DCoffset_V_min = (512L - 100L) * 256L;
106 constexpr int32_t l_DCoffset_V_max = (512L + 100L) * 256L;
183 int16_t sampleIminusDC = (rawSample | 32U) -
i_DCoffset_V;
187 const int16_t filtI_div4 = sampleIminusDC >> 2;
188 int32_t instP =
static_cast< int32_t
>(filtV_div4) * filtI_div4;
201 int32_t inst_Vsquared =
static_cast< int32_t
>(filtV_div4) * filtV_div4;
284 if (amplitude == 0)
return 0;
285 return static_cast< int16_t
>((
next() % (2 * amplitude + 1)) - amplitude);
313 float pctP = (
oldSumP != 0) ? 100.0f * fabsf(
static_cast< float >(diffP)) / fabsf(
static_cast< float >(
oldSumP)) : 0.0f;
317 printf(
" Power - Old: %ld, New: %ld, Diff: %ld (%.4f%%)\n",
320 static_cast< long >(diffP),
321 static_cast< double >(pctP));
325 int32_t diffVsq =
static_cast< int32_t
>(
newSumVsq) -
static_cast< int32_t
>(
oldSumVsq);
326 float pctVsq = (
oldSumVsq != 0) ? 100.0f * fabsf(
static_cast< float >(diffVsq)) /
static_cast< float >(
oldSumVsq) : 0.0f;
327 printf(
" V^2 - Old: %lu, New: %lu, Diff: %ld (%.4f%%)\n",
330 static_cast< long >(diffVsq),
331 static_cast< double >(pctVsq));
339uint16_t
generateSineADC(uint16_t sampleIndex, uint16_t dcLevel, int16_t amplitude,
float phaseShift = 0.0f)
342 int16_t ac =
static_cast< int16_t
>(amplitude * sinf(angle));
343 int16_t adc = dcLevel + ac;
344 if (adc < 0) adc = 0;
345 if (adc > 1023) adc = 1023;
346 return static_cast< uint16_t
>(adc);
353 int16_t noiseAmplitude,
float phaseShift = 0.0f)
356 int16_t ac =
static_cast< int16_t
>(amplitude * sinf(angle));
357 int16_t noise =
rng.noise(noiseAmplitude);
358 int16_t adc = dcLevel + ac + noise;
359 if (adc < 0) adc = 0;
360 if (adc > 1023) adc = 1023;
361 return static_cast< uint16_t
>(adc);
369 return rightAligned << 6;
404 uint16_t voltage_adc;
405 uint16_t current_adc;
418 for (
const auto& tc : cases)
436 int32_t tolerance = abs(oldTest.
l_sumP) / 50 + 5;
437 TEST_ASSERT_INT32_WITHIN(tolerance, oldTest.
l_sumP, newTest.
l_sumP);
452 oldImpl.processPolarity(v_adc);
453 oldImpl.processCurrentRawSample(i_adc);
462 TEST_ASSERT_GREATER_THAN(0,
oldImpl.l_sumP);
463 TEST_ASSERT_GREATER_THAN(0,
newImpl.l_sumP);
468 float old_power =
static_cast< float >(
oldImpl.l_sumP);
469 float new_power =
static_cast< float >(
newImpl.l_sumP);
470 float tolerance = fabsf(old_power) * 0.03f;
471 TEST_ASSERT_FLOAT_WITHIN(tolerance, old_power, new_power);
485 oldImpl.processPolarity(v_adc);
486 oldImpl.processCurrentRawSample(i_adc);
497 float tolerance = max_power * 0.05f;
499 TEST_ASSERT_FLOAT_WITHIN(tolerance, 0.0f,
static_cast< float >(
oldImpl.l_sumP));
500 TEST_ASSERT_FLOAT_WITHIN(tolerance, 0.0f,
static_cast< float >(
newImpl.l_sumP));
514 oldImpl.processPolarity(v_adc);
515 oldImpl.processCurrentRawSample(i_adc);
524 TEST_ASSERT_LESS_THAN(0,
oldImpl.l_sumP);
525 TEST_ASSERT_LESS_THAN(0,
newImpl.l_sumP);
528 float old_power =
static_cast< float >(
oldImpl.l_sumP);
529 float new_power =
static_cast< float >(
newImpl.l_sumP);
530 float tolerance = fabsf(old_power) * 0.05f;
531 TEST_ASSERT_FLOAT_WITHIN(tolerance, old_power, new_power);
548 oldImpl.processPolarity(v_adc);
556 float old_rms =
oldImpl.getRmsVoltage();
557 float new_rms =
newImpl.getRmsVoltage();
558 float tolerance = old_rms * 0.05f;
560 TEST_ASSERT_FLOAT_WITHIN(tolerance, old_rms, new_rms);
577 oldImpl.processPolarity(v_adc);
588 float old_rms =
oldImpl.getRmsVoltage();
589 float new_rms =
newImpl.getRmsVoltage();
592 TEST_ASSERT_FLOAT_WITHIN(theoretical_rms * 0.1f, theoretical_rms, old_rms);
593 TEST_ASSERT_FLOAT_WITHIN(theoretical_rms * 0.1f, theoretical_rms, new_rms);
614 oldImpl.processPolarity(v_adc);
615 oldImpl.processCurrentRawSample(i_adc);
626 oldImpl.processMinusHalfCycle();
627 newImpl.processMinusHalfCycle();
633 int16_t old_offset =
oldImpl.getOffsetAsADC();
634 int16_t new_offset =
newImpl.getOffsetAsADC();
635 TEST_ASSERT_INT16_WITHIN(10, old_offset, new_offset);
638 float old_avg_power =
oldImpl.getAveragePower();
639 float new_avg_power =
newImpl.getAveragePower();
640 float power_tolerance = fabsf(old_avg_power) * 0.05f + 1.0f;
641 TEST_ASSERT_FLOAT_WITHIN(power_tolerance, old_avg_power, new_avg_power);
644 float old_rms =
oldImpl.getRmsVoltage();
645 float new_rms =
newImpl.getRmsVoltage();
646 float rms_tolerance = old_rms * 0.05f;
647 TEST_ASSERT_FLOAT_WITHIN(rms_tolerance, old_rms, new_rms);
658 const uint16_t dc_offset = 530;
668 oldImpl.processPolarity(v_adc);
669 oldImpl.processCurrentRawSample(i_adc);
678 oldImpl.processMinusHalfCycle();
679 newImpl.processMinusHalfCycle();
685 int16_t old_offset =
oldImpl.getOffsetAsADC();
686 int16_t new_offset =
newImpl.getOffsetAsADC();
690 TEST_ASSERT_INT16_WITHIN(20, dc_offset, new_offset);
693 float old_avg_power =
oldImpl.getAveragePower();
694 float new_avg_power =
newImpl.getAveragePower();
695 float tolerance = fabsf(old_avg_power) * 0.10f + 5.0f;
696 TEST_ASSERT_FLOAT_WITHIN(tolerance, old_avg_power, new_avg_power);
708 float phase_offsets[3] = { 0.0f, 2.0f *
PI / 3.0f, 4.0f *
PI / 3.0f };
715 for (
int phase = 0; phase < 3; ++phase)
731 for (
int phase = 0; phase < 3; ++phase)
741 int32_t old_total_power = 0;
742 int32_t new_total_power = 0;
743 for (
int phase = 0; phase < 3; ++phase)
745 old_total_power += oldPhases[phase].
l_sumP;
746 new_total_power += newPhases[phase].
l_sumP;
750 float tolerance = fabsf(
static_cast< float >(old_total_power)) * 0.05f;
751 TEST_ASSERT_INT32_WITHIN(
static_cast< int32_t
>(tolerance), old_total_power, new_total_power);
769 oldImpl.processPolarity(v_adc);
770 oldImpl.processCurrentRawSample(i_adc);
781 TEST_ASSERT_INT32_WITHIN(500, 0,
oldImpl.l_sumP);
782 TEST_ASSERT_INT32_WITHIN(500, 0,
newImpl.l_sumP);
791 const uint16_t high_amplitude = 450;
798 oldImpl.processPolarity(v_adc);
799 oldImpl.processCurrentRawSample(i_adc);
808 float old_power =
static_cast< float >(
oldImpl.l_sumP);
809 float new_power =
static_cast< float >(
newImpl.l_sumP);
810 float tolerance = fabsf(old_power) * 0.10f;
811 TEST_ASSERT_FLOAT_WITHIN(tolerance, old_power, new_power);
823 int32_t old_power_per_cycle[10];
824 int32_t new_power_per_cycle[10];
837 oldImpl.processPolarity(v_adc);
838 oldImpl.processCurrentRawSample(i_adc);
847 oldImpl.processMinusHalfCycle();
848 newImpl.processMinusHalfCycle();
857 for (
int i = 0;
i < 10; ++
i)
859 TEST_ASSERT_GREATER_THAN(0, old_power_per_cycle[
i]);
860 TEST_ASSERT_GREATER_THAN(0, new_power_per_cycle[
i]);
864 int32_t old_avg = old_power_per_cycle[0];
865 int32_t new_avg = new_power_per_cycle[0];
866 for (
int i = 1;
i < 10; ++
i)
868 TEST_ASSERT_INT32_WITHIN(old_avg / 20, old_avg, old_power_per_cycle[
i]);
869 TEST_ASSERT_INT32_WITHIN(new_avg / 20, new_avg, new_power_per_cycle[
i]);
873 float tolerance = fabsf(
static_cast< float >(old_avg)) * 0.05f;
874 TEST_ASSERT_FLOAT_WITHIN(tolerance,
static_cast< float >(old_avg),
static_cast< float >(new_avg));
908 float diff_percent = 100.0f * fabsf(
static_cast< float >(newTest.
l_sumP - oldTest.
l_sumP))
909 / fabsf(
static_cast< float >(oldTest.
l_sumP));
912 TEST_ASSERT_FLOAT_WITHIN(0.1f, 0.0f, diff_percent);
917 TEST_ASSERT_FLOAT_WITHIN(0.1f, 0.0f, vsq_diff_percent);
928 const uint16_t v_adc = 712;
929 const uint16_t i_adc = 612;
931 oldImpl.processPolarity(v_adc);
932 oldImpl.processCurrentRawSample(i_adc);
944 float tolerance = fabsf(
static_cast< float >(
oldImpl.l_sumP)) * 0.05f + 100.0f;
945 TEST_ASSERT_INT32_WITHIN(
static_cast< int32_t
>(tolerance),
oldImpl.l_sumP,
newImpl.l_sumP);
966 const int NUM_CYCLES = 10000;
969 const int DATALOG_CYCLES = 500;
970 float max_diff_percent = 0.0f;
995 if ((
cycle + 1) % DATALOG_CYCLES == 0)
997 float diff = 100.0f * fabsf(
static_cast< float >(newTest.
l_sumP - oldTest.
l_sumP))
998 / fabsf(
static_cast< float >(oldTest.
l_sumP));
999 if (diff > max_diff_percent) max_diff_percent = diff;
1010 TEST_ASSERT_FLOAT_WITHIN(0.1f, 0.0f, max_diff_percent);
1029 const int NUM_CYCLES = 1000;
1030 const int16_t NOISE_AMPLITUDE = 2;
1057 float diff_percent = 100.0f * fabsf(
static_cast< float >(newTest.
l_sumP - oldTest.
l_sumP))
1058 / fabsf(
static_cast< float >(oldTest.
l_sumP));
1059 TEST_ASSERT_FLOAT_WITHIN(0.1f, 0.0f, diff_percent);
1064 TEST_ASSERT_FLOAT_WITHIN(0.1f, 0.0f, vsq_diff);
1082 const int NUM_CYCLES = 1000;
1083 const int16_t NOISE_AMPLITUDE = 5;
1108 float diff_percent = 100.0f * fabsf(
static_cast< float >(newTest.
l_sumP - oldTest.
l_sumP))
1109 / fabsf(
static_cast< float >(oldTest.
l_sumP));
1110 TEST_ASSERT_FLOAT_WITHIN(0.1f, 0.0f, diff_percent);
1122 float phase_shifts[] = { 0.0f,
PI / 6,
PI / 4,
PI / 3,
PI / 2 };
1123 const int16_t NOISE_AMPLITUDE = 3;
1125 for (
float phase_shift : phase_shifts)
1162 int32_t abs_diff = abs(newTest.
l_sumP - oldTest.
l_sumP);
1164 if (phase_shift <
PI / 4)
1166 float diff = 100.0f *
static_cast< float >(abs_diff)
1167 / fabsf(
static_cast< float >(oldTest.
l_sumP));
1168 TEST_ASSERT_FLOAT_WITHIN(0.2f, 0.0f, diff);
1175 TEST_ASSERT_TRUE((oldTest.
l_sumP > 0) == (newTest.
l_sumP > 0) || abs(oldTest.
l_sumP) < 50000);
1195 const int NUM_CYCLES = 50000;
1196 const int16_t NOISE_AMPLITUDE = 2;
1197 float max_diff = 0.0f;
1222 if ((
cycle + 1) % 1000 == 0)
1224 float diff = 100.0f * fabsf(
static_cast< float >(newTest.
l_sumP - oldTest.
l_sumP))
1225 / fabsf(
static_cast< float >(oldTest.
l_sumP));
1226 if (diff > max_diff) max_diff = diff;
1234 TEST_ASSERT_FLOAT_WITHIN(0.1f, 0.0f, max_diff);
1251 int16_t current_amplitudes[] = { 20, 50, 100, 150, 200, 300, 400, 450, 500, 505 };
1252 const int16_t NOISE_AMPLITUDE = 2;
1253 const int NUM_CYCLES = 1000;
1255 printf(
"\n [test_multiple_current_amplitudes]\n");
1256 printf(
" Testing %zu amplitude levels, %d cycles each:\n",
sizeof(current_amplitudes) /
sizeof(current_amplitudes[0]), NUM_CYCLES);
1257 printf(
" %6s %12s %12s %12s %10s\n",
"Amp",
"Old_sumP",
"New_sumP",
"Diff",
"Diff%");
1259 for (int16_t i_amp : current_amplitudes)
1293 float pct = (oldTest.
l_sumP != 0) ? 100.0f * fabsf(
static_cast< float >(diff)) / fabsf(
static_cast< float >(oldTest.
l_sumP)) : 0.0f;
1294 printf(
" %6d %12ld %12ld %12ld %9.4f%%\n",
1296 static_cast< long >(oldTest.
l_sumP),
1297 static_cast< long >(newTest.
l_sumP),
1298 static_cast< long >(diff),
1299 static_cast< double >(pct));
1304 TEST_ASSERT_FLOAT_WITHIN(0.1f, 0.0f, pct);
1324 const int NUM_CYCLES = 5000;
1325 const int16_t NOISE_AMPLITUDE = 2;
1326 const int16_t MIN_CURRENT = 50;
1327 const int16_t MAX_CURRENT = 500;
1328 const int VARIATION_PERIOD = 200;
1333 float load_angle = 2.0f *
PI *
cycle / VARIATION_PERIOD;
1334 int16_t current_amp = MIN_CURRENT +
static_cast< int16_t
>((MAX_CURRENT - MIN_CURRENT) * (0.5f + 0.5f * sinf(load_angle)));
1358 float pctP = 100.0f * fabsf(
static_cast< float >(diffP)) / fabsf(
static_cast< float >(oldTest.
l_sumP));
1360 printf(
"\n [test_sinusoidal_load_variation]\n");
1361 printf(
" Cycles: %d, Current range: %d-%d ADC (sinusoidal variation)\n", NUM_CYCLES, MIN_CURRENT, MAX_CURRENT);
1362 printf(
" Power - Old: %ld, New: %ld, Diff: %ld (%.4f%%)\n",
1363 static_cast< long >(oldTest.
l_sumP),
1364 static_cast< long >(newTest.
l_sumP),
1365 static_cast< long >(diffP),
1366 static_cast< double >(pctP));
1368 TEST_ASSERT_FLOAT_WITHIN(0.1f, 0.0f, pctP);
1386 const int NUM_CYCLES = 10000;
1387 const int16_t NOISE_AMPLITUDE = 2;
1388 const int16_t MIN_CURRENT = 20;
1389 const int16_t MAX_CURRENT = 505;
1390 int16_t current_amp = 200;
1391 int cycles_until_change = 30;
1396 if (--cycles_until_change <= 0)
1399 current_amp = MIN_CURRENT +
static_cast< int16_t
>(
rng.next() % (MAX_CURRENT - MIN_CURRENT + 1));
1401 cycles_until_change = 10 +
static_cast< int >(
rng.next() % 40);
1426 float pctP = 100.0f * fabsf(
static_cast< float >(diffP)) / fabsf(
static_cast< float >(oldTest.
l_sumP));
1428 printf(
"\n [test_random_load_steps]\n");
1429 printf(
" Cycles: %d, Current range: %d-%d ADC (random steps)\n", NUM_CYCLES, MIN_CURRENT, MAX_CURRENT);
1430 printf(
" Power - Old: %ld, New: %ld, Diff: %ld (%.4f%%)\n",
1431 static_cast< long >(oldTest.
l_sumP),
1432 static_cast< long >(newTest.
l_sumP),
1433 static_cast< long >(diffP),
1434 static_cast< double >(pctP));
1437 TEST_ASSERT_FLOAT_WITHIN(0.2f, 0.0f, pctP);
1454 const int NUM_CYCLES = 10000;
1455 const int16_t NOISE_AMPLITUDE = 3;
1456 const int16_t CLEAR_SKY_CURRENT = 500;
1457 const int16_t CLOUDY_CURRENT = 80;
1459 float cloud_factor = 1.0f;
1460 float cloud_velocity = 0.0f;
1466 if (
rng.next() % 500 == 0)
1468 cloud_velocity = (
rng.next() % 2 == 0) ? -0.05f : 0.05f;
1471 cloud_factor += cloud_velocity;
1472 if (cloud_factor > 1.0f)
1474 cloud_factor = 1.0f;
1477 if (cloud_factor < 0.2f)
1479 cloud_factor = 0.2f;
1483 int16_t current_amp = CLOUDY_CURRENT +
static_cast< int16_t
>((CLEAR_SKY_CURRENT - CLOUDY_CURRENT) * cloud_factor);
1507 float pctP = 100.0f * fabsf(
static_cast< float >(diffP)) / fabsf(
static_cast< float >(oldTest.
l_sumP));
1509 printf(
"\n [test_cloud_shadow_simulation]\n");
1510 printf(
" Cycles: %d, Current range: %d-%d ADC (cloud dynamics)\n", NUM_CYCLES, CLOUDY_CURRENT, CLEAR_SKY_CURRENT);
1511 printf(
" Power - Old: %ld, New: %ld, Diff: %ld (%.4f%%)\n",
1512 static_cast< long >(oldTest.
l_sumP),
1513 static_cast< long >(newTest.
l_sumP),
1514 static_cast< long >(diffP),
1515 static_cast< double >(pctP));
1518 TEST_ASSERT_FLOAT_WITHIN(0.2f, 0.0f, pctP);
1538 const int NUM_CYCLES = 8640;
1539 const int16_t NOISE_AMPLITUDE = 2;
1540 const int16_t NIGHT_CURRENT = 5;
1541 const int16_t PEAK_CURRENT = 500;
1547 float hour = 24.0f *
cycle / NUM_CYCLES;
1550 if (hour < 6.0f || hour > 20.0f)
1552 solar_factor = 0.0f;
1557 float day_progress = (hour - 6.0f) / 14.0f;
1558 solar_factor = sinf(day_progress *
PI);
1561 int16_t current_amp =
static_cast< int16_t
>(PEAK_CURRENT * solar_factor);
1562 if (current_amp < NIGHT_CURRENT) current_amp = NIGHT_CURRENT;
1586 int32_t abs_diff = abs(diffP);
1588 float pctP = (max_power > 0) ? 100.0f *
static_cast< float >(abs_diff) /
static_cast< float >(max_power) : 0.0f;
1590 printf(
"\n [test_daily_solar_profile]\n");
1591 printf(
" Cycles: %d, Current range: %d-%d ADC (24h solar profile)\n", NUM_CYCLES, NIGHT_CURRENT, PEAK_CURRENT);
1592 printf(
" Power - Old: %ld, New: %ld, Diff: %ld (%.4f%%)\n",
1593 static_cast< long >(oldTest.
l_sumP),
1594 static_cast< long >(newTest.
l_sumP),
1595 static_cast< long >(diffP),
1596 static_cast< double >(pctP));
1600 if (max_power > 100000)
1602 TEST_ASSERT_FLOAT_WITHIN(3.0f, 0.0f, pctP);
1606 TEST_ASSERT_INT32_WITHIN(5000, oldTest.
l_sumP, newTest.
l_sumP);
1625 const int NUM_CYCLES = 10000;
1626 const int16_t NOISE_AMPLITUDE = 2;
1630 float phase_velocity = 0.0f;
1635 if (
rng.next() % 200 == 0)
1637 phase_velocity = (
static_cast< float >(
rng.next() % 100) - 50) / 1000.0f;
1640 phase += phase_velocity;
1641 if (phase >
PI) phase =
PI;
1642 if (phase < 0) phase = 0;
1644 int16_t current_amp = 150 +
rng.noise(50);
1668 int32_t abs_diff = abs(newTest.
l_sumP - oldTest.
l_sumP);
1670 float pctP = (max_power > 0) ? 100.0f *
static_cast< float >(abs_diff) /
static_cast< float >(max_power) : 0.0f;
1672 printf(
"\n [test_import_export_transitions]\n");
1673 printf(
" Cycles: %d, Current range: ~100-200 ADC (phase 0-PI transitions)\n", NUM_CYCLES);
1674 printf(
" Power - Old: %ld, New: %ld, Diff: %ld (%.4f%%)\n",
1675 static_cast< long >(oldTest.
l_sumP),
1676 static_cast< long >(newTest.
l_sumP),
1678 static_cast< double >(pctP));
1680 if (max_power > 100000)
1684 float diff = 100.0f *
static_cast< float >(abs_diff) /
static_cast< float >(max_power);
1685 TEST_ASSERT_FLOAT_WITHIN(1.0f, 0.0f, diff);
1690 TEST_ASSERT_INT32_WITHIN(10000, oldTest.
l_sumP, newTest.
l_sumP);
Simple deterministic PRNG (xorshift32) for reproducible noise.
SimpleRNG(uint32_t seed=12345)
int16_t noise(int16_t amplitude)
Generate noise in range [-amplitude, +amplitude].
void test_multiple_current_amplitudes(void)
Test with multiple current amplitudes (light to heavy load)
uint16_t generateSineADC(uint16_t sampleIndex, uint16_t dcLevel, int16_t amplitude, float phaseShift=0.0f)
Generate sinusoidal ADC value.
void test_vsquared_accumulation_equivalence(void)
Test V² accumulation equivalence.
void test_dc_offset_compensation(void)
Test with DC offset drift.
NewImplementation newImpl
constexpr uint16_t ADC_MID_POINT
void test_mathematical_identity(void)
CRITICAL TEST: Prove implementations are IDENTICAL over a full cycle.
void test_random_load_steps(void)
Test with random step changes in current (simulating appliance switching)
uint16_t generateSineADCWithNoise(uint16_t sampleIndex, uint16_t dcLevel, int16_t amplitude, int16_t noiseAmplitude, float phaseShift=0.0f)
Generate sinusoidal ADC value with noise.
void test_daily_solar_profile(void)
Test simulating a full day solar profile.
void test_full_cycle_equivalence(void)
Test complete mains cycle processing equivalence.
void test_import_export_transitions(void)
Test with import/export transitions (bidirectional power flow)
void test_cloud_shadow_simulation(void)
Test simulating cloud passing over solar panels.
void test_no_accumulator_overflow(void)
Test accumulator behavior over datalog period.
void test_near_adc_limits(void)
Test with maximum values (near ADC limits)
constexpr uint16_t SAMPLES_PER_CYCLE
void test_stress_50000_cycles(void)
Stress test: 50,000 cycles (~17 minutes at 50Hz)
void test_rms_theoretical_value(void)
Test RMS calculation for known amplitude.
void test_power_export(void)
Test power calculation with 180° phase shift (export)
void test_with_realistic_noise_2lsb(void)
Extended test with realistic ADC noise (±2 LSB)
void test_varying_power_factor_with_noise(void)
Test with varying power factor and noise.
void test_power_unity_power_factor(void)
Test power calculation with unity power factor (V and I in phase)
void test_extended_10000_cycles(void)
Extended test: 10,000 cycles (~200 seconds at 50Hz)
OldImplementation oldImpl
void test_sinusoidal_load_variation(void)
Test with sinusoidally varying current (simulating slow load changes)
void test_power_reactive_load(void)
Test power calculation with 90° phase shift (reactive load)
constexpr uint16_t SAMPLES_PER_HALF_CYCLE
void test_instant_power_equivalence(void)
Test instantaneous power calculation equivalence.
void test_with_higher_noise_5lsb(void)
Extended test with higher noise (±5 LSB)
void test_scaling_equivalence(void)
Test scaling factor equivalence.
constexpr uint16_t VOLTAGE_AMPLITUDE
void test_three_phase_simulation(void)
Test multiple phases (3-phase simulation)
constexpr uint16_t CURRENT_AMPLITUDE
void test_zero_load(void)
Test with zero load (no current)
constexpr uint16_t toLeftAligned(uint16_t rightAligned)
Convert right-aligned ADC to left-aligned.
static void cycle(RemoteLoadCore< NumUnits > &core, const LoadStates(&states)[NumLoads])
Run one mains cycle: feed the states to the core, for the given map.
void processPolarity(uint16_t rawSample)
Process voltage sample (new method - left aligned)
void resetCycle()
Reset accumulators for new cycle.
int32_t l_sumP_atSupplyPoint
float getAveragePower() const
Get average power per sample.
void processMinusHalfCycle()
Update DC filter at end of half cycle (new method)
static constexpr uint16_t i_DCoffset_V_nom
int16_t getOffsetAsADC() const
Get DC offset as ADC value.
void processVoltage()
Process voltage for RMS calculation (new method)
float getRmsVoltage() const
Get RMS voltage (relative, not calibrated)
void processCurrentRawSample(uint16_t rawSample)
Process current sample and calculate power (new method)
int16_t getOffsetAsADC() const
Get DC offset as ADC value.
void resetCycle()
Reset accumulators for new cycle.
float getRmsVoltage() const
Get RMS voltage (relative, not calibrated)
void processVoltage()
Process voltage for RMS calculation (old method)
void processMinusHalfCycle()
Update DC filter at end of half cycle (old method)
void processPolarity(int16_t rawSample)
Process voltage sample (old method)
int32_t l_cumVdeltasThisCycle
int32_t l_sumP_atSupplyPoint
float getAveragePower() const
Get average power per sample.
void processCurrentRawSample(int16_t rawSample)
Process current sample and calculate power (old method)
Structure to hold test statistics for comparison.