From 70d20f3debdb3e42d0ab056c2c9109770e6749a9 Mon Sep 17 00:00:00 2001 From: atse Date: Mon, 19 Feb 2024 10:23:19 +0800 Subject: [PATCH] Add calibration VREF option and set as default --- README.md | 1 + src/channel.rs | 8 ++++---- src/channel_state.rs | 2 +- src/channels.rs | 13 +++++++++++-- src/command_parser.rs | 2 ++ 5 files changed, 19 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b0b5d1a..49088b3 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,7 @@ formatted as line-delimited JSON. | `pwm <0/1> pid` | Let output current to be controlled by the PID | | `center <0/1> ` | Set the MAX1968 0A-centerpoint to the specified fixed voltage | | `center <0/1> vref` | Set the MAX1968 0A-centerpoint to measure from VREF | +| `center <0/1> vref_calib` | Set the MAX1968 0A-centerpoint to a stable calibrated VREF | | `pid` | Show PID configuration | | `pid <0/1> target ` | Set the PID controller target temperature | | `pid <0/1> kp ` | Set proportional gain | diff --git a/src/channel.rs b/src/channel.rs index c867894..1be351c 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -20,8 +20,8 @@ pub struct Channel { pub state: ChannelState, /// for `i_set` pub dac: ad5680::Dac, - /// Measured vref of MAX driver chip - pub vref_meas: ElectricPotential, + /// Calibrated vref of MAX driver chip + pub vref_calib: ElectricPotential, pub shdn: C::Shdn, pub vref_pin: C::VRefPin, pub itec_pin: C::ItecPin, @@ -36,11 +36,11 @@ impl Channel { let mut dac = ad5680::Dac::new(pins.dac_spi, pins.dac_sync); let _ = dac.set(0); // sensible dummy preset taken from datasheet. calibrate_dac_value() should be used to override this value. - let vref_meas = ElectricPotential::new::(1.5); + let vref_calib = ElectricPotential::new::(1.5); Channel { state, - dac, vref_meas, + dac, vref_calib, shdn: pins.shdn, vref_pin: pins.vref_pin, itec_pin: pins.itec_pin, diff --git a/src/channel_state.rs b/src/channel_state.rs index 4820131..70f2ef6 100644 --- a/src/channel_state.rs +++ b/src/channel_state.rs @@ -45,7 +45,7 @@ impl ChannelState { adc_time: Instant::from_secs(0), // default: 10 Hz adc_interval: Duration::from_millis(100), - center: CenterPoint::Vref, + center: CenterPoint::VrefCalib, dac_value: ElectricPotential::new::(0.0), i_set: ElectricCurrent::new::(0.0), pid_engaged: false, diff --git a/src/channels.rs b/src/channels.rs index 20aa8e6..588e994 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -101,6 +101,13 @@ impl<'a> Channels<'a> { match self.channel_state(channel).center { CenterPoint::Vref => self.read_vref(channel), + CenterPoint::VrefCalib => { + match channel { + 0 => self.channel0.vref_calib, + 1 => self.channel1.vref_calib, + _ => unreachable!(), + } + }, CenterPoint::Override(center_point) => ElectricPotential::new::(center_point.into()), } @@ -293,8 +300,8 @@ impl<'a> Channels<'a> { let vref = (value as f64 / ad5680::MAX_VALUE as f64) * ElectricPotential::new::(DAC_OUT_V_MAX); match channel { - 0 => self.channel0.vref_meas = vref, - 1 => self.channel1.vref_meas = vref, + 0 => self.channel0.vref_calib = vref, + 1 => self.channel1.vref_calib = vref, _ => unreachable!(), } } @@ -560,6 +567,8 @@ impl Serialize for CenterPointJson { match self.0 { CenterPoint::Vref => serializer.serialize_str("vref"), + CenterPoint::VrefCalib => + serializer.serialize_str("vref_calib"), CenterPoint::Override(vref) => serializer.serialize_f32(vref), } diff --git a/src/command_parser.rs b/src/command_parser.rs index b9e70bb..3d2fcaf 100644 --- a/src/command_parser.rs +++ b/src/command_parser.rs @@ -133,6 +133,7 @@ pub enum PwmPin { #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub enum CenterPoint { Vref, + VrefCalib, Override(f32), } @@ -357,6 +358,7 @@ fn center_point(input: &[u8]) -> IResult<&[u8], Result> { let (input, _) = whitespace(input)?; let (input, center) = alt(( value(Ok(CenterPoint::Vref), tag("vref")), + value(Ok(CenterPoint::VrefCalib), tag("vref_calib")), |input| { let (input, value) = float(input)?; Ok((input, value.map(|value| CenterPoint::Override(value as f32))))