From 38adaa3f8f7660bbedd282cc10c85a3946ac4e86 Mon Sep 17 00:00:00 2001 From: atse Date: Tue, 6 Aug 2024 12:51:58 +0800 Subject: [PATCH] channels: Start to make reports make sense Names in the report make no sense, and expose implementation details. Start simplifying the interface. --- README.md | 27 ++++++++++++--------------- src/channels.rs | 35 +++++++++++++---------------------- 2 files changed, 25 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 244daae..74db17c 100644 --- a/README.md +++ b/README.md @@ -255,21 +255,18 @@ Use the bare `report` command to obtain a single report. Enable continuous reporting with `report mode on`. Reports are JSON objects with the following keys. -| Key | Unit | Description | -| --- | :---: | --- | -| `channel` | Integer | Channel `0`, or `1` | -| `time` | Seconds | Temperature measurement time | -| `adc` | Volts | AD7172 input | -| `sens` | Ohms | Thermistor resistance derived from `adc` | -| `temperature` | Degrees Celsius | Steinhart-Hart conversion result derived from `sens` | -| `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 | +| Key | Unit | Description | +| --- | :---: | --- | +| `channel` | Integer | Channel `0`, or `1` | +| `time` | Seconds | Report timestamp (since thermostat reset) | +| `interval` | Seconds | ADC Sampling Interval | +| `sens` | Ohms | Thermistor resistance | +| `temperature` | Degrees Celsius | Temperature derived from thermistor resistance | +| `pid_engaged` | Boolean | `true` if in closed-loop mode | +| `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]. diff --git a/src/channels.rs b/src/channels.rs index f9f9845..bb7f84c 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -408,15 +408,15 @@ impl Channels { (duty * max, MAX_TEC_I) } - // Get current passing through TEC - pub fn get_tec_i(&mut self, channel: usize) -> ElectricCurrent { + // Measure current passing through TEC + pub fn get_i_measured(&mut self, channel: usize) -> ElectricCurrent { (self.adc_read(channel, PinsAdcReadTarget::ITec, 16) - self.adc_read(channel, PinsAdcReadTarget::VREF, 16)) / ElectricalResistance::new::(0.4) } - // Get voltage across TEC - pub fn get_tec_v(&mut self, channel: usize) -> ElectricPotential { + // Measure voltage across TEC + pub fn get_v_measured(&mut self, channel: usize) -> ElectricPotential { (self.adc_read(channel, PinsAdcReadTarget::VTec, 16) - ElectricPotential::new::(1.5)) * 4.0 } @@ -475,28 +475,23 @@ impl Channels { fn report(&mut self, channel: usize) -> Report { let i_set = self.get_i_set(channel); - let i_tec = self.adc_read(channel, PinsAdcReadTarget::ITec, 16); - let tec_i = self.get_tec_i(channel); - let dac_value = self.get_dac(channel); + let i_measured = self.get_i_measured(channel); + let v_measured = self.get_v_measured(channel); let state = self.channel_state(channel); let pid_output = ElectricCurrent::new::(state.pid.y1); Report { channel, time: state.get_adc_time(), interval: state.get_adc_interval(), - adc: state.get_adc(), sens: state.get_sens(), temperature: state .get_temperature() .map(|temperature| temperature.get::()), 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, + i_set, + i_measured, + v_measured, } } @@ -578,7 +573,7 @@ impl Channels { pub fn current_abs_max_tec_i(&mut self) -> ElectricCurrent { (0..CHANNELS) - .map(|channel| self.get_tec_i(channel).abs()) + .map(|channel| self.get_i_measured(channel).abs()) .max_by(|a, b| a.partial_cmp(b).unwrap_or(core::cmp::Ordering::Equal)) .unwrap() } @@ -589,17 +584,13 @@ pub struct Report { channel: usize, time: Time, interval: Time, - adc: Option, sens: Option, temperature: Option, 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, + i_set: ElectricCurrent, + i_measured: ElectricCurrent, + v_measured: ElectricPotential, } pub struct CenterPointJson(CenterPoint);