Conversation
This was referenced Sep 23, 2026
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
force-pushed
the
feat/predictive-switching
branch
from
October 1, 2026 04:32
147ed85 to
c4049d8
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Mk2_fasterControlsketches: 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.Energy::contribution(L1's partial sumP, cal / n).cal / nfor 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.PREDICTIVE_LOAD_SWITCHING{ false }inprocessing.cpprestores the old behaviour.Thresholds,
b_recentTransition, the priority rotation, relays and datalogging are unchanged.processStartNewCycle(),proceedHighEnergyLevel()andproceedLowEnergyLevel()take the decision's energy value as a parameter.processStartNewCycle()now has two call sites (predictive and fallback) and isnoinline. With a single inlined call site at the end ofprocessRawSamples(), the ISR needed a 4-byte stack frame on every call, even with the switch off. The ISR already callsupdatePortsStates(), 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),
cloudsscenario:devmake check(3 scenarios, 30 expectations) andmake check-rf(4 scenarios, 36 expectations, two remote units running the receiver firmware). After start-up, every decision was predictive.dev, the ISR pushes one more register (~4 cycles per call) and has no stack frame.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 ondev.Cost
basic,emonesp,rfbasic_debugbasic, switch offBench checklist
n_lowestNoOfSampleSetsPerMainsCyclestays ~32 (no missed samples).Notes
devafter feat: support several remote load units #160 (multiple remote units), perf: integer energy bucket, no float or division in the ADC ISR #163 (integer energy bucket) and ci(sim): run the grid scenarios in parallel #174.🤖 Generated with Claude Code