From 00d5feaa8d8c46617fd6c7c38f5f469af5aa67bd Mon Sep 17 00:00:00 2001 From: atse Date: Wed, 3 Apr 2024 11:31:34 +0800 Subject: [PATCH] Limit i_set within range of MAX1968 chip --- src/channels.rs | 9 +++++++++ src/fan_ctrl.rs | 6 ++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/channels.rs b/src/channels.rs index 70ef7a0..488a8d6 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -25,6 +25,10 @@ use crate::{ pub const CHANNELS: usize = 2; pub const R_SENSE: f64 = 0.05; + +// as stated in the MAX1968 datasheet +pub const MAX_TEC_I: f64 = 3.0; + // DAC chip outputs 0-5v, which is then passed through a resistor dividor to provide 0-3v range const DAC_OUT_V_MAX: f64 = 3.0; @@ -130,6 +134,11 @@ impl<'a> Channels<'a> { } pub fn set_i(&mut self, channel: usize, i_set: ElectricCurrent) -> ElectricCurrent { + // Silently clamp i_set + let i_ceiling = ElectricCurrent::new::(MAX_TEC_I); + let i_floor = ElectricCurrent::new::(-MAX_TEC_I); + let i_set = i_set.min(i_ceiling).max(i_floor); + let vref_meas = match channel.into() { 0 => self.channel0.vref_meas, 1 => self.channel1.vref_meas, diff --git a/src/fan_ctrl.rs b/src/fan_ctrl.rs index 30416f3..614510b 100644 --- a/src/fan_ctrl.rs +++ b/src/fan_ctrl.rs @@ -11,13 +11,11 @@ use uom::si::{ use crate::{ hw_rev::HWSettings, command_handler::JsonBuffer, + channels::MAX_TEC_I, }; pub type FanPin = PwmChannels; -// as stated in the schematics -const MAX_TEC_I: f32 = 3.0; - const MAX_USER_FAN_PWM: f32 = 100.0; const MIN_USER_FAN_PWM: f32 = 1.0; @@ -56,7 +54,7 @@ impl FanCtrl { pub fn cycle(&mut self, abs_max_tec_i: ElectricCurrent) { self.abs_max_tec_i = abs_max_tec_i.get::() as f32; if self.fan_auto && self.hw_settings.fan_available { - let scaled_current = self.abs_max_tec_i / MAX_TEC_I; + let scaled_current = self.abs_max_tec_i / MAX_TEC_I as f32; // do not limit upper bound, as it will be limited in the set_pwm() let pwm = (MAX_USER_FAN_PWM * (scaled_current * (scaled_current * self.k_a + self.k_b) + self.k_c)) as u32; self.set_pwm(pwm);