Skip to content

Commit f5ad638

Browse files
committedOct 15, 2018
Apply rustfmt.
1 parent f4b28fe commit f5ad638

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed
 

‎src/lib.rs

+29-24
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ pub struct ControlOutput<T: Float> {
3737
pub output: T,
3838
}
3939

40-
impl<T> Pid<T> where T: Float {
40+
impl<T> Pid<T>
41+
where
42+
T: Float,
43+
{
4144
pub fn new(kp: T, ki: T, kd: T, p_limit: T, i_limit: T, d_limit: T) -> Self {
4245
Self {
4346
kp,
@@ -86,17 +89,14 @@ impl<T> Pid<T> where T: Float {
8689
self.integral_term = self.integral_term + error * self.ki;
8790
// Mitigate integral windup: Don't want to keep building up error
8891
// beyond what i_limit will allow.
89-
self.integral_term = self.i_limit.min(self.integral_term.abs()) * self.integral_term.signum();
92+
self.integral_term =
93+
self.i_limit.min(self.integral_term.abs()) * self.integral_term.signum();
9094

9195
// Mitigate derivative kick: Use the derivative of the measurement
9296
// rather than the derivative of the error.
9397
let d_unbounded = -match self.prev_measurement.as_ref() {
94-
Some(prev_measurement) => {
95-
measurement - *prev_measurement
96-
},
97-
None => {
98-
T::zero()
99-
}
98+
Some(prev_measurement) => measurement - *prev_measurement,
99+
None => T::zero(),
100100
} * self.kd;
101101
self.prev_measurement = Some(measurement);
102102
let d = self.d_limit.min(d_unbounded.abs()) * d_unbounded.signum();
@@ -105,12 +105,11 @@ impl<T> Pid<T> where T: Float {
105105
p,
106106
i: self.integral_term,
107107
d,
108-
output: (p + self.integral_term + d)
108+
output: (p + self.integral_term + d),
109109
}
110110
}
111111
}
112112

113-
114113
#[cfg(test)]
115114
mod tests {
116115
use super::Pid;
@@ -178,27 +177,27 @@ mod tests {
178177
pid.update_setpoint(10.0);
179178

180179
let out = pid.next_control_output(0.0);
181-
assert_eq!(out.p, 10.0); // 1.0 * 10.0
182-
assert_eq!(out.i, 1.0); // 0.1 * 10.0
183-
assert_eq!(out.d, 0.0); // -(1.0 * 0.0)
180+
assert_eq!(out.p, 10.0); // 1.0 * 10.0
181+
assert_eq!(out.i, 1.0); // 0.1 * 10.0
182+
assert_eq!(out.d, 0.0); // -(1.0 * 0.0)
184183
assert_eq!(out.output, 11.0);
185184

186185
let out = pid.next_control_output(5.0);
187-
assert_eq!(out.p, 5.0); // 1.0 * 5.0
188-
assert_eq!(out.i, 1.5); // 0.1 * (10.0 + 5.0)
189-
assert_eq!(out.d, -5.0); // -(1.0 * 5.0)
186+
assert_eq!(out.p, 5.0); // 1.0 * 5.0
187+
assert_eq!(out.i, 1.5); // 0.1 * (10.0 + 5.0)
188+
assert_eq!(out.d, -5.0); // -(1.0 * 5.0)
190189
assert_eq!(out.output, 1.5);
191190

192191
let out = pid.next_control_output(11.0);
193-
assert_eq!(out.p, -1.0); // 1.0 * -1.0
194-
assert_eq!(out.i, 1.4); // 0.1 * (10.0 + 5.0 - 1)
195-
assert_eq!(out.d, -6.0); // -(1.0 * 6.0)
192+
assert_eq!(out.p, -1.0); // 1.0 * -1.0
193+
assert_eq!(out.i, 1.4); // 0.1 * (10.0 + 5.0 - 1)
194+
assert_eq!(out.d, -6.0); // -(1.0 * 6.0)
196195
assert_eq!(out.output, -5.6);
197196

198197
let out = pid.next_control_output(10.0);
199-
assert_eq!(out.p, 0.0); // 1.0 * 0.0
200-
assert_eq!(out.i, 1.4); // 0.1 * (10.0 + 5.0 - 1.0 + 0.0)
201-
assert_eq!(out.d, 1.0); // -(1.0 * -1.0)
198+
assert_eq!(out.p, 0.0); // 1.0 * 0.0
199+
assert_eq!(out.i, 1.4); // 0.1 * (10.0 + 5.0 - 1.0 + 0.0)
200+
assert_eq!(out.d, 1.0); // -(1.0 * -1.0)
202201
assert_eq!(out.output, 2.4);
203202
}
204203

@@ -210,8 +209,14 @@ mod tests {
210209
let mut pid64 = Pid::new(2.0f64, 0.0, 0.0, 100.0, 100.0, 100.0);
211210
pid64.update_setpoint(10.0);
212211

213-
assert_eq!(pid32.next_control_output(0.0).output, pid64.next_control_output(0.0).output as f32);
214-
assert_eq!(pid32.next_control_output(0.0).output as f64, pid64.next_control_output(0.0).output);
212+
assert_eq!(
213+
pid32.next_control_output(0.0).output,
214+
pid64.next_control_output(0.0).output as f32
215+
);
216+
assert_eq!(
217+
pid32.next_control_output(0.0).output as f64,
218+
pid64.next_control_output(0.0).output
219+
);
215220
}
216221

217222
}

0 commit comments

Comments
 (0)