Skip to content

fix(controller): persist LED intensity and white balance calibration - #1013

Open
babo989 wants to merge 3 commits into
mainfrom
fix/575-persist-led-wb
Open

babo989 wants to merge 3 commits into
mainfrom
fix/575-persist-led-wb

Conversation

@babo989

@babo989 babo989 commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

We need to decide on a permanent fix, issue is now solved but fragile.

Consolidate the three duplicated auto-calibration implementations in the dashboard

The LED/white-balance auto-calibration sequence (runInitrunSaturationLooprunLightnessLoop) exists as three independent copies, all on enabled tabs:

Node Tab Size
db1ef284071e60a1 Preview — the "Camera Calibration" modal operators actually use ~707 lines
79bccc0355eb5d87 Acquisition ~1196 lines
97634d7d17fefdeb Calibration ~371 lines

They have already drifted: the Acquisition copy uses cal_status/cal_step where the others use status/step. There are also two older wizard tabs, "Calibration - Lightness" (c9a6d6f19ff18dda) and "Calibration - Saturation Level" (10c29a65226c0da4), which are enabled but unused.

This duplication is the structural reason calibration bugs keep reopening. #473 reopened four times because each fix landed on one node while its siblings kept the old behaviour, and it was closed only once the same change was made in every copy. #575 had the same shape — persisting on success had to be written three times, and any future change to calibration behaviour will too. Each round the fix looks complete, then reappears as "it works sometimes, depending on which screen you calibrate from."

Two things worth doing, in order:

  1. Decide which copies should exist at all. If Acquisition and Calibration are legacy, disabling them removes two thirds of the problem for almost no effort, and the same question applies to the two unused wizard tabs.
  2. If more than one must stay, extract the sequence into one shared unit — a subflow or a single function node the templates drive — so the loop logic and its hardware I/O live in exactly one place.

Worth checking for other duplicated logic in the flows at the same time; these two issues suggest it may not be limited to calibration.

@sonnyp sonnyp left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hardware controllers (light, bubbler, etc) are designed only to control hardware.
They should not be involved in "business logic". Ideally there are no changes to controller/light to solve this issue.

I see several things to take into consideration:

  1. We want calibration to work without a browser
  2. We want calibration to happen automatically
  3. The code duplication problem you described
  4. The quality of the current implementation
  5. We have to consider https://github.com/fairscope/PlanktoScope3/issues/327 for later

I think we should discuss this but my thoughts for now:

Calibration should be implemented in the backend, not in the browser. Probably as a new MQTT service.

Dashboard would automatically run calibration before starting an acquisition. Possibly when loading preview too. TBD

Since calibration would happen automatically, I'm not even sure we have anything to save at all.

on() drove the DAC to full scale, so every switch-on flashed at maximum brightness before the caller set the requested level. Remember the last level that was set and restore that instead.
hardware.json and config.json are written while controllers read them, and a partially written file leaves the instrument unbootable. Write to a temporary file in the same directory, fsync it and rename over the target, so a reader sees either the old file or the new one.

Also fixes a typo that passed `forrce` instead of `force` when removing calibration.json, which made /api/reset throw if the file did not exist.
The calibration an operator performs did not survive a reboot, so it had to be redone on every start.

Calibration is stored in calibration.json, which initConfigFiles never overwrites, rather than hardware.json, which a hardware version switch rewrites from defaults. The backend exposes calibration/save and calibration/read, and publishes the stored values on a retained status/calibration.

White balance is applied without operator action: the gains are mirrored into hardware.json, which the camera already reads at startup, and published as a live settings command for a running camera.

The hardware controllers are unchanged. They keep driving hardware over the MQTT API they already expose, and hold no calibration state of their own.
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.

2 participants