Skip to content

Commit e4908de

Browse files
harrisonmgOwez
authored andcommitted
fix negative limits and add test
1 parent 60e07b9 commit e4908de

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/lib.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,9 @@ where
265265
}
266266
}
267267

268-
/// Saturating the input `value` according the absolute `limit` (`-limit <= output <= limit`).
268+
/// Saturating the input `value` according the absolute `limit` (`-abs(limit) <= output <= abs(limit)`).
269269
fn apply_limit<T: Number>(limit: T, value: T) -> T {
270-
num_traits::clamp(value, -limit, limit)
270+
num_traits::clamp(value, -limit.abs(), limit.abs())
271271
}
272272

273273
#[cfg(test)]
@@ -442,4 +442,17 @@ mod tests {
442442
}
443443
);
444444
}
445+
446+
/// Make sure negative limits don't break the controller
447+
#[test]
448+
fn negative_limits() {
449+
let mut pid = Pid::new(10.0f32, -10.0);
450+
pid.p(1.0, -50.0).i(1.0, -50.0).d(1.0, -50.0);
451+
452+
let out = pid.next_control_output(0.0);
453+
assert_eq!(out.p, 10.0);
454+
assert_eq!(out.i, 10.0);
455+
assert_eq!(out.d, 0.0);
456+
assert_eq!(out.output, 10.0);
457+
}
445458
}

0 commit comments

Comments
 (0)