From 8b975e656ec818fa7a69df536b045885a5e1f535 Mon Sep 17 00:00:00 2001 From: atse Date: Wed, 14 Feb 2024 16:42:43 +0800 Subject: [PATCH] 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. --- src/channel_state.rs | 4 ++++ src/channels.rs | 8 +++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/channel_state.rs b/src/channel_state.rs index 0f00f0b..4820131 100644 --- a/src/channel_state.rs +++ b/src/channel_state.rs @@ -2,11 +2,13 @@ use smoltcp::time::{Duration, Instant}; use uom::si::{ f64::{ ElectricPotential, + ElectricCurrent, ElectricalResistance, ThermodynamicTemperature, Time, }, electric_potential::volt, + electric_current::ampere, electrical_resistance::ohm, thermodynamic_temperature::degree_celsius, time::millisecond, @@ -29,6 +31,7 @@ pub struct ChannelState { /// i_set 0A center point pub center: CenterPoint, pub dac_value: ElectricPotential, + pub i_set: ElectricCurrent, pub pid_engaged: bool, pub pid: pid::Controller, pub sh: sh::Parameters, @@ -44,6 +47,7 @@ impl ChannelState { adc_interval: Duration::from_millis(100), center: CenterPoint::Vref, dac_value: ElectricPotential::new::(0.0), + i_set: ElectricCurrent::new::(0.0), pid_engaged: false, pid: pid::Controller::new(pid::Parameters::default()), sh: sh::Parameters::default(), diff --git a/src/channels.rs b/src/channels.rs index f32a939..b531e86 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -113,11 +113,8 @@ impl<'a> Channels<'a> { } pub fn get_i(&mut self, channel: usize) -> ElectricCurrent { - let center_point = self.get_center(channel); - let r_sense = ElectricalResistance::new::(R_SENSE); - let voltage = self.get_dac(channel); - let i_tec = (voltage - center_point) / (10.0 * r_sense); - i_tec + let i_set = self.channel_state(channel).i_set; + i_set } /// i_set DAC @@ -143,6 +140,7 @@ impl<'a> Channels<'a> { let voltage = i_tec * 10.0 * r_sense + center_point; let voltage = self.set_dac(channel, voltage); let i_tec = (voltage - center_point) / (10.0 * r_sense); + self.channel_state(channel).i_set = i_tec; i_tec }