From 6b9c416db800c824788e7ae06bddff825061e728 Mon Sep 17 00:00:00 2001 From: Albin Hedman Date: Wed, 19 Jun 2024 14:45:09 +0200 Subject: [PATCH 1/2] Add methods for reading writing the integral term --- src/lib.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index db2f5c5..d35b788 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -263,6 +263,17 @@ where pub fn reset_integral_term(&mut self) { self.integral_term = T::zero(); } + + /// Overwrite the integral term, this may drastically change the + /// control output. + pub fn set_integral_term(&mut self, integral: T) { + self.integral_term = integral; + } + + /// Get the integral term. + pub fn get_integral_term(&self) -> T { + self.integral_term + } } /// Saturating the input `value` according the absolute `limit` (`-abs(limit) <= output <= abs(limit)`). From a1bc1c2e5f774f938fa9a8cff724a8778ac13973 Mon Sep 17 00:00:00 2001 From: Albin Hedman Date: Wed, 19 Jun 2024 14:46:25 +0200 Subject: [PATCH 2/2] Add more tests --- src/lib.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d35b788..2b291fb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -411,20 +411,60 @@ mod tests { } } - // NOTE: use for new test in future: /// Full PID operation with mixed signed integer checking to make sure they're equal + /// Full PID operation with mixed signed integer checking to make sure they're equal /// PID operation with zero'd values, checking to see if different floats equal each other #[test] - fn signed_integers_zeros() { + fn signed_integers() { let mut pid_i8 = Pid::new(10i8, 100); pid_i8.p(0, 100).i(0, 100).d(0, 100); + let mut pid_i16 = Pid::new(10i16, 100i16); + pid_i16.p(0i16, 100i16).i(0i16, 100i16).d(0i16, 100i16); + let mut pid_i32 = Pid::new(10i32, 100); pid_i32.p(0, 100).i(0, 100).d(0, 100); + let mut pid_i64 = Pid::new(10i64, 100); + pid_i64.p(0, 100).i(0, 100).d(0, 100); + + let mut pid_i128 = Pid::new(10i128, 100); + pid_i128.p(0, 100).i(0, 100).d(0, 100); + + for _ in 0..5 { + assert_eq!( + pid_i8.next_control_output(0i8).output as i16, + pid_i16.next_control_output(0i16).output, + ); + + assert_eq!( + pid_i16.next_control_output(0i16).output as i32, + pid_i32.next_control_output(0i32).output, + ); + + assert_eq!( + pid_i32.next_control_output(0i32).output as i64, + pid_i64.next_control_output(0i64).output + ); + + assert_eq!( + pid_i64.next_control_output(0i64).output as i128, + pid_i128.next_control_output(0i128).output + ); + } + } + + #[test] + fn float_match_integers() { + let mut pid_i32 = Pid::new(10i32, 100); + pid_i32.p(0, 100).i(0, 100).d(0, 100); + + let mut pid_f32 = Pid::new(10.0f32, 100.0); + pid_f32.p(0.0, 100.0).i(0.0, 100.0).d(0.0, 100.0); + for _ in 0..5 { assert_eq!( - pid_i32.next_control_output(0).output, - pid_i8.next_control_output(0i8).output as i32 + pid_i32.next_control_output(0i32).output as f32, + pid_f32.next_control_output(0f32).output ); } }