From 088d26a2c3af77c321fb4e7a41289252bddbf30d 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 1/2] 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) }) } } From cadb2df7d566d763dffd024f3edd8035b88c4fab 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: Thu, 10 Jul 2025 19:32:20 +0800 Subject: [PATCH 2/2] correct pid error measurement --- src/pid.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/pid.rs b/src/pid.rs index 639bcdd..dc8a93e 100644 --- a/src/pid.rs +++ b/src/pid.rs @@ -21,8 +21,8 @@ impl PIController { } /// Update the PI controller, returning the new output value. - pub fn update(&mut self, measurement: I16F16, setpoint: I16F16, dt: I16F16) -> I16F16 { - let error = measurement - setpoint; + pub fn update(&mut self, setpoint: I16F16, measurement: I16F16, dt: I16F16) -> I16F16 { + let error = setpoint - measurement; self.k_p * error + self.integral.update(error, dt) } } @@ -54,8 +54,8 @@ impl PIDController { } /// Update the PID controller, returning the new output value. - pub fn update(&mut self, measurement: I16F16, setpoint: I16F16, dt: I16F16) -> I16F16 { - let error = measurement - setpoint; + pub fn update(&mut self, setpoint: I16F16, measurement: I16F16, dt: I16F16) -> I16F16 { + let error = setpoint - measurement; self.k_p * error + self.integral.update(error, dt) + self.derivative.update(measurement, dt) } }