Stop i_set from fluctuating in every report

i_set is a user-provided value that shouldn't fluctuate with every VREF
measurement. Storing i_set as channel state is the simplest way to avoid
that.
This commit is contained in:
atse 2024-02-14 16:42:43 +08:00
parent ae3d8b51d4
commit 8b975e656e
2 changed files with 7 additions and 5 deletions

View File

@ -2,11 +2,13 @@ use smoltcp::time::{Duration, Instant};
use uom::si::{ use uom::si::{
f64::{ f64::{
ElectricPotential, ElectricPotential,
ElectricCurrent,
ElectricalResistance, ElectricalResistance,
ThermodynamicTemperature, ThermodynamicTemperature,
Time, Time,
}, },
electric_potential::volt, electric_potential::volt,
electric_current::ampere,
electrical_resistance::ohm, electrical_resistance::ohm,
thermodynamic_temperature::degree_celsius, thermodynamic_temperature::degree_celsius,
time::millisecond, time::millisecond,
@ -29,6 +31,7 @@ pub struct ChannelState {
/// i_set 0A center point /// i_set 0A center point
pub center: CenterPoint, pub center: CenterPoint,
pub dac_value: ElectricPotential, pub dac_value: ElectricPotential,
pub i_set: ElectricCurrent,
pub pid_engaged: bool, pub pid_engaged: bool,
pub pid: pid::Controller, pub pid: pid::Controller,
pub sh: sh::Parameters, pub sh: sh::Parameters,
@ -44,6 +47,7 @@ impl ChannelState {
adc_interval: Duration::from_millis(100), adc_interval: Duration::from_millis(100),
center: CenterPoint::Vref, center: CenterPoint::Vref,
dac_value: ElectricPotential::new::<volt>(0.0), dac_value: ElectricPotential::new::<volt>(0.0),
i_set: ElectricCurrent::new::<ampere>(0.0),
pid_engaged: false, pid_engaged: false,
pid: pid::Controller::new(pid::Parameters::default()), pid: pid::Controller::new(pid::Parameters::default()),
sh: sh::Parameters::default(), sh: sh::Parameters::default(),

View File

@ -113,11 +113,8 @@ impl<'a> Channels<'a> {
} }
pub fn get_i(&mut self, channel: usize) -> ElectricCurrent { pub fn get_i(&mut self, channel: usize) -> ElectricCurrent {
let center_point = self.get_center(channel); let i_set = self.channel_state(channel).i_set;
let r_sense = ElectricalResistance::new::<ohm>(R_SENSE); i_set
let voltage = self.get_dac(channel);
let i_tec = (voltage - center_point) / (10.0 * r_sense);
i_tec
} }
/// i_set DAC /// i_set DAC
@ -143,6 +140,7 @@ impl<'a> Channels<'a> {
let voltage = i_tec * 10.0 * r_sense + center_point; let voltage = i_tec * 10.0 * r_sense + center_point;
let voltage = self.set_dac(channel, voltage); let voltage = self.set_dac(channel, voltage);
let i_tec = (voltage - center_point) / (10.0 * r_sense); let i_tec = (voltage - center_point) / (10.0 * r_sense);
self.channel_state(channel).i_set = i_tec;
i_tec i_tec
} }