feat(sim): run the firmware on a simulated 3-phase grid - #169
Merged
Merged
Conversation
grid_sim runs the unmodified production firmware in simavr and connects it to a simulated installation: the 6 ADC inputs get 50 Hz voltage and current waves computed at the exact cycle of each conversion, and the load pins drive loads through zero-crossing triac drivers. The grid current is the scenario's surplus minus the conducting loads, as the firmware measures it. Measured: ADC conversion period, cycle-exact ADC ISR duration and overruns, sample sets per mains cycle (from the firmware's datalog), per-load switching and latency to the zero crossing, grid import/export. - Scenarios are text files with expectations (pin states, grid average, ISR overruns, sample sets); `make check` fails on any unmet one. Three scenarios: no surplus, surplus steps, clouds. - simavr's ADC does not time free-running conversions correctly (~256 CPU cycles instead of 1664 with this firmware): sim/avr_adc.c is upstream's file with that fix, linked ahead of libsimavr.a. grid_sim checks the conversion period on every run. - CI: new "Grid simulation" job, builds the basic firmware and runs every scenario; summaries in the job summary, traces as an artifact. - pre-commit: the vendored avr_adc.c is kept as upstream (no reformatting). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Summary
grid_simruns the unmodified production firmware in simavr, connected to a simulated 3-phase installation. With it, the regulation, load priorities, ISR timing and switching instants can be checked without hardware or sun, locally (Linux/WSL) and in CI. Details are insim/README.md.How it works
make checkfails on any unmet one. Three are included: no surplus, surplus steps, clouds.simavr ADC fix
simavr's ADC does not time free-running conversions correctly: with this firmware it converts every ~256 CPU cycles instead of 1664.
sim/avr_adc.cis upstream's file with that fix, linked ahead oflibsimavr.a.grid_simchecks the conversion period on every run and fails if it's wrong. With the fix, the firmware reports 32 sample sets per mains cycle, as on the real board. The bug could be reported upstream later.Validation on
devREQUIRED_EXPORT_IN_WATTS= 20 W.First use: the three open PRs
The same scenarios were run on the firmware of each branch:
devdev(energies within 0.01 Wh on every scenario), ISR worst case −30%, no more overruns.devin the default configuration. The RF path cannot be simulated.dev.CI
A new Grid simulation job builds the
basicfirmware and runsmake -C sim check. The summaries go to the job summary, and the traces are uploaded as an artifact. The scenarios allow at most 40 overruns per run (dev: 10-18), as a regression guard: #162 in its current form would fail it.Limits
config.h.The pre-commit hooks leave the vendored
avr_adc.cas upstream wrote it (no reformatting).🤖 Generated with Claude Code