From d2a57245b22b9bf5a0293ca18de489b136cbc1d8 Mon Sep 17 00:00:00 2001 From: linuswck Date: Thu, 19 Sep 2024 17:39:05 +0800 Subject: [PATCH] Fix wrong current limit duty cycle calculation - prev commit assumed setting 3.3V -> 3A current limit which is wrong - Please refer to the MAX1968 datasheet for the duty cycle calculation equation --- src/channels.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/channels.rs b/src/channels.rs index 37ba961..1d35038 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -45,7 +45,11 @@ pub const MAX_TEC_V: ElectricPotential = ElectricPotential { units: PhantomData, value: 4.0, }; - +const MAX_TEC_I_DUTY_TO_CURRENT_RATE: ElectricCurrent = ElectricCurrent { + dimension: PhantomData, + units: PhantomData, + value: 1.0 / (10.0 * R_SENSE / 3.3), +}; // DAC chip outputs 0-5v, which is then passed through a resistor dividor to provide 0-3v range const DAC_OUT_V_MAX: ElectricPotential = ElectricPotential { dimension: PhantomData, @@ -385,15 +389,13 @@ impl Channels { } pub fn get_max_i_pos(&mut self, channel: usize) -> (ElectricCurrent, ElectricCurrent) { - let max = ElectricCurrent::new::(3.0); let duty = self.get_pwm(channel, PwmPin::MaxIPos); - (duty * max, MAX_TEC_I) + (duty * MAX_TEC_I_DUTY_TO_CURRENT_RATE, MAX_TEC_I) } pub fn get_max_i_neg(&mut self, channel: usize) -> (ElectricCurrent, ElectricCurrent) { - let max = ElectricCurrent::new::(3.0); let duty = self.get_pwm(channel, PwmPin::MaxINeg); - (duty * max, MAX_TEC_I) + (duty * MAX_TEC_I_DUTY_TO_CURRENT_RATE, MAX_TEC_I) } // Get current passing through TEC @@ -442,16 +444,16 @@ impl Channels { pub fn set_max_i_pos(&mut self, channel: usize, max_i_pos: ElectricCurrent) -> (ElectricCurrent, ElectricCurrent) { let max = ElectricCurrent::new::(3.0); - let duty = (max_i_pos.min(MAX_TEC_I).max(ElectricCurrent::zero()) / max).get::(); + let duty = (max_i_pos.max(ElectricCurrent::zero()).min(max) / MAX_TEC_I_DUTY_TO_CURRENT_RATE).get::(); let duty = self.set_pwm(channel, PwmPin::MaxIPos, duty); - (duty * max, max) + (duty * MAX_TEC_I_DUTY_TO_CURRENT_RATE, max) } pub fn set_max_i_neg(&mut self, channel: usize, max_i_neg: ElectricCurrent) -> (ElectricCurrent, ElectricCurrent) { let max = ElectricCurrent::new::(3.0); - let duty = (max_i_neg.min(MAX_TEC_I).max(ElectricCurrent::zero()) / max).get::(); + let duty = (max_i_neg.max(ElectricCurrent::zero()).min(max) / MAX_TEC_I_DUTY_TO_CURRENT_RATE).get::(); let duty = self.set_pwm(channel, PwmPin::MaxINeg, duty); - (duty * max, max) + (duty * MAX_TEC_I_DUTY_TO_CURRENT_RATE, max) } fn report(&mut self, channel: usize) -> Report {