1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//! The `config` module provides structures for configuring a PID controller.
//!
//! [`Gain`] is a structure that holds the proportional, integral, and derivative gains for a PID controller.
//! [`Config`] is a structure that holds a [`Gain`] and also provides optional limits for the controller output.
//!
//! # Examples
//!
//! ```rust
//! use advanced_pid::config::{Config, Gain};
//!
//! let gain = Gain { kp: 1.0, ki: 0.1, kd: 0.1 };
//! let config = Config::from(gain);
//!
//! let config_with_limits = Config::new(1.0, 0.1, 0.1).with_limits(-1.0, 1.0);
//! ```
use super::FloatType;

/// `Gain` holds the proportional, integral, and derivative gains for a PID controller.
///
/// See also: [`Config`]
#[derive(Debug, Clone, Default)]
pub struct Gain {
    pub kp: FloatType,
    pub ki: FloatType,
    pub kd: FloatType,
}

/// `Config` holds a [`Gain`] and also provides optional limits for the controller output.
#[derive(Debug, Clone)]
pub struct Config {
    pub gain: Gain,
    pub min: FloatType,
    pub max: FloatType,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            gain: Default::default(),
            min: FloatType::NEG_INFINITY,
            max: FloatType::INFINITY,
        }
    }
}

impl Config {
    /// Creates a new `Config` with the specified gains.
    pub fn new(kp: FloatType, ki: FloatType, kd: FloatType) -> Self {
        Self {
            gain: Gain { kp, ki, kd },
            ..Default::default()
        }
    }

    /// Returns a new `Config` with the specified limits.
    pub fn with_limits(self, min: FloatType, max: FloatType) -> Self {
        Self { min, max, ..self }
    }
}

impl From<Gain> for Config {
    /// Converts a `Gain` into a `Config`.
    fn from(gain: Gain) -> Self {
        Self {
            gain,
            ..Default::default()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_gain_default() {
        let gain = Gain::default();
        assert_eq!(gain.kp, 0.0);
        assert_eq!(gain.ki, 0.0);
        assert_eq!(gain.kd, 0.0);
    }

    #[test]
    fn test_config_new() {
        let config = Config::new(1.0, 0.5, 0.1);
        assert_eq!(config.gain.kp, 1.0);
        assert_eq!(config.gain.ki, 0.5);
        assert_eq!(config.gain.kd, 0.1);
        assert_eq!(config.min, FloatType::NEG_INFINITY);
        assert_eq!(config.max, FloatType::INFINITY);
    }

    #[test]
    fn test_config_with_limits() {
        let config = Config::new(1.0, 0.5, 0.1).with_limits(-2.0, 2.0);
        assert_eq!(config.gain.kp, 1.0);
        assert_eq!(config.gain.ki, 0.5);
        assert_eq!(config.gain.kd, 0.1);
        assert_eq!(config.min, -2.0);
        assert_eq!(config.max, 2.0);
    }

    #[test]
    fn test_config_from_gain() {
        let gain = Gain {
            kp: 1.0,
            ki: 0.5,
            kd: 0.1,
        };
        let config = Config::from(gain);
        assert_eq!(config.gain.kp, 1.0);
        assert_eq!(config.gain.ki, 0.5);
        assert_eq!(config.gain.kd, 0.1);
        assert_eq!(config.min, FloatType::NEG_INFINITY);
        assert_eq!(config.max, FloatType::INFINITY);
    }

    #[test]
    fn test_config_default() {
        let config = Config::default();
        assert_eq!(config.gain.kp, 0.0);
        assert_eq!(config.gain.ki, 0.0);
        assert_eq!(config.gain.kd, 0.0);
        assert_eq!(config.min, FloatType::NEG_INFINITY);
        assert_eq!(config.max, FloatType::INFINITY);
    }
}