1use crate::controllers::derivative::DerivativeState;
2use crate::controllers::{Gains, InvalidConfig, check_limit, rescale_integral};
3use crate::math::{assert_dt, clamp_symmetric, wrap_angle};
4
5#[derive(Clone, Copy, Debug, PartialEq)]
10pub struct YawControllerConfig {
11 pub gains: Gains,
17
18 pub max_integral: f32,
23
24 pub max_rate_radps: f32,
29}
30
31impl Default for YawControllerConfig {
32 fn default() -> Self {
33 Self {
34 gains: Gains {
36 p: 2.5,
37 i: 0.0,
38 d: 0.06,
39 },
40 max_integral: 100.0, max_rate_radps: 2.0, }
43 }
44}
45
46impl YawControllerConfig {
47 pub fn validate(&self) -> Result<(), InvalidConfig> {
59 check_limit("max_integral", self.max_integral)?;
60 check_limit("max_rate_radps", self.max_rate_radps)
61 }
62}
63
64#[derive(Clone, Debug, Default, PartialEq)]
76pub struct YawController {
77 derivative: DerivativeState<f32>,
79 integrated_error: f32,
80}
81
82impl YawController {
83 pub fn control(
126 &mut self,
127 config: &YawControllerConfig,
128 dt: f32,
129 desired_yaw_rad: f32,
130 estimated_yaw_rad: f32,
131 ) -> f32 {
132 let error = angle_diff(desired_yaw_rad, estimated_yaw_rad); self.control_error(config, dt, error)
134 }
135
136 pub(crate) fn control_error(
144 &mut self,
145 config: &YawControllerConfig,
146 dt: f32,
147 error: f32,
148 ) -> f32 {
149 assert_dt(dt);
150
151 let gains = &config.gains;
152
153 if !error.is_finite() {
154 self.derivative.forget();
158 return 0.0;
159 }
160 let error_derivative = self.derivative.step(error, dt);
161
162 self.integrated_error =
163 clamp_symmetric(self.integrated_error + dt * error, config.max_integral);
164
165 let yaw_rate =
166 gains.p * error + gains.i * self.integrated_error + gains.d * error_derivative;
167
168 clamp_symmetric(yaw_rate, config.max_rate_radps)
169 }
170
171 pub(crate) fn clear_integral(&mut self) {
173 self.integrated_error = 0.0;
174 }
175
176 pub(crate) fn rescale_integral(
179 &mut self,
180 old: &YawControllerConfig,
181 new: &YawControllerConfig,
182 ) {
183 self.integrated_error = rescale_integral(self.integrated_error, old.gains.i, new.gains.i);
184 }
185
186 pub(crate) fn hand_back(&mut self) {
188 self.derivative = DerivativeState::default();
189 }
190}
191
192pub(crate) fn angle_diff(desired_yaw: f32, current_yaw: f32) -> f32 {
203 wrap_angle(desired_yaw - current_yaw)
204}
205
206#[cfg(test)]
207mod tests {
208
209 use std::f32::consts::PI;
210
211 use approx::assert_relative_eq;
212
213 use crate::controllers::Gains;
214
215 use super::*;
216
217 const EPSILON: f32 = 1e-5;
218
219 #[test]
221 fn hand_back_keeps_the_integral() {
222 let config = YawControllerConfig {
223 gains: Gains {
224 p: 1.0,
225 i: 2.0,
226 d: 0.5,
227 },
228 ..p_only_config(1.0, 10.0)
229 };
230 let mut controller = YawController::default();
231 controller.control_error(&config, 0.1, 0.5);
232 let integral = controller.integrated_error;
233
234 controller.hand_back();
235
236 assert_eq!(controller.integrated_error, integral);
237 assert_eq!(controller.derivative, DerivativeState::default());
238 }
239
240 fn p_only_config(p_gain: f32, max_rate_radps: f32) -> YawControllerConfig {
244 YawControllerConfig {
245 gains: Gains {
246 p: p_gain,
247 i: 0.0,
248 d: 0.0,
249 },
250 max_integral: 10.0,
251 max_rate_radps,
252 }
253 }
254
255 fn i_only_config(i_gain: f32, max_integral: f32, max_rate_radps: f32) -> YawControllerConfig {
257 YawControllerConfig {
258 gains: Gains {
259 p: 0.0,
260 i: i_gain,
261 d: 0.0,
262 },
263 max_integral,
264 max_rate_radps,
265 }
266 }
267
268 fn d_only_config(d_gain: f32, max_rate_radps: f32) -> YawControllerConfig {
270 YawControllerConfig {
271 gains: Gains {
272 p: 0.0,
273 i: 0.0,
274 d: d_gain,
275 },
276 max_integral: 10.0,
277 max_rate_radps,
278 }
279 }
280
281 #[test]
283 fn test_default_config() {
284 let config = YawControllerConfig::default();
285
286 assert_relative_eq!(config.gains.p, 2.5, epsilon = EPSILON, max_relative = 1e-3);
287 assert_relative_eq!(config.gains.i, 0.0, epsilon = EPSILON, max_relative = 1e-3);
288 assert_relative_eq!(config.gains.d, 0.06, epsilon = EPSILON, max_relative = 1e-3);
289 assert_relative_eq!(
290 config.max_integral,
291 100.0,
292 epsilon = EPSILON,
293 max_relative = 1e-3
294 );
295 assert_relative_eq!(
296 config.max_rate_radps,
297 2.0,
298 epsilon = EPSILON,
299 max_relative = 1e-3
300 );
301 }
302
303 #[test]
304 fn default_is_zeroed_state() {
305 assert_eq!(
306 YawController::default(),
307 YawController {
308 derivative: DerivativeState::default(),
309 integrated_error: 0.0,
310 }
311 );
312 }
313
314 #[test]
316 fn test_proportional_control() {
317 let config = p_only_config(2.0, 5.0);
319 let mut controller = YawController::default();
320
321 let dt = 0.01;
323 let desired_yaw = 0.5;
324 let estimated_yaw = 0.0;
325
326 let rate = controller.control(&config, dt, desired_yaw, estimated_yaw);
327 assert_relative_eq!(
328 rate,
329 1.0,
330 epsilon = EPSILON,
331 max_relative = 1e-3,
332 max_relative = 1e-3
333 );
334
335 let rate = controller.control(&config, dt, 0.0, 0.5);
337 assert_relative_eq!(rate, -1.0, epsilon = EPSILON, max_relative = 1e-3);
338 }
339
340 #[test]
342 fn test_integral_control() {
343 let config = i_only_config(1.0, 10.0, 5.0);
345 let mut controller = YawController::default();
346
347 let dt = 1.0; let desired_yaw = 0.5;
350 let estimated_yaw = 0.0;
351
352 let rate = controller.control(&config, dt, desired_yaw, estimated_yaw);
354 assert_relative_eq!(rate, 0.5, epsilon = EPSILON, max_relative = 1e-3);
355
356 let rate = controller.control(&config, dt, desired_yaw, estimated_yaw);
358 assert_relative_eq!(rate, 1.0, epsilon = EPSILON, max_relative = 1e-3);
359
360 let config = i_only_config(1.0, 1.5, 5.0);
362 let mut controller = YawController::default();
363
364 controller.control(&config, dt, desired_yaw, estimated_yaw); controller.control(&config, dt, desired_yaw, estimated_yaw); controller.control(&config, dt, desired_yaw, estimated_yaw); let rate = controller.control(&config, dt, desired_yaw, estimated_yaw);
369
370 assert_relative_eq!(rate, 1.5, epsilon = EPSILON, max_relative = 1e-3);
372 }
373
374 #[test]
376 fn test_derivative_control() {
377 let config = d_only_config(2.0, 5.0);
379 let mut controller = YawController::default();
380
381 let dt = 1.0; let rate = controller.control(&config, dt, 0.5, 0.0);
385 assert_relative_eq!(
386 rate,
387 0.0,
388 epsilon = EPSILON,
389 max_relative = 1e-3,
390 max_relative = 1e-3
391 );
392
393 let rate = controller.control(&config, dt, 0.5, 0.0);
395 assert_relative_eq!(rate, 0.0, epsilon = EPSILON, max_relative = 1e-3);
396
397 let rate = controller.control(&config, dt, 0.6, 0.0);
399 assert_relative_eq!(rate, 0.2, epsilon = EPSILON, max_relative = 1e-3);
400
401 let rate = controller.control(&config, dt, 0.4, 0.0);
403 assert_relative_eq!(rate, -0.4, epsilon = EPSILON, max_relative = 1e-3);
404 }
405
406 #[test]
408 fn test_pid_combined() {
409 let config = YawControllerConfig {
411 gains: Gains {
412 p: 1.0,
413 i: 0.5,
414 d: 0.2,
415 },
416 max_integral: 10.0,
417 max_rate_radps: 5.0,
418 };
419
420 let mut controller = YawController::default();
421
422 let dt = 1.0;
423
424 let rate = controller.control(&config, dt, 0.5, 0.0);
430 assert_relative_eq!(rate, 0.75, epsilon = EPSILON, max_relative = 1e-3);
431
432 let rate = controller.control(&config, dt, 0.5, 0.0);
438 assert_relative_eq!(rate, 1.0, epsilon = EPSILON, max_relative = 1e-3);
439
440 let rate = controller.control(&config, dt, 0.6, 0.0);
446 assert_relative_eq!(rate, 1.42, epsilon = EPSILON, max_relative = 1e-3);
447 }
448
449 #[test]
451 fn test_varying_time_steps() {
452 let config = YawControllerConfig {
453 gains: Gains {
454 p: 0.0,
455 i: 1.0,
456 d: 0.0,
457 },
458 max_integral: 10.0,
459 max_rate_radps: 5.0,
460 };
461
462 let mut controller = YawController::default();
463
464 let rate_1s = controller.control(&config, 1.0, 0.5, 0.0);
466 assert_relative_eq!(rate_1s, 0.5, epsilon = EPSILON, max_relative = 1e-3);
467
468 let rate_05s = controller.control(&config, 0.5, 0.5, 0.0);
470 assert_relative_eq!(
471 rate_05s,
472 0.5 + 0.25, epsilon = EPSILON,
474 max_relative = 1e-3
475 );
476
477 let rate_025s = controller.control(&config, 0.25, 0.5, 0.0);
479 assert_relative_eq!(
480 rate_025s,
481 0.5 + 0.25 + 0.125, epsilon = EPSILON,
483 max_relative = 1e-3
484 );
485 }
486
487 #[test]
489 fn test_small_time_steps() {
490 let config = YawControllerConfig {
491 gains: Gains {
492 p: 1.0,
493 i: 1.0,
494 d: 1.0,
495 },
496 max_integral: 10.0,
497 max_rate_radps: 5.0,
498 };
499
500 let mut controller = YawController::default();
501
502 let dt = 0.000001; let error = 0.5;
505 let rate = controller.control(&config, dt, error, 0.0);
506
507 assert_relative_eq!(
512 rate,
513 0.5, epsilon = EPSILON,
515 max_relative = 1e-3
516 );
517
518 let rate = controller.control(&config, dt, error, 0.0);
520 assert_relative_eq!(
521 rate,
522 0.5, epsilon = EPSILON,
524 max_relative = 1e-3
525 );
526 }
527
528 #[test]
530 fn test_angle_wrapping() {
531 let config = p_only_config(1.0, 5.0);
533 let mut controller = YawController::default();
534
535 let dt = 0.01;
536
537 let desired_yaw = 175.0 * PI / 180.0;
540 let estimated_yaw = -175.0 * PI / 180.0;
541
542 let rate = controller.control(&config, dt, desired_yaw, estimated_yaw);
543 let expected_rate = -10.0 * PI / 180.0; assert_relative_eq!(rate, expected_rate, epsilon = EPSILON, max_relative = 1e-3);
545
546 let desired_yaw = -175.0 * PI / 180.0;
549 let estimated_yaw = 175.0 * PI / 180.0;
550
551 let rate = controller.control(&config, dt, desired_yaw, estimated_yaw);
552 let expected_rate = 10.0 * PI / 180.0; assert_relative_eq!(rate, expected_rate, epsilon = EPSILON, max_relative = 1e-3);
554 }
555
556 #[test]
558 fn angle_diff_is_the_shortest_signed_path() {
559 let deg = PI / 180.0;
560 let cases = [
562 (0.5, 0.0, 0.5), (PI + 0.1, -PI - 0.1, 0.2), (350.0 * deg, 10.0 * deg, -20.0 * deg), (10.0 * deg, 350.0 * deg, 20.0 * deg), (PI, -PI, 0.0), (0.0, 2.0 * PI, 0.0), (5.0, 5.0 - 2.0 * PI, 0.0), ];
570 for (desired, current, expected) in cases {
571 assert_relative_eq!(
572 angle_diff(desired, current),
573 expected,
574 epsilon = EPSILON,
575 max_relative = 1e-3
576 );
577 }
578 }
579
580 #[test]
581 fn test_valid_inputs() {
582 let config = YawControllerConfig::default();
583 let mut controller = YawController::default();
584 let dt = 0.01;
585
586 let _ = controller.control(&config, dt, 0.0, 0.0);
588 let _ = controller.control(&config, dt, PI, -PI);
589 let _ = controller.control(&config, dt, 2.0 * PI, 0.0);
590 }
591
592 #[test]
593 #[should_panic(expected = "dt must be positive and finite")]
594 fn zero_dt_panics() {
595 let config = YawControllerConfig::default();
596 let mut controller = YawController::default();
597 let dt = 0.0;
598 let _ = controller.control(&config, dt, 0.0, 0.0);
599 }
600
601 #[test]
602 fn any_finite_target_is_equivalent_to_its_wrapped_heading() {
603 let cases = [
605 (3.0 * PI, -PI),
606 (10.0 * PI + 0.5, 0.5),
607 (-7.0 * PI / 2.0, PI / 2.0),
608 (1000.0, wrap_angle(1000.0)),
609 ];
610 let config = YawControllerConfig::default();
611 for (target, equivalent) in cases {
612 let rate = YawController::default().control(&config, 0.01, target, 0.2);
613 let expected = YawController::default().control(&config, 0.01, equivalent, 0.2);
614 assert_relative_eq!(rate, expected, epsilon = 1e-3);
615 }
616 }
617
618 #[test]
619 fn non_finite_target_holds_and_does_not_poison_later_ticks() {
620 let config = YawControllerConfig::default();
621 let mut controller = YawController::default();
622 for target in [f32::NAN, f32::INFINITY] {
623 assert!(controller.control(&config, 0.01, target, 0.2) == 0.0);
624 }
625 let after = controller.control(&config, 0.01, 1.0, 0.2);
626 let fresh = YawController::default().control(&config, 0.01, 1.0, 0.2);
627 assert_relative_eq!(after, fresh);
628 }
629
630 #[test]
631 fn nan_gap_does_not_cause_a_derivative_kick() {
632 let config = YawControllerConfig {
634 gains: Gains {
635 p: 1.0,
636 i: 0.0,
637 d: 0.5,
638 },
639 max_integral: 1.0,
640 max_rate_radps: 100.0,
641 };
642 let mut controller = YawController::default();
643
644 controller.control(&config, 0.01, 1.0, 0.2); controller.control(&config, 0.01, f32::NAN, 0.2); let after_gap = controller.control(&config, 0.01, 0.5, 0.2); let fresh = YawController::default().control(&config, 0.01, 0.5, 0.2);
650 assert_relative_eq!(after_gap, fresh);
651 }
652
653 #[test]
655 fn test_max_rate_limiting() {
656 let config = p_only_config(10.0, 2.0);
658 let mut controller = YawController::default();
659
660 let dt = 0.01;
661
662 let rate = controller.control(&config, dt, 0.5, 0.0);
665
666 assert_relative_eq!(rate, 2.0, epsilon = EPSILON, max_relative = 1e-3);
667
668 let rate = controller.control(&config, dt, -0.5, 0.0);
670 assert_relative_eq!(rate, -2.0, epsilon = EPSILON, max_relative = 1e-3);
671 }
672
673 #[test]
675 fn test_realistic_maneuvers() {
676 let config = YawControllerConfig {
678 gains: Gains {
679 p: 2.0,
680 i: 0.3,
681 d: 0.5,
682 },
683 max_integral: 5.0,
684 max_rate_radps: 3.0,
685 };
686
687 let mut controller = YawController::default();
688
689 let dt = 0.1; let desired_yaw = 90.0 * PI / 180.0;
693 let mut current_yaw = 0.0;
694 let mut rates = Vec::new();
695
696 for _ in 0..20 {
698 let rate = controller.control(&config, dt, desired_yaw, current_yaw);
699 rates.push(rate);
700
701 current_yaw += rate * dt;
703
704 if current_yaw > PI {
706 current_yaw -= 2.0 * PI;
707 } else if current_yaw < -PI {
708 current_yaw += 2.0 * PI;
709 }
710 }
711
712 assert!(
714 (desired_yaw - current_yaw).abs() < 0.1,
715 "Controller should converge to desired yaw (within 0.1 rad)"
716 );
717
718 assert!(
720 rates.last().unwrap().abs() < 0.2,
721 "Rate should approach zero as error decreases"
722 );
723
724 assert!(
728 rates[0] > 1.0,
729 "Initial rate should be high when error is large"
730 );
731
732 assert!(
734 rates[0] > rates[rates.len() / 2] && rates[rates.len() / 2] > rates[rates.len() - 1],
735 "Rates should decrease as controller approaches target"
736 );
737 }
738
739 #[test]
740 fn validate_rejects_negative_or_non_finite_limits() {
741 let cases = [(-1.0, 1.0, "max_integral"), (1.0, -1.0, "max_rate_radps")];
743 for (max_integral, max_rate_radps, field) in cases {
744 let config = YawControllerConfig {
745 max_integral,
746 max_rate_radps,
747 ..Default::default()
748 };
749 assert_eq!(config.validate().map_err(|e| e.field), Err(field));
750 }
751 }
752
753 #[test]
755 fn invalid_limits_give_neutral_output_without_panicking() {
756 for bad in [-1.0, f32::NAN] {
757 let config = YawControllerConfig {
758 gains: Gains {
759 p: 1.0,
760 i: 1.0,
761 d: 0.0,
762 },
763 max_integral: bad,
764 max_rate_radps: bad,
765 };
766 let mut controller = YawController::default();
767 assert_eq!(
768 controller.control(&config, 0.1, 1.0, 0.0),
769 0.0,
770 "limit {bad}"
771 );
772 assert_eq!(controller.integrated_error, 0.0, "limit {bad}");
773 }
774 }
775
776 #[test]
778 fn gain_change_between_ticks_keeps_state() {
779 let mut config = i_only_config(1.0, 10.0, 10.0);
780 let mut controller = YawController::default();
781 controller.control(&config, 1.0, 0.5, 0.0);
782 let integral = controller.integrated_error;
783
784 config.gains.i = 2.0;
785 let rate = controller.control(&config, 0.01, 0.0, 0.0);
786
787 assert_eq!(controller.integrated_error, integral);
788 assert_relative_eq!(rate, 2.0 * integral, epsilon = EPSILON);
789 }
790}