From 845ffbff55905ba4c798003f65e4c9fbaa1e94f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=BCha=20=C3=9Cn=C3=BCvar?= <87157627+phycrax@users.noreply.github.com> Date: Fri, 4 Jul 2025 10:40:23 +0800 Subject: [PATCH] use param instead of const generics for pwm resolution --- src/lib.rs | 9 ++++++--- src/pwm.rs | 6 +++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 47b0b37..91ed5a9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,22 +18,25 @@ const SQRT_3: I16F16 = I16F16::lit("1.7320508"); /// If this controller does not match the exact setup that you desire, then all /// of the underlying algorithms are available to use instead (see the /// [`park_clarke`], [`pwm`], and [`pid`] modules). -pub struct Foc { +pub struct Foc { flux_current_controller: pid::PIController, torque_current_controller: pid::PIController, + max_pwm_duty: u16, _phantom: PhantomData, } -impl Foc { +impl Foc { /// Create a new FOC controller with the desired PI controllers for the flux /// and torque components. pub fn new( flux_current_controller: pid::PIController, torque_current_controller: pid::PIController, + max_pwm_duty: u16, ) -> Self { Self { flux_current_controller, torque_current_controller, + max_pwm_duty, _phantom: PhantomData, } } @@ -83,6 +86,6 @@ impl Foc(orthogonal_voltage) + Modulator::as_compare_value(orthogonal_voltage, self.max_pwm_duty) } } diff --git a/src/pwm.rs b/src/pwm.rs index 43132d9..7a19549 100644 --- a/src/pwm.rs +++ b/src/pwm.rs @@ -13,12 +13,12 @@ pub trait Modulation { /// Module the value, returning the result as a value between 0 and the specified /// maximum value inclusive. - fn as_compare_value(value: TwoPhaseReferenceFrame) -> [u16; 3] { + fn as_compare_value(value: TwoPhaseReferenceFrame, max: u16) -> [u16; 3] { Self::modulate(value).map(|val| { - (((val + I16F16::from_num(1)) * (MAX as i32 + 1)) / 2) + (((val + I16F16::from_num(1)) * (max as i32 + 1)) / 2) .round() .saturating_to_num::() - .clamp(0, MAX) + .clamp(0, max) }) } }