STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
CubicCalibrationStore Class Reference

Owns the per-channel captured points and the fitted cubic for the calibration service. More...

#include <CubicCalibrationStore.hpp>

Collaboration diagram for CubicCalibrationStore:
[legend]

Public Member Functions

 CubicCalibrationStore (std::string file_path)
void register_channel (uint16_t uid, uint8_t board_id, uint8_t connector, uint8_t logical_ch, const std::string &role, const std::string &active_model="cubic", SensorKind kind=SensorKind::PT, const std::string &raw_unit="adc")
 Attach/refresh identity + role + streaming model for a uid (idempotent; call at startup).
CubicFit add_point (uint16_t uid, double adc, double psi)
 Append a capture point (raw ADC, reference PSI), cap history, and re-fit.
void note_capture (uint16_t uid, const CaptureQuality &q)
 Record how the last capture on this uid went, for the UI to show.
void set_fit_curve (uint16_t uid, const std::vector< std::pair< double, double > > &curve)
 Replace a robust uid's display fit-curve (sampled predict_pressure_psi points).
void clear_channel (uint16_t uid)
 Drop all points, fit, and fit-curve for a uid (channel goes back to PENDING).
const CubicFit * fit_for (uint16_t uid) const
const CubicChannel * channel (uint16_t uid) const
std::vector< uint16_t > uids () const
bool save () const
 Atomically write the JSON record (temp + rename).
size_t load ()
 Load the JSON record, resume points, and re-fit each channel.
bool load_failed () const
 True when load() could not read an existing file, so every curve here is a fallback rather than the operator's.

Static Public Member Functions

static CubicFit compute_fit (const std::vector< CubicPoint > &pts, int max_degree=3)
 Least-squares polynomial of degree min(max_degree, n - 1) over pts.

Static Public Attributes

static constexpr size_t kMaxPoints = 20

Private Member Functions

std::string serialize () const

Static Private Member Functions

static void refit (CubicChannel &ch)
 Recompute fit_cubic / fit_linear / fit / status from ch.points per ch.active_model.

Private Attributes

std::mutex mutex_
std::string file_path_
bool load_failed_ = false
 Set when load() could not read an existing file.
std::map< uint16_t, CubicChannel > channels_

Detailed Description

Owns the per-channel captured points and the fitted cubic for the calibration service.

Unit tests for the PT/LC split in fsw::calibration::CubicCalibrationStore.

Thread-safe. Persists atomically to a single JSON file that is:

  • re-loadable by this class on restart (resume points, re-fit), and
  • backward-compatible with the existing calibration_polynomials/poly_coeffs loaders (logical-channel-keyed) so the file doubles as the factory-cubic overlay the service loads. Those maps are PT-only; load cells get lc_-prefixed equivalents, because the two share a logical-channel space (see SensorKind). The service is the sole writer; the Node backend only reads the file to serve the UI.

logical_ch is (slot - 1) * 10 + connector with slot = board_id % 10, and check_board_slots only stops two boards of the SAME kind sharing a slot. So PT board 22 and LC board 42 are both slot 2, and PT uid 2201 and LC uid 4201 both land on logical channel 11. The logical-keyed maps were written from a uid-ordered map, so the higher uid silently won: on 2026-09-13 the LOX Scale's curve replaced GN2 High's (a 5000 psi transducer) on disk, and evaluating GN2 High's ADCs against it gave 1.2e10 psi.

These tests pin the split and the recovery. Recovery needs no migration: load() reads only cubic_state and recomputes every fit from the points, which survived.

  1. the field collision at logical 11 4. legacy file without "kind"
  2. the second live collision at logical 1 5. add_point refuses an unknown uid
  3. kind round-trips through the file 6. recovery regenerates the PT curve

Definition at line 161 of file CubicCalibrationStore.hpp.

Constructor & Destructor Documentation

◆ CubicCalibrationStore()

Definition at line 32 of file CubicCalibrationStore.cpp.

Member Function Documentation

◆ add_point()

CubicFit fsw::calibration::CubicCalibrationStore::add_point ( uint16_t uid,
double adc,
double psi )

Append a capture point (raw ADC, reference PSI), cap history, and re-fit.

Returns the fit. For a robust uid the point is recorded for display only and an invalid fit is returned (the robust learner is updated by the service, not here).

Definition at line 153 of file CubicCalibrationStore.cpp.

◆ channel()

Definition at line 226 of file CubicCalibrationStore.cpp.

◆ clear_channel()

Drop all points, fit, and fit-curve for a uid (channel goes back to PENDING).

Definition at line 203 of file CubicCalibrationStore.cpp.

◆ compute_fit()

CubicFit fsw::calibration::CubicCalibrationStore::compute_fit ( const std::vector< CubicPoint > & pts,
int max_degree = 3 )
static

Least-squares polynomial of degree min(max_degree, n - 1) over pts.

Public for tests.

Definition at line 90 of file CubicCalibrationStore.cpp.

Referenced by anonymous_namespace{test_lc_linear_fit.cpp}::cubic_model_is_unchanged().

◆ fit_for()

Definition at line 218 of file CubicCalibrationStore.cpp.

◆ load()

Load the JSON record, resume points, and re-fit each channel.

Returns channels loaded.

Definition at line 387 of file CubicCalibrationStore.cpp.

◆ load_failed()

True when load() could not read an existing file, so every curve here is a fallback rather than the operator's.

Callers that DERIVE from a curve (the LC tare recompute) must skip rather than persist a number computed against one.

Definition at line 201 of file CubicCalibrationStore.hpp.

◆ note_capture()

void fsw::calibration::CubicCalibrationStore::note_capture ( uint16_t uid,
const CaptureQuality & q )

Record how the last capture on this uid went, for the UI to show.

Definition at line 188 of file CubicCalibrationStore.cpp.

◆ refit()

Recompute fit_cubic / fit_linear / fit / status from ch.points per ch.active_model.

Caller holds mutex_. Robust channels are left to their own branch (no fit).

Definition at line 61 of file CubicCalibrationStore.cpp.

◆ register_channel()

void fsw::calibration::CubicCalibrationStore::register_channel ( uint16_t uid,
uint8_t board_id,
uint8_t connector,
uint8_t logical_ch,
const std::string & role,
const std::string & active_model = "cubic",
SensorKind kind = SensorKind::PT,
const std::string & raw_unit = "adc" )

Attach/refresh identity + role + streaming model for a uid (idempotent; call at startup).

active_model is the config truth ("cubic"|"robust"|"linear"|"physics"); it takes precedence over a value restored from disk by load(). A channel that already holds points is re-fitted when its model changes, so switching cubic <-> linear in config needs no recapture.

Definition at line 36 of file CubicCalibrationStore.cpp.

◆ save()

Atomically write the JSON record (temp + rename).

Definition at line 359 of file CubicCalibrationStore.cpp.

◆ serialize()

Definition at line 241 of file CubicCalibrationStore.cpp.

◆ set_fit_curve()

void fsw::calibration::CubicCalibrationStore::set_fit_curve ( uint16_t uid,
const std::vector< std::pair< double, double > > & curve )

Replace a robust uid's display fit-curve (sampled predict_pressure_psi points).

Definition at line 195 of file CubicCalibrationStore.cpp.

◆ uids()

std::vector< uint16_t > fsw::calibration::CubicCalibrationStore::uids ( ) const

Definition at line 232 of file CubicCalibrationStore.cpp.

Member Data Documentation

◆ channels_

Definition at line 214 of file CubicCalibrationStore.hpp.

◆ file_path_

◆ kMaxPoints

Definition at line 206 of file CubicCalibrationStore.hpp.

◆ load_failed_

Set when load() could not read an existing file.

Blocks save(), so an unreadable store is never replaced by the empty one we fell back to.

Definition at line 213 of file CubicCalibrationStore.hpp.

◆ mutex_

Definition at line 209 of file CubicCalibrationStore.hpp.


The documentation for this class was generated from the following files: