STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
RobustCalibrationManager.hpp
Go to the documentation of this file.
1#ifndef ROBUST_CALIBRATION_MANAGER_HPP
2#define ROBUST_CALIBRATION_MANAGER_HPP
3
4#include <Eigen/Dense>
5#include <cstdint>
6#include <map>
7#include <memory>
8#include <mutex>
9#include <optional>
10#include <string>
11
14
15namespace fsw {
16namespace calibration {
17
20 std::unique_ptr<RobustCalibrationFramework> framework;
22};
23
24/**
25 * Runtime PT calibration via RobustCalibrationFramework (9-D basis, TLS / Bayesian / RLS / GLR /
26 * bias), seeded from factory cubic. Streaming pressure uses predict_pressure_psi(), not a live
27 * cubic polynomial.
28 */
30public:
32
33 void initialize_sensor(uint16_t sensor_id, const PTCalibrationCoeffs& baseline);
34
35 void update_calibration(uint16_t sensor_id, int32_t adc_code, double reference_pressure);
36
37 void zero_sensor(uint16_t sensor_id, int32_t adc_code);
38
39 /** Reseed a sensor's framework cleanly from the given cubic baseline, discarding any learned
40 * adjustments and ignoring restored/population priors. Used when a calibration profile is
41 * swapped live: the profile is the whole cal, so robust resets and relearns from it. */
42 void reseed_sensor(uint16_t sensor_id, const PTCalibrationCoeffs& baseline);
43
44 /** Mean PSI from the robust model (default environment). */
45 double predict_pressure_psi(uint16_t sensor_id, int32_t adc_code);
46
47 /** True if this sensor_id was initialized for robust tracking (factory-baselined). */
48 bool has_sensor(uint16_t sensor_id) const;
49
50 void reset_adjustment(uint16_t sensor_id);
51
52 /** Persist per-sensor θ. When uid_to_role is given, each entry also records its role so a later
53 * load can re-attach it to the role's current sensor_id (calibration follows the sensor). */
54 bool save_adjustments(const std::string& path,
55 const std::map<uint16_t, std::string>* uid_to_role = nullptr) const;
56
57 /** Restore per-sensor θ. When uid_to_role is given, an entry tagged with a role is restored
58 * into the sensor_id that role currently maps to (so a moved sensor keeps its learned state),
59 * falling back to the persisted numeric id for untagged/legacy entries. */
60 bool load_adjustments(const std::string& path,
61 const std::map<uint16_t, std::string>* uid_to_role = nullptr);
62
63private:
64 mutable std::mutex mutex_;
65 std::map<uint16_t, SensorState> states_;
66
67 // Priors loaded from calibration backups.
68 // If a per-sensor prior exists, it overrides the population prior for that sensor.
69 std::optional<Eigen::VectorXd> population_theta_mean_;
70 std::optional<Eigen::MatrixXd> population_theta_cov_;
71
72 std::map<uint16_t, Eigen::VectorXd> restored_theta_mean_;
73 std::map<uint16_t, Eigen::MatrixXd> restored_theta_cov_;
74};
75
76} // namespace calibration
77} // namespace fsw
78
79#endif // ROBUST_CALIBRATION_MANAGER_HPP
void update_calibration(uint16_t sensor_id, int32_t adc_code, double reference_pressure)
bool load_adjustments(const std::string &path, const std::map< uint16_t, std::string > *uid_to_role=nullptr)
Restore per-sensor θ.
bool save_adjustments(const std::string &path, const std::map< uint16_t, std::string > *uid_to_role=nullptr) const
Persist per-sensor θ.
void zero_sensor(uint16_t sensor_id, int32_t adc_code)
std::map< uint16_t, Eigen::VectorXd > restored_theta_mean_
void reseed_sensor(uint16_t sensor_id, const PTCalibrationCoeffs &baseline)
Reseed a sensor's framework cleanly from the given cubic baseline, discarding any learned adjustments...
std::optional< Eigen::VectorXd > population_theta_mean_
void initialize_sensor(uint16_t sensor_id, const PTCalibrationCoeffs &baseline)
std::optional< Eigen::MatrixXd > population_theta_cov_
double predict_pressure_psi(uint16_t sensor_id, int32_t adc_code)
Mean PSI from the robust model (default environment).
std::map< uint16_t, Eigen::MatrixXd > restored_theta_cov_
bool has_sensor(uint16_t sensor_id) const
True if this sensor_id was initialized for robust tracking (factory-baselined).
Output smoothing for calibrated PT pressure, per sensor uid (ESP32 and LabJack T7 alike).
Motorized ball valve position: raw ADC counts -> % open.
PT Calibration Coefficients.
std::unique_ptr< RobustCalibrationFramework > framework