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 assert_dt(dt);
133
134 let gains = &config.gains;
135
136 let error = angle_diff(desired_yaw_rad, estimated_yaw_rad); if !error.is_finite() {
138 self.derivative.forget();
142 return 0.0;
143 }
144 let error_derivative = self.derivative.step(error, dt);
145
146 self.integrated_error =
147 clamp_symmetric(self.integrated_error + dt * error, config.max_integral);
148
149 let yaw_rate =
150 gains.p * error + gains.i * self.integrated_error + gains.d * error_derivative;
151
152 clamp_symmetric(yaw_rate, config.max_rate_radps)
153 }
154
155 pub(crate) fn rescale_integral(
158 &mut self,
159 old: &YawControllerConfig,
160 new: &YawControllerConfig,
161 ) {
162 self.integrated_error = rescale_integral(self.integrated_error, old.gains.i, new.gains.i);
163 }
164}
165
166fn angle_diff(desired_yaw: f32, current_yaw: f32) -> f32 {
177 wrap_angle(desired_yaw - current_yaw)
178}
179
180#[cfg(test)]
181mod tests {
182
183 use std::f32::consts::PI;
184
185 use approx::assert_relative_eq;
186
187 use crate::controllers::Gains;
188
189 use super::*;
190
191 const EPSILON: f32 = 1e-5;
192
193 fn p_only_config(p_gain: f32, max_rate_radps: f32) -> YawControllerConfig {
197 YawControllerConfig {
198 gains: Gains {
199 p: p_gain,
200 i: 0.0,
201 d: 0.0,
202 },
203 max_integral: 10.0,
204 max_rate_radps,
205 }
206 }
207
208 fn i_only_config(i_gain: f32, max_integral: f32, max_rate_radps: f32) -> YawControllerConfig {
210 YawControllerConfig {
211 gains: Gains {
212 p: 0.0,
213 i: i_gain,
214 d: 0.0,
215 },
216 max_integral,
217 max_rate_radps,
218 }
219 }
220
221 fn d_only_config(d_gain: f32, max_rate_radps: f32) -> YawControllerConfig {
223 YawControllerConfig {
224 gains: Gains {
225 p: 0.0,
226 i: 0.0,
227 d: d_gain,
228 },
229 max_integral: 10.0,
230 max_rate_radps,
231 }
232 }
233
234 #[test]
236 fn test_default_config() {
237 let config = YawControllerConfig::default();
238
239 assert_relative_eq!(config.gains.p, 2.5, epsilon = EPSILON, max_relative = 1e-3);
240 assert_relative_eq!(config.gains.i, 0.0, epsilon = EPSILON, max_relative = 1e-3);
241 assert_relative_eq!(config.gains.d, 0.06, epsilon = EPSILON, max_relative = 1e-3);
242 assert_relative_eq!(
243 config.max_integral,
244 100.0,
245 epsilon = EPSILON,
246 max_relative = 1e-3
247 );
248 assert_relative_eq!(
249 config.max_rate_radps,
250 2.0,
251 epsilon = EPSILON,
252 max_relative = 1e-3
253 );
254 }
255
256 #[test]
257 fn default_is_zeroed_state() {
258 assert_eq!(
259 YawController::default(),
260 YawController {
261 derivative: DerivativeState::default(),
262 integrated_error: 0.0,
263 }
264 );
265 }
266
267 #[test]
269 fn test_proportional_control() {
270 let config = p_only_config(2.0, 5.0);
272 let mut controller = YawController::default();
273
274 let dt = 0.01;
276 let desired_yaw = 0.5;
277 let estimated_yaw = 0.0;
278
279 let rate = controller.control(&config, dt, desired_yaw, estimated_yaw);
280 assert_relative_eq!(
281 rate,
282 1.0,
283 epsilon = EPSILON,
284 max_relative = 1e-3,
285 max_relative = 1e-3
286 );
287
288 let rate = controller.control(&config, dt, 0.0, 0.5);
290 assert_relative_eq!(rate, -1.0, epsilon = EPSILON, max_relative = 1e-3);
291 }
292
293 #[test]
295 fn test_integral_control() {
296 let config = i_only_config(1.0, 10.0, 5.0);
298 let mut controller = YawController::default();
299
300 let dt = 1.0; let desired_yaw = 0.5;
303 let estimated_yaw = 0.0;
304
305 let rate = controller.control(&config, dt, desired_yaw, estimated_yaw);
307 assert_relative_eq!(rate, 0.5, epsilon = EPSILON, max_relative = 1e-3);
308
309 let rate = controller.control(&config, dt, desired_yaw, estimated_yaw);
311 assert_relative_eq!(rate, 1.0, epsilon = EPSILON, max_relative = 1e-3);
312
313 let config = i_only_config(1.0, 1.5, 5.0);
315 let mut controller = YawController::default();
316
317 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);
322
323 assert_relative_eq!(rate, 1.5, epsilon = EPSILON, max_relative = 1e-3);
325 }
326
327 #[test]
329 fn test_derivative_control() {
330 let config = d_only_config(2.0, 5.0);
332 let mut controller = YawController::default();
333
334 let dt = 1.0; let rate = controller.control(&config, dt, 0.5, 0.0);
338 assert_relative_eq!(
339 rate,
340 0.0,
341 epsilon = EPSILON,
342 max_relative = 1e-3,
343 max_relative = 1e-3
344 );
345
346 let rate = controller.control(&config, dt, 0.5, 0.0);
348 assert_relative_eq!(rate, 0.0, epsilon = EPSILON, max_relative = 1e-3);
349
350 let rate = controller.control(&config, dt, 0.6, 0.0);
352 assert_relative_eq!(rate, 0.2, epsilon = EPSILON, max_relative = 1e-3);
353
354 let rate = controller.control(&config, dt, 0.4, 0.0);
356 assert_relative_eq!(rate, -0.4, epsilon = EPSILON, max_relative = 1e-3);
357 }
358
359 #[test]
361 fn test_pid_combined() {
362 let config = YawControllerConfig {
364 gains: Gains {
365 p: 1.0,
366 i: 0.5,
367 d: 0.2,
368 },
369 max_integral: 10.0,
370 max_rate_radps: 5.0,
371 };
372
373 let mut controller = YawController::default();
374
375 let dt = 1.0;
376
377 let rate = controller.control(&config, dt, 0.5, 0.0);
383 assert_relative_eq!(rate, 0.75, epsilon = EPSILON, max_relative = 1e-3);
384
385 let rate = controller.control(&config, dt, 0.5, 0.0);
391 assert_relative_eq!(rate, 1.0, epsilon = EPSILON, max_relative = 1e-3);
392
393 let rate = controller.control(&config, dt, 0.6, 0.0);
399 assert_relative_eq!(rate, 1.42, epsilon = EPSILON, max_relative = 1e-3);
400 }
401
402 #[test]
404 fn test_varying_time_steps() {
405 let config = YawControllerConfig {
406 gains: Gains {
407 p: 0.0,
408 i: 1.0,
409 d: 0.0,
410 },
411 max_integral: 10.0,
412 max_rate_radps: 5.0,
413 };
414
415 let mut controller = YawController::default();
416
417 let rate_1s = controller.control(&config, 1.0, 0.5, 0.0);
419 assert_relative_eq!(rate_1s, 0.5, epsilon = EPSILON, max_relative = 1e-3);
420
421 let rate_05s = controller.control(&config, 0.5, 0.5, 0.0);
423 assert_relative_eq!(
424 rate_05s,
425 0.5 + 0.25, epsilon = EPSILON,
427 max_relative = 1e-3
428 );
429
430 let rate_025s = controller.control(&config, 0.25, 0.5, 0.0);
432 assert_relative_eq!(
433 rate_025s,
434 0.5 + 0.25 + 0.125, epsilon = EPSILON,
436 max_relative = 1e-3
437 );
438 }
439
440 #[test]
442 fn test_small_time_steps() {
443 let config = YawControllerConfig {
444 gains: Gains {
445 p: 1.0,
446 i: 1.0,
447 d: 1.0,
448 },
449 max_integral: 10.0,
450 max_rate_radps: 5.0,
451 };
452
453 let mut controller = YawController::default();
454
455 let dt = 0.000001; let error = 0.5;
458 let rate = controller.control(&config, dt, error, 0.0);
459
460 assert_relative_eq!(
465 rate,
466 0.5, epsilon = EPSILON,
468 max_relative = 1e-3
469 );
470
471 let rate = controller.control(&config, dt, error, 0.0);
473 assert_relative_eq!(
474 rate,
475 0.5, epsilon = EPSILON,
477 max_relative = 1e-3
478 );
479 }
480
481 #[test]
483 fn test_angle_wrapping() {
484 let config = p_only_config(1.0, 5.0);
486 let mut controller = YawController::default();
487
488 let dt = 0.01;
489
490 let desired_yaw = 175.0 * PI / 180.0;
493 let estimated_yaw = -175.0 * PI / 180.0;
494
495 let rate = controller.control(&config, dt, desired_yaw, estimated_yaw);
496 let expected_rate = -10.0 * PI / 180.0; assert_relative_eq!(rate, expected_rate, epsilon = EPSILON, max_relative = 1e-3);
498
499 let desired_yaw = -175.0 * PI / 180.0;
502 let estimated_yaw = 175.0 * PI / 180.0;
503
504 let rate = controller.control(&config, dt, desired_yaw, estimated_yaw);
505 let expected_rate = 10.0 * PI / 180.0; assert_relative_eq!(rate, expected_rate, epsilon = EPSILON, max_relative = 1e-3);
507 }
508
509 #[test]
511 fn angle_diff_is_the_shortest_signed_path() {
512 let deg = PI / 180.0;
513 let cases = [
515 (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), ];
523 for (desired, current, expected) in cases {
524 assert_relative_eq!(
525 angle_diff(desired, current),
526 expected,
527 epsilon = EPSILON,
528 max_relative = 1e-3
529 );
530 }
531 }
532
533 #[test]
534 fn test_valid_inputs() {
535 let config = YawControllerConfig::default();
536 let mut controller = YawController::default();
537 let dt = 0.01;
538
539 let _ = controller.control(&config, dt, 0.0, 0.0);
541 let _ = controller.control(&config, dt, PI, -PI);
542 let _ = controller.control(&config, dt, 2.0 * PI, 0.0);
543 }
544
545 #[test]
546 #[should_panic(expected = "dt must be positive and finite")]
547 fn zero_dt_panics() {
548 let config = YawControllerConfig::default();
549 let mut controller = YawController::default();
550 let dt = 0.0;
551 let _ = controller.control(&config, dt, 0.0, 0.0);
552 }
553
554 #[test]
555 fn any_finite_target_is_equivalent_to_its_wrapped_heading() {
556 let cases = [
558 (3.0 * PI, -PI),
559 (10.0 * PI + 0.5, 0.5),
560 (-7.0 * PI / 2.0, PI / 2.0),
561 (1000.0, wrap_angle(1000.0)),
562 ];
563 let config = YawControllerConfig::default();
564 for (target, equivalent) in cases {
565 let rate = YawController::default().control(&config, 0.01, target, 0.2);
566 let expected = YawController::default().control(&config, 0.01, equivalent, 0.2);
567 assert_relative_eq!(rate, expected, epsilon = 1e-3);
568 }
569 }
570
571 #[test]
572 fn non_finite_target_holds_and_does_not_poison_later_ticks() {
573 let config = YawControllerConfig::default();
574 let mut controller = YawController::default();
575 for target in [f32::NAN, f32::INFINITY] {
576 assert!(controller.control(&config, 0.01, target, 0.2) == 0.0);
577 }
578 let after = controller.control(&config, 0.01, 1.0, 0.2);
579 let fresh = YawController::default().control(&config, 0.01, 1.0, 0.2);
580 assert_relative_eq!(after, fresh);
581 }
582
583 #[test]
584 fn nan_gap_does_not_cause_a_derivative_kick() {
585 let config = YawControllerConfig {
587 gains: Gains {
588 p: 1.0,
589 i: 0.0,
590 d: 0.5,
591 },
592 max_integral: 1.0,
593 max_rate_radps: 100.0,
594 };
595 let mut controller = YawController::default();
596
597 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);
603 assert_relative_eq!(after_gap, fresh);
604 }
605
606 #[test]
608 fn test_max_rate_limiting() {
609 let config = p_only_config(10.0, 2.0);
611 let mut controller = YawController::default();
612
613 let dt = 0.01;
614
615 let rate = controller.control(&config, dt, 0.5, 0.0);
618
619 assert_relative_eq!(rate, 2.0, epsilon = EPSILON, max_relative = 1e-3);
620
621 let rate = controller.control(&config, dt, -0.5, 0.0);
623 assert_relative_eq!(rate, -2.0, epsilon = EPSILON, max_relative = 1e-3);
624 }
625
626 #[test]
628 fn test_realistic_maneuvers() {
629 let config = YawControllerConfig {
631 gains: Gains {
632 p: 2.0,
633 i: 0.3,
634 d: 0.5,
635 },
636 max_integral: 5.0,
637 max_rate_radps: 3.0,
638 };
639
640 let mut controller = YawController::default();
641
642 let dt = 0.1; let desired_yaw = 90.0 * PI / 180.0;
646 let mut current_yaw = 0.0;
647 let mut rates = Vec::new();
648
649 for _ in 0..20 {
651 let rate = controller.control(&config, dt, desired_yaw, current_yaw);
652 rates.push(rate);
653
654 current_yaw += rate * dt;
656
657 if current_yaw > PI {
659 current_yaw -= 2.0 * PI;
660 } else if current_yaw < -PI {
661 current_yaw += 2.0 * PI;
662 }
663 }
664
665 assert!(
667 (desired_yaw - current_yaw).abs() < 0.1,
668 "Controller should converge to desired yaw (within 0.1 rad)"
669 );
670
671 assert!(
673 rates.last().unwrap().abs() < 0.2,
674 "Rate should approach zero as error decreases"
675 );
676
677 assert!(
681 rates[0] > 1.0,
682 "Initial rate should be high when error is large"
683 );
684
685 assert!(
687 rates[0] > rates[rates.len() / 2] && rates[rates.len() / 2] > rates[rates.len() - 1],
688 "Rates should decrease as controller approaches target"
689 );
690 }
691
692 #[test]
693 fn validate_rejects_negative_or_non_finite_limits() {
694 let cases = [(-1.0, 1.0, "max_integral"), (1.0, -1.0, "max_rate_radps")];
696 for (max_integral, max_rate_radps, field) in cases {
697 let config = YawControllerConfig {
698 max_integral,
699 max_rate_radps,
700 ..Default::default()
701 };
702 assert_eq!(config.validate().map_err(|e| e.field), Err(field));
703 }
704 }
705
706 #[test]
708 fn invalid_limits_give_neutral_output_without_panicking() {
709 for bad in [-1.0, f32::NAN] {
710 let config = YawControllerConfig {
711 gains: Gains {
712 p: 1.0,
713 i: 1.0,
714 d: 0.0,
715 },
716 max_integral: bad,
717 max_rate_radps: bad,
718 };
719 let mut controller = YawController::default();
720 assert_eq!(
721 controller.control(&config, 0.1, 1.0, 0.0),
722 0.0,
723 "limit {bad}"
724 );
725 assert_eq!(controller.integrated_error, 0.0, "limit {bad}");
726 }
727 }
728
729 #[test]
731 fn gain_change_between_ticks_keeps_state() {
732 let mut config = i_only_config(1.0, 10.0, 10.0);
733 let mut controller = YawController::default();
734 controller.control(&config, 1.0, 0.5, 0.0);
735 let integral = controller.integrated_error;
736
737 config.gains.i = 2.0;
738 let rate = controller.control(&config, 0.01, 0.0, 0.0);
739
740 assert_eq!(controller.integrated_error, integral);
741 assert_relative_eq!(rate, 2.0 * integral, epsilon = EPSILON);
742 }
743}