Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust integral term #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 55 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)`).
Expand Down Expand Up @@ -400,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
);
}
}
Expand Down