@@ -30,46 +30,42 @@ A proportional-integral-derivative (PID) controller.
3030## Example
3131
3232``` rust
33- extern crate pid;
3433use pid :: Pid ;
3534
36- fn main () {
37- // Set only kp (proportional) to 10. The setpoint is 15.
38- // Set limits for P, I, and D to 100 each.
39- let mut pid = Pid :: new (10.0 , 0.0 , 0.0 , 100.0 , 100.0 , 100.0 , 100.0 , 15.0 );
40- // Fake a measurement of 10.0, which is an error of 5.0.
41- let output = pid . next_control_output (10.0 );
42- // Verify that kp * error = 10.0 * 5.0 = 50.0
43- assert_eq! (output . output, 50.0 );
44- // Verify that all output was from the proportional term
45- assert_eq! (output . p, 50.0 );
46- assert_eq! (output . i, 0.0 );
47- assert_eq! (output . d, 0.0 );
48-
49- // Verify that the same measurement produces the same output since we
50- // aren't using the stateful derivative & integral terms.
51- let output = pid . next_control_output (10.0 );
52- assert_eq! (output . p, 50.0 );
53-
54- // Add an integral term
55- pid . ki = 1.0 ;
56- let output = pid . next_control_output (10.0 );
57- assert_eq! (output . p, 50.0 );
58- // Verify that the integral term is adding to the output signal.
59- assert_eq! (output . i, 5.0 );
60- assert_eq! (output . output, 55.0 );
61-
62- // Add a derivative term
63- pid . kd = 2.0 ;
64- let output = pid . next_control_output (15.0 ); // Match the desired target
65- // No proportional term since no error
66- assert_eq! (output . p, 0.0 );
67- // Integral term stays the same
68- assert_eq! (output . i, 5.0 );
69- // Derivative on measurement produces opposing signal
70- assert_eq! (output . d, - 10.0 );
71- assert_eq! (output . output, - 5.0 );
72- }
35+ // Create a new proportional-only PID controller with a setpoint of 15
36+ let mut pid = Pid :: new (15.0 , 100.0 );
37+ pid . p (10.0 , 100.0 );
38+
39+ // Input a mesurement with an error of 5.0 from our setpoint
40+ let output = pid . next_control_output (10.0 );
41+
42+ // Show that the error is correct by multiplying by our kp
43+ assert_eq! (output . output, 50.0 ); // <--
44+ assert_eq! (output . p, 50.0 );
45+
46+ // It won't change on repeat; the controller is proportional-only
47+ let output = pid . next_control_output (10.0 );
48+ assert_eq! (output . output, 50.0 ); // <--
49+ assert_eq! (output . p, 50.0 );
50+
51+ // Add a new integral term to the controller and input again
52+ pid . i (1.0 , 100.0 );
53+ let output = pid . next_control_output (10.0 );
54+
55+ // Now that the integral makes the controller stateful, it will change
56+ assert_eq! (output . output, 55.0 ); // <--
57+ assert_eq! (output . p, 50.0 );
58+ assert_eq! (output . i, 5.0 );
59+
60+ // Add our final derivative term and match our setpoint target
61+ pid . d (2.0 , 100.0 );
62+ let output = pid . next_control_output (15.0 );
63+
64+ // The output will now say to go down due to the derivative
65+ assert_eq! (output . output, - 5.0 ); // <--
66+ assert_eq! (output . p, 0.0 );
67+ assert_eq! (output . i, 5.0 );
68+ assert_eq! (output . d, - 10.0 );
7369```
7470
7571## Assumptions
0 commit comments