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

Add features: A combination of other PRs + Making the API more flexible. #24

Closed
wants to merge 58 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
e94f44a
Update lib.rs
May 13, 2024
b6b61ea
Change the API to be more flexible
May 14, 2024
d79c459
remove num-traits dependency
May 14, 2024
9d61512
Fix minor errors
May 14, 2024
b839747
Implement 'Default' trait for Pid
May 14, 2024
91cd04a
Add 'Send + Sync' traits
May 14, 2024
9a8e9ad
Undo last commit because of 'no-std'
May 14, 2024
170f61c
bring num traits dependency back
May 14, 2024
aa8066a
Implement constant constructor method
May 14, 2024
80135e9
fix errors (1)
May 14, 2024
295cd89
fix errors (2)
May 14, 2024
7e2c78f
fix errors (3)
May 14, 2024
fbbf658
fix errors (4)
May 14, 2024
e7d3dc6
Implement const constructor
May 14, 2024
80d9bfd
Implement core::Send
May 14, 2024
2114739
Implement Send explicit
May 14, 2024
c886603
Fix implement send
May 14, 2024
b7caf01
Remove unnecessary unsafe
May 14, 2024
e76e126
Try impl constructor
May 15, 2024
d5c92f4
try into
May 15, 2024
a527d23
try const trait
May 15, 2024
0073c79
fix const syntax
May 15, 2024
52065f0
Remove const trait
May 15, 2024
bae75b6
try generic except types
May 15, 2024
b83262c
impl const new
May 15, 2024
1e6d66f
remove conflicting trait
May 15, 2024
172cdb5
implement error type
May 20, 2024
47bdeb3
fix errors
May 20, 2024
f8e8651
Update lib.rs
May 20, 2024
ed9361f
Update lib.rs
May 20, 2024
ffa9be6
Update lib.rs
May 20, 2024
ba95af6
Update lib.rs
May 20, 2024
8389e50
fix errors
May 20, 2024
ee99e4b
remove checked
May 20, 2024
7a6715f
Update lib.rs
May 20, 2024
eb1a9b6
Update lib.rs
May 20, 2024
4799974
Update lib.rs
May 20, 2024
edf59aa
Update lib.rs
May 21, 2024
36b8304
change error to option
May 21, 2024
9324cab
Update lib.rs
May 21, 2024
142754c
Update lib.rs
May 21, 2024
10e5c8d
implement dt and functional style
May 21, 2024
c4a0cb3
Update lib.rs
May 21, 2024
c9fd304
Update lib.rs
May 22, 2024
b343cea
Update lib.rs
May 22, 2024
b3094d0
fix bugs
May 22, 2024
9357d6a
fix error
May 22, 2024
0a4d39f
add kd dt adjustment
May 22, 2024
31b5ca2
make dt guard better
May 22, 2024
94b78f9
Update lib.rs
May 22, 2024
8968980
fix bugs
May 22, 2024
10a186c
fix bugs
May 22, 2024
7da4f7f
fix bugs
May 22, 2024
102b563
fix typo
May 22, 2024
8472585
Improve control output limits
May 25, 2024
2b32fbf
do not rely on `Eq` impl
May 25, 2024
29b8e52
fix typo
May 27, 2024
b4433db
fix typo
May 27, 2024
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
Prev Previous commit
Next Next commit
Implement constant constructor method
un3481 authored May 14, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit aa8066a7a0166b9fdd571d20c29de4408a5a9794
81 changes: 55 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -106,7 +106,7 @@ where
/// [Number] is abstract and can be used with anything from a [i32] to an [i128] (as well as user-defined types). Because of this, very small types might overflow during calculation in [`next_control_output`](Self::next_control_output). You probably don't want to use [i8] or user-defined types around that size so keep that in mind when designing your controller.
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Pid<T> {
pub struct Pid<T: num_traits::Num> {
/// Ideal setpoint to strive for.
pub setpoint: T,
/// Proportional gain.
@@ -159,7 +159,8 @@ pub struct Pid<T> {
/// println!("P: {}\nI: {}\nD: {}\nFinal Output: {}", output.p, output.i, output.d, output.output);
/// ```
#[derive(Debug, PartialEq, Eq)]
pub struct ControlOutput<T> {
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct ControlOutput<T: num_traits::Num> {
/// Contribution of the P term to the output.
pub p: T,
/// Contribution of the I term to the output.
@@ -177,46 +178,74 @@ where
T: Default,
{
fn default() -> Self {
Self::new()
}
}

impl<T> Pid<T>
where
T: num_traits::PrimInt,
{
/// Creates a new controller
///
/// To set your P, I, and D gains into this controller, please use the following builder methods:
/// - [Self::p()]: Proportional gain setting
/// - [Self::i()]: Integral gain setting
/// - [Self::d()]: Derivative gain setting
pub const fn new() -> Self {
Self {
setpoint: T::default(),
kp: T::default(),
ki: T::default(),
kd: T::default(),
p_limit_high: T::default(),
p_limit_low: T::default(),
i_limit_high: T::default(),
i_limit_low: T::default(),
d_limit_high: T::default(),
d_limit_low: T::default(),
o_limit_high: T::default(),
o_limit_low: T::default(),
i_term: T::default(),
setpoint: 0,
kp: 0,
ki: 0,
kd: 0,
p_limit_high: 0,
p_limit_low: 0,
i_limit_high: 0,
i_limit_low: 0,
d_limit_high: 0,
d_limit_low: 0,
o_limit_high: 0,
o_limit_low: 0,
i_term: 0,
prev_measurement: None,
}
}
}

impl<T> Pid<T>
where
T: core::ops::Sub<T, Output = T>
+ core::ops::Add<T, Output = T>
+ core::ops::Mul<T, Output = T>
+ core::ops::Div<T, Output = T>
+ core::cmp::PartialOrd
+ Default
+ core::ops::Neg<Output = T>
+ core::marker::Copy,
T: num_traits::Num,
{
/// Creates a new controller with the target setpoint and the output limit
/// Creates a new controller
///
/// To set your P, I, and D gains into this controller, please use the following builder methods:
/// - [Self::p()]: Proportional gain setting
/// - [Self::i()]: Integral gain setting
/// - [Self::d()]: Derivative gain setting
pub fn new() -> Self {
Self::default()
pub const fn new() -> Self {
Self {
setpoint: 0.0,
kp: 0.0,
ki: 0.0,
kd: 0.0,
p_limit_high: 0.0,
p_limit_low: 0.0,
i_limit_high: 0.0,
i_limit_low: 0.0,
d_limit_high: 0.0,
d_limit_low: 0.0,
o_limit_high: 0.0,
o_limit_low: 0.0,
i_term: 0.0,
prev_measurement: None,
}
}
}

impl<T> Pid<T>
where
T: num_traits::Num,
{
/// Sets the [Self::p] gain for this controller.
pub fn p(&mut self, gain: impl Into<T>) -> &mut Self {
self.kp = gain.into();