channels: Start to make reports make sense

Names in the report make no sense, and expose implementation details.
Start simplifying the interface.
This commit is contained in:
atse 2024-08-06 12:51:58 +08:00
parent ef362643a7
commit cee633845e
2 changed files with 25 additions and 37 deletions

View File

@ -258,18 +258,15 @@ with the following keys.
| Key | Unit | Description | | Key | Unit | Description |
| --- | :---: | --- | | --- | :---: | --- |
| `channel` | Integer | Channel `0`, or `1` | | `channel` | Integer | Channel `0`, or `1` |
| `time` | Seconds | Temperature measurement time | | `time` | Seconds | Report timestamp (since thermostat reset) |
| `adc` | Volts | AD7172 input | | `interval` | Seconds | ADC Sampling Interval |
| `sens` | Ohms | Thermistor resistance derived from `adc` | | `sens` | Ohms | Thermistor resistance |
| `temperature` | Degrees Celsius | Steinhart-Hart conversion result derived from `sens` | | `temperature` | Degrees Celsius | Temperature derived from thermistor resistance |
| `pid_engaged` | Boolean | `true` if in closed-loop mode | | `pid_engaged` | Boolean | `true` if in closed-loop mode |
| `i_set` | Amperes | TEC output current |
| `dac_value` | Volts | AD5680 output derived from `i_set` |
| `dac_feedback` | Volts | ADC measurement of the AD5680 output |
| `i_tec` | Volts | MAX1968 TEC current monitor |
| `tec_i` | Amperes | TEC output current feedback derived from `i_tec` |
| `tec_u_meas` | Volts | Measurement of the voltage across the TEC |
| `pid_output` | Amperes | PID control output | | `pid_output` | Amperes | PID control output |
| `i_set` | Amperes | Set TEC output current |
| `i_measured` | Amperes | Measured current passing through TEC |
| `v_measured` | Volts | Measured voltage across TEC |
Note: With Thermostat v2 and below, the voltage and current readouts `i_tec` and `tec_i` are noisy without the hardware fix shown in [this PR][https://git.m-labs.hk/M-Labs/thermostat/pulls/105]. Note: With Thermostat v2 and below, the voltage and current readouts `i_tec` and `tec_i` are noisy without the hardware fix shown in [this PR][https://git.m-labs.hk/M-Labs/thermostat/pulls/105].

View File

@ -408,15 +408,15 @@ impl Channels {
(duty * max, MAX_TEC_I) (duty * max, MAX_TEC_I)
} }
// Get current passing through TEC // Measure current passing through TEC
pub fn get_tec_i(&mut self, channel: usize) -> ElectricCurrent { pub fn get_i_measured(&mut self, channel: usize) -> ElectricCurrent {
(self.adc_read(channel, PinsAdcReadTarget::ITec, 16) (self.adc_read(channel, PinsAdcReadTarget::ITec, 16)
- self.adc_read(channel, PinsAdcReadTarget::VREF, 16)) - self.adc_read(channel, PinsAdcReadTarget::VREF, 16))
/ ElectricalResistance::new::<ohm>(0.4) / ElectricalResistance::new::<ohm>(0.4)
} }
// Get voltage across TEC // Measure voltage across TEC
pub fn get_tec_v(&mut self, channel: usize) -> ElectricPotential { pub fn get_v_measured(&mut self, channel: usize) -> ElectricPotential {
(self.adc_read(channel, PinsAdcReadTarget::VTec, 16) - ElectricPotential::new::<volt>(1.5)) (self.adc_read(channel, PinsAdcReadTarget::VTec, 16) - ElectricPotential::new::<volt>(1.5))
* 4.0 * 4.0
} }
@ -475,28 +475,23 @@ impl Channels {
fn report(&mut self, channel: usize) -> Report { fn report(&mut self, channel: usize) -> Report {
let i_set = self.get_i_set(channel); let i_set = self.get_i_set(channel);
let i_tec = self.adc_read(channel, PinsAdcReadTarget::ITec, 16); let i_measured = self.get_i_measured(channel);
let tec_i = self.get_tec_i(channel); let v_measured = self.get_v_measured(channel);
let dac_value = self.get_dac(channel);
let state = self.channel_state(channel); let state = self.channel_state(channel);
let pid_output = ElectricCurrent::new::<ampere>(state.pid.y1); let pid_output = ElectricCurrent::new::<ampere>(state.pid.y1);
Report { Report {
channel, channel,
time: state.get_adc_time(), time: state.get_adc_time(),
interval: state.get_adc_interval(), interval: state.get_adc_interval(),
adc: state.get_adc(),
sens: state.get_sens(), sens: state.get_sens(),
temperature: state temperature: state
.get_temperature() .get_temperature()
.map(|temperature| temperature.get::<degree_celsius>()), .map(|temperature| temperature.get::<degree_celsius>()),
pid_engaged: state.pid_engaged, pid_engaged: state.pid_engaged,
i_set,
dac_value,
dac_feedback: self.adc_read(channel, PinsAdcReadTarget::DacVfb, 1),
i_tec,
tec_i,
tec_u_meas: self.get_tec_v(channel),
pid_output, pid_output,
i_set,
i_measured,
v_measured,
} }
} }
@ -576,8 +571,8 @@ impl Channels {
serde_json_core::to_vec(&summaries) serde_json_core::to_vec(&summaries)
} }
max_by(self.get_tec_i(0).abs(), self.get_tec_i(1).abs(), |a, b| {
pub fn max_abs_i_measured(&mut self) -> ElectricCurrent { pub fn max_abs_i_measured(&mut self) -> ElectricCurrent {
max_by(self.get_i_measured(0).abs(), self.get_i_measured(1).abs(), |a, b| {
a.partial_cmp(b).unwrap_or(core::cmp::Ordering::Equal) a.partial_cmp(b).unwrap_or(core::cmp::Ordering::Equal)
}) })
} }
@ -588,17 +583,13 @@ pub struct Report {
channel: usize, channel: usize,
time: Time, time: Time,
interval: Time, interval: Time,
adc: Option<ElectricPotential>,
sens: Option<ElectricalResistance>, sens: Option<ElectricalResistance>,
temperature: Option<f64>, temperature: Option<f64>,
pid_engaged: bool, pid_engaged: bool,
i_set: ElectricCurrent,
dac_value: ElectricPotential,
dac_feedback: ElectricPotential,
i_tec: ElectricPotential,
tec_i: ElectricCurrent,
tec_u_meas: ElectricPotential,
pid_output: ElectricCurrent, pid_output: ElectricCurrent,
i_set: ElectricCurrent,
i_measured: ElectricCurrent,
v_measured: ElectricPotential,
} }
pub struct CenterPointJson(CenterPoint); pub struct CenterPointJson(CenterPoint);