Add calibration VREF option and set as default

This commit is contained in:
atse 2024-02-19 10:23:19 +08:00
parent 832df121a2
commit e702e01344
5 changed files with 19 additions and 7 deletions

View File

@ -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> <volt>` | 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 <deg_celsius>` | Set the PID controller target temperature |
| `pid <0/1> kp <value>` | Set proportional gain |

View File

@ -20,8 +20,8 @@ pub struct Channel<C: ChannelPins> {
pub state: ChannelState,
/// for `i_set`
pub dac: ad5680::Dac<C::DacSpi, C::DacSync>,
/// 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<C: ChannelPins> Channel<C> {
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::<volt>(1.5);
let vref_calib = ElectricPotential::new::<volt>(1.5);
Channel {
state,
dac, vref_meas,
dac, vref_calib,
shdn: pins.shdn,
vref_pin: pins.vref_pin,
itec_pin: pins.itec_pin,

View File

@ -42,7 +42,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::<volt>(0.0),
pid_engaged: false,
pid: pid::Controller::new(pid::Parameters::default()),

View File

@ -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::<volt>(center_point.into()),
}
@ -295,8 +302,8 @@ impl<'a> Channels<'a> {
let vref = (value as f64 / ad5680::MAX_VALUE as f64) * ElectricPotential::new::<volt>(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!(),
}
}
@ -562,6 +569,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),
}

View File

@ -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<Command, Error>> {
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))))