Skip to content

feat: decide the loads just before L1's zero crossing, on a predicted bucket - #162

Draft
FredM67 wants to merge 2 commits into
devfrom
feat/predictive-switching
Draft

FredM67 wants to merge 2 commits into
devfrom
feat/predictive-switching

Conversation

@FredM67

@FredM67 FredM67 commented Sep 23, 2026 •

Copy link
Copy Markdown
Owner

Refs #161. Needs bench validation before merge.

Problem

A zero-crossing triac driver only fires at the next zero crossing of its own phase. The load decision ran ~1.5 ms after L1's positive crossing, so a load on L1 fired at its negative crossing, 10 ms later, halfway through L1's measurement window. Full analysis in #161.

Change

  • When: one sample set after the negative crossing of the phase that crosses last before L1's positive one. That is L2, or L3 with reversed rotation: whichever is negative at L1's positive crossing. The pins are written ~1.6 ms before L1's crossing, clear of the drivers' ±0.2 ms firing windows. L1 and L2 then switch at the start of their measurement windows, and L3 at its negative crossing, before its window starts. No load switches in the middle of its window any more. (No single decision instant can put all three phases on a rising crossing.)
  • On what: a predicted bucket level, as in Robin Emley's Mk2_fasterControl sketches: the bucket, plus L1's average power so far this cycle, minus the export / start-threshold adjustment. L2's and L3's latest contributions are already in the bucket at that point.
  • Integer arithmetic (rebased on perf: integer energy bucket, no float or division in the ADC ISR #163): the prediction is Energy::contribution(L1's partial sumP, cal / n). cal / n for L1's partial sample count comes from a second flash table, for L1 only (23-31 sample sets at 50 Hz, 18-26 at 60 Hz, 18 bytes). There is no float and no division in the ISR.
  • Fallback: if no predictive decision was taken during an L1 cycle (start-up, a missing phase, a sample count outside the table), the old decision point is used on the measured bucket.
  • A/B switch: PREDICTIVE_LOAD_SWITCHING{ false } in processing.cpp restores the old behaviour.

Thresholds, b_recentTransition, the priority rotation, relays and datalogging are unchanged. processStartNewCycle(), proceedHighEnergyLevel() and proceedLowEnergyLevel() take the decision's energy value as a parameter.

processStartNewCycle() now has two call sites (predictive and fallback) and is noinline. With a single inlined call site at the end of processRawSamples(), the ISR needed a 4-byte stack frame on every call, even with the switch off. The ISR already calls updatePortsStates(), so it saves every register anyway: the extra call costs ~8 cycles, once per mains cycle.

Simulation (grid_sim)

Switching latency (pin write to the zero crossing where the triac switches), clouds scenario:

Load on dev This PR
L1 7.70 ms, 0 of 316 at a rising crossing 1.63 ms, 382 of 382 at a rising crossing
L2 4.35 ms, all at a rising crossing 8.29 ms, all at a rising crossing
L3 1.03 ms, at its falling crossing 4.96 ms, at its falling crossing
  • Every scenario passes: make check (3 scenarios, 30 expectations) and make check-rf (4 scenarios, 36 expectations, two remote units running the receiver firmware). After start-up, every decision was predictive.
  • ADC ISR: average 350.5 → 328.8 cycles, worst case 1389 → 1526 cycles (of 1664), 0 overruns. The worst case is a single event at start-up in both builds. Compared with dev, the ISR pushes one more register (~4 cycles per call) and has no stack frame.
  • Control: imported and exported energy and on-times are the same. Where the first load modulates a partial surplus (clouds, sunset), it switches 10-30 % more often, in shorter bursts: less delay in the loop means less overshoot. The grid power per cycle is the same within a few watts.
  • Remote loads on L1 don't benefit: their RF frame is sent from loop() after the decision and takes ~1.7 ms on air, so it arrives after L1's positive crossing. They still switch at L1's negative crossing, as on dev.

Cost

Flash RAM
basic, emonesp, rf +308 bytes +4 bytes
basic_debug +428 bytes +4 bytes
basic, switch off −42 bytes +2 bytes

Bench checklist

  • Scope the triac gate of a load on each phase against its mains voltage, switching on and off: L1's loads switch at its positive crossing, L2's at its positive crossing, L3's at its negative crossing.
  • n_lowestNoOfSampleSetsPerMainsCycle stays ~32 (no missed samples).
  • Control quality with the switch on vs off: import/export around the set point, number of switching events.
  • Reversed phase rotation still triggers on the right phase.

Notes

🤖 Generated with Claude Code

FredM67 added a commit that referenced this pull request Sep 30, 2026
The firmware files the conversions as V1 I1 V2 I2 V3 I3 in turn and writes
ADMUX at the end of its ISR, for the conversion after next. When the ISR
runs past the start of that conversion, it runs on the previous channel and
its sample is filed under the wrong one.

grid_sim now follows the firmware's sequence and counts those conversions
(ignoring the first two rounds after the ADC starts), and the scenarios
expect none: `expect misfiled_samples 0`.

Measured on the surplus-step scenario: dev before #163 misfiled 10 samples
(one per ISR overrun, at datalog time), dev with #163 none. It is a more
precise check than the overrun count: of #162's 1928 overruns, only 14
misfiled a sample.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
… bucket

A zero-crossing triac driver only fires at the next zero crossing of its
own phase. The load decision ran ~1.5 ms after L1's positive crossing,
so a load on L1 fired at its negative crossing, 10 ms later, halfway
through L1's measurement window.

The decision now runs one sample set after the negative crossing of the
phase that crosses last before L1's positive one: L2, or L3 with
reversed rotation - whichever is negative at L1's positive crossing.
That lands ~1.6 ms before L1's crossing: L1 and L2 then switch at the
start of their measurement windows, L3 before its window starts.

L1's contribution for the cycle is not in the bucket yet, so the
decision uses a prediction, as in Robin Emley's Mk2_fasterControl
sketches: the bucket plus L1's average power so far this cycle, minus
the export / start-threshold adjustment. It uses the integer bucket:
cal / n for L1's partial sample count comes from a second flash table,
so there is no float and no division in the ISR.

If no predictive decision was taken during an L1 cycle (start-up, a
missing phase, an unexpected sample count), the old decision point is
used on the measured bucket. PREDICTIVE_LOAD_SWITCHING{ false } restores
the old behaviour for A/B comparison.

processStartNewCycle() now has two call sites and is not inlined: with
a single inlined call site, the ISR needed a stack frame on every call.

grid_sim: L1's loads switch 1.6 ms after the decision, all at a rising
crossing (7.7 ms, at the falling one, before); every scenario passes.
ADC ISR average 350 -> 329 cycles.
basic: +308 bytes flash, +4 bytes RAM (-42 bytes with the switch off).
Not yet validated on hardware.

Refs #161

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@FredM67
FredM67 force-pushed the feat/predictive-switching branch from 147ed85 to c4049d8 Compare October 1, 2026 04:32

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant