stable-flight 0.2.0

no_std autopilot core for Stable Flight: copter, plane, tailsitter, and quadplane guidance and stabilizers, waypoint navigation, failsafe, autotune, mixers, and the per-tick flight loop
Documentation
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
use crate::{CruiseOutput, Vec3};

use super::*;

/// Helper function for approximate equality of f32 values.
fn approx_eq(a: f32, b: f32) -> bool {
    (a - b).abs() < 1e-5
}

/// The quad X preset: the rows below are the former `QuadCopterMixer`'s tests, unchanged, so
/// the preset is checked to mix the same.
fn create_mixer() -> TableMixer<4> {
    TableMixer::quad_x()
}

/// Test 1: Hover (no moments, thrust only)
///
/// With full thrust (1.0) and no moments, the baseline thrust is scaled by `THRUST_FACTOR` (0.9)
/// and equally distributed among the four motors. Thus, each motor should receive:
///
///     baseline = 1.0 * 0.9 = 0.9
#[test]
fn test_hover_no_moments() {
    let mut mixer = create_mixer();
    let input = ControlOutput::new(1.0, Vec3::zeros());
    let output = mixer.mix(&input);

    let expected = 0.9;
    for i in 0..4 {
        assert!(
            approx_eq(output.motors[i], expected),
            "Motor {} expected {} but got {}",
            i,
            expected,
            output.motors[i]
        );
    }
}

/// Test 2: Pure Roll (max roll, no thrust/pitch/yaw)
///
/// With no thrust and maximum positive roll (1.0):
/// - Roll contribution = 1.0 * `roll_pitch_scaling` = 1.0 * 0.1 = 0.1
///
/// Motor outputs:
/// - Motor 0 (Front Left): 0 + 0 + 0 + 0.1 = 0.1
/// - Motor 1 (Front Right): 0 + 0 + 0 - 0.1 = -0.1 -> clamped to 0.0
/// - Motor 2 (Rear Left): 0 + 0 + 0 + 0.1 = 0.1
/// - Motor 3 (Rear Right): 0 + 0 + 0 - 0.1 = -0.1 -> clamped to 0.0
#[test]
fn test_pure_roll() {
    let mut mixer = create_mixer();
    let input = ControlOutput::new(0.0, Vec3::new(1.0, 0.0, 0.0));
    let output = mixer.mix(&input);

    assert!(
        approx_eq(output.motors[0], 0.1),
        "Motor 0 expected 0.1 but got {}",
        output.motors[0]
    ); // Front Left
    assert!(
        approx_eq(output.motors[1], 0.0),
        "Motor 1 expected 0.0 but got {}",
        output.motors[1]
    ); // Front Right
    assert!(
        approx_eq(output.motors[2], 0.1),
        "Motor 2 expected 0.1 but got {}",
        output.motors[2]
    ); // Rear Left
    assert!(
        approx_eq(output.motors[3], 0.0),
        "Motor 3 expected 0.0 but got {}",
        output.motors[3]
    ); // Rear Right
}

/// Test 3: Pure Pitch (max pitch, no thrust/roll/yaw)
///
/// With no thrust and maximum positive pitch (1.0):
/// - Pitch contribution = 1.0 * `roll_pitch_scaling` = 1.0 * 0.1 = 0.1
///
/// Motor outputs:
/// - Motor 0 (Front Left): 0 + 0 + 0.1 + 0 = 0.1
/// - Motor 1 (Front Right): 0 + 0 + 0.1 + 0 = 0.1
/// - Motor 2 (Rear Left): 0 + 0 - 0.1 + 0 = -0.1 -> clamped to 0.0
/// - Motor 3 (Rear Right): 0 + 0 - 0.1 + 0 = -0.1 -> clamped to 0.0
#[test]
fn test_pure_pitch() {
    let mut mixer = create_mixer();
    let input = ControlOutput::new(0.0, Vec3::new(0.0, 1.0, 0.0));
    let output = mixer.mix(&input);

    assert!(
        approx_eq(output.motors[0], 0.1),
        "Motor 0 expected 0.1 but got {}",
        output.motors[0]
    ); // Front Left
    assert!(
        approx_eq(output.motors[1], 0.1),
        "Motor 1 expected 0.1 but got {}",
        output.motors[1]
    ); // Front Right
    assert!(
        approx_eq(output.motors[2], 0.0),
        "Motor 2 expected 0.0 but got {}",
        output.motors[2]
    ); // Rear Left
    assert!(
        approx_eq(output.motors[3], 0.0),
        "Motor 3 expected 0.0 but got {}",
        output.motors[3]
    ); // Rear Right
}

/// Test 4: Pure Yaw (max yaw, no thrust/roll/pitch)
///
/// With no thrust and maximum positive yaw (1.0), nose right:
/// - Yaw contribution = 1.0 * `yaw_scaling` = 1.0 * 0.25 = 0.25
///
/// Motor outputs (the CCW props speed up):
/// - Motor 0 (Front Left, CW): 0 - 0.25 = -0.25 -> clamped to 0.0
/// - Motor 1 (Front Right, CCW): 0 + 0.25 = 0.25
/// - Motor 2 (Rear Left, CCW): 0 + 0.25 = 0.25
/// - Motor 3 (Rear Right, CW): 0 - 0.25 = -0.25 -> clamped to 0.0
#[test]
fn test_pure_yaw() {
    let mut mixer = create_mixer();
    let input = ControlOutput::new(0.0, Vec3::new(0.0, 0.0, 1.0));
    let output = mixer.mix(&input);

    assert!(
        approx_eq(output.motors[0], 0.0),
        "Motor 0 expected 0.0 but got {}",
        output.motors[0]
    ); // Front Left (CW)
    assert!(
        approx_eq(output.motors[1], 0.25),
        "Motor 1 expected 0.25 but got {}",
        output.motors[1]
    ); // Front Right (CCW)
    assert!(
        approx_eq(output.motors[2], 0.25),
        "Motor 2 expected 0.25 but got {}",
        output.motors[2]
    ); // Rear Left (CCW)
    assert!(
        approx_eq(output.motors[3], 0.0),
        "Motor 3 expected 0.0 but got {}",
        output.motors[3]
    ); // Rear Right (CW)
}

/// Test 5: Max Inputs (max thrust and max moments)
///
/// With maximum thrust (1.0) and maximum moments (roll = 1.0, pitch = 1.0, yaw = 1.0):
/// - Baseline thrust = 1.0 * `THRUST_FACTOR` = 0.9
/// - Roll = 1.0 * `roll_pitch_scaling` = 0.1
/// - Pitch = 1.0 * `roll_pitch_scaling` = 0.1
/// - Yaw = 1.0 * `yaw_scaling` = 0.25
///
/// Raw outputs 0.85, 1.15, 1.15, 0.45 exceed full throttle by 0.15, so all four drop by 0.15,
/// keeping the moments and giving up thrust:
/// - Motor 0: 0.85 - 0.15 = 0.70
/// - Motor 1: 1.15 - 0.15 = 1.0
/// - Motor 2: 1.15 - 0.15 = 1.0
/// - Motor 3: 0.45 - 0.15 = 0.30
#[test]
fn test_max_inputs() {
    let mut mixer = create_mixer();
    let input = ControlOutput::new(1.0, Vec3::new(1.0, 1.0, 1.0));
    let output = mixer.mix(&input);

    assert!(
        approx_eq(output.motors[0], 0.7),
        "Motor 0 expected 0.7 but got {}",
        output.motors[0]
    ); // Front Left (CW)
    assert!(
        approx_eq(output.motors[1], 1.0),
        "Motor 1 expected 1.0 but got {}",
        output.motors[1]
    ); // Front Right (CCW)
    assert!(
        approx_eq(output.motors[2], 1.0),
        "Motor 2 expected 1.0 but got {}",
        output.motors[2]
    ); // Rear Left (CCW)
    assert!(
        approx_eq(output.motors[3], 0.3),
        "Motor 3 expected 0.3 but got {}",
        output.motors[3]
    ); // Rear Right (CW)
}

/// Test 6: Zero Inputs (zero thrust and zero moments)
///
/// With zero thrust and zero moments, all motor outputs should be zero.
#[test]
fn test_zero_inputs() {
    let mut mixer = create_mixer();
    let input = ControlOutput::new(0.0, Vec3::zeros());
    let output = mixer.mix(&input);

    let expected = 0.0;
    for motor in output.motors.iter() {
        assert!(
            approx_eq(*motor, expected),
            "Expected {} but got {}",
            expected,
            motor
        );
    }
}

/// Test 7: Negative Moments (max negative moments, no thrust)
///
/// With no thrust and maximum negative moments (roll = -1.0, pitch = -1.0, yaw = -1.0):
/// - Roll = -1.0 * `roll_pitch_scaling` = -0.1
/// - Pitch = -1.0 * `roll_pitch_scaling` = -0.1
/// - Yaw = -1.0 * `yaw_scaling` = -0.25
///
/// Motor outputs:
/// - Motor 0: 0 - (-0.25) + (-0.1) + (-0.1) = 0.05
/// - Motor 1: 0 + (-0.25) + (-0.1) - (-0.1) = -0.25 -> clamped to 0.0
/// - Motor 2: 0 + (-0.25) - (-0.1) + (-0.1) = -0.25 -> clamped to 0.0
/// - Motor 3: 0 - (-0.25) - (-0.1) - (-0.1) = 0.45
#[test]
fn test_negative_outputs() {
    let mut mixer = create_mixer();
    let input = ControlOutput::new(0.0, Vec3::new(-1.0, -1.0, -1.0));
    let output = mixer.mix(&input);

    assert!(
        approx_eq(output.motors[0], 0.05),
        "Motor 0 expected 0.05 but got {}",
        output.motors[0]
    ); // Front Left (CW)
    assert!(
        approx_eq(output.motors[1], 0.0),
        "Motor 1 expected 0.0 but got {}",
        output.motors[1]
    ); // Front Right (CCW)
    assert!(
        approx_eq(output.motors[2], 0.0),
        "Motor 2 expected 0.0 but got {}",
        output.motors[2]
    ); // Rear Left (CCW)
    assert!(
        approx_eq(output.motors[3], 0.45),
        "Motor 3 expected 0.45 but got {}",
        output.motors[3]
    ); // Rear Right (CW)
}

/// Test 8: Partial Thrust (intermediate thrust, no moments)
///
/// With thrust = 0.5 and no moments:
/// - Baseline thrust = 0.5 * `THRUST_FACTOR` = 0.5 * 0.9 = 0.45
///
/// Motor outputs:
/// - All motors: 0.45
#[test]
fn test_partial_thrust() {
    let mut mixer = create_mixer();
    let input = ControlOutput::new(0.5, Vec3::zeros());
    let output = mixer.mix(&input);

    let expected = 0.45;
    for i in 0..4 {
        assert!(
            approx_eq(output.motors[i], expected),
            "Motor {} expected {} but got {}",
            i,
            expected,
            output.motors[i]
        );
    }
}

/// Test 9: Mixed Moments (partial mixed moments, no thrust)
///
/// With no thrust and moments (roll = 0.5, pitch = -0.5, yaw = 0.0):
/// - Roll = 0.5 * `roll_pitch_scaling` = 0.5 * 0.1 = 0.05
/// - Pitch = -0.5 * `roll_pitch_scaling` = -0.5 * 0.1 = -0.05
/// - Yaw = 0.0 * `yaw_scaling` = 0.0
///
/// Motor outputs:
/// - Motor 0: 0 + 0 + (-0.05) + 0.05 = 0.0
/// - Motor 1: 0 + 0 + (-0.05) - 0.05 = -0.1 -> clamped to 0.0
/// - Motor 2: 0 + 0 - (-0.05) + 0.05 = 0.1
/// - Motor 3: 0 + 0 - (-0.05) - 0.05 = 0.0
#[test]
fn test_mixed_moments() {
    let mut mixer = create_mixer();
    let input = ControlOutput::new(0.0, Vec3::new(0.5, -0.5, 0.0));
    let output = mixer.mix(&input);

    assert!(
        approx_eq(output.motors[0], 0.0),
        "Motor 0 expected 0.0 but got {}",
        output.motors[0]
    ); // Front Left
    assert!(
        approx_eq(output.motors[1], 0.0),
        "Motor 1 expected 0.0 but got {}",
        output.motors[1]
    ); // Front Right
    assert!(
        approx_eq(output.motors[2], 0.1),
        "Motor 2 expected 0.1 but got {}",
        output.motors[2]
    ); // Rear Left
    assert!(
        approx_eq(output.motors[3], 0.0),
        "Motor 3 expected 0.0 but got {}",
        output.motors[3]
    ); // Rear Right
}

/// Test 10: Boundary Condition (max thrust with mixed moments)
///
/// With thrust = 1.0 and moments (roll = -1.0, pitch = 1.0, yaw = 0.0):
/// - Baseline = 1.0 * `THRUST_FACTOR` = 0.9
/// - Roll = -1.0 * `roll_pitch_scaling` = -0.1
/// - Pitch = 1.0 * `roll_pitch_scaling` = 0.1
/// - Yaw = 0.0 * `yaw_scaling` = 0.0
///
/// Raw outputs 0.9, 1.1, 0.7, 0.9 exceed full throttle by 0.1, so all four drop by 0.1:
/// - Motor 0: 0.8
/// - Motor 1: 1.0
/// - Motor 2: 0.6
/// - Motor 3: 0.8
#[test]
fn test_boundary_condition() {
    let mut mixer = create_mixer();
    let input = ControlOutput::new(1.0, Vec3::new(-1.0, 1.0, 0.0));
    let output = mixer.mix(&input);

    assert!(
        approx_eq(output.motors[0], 0.8),
        "Motor 0 expected 0.8 but got {}",
        output.motors[0]
    ); // Front Left
    assert!(
        approx_eq(output.motors[1], 1.0),
        "Motor 1 expected 1.0 but got {}",
        output.motors[1]
    ); // Front Right
    assert!(
        approx_eq(output.motors[2], 0.6),
        "Motor 2 expected 0.6 but got {}",
        output.motors[2]
    ); // Rear Left
    assert!(
        approx_eq(output.motors[3], 0.8),
        "Motor 3 expected 0.8 but got {}",
        output.motors[3]
    ); // Rear Right
}

/// Out-of-range or NaN inputs saturate instead of panicking the loop: each row mixes the same
/// as its in-range equivalent.
#[test]
fn out_of_range_or_nan_inputs_saturate_instead_of_panicking() {
    // (input thrust, input moments, equivalent thrust, equivalent moments)
    let cases = [
        (-0.1, Vec3::zeros(), 0.0, Vec3::zeros()),
        (1.5, Vec3::zeros(), 1.0, Vec3::zeros()),
        (0.5, Vec3::new(1.5, 0.0, 0.0), 0.5, Vec3::new(1.0, 0.0, 0.0)),
        (
            0.5,
            Vec3::new(0.0, -3.0, 2.0),
            0.5,
            Vec3::new(0.0, -1.0, 1.0),
        ),
        (
            f32::NAN,
            Vec3::new(0.2, 0.0, 0.0),
            0.0,
            Vec3::new(0.2, 0.0, 0.0),
        ),
        (
            0.5,
            Vec3::new(f32::NAN, 0.1, f32::NAN),
            0.5,
            Vec3::new(0.0, 0.1, 0.0),
        ),
    ];
    let mut mixer = create_mixer();
    for (thrust, moments, eq_thrust, eq_moments) in cases {
        let mixed = mixer.mix(&ControlOutput::new(thrust, moments));
        let expected = mixer.mix(&ControlOutput::new(eq_thrust, eq_moments));
        assert!(
            mixed.motors.iter().all(|m| m.is_finite()),
            "({thrust}, {moments:?}) gave non-finite motors {:?}",
            mixed.motors
        );
        assert!(
            mixed.motors == expected.motors,
            "({thrust}, {moments:?}) gave {:?}, expected {:?}",
            mixed.motors,
            expected.motors
        );
    }
}

/// At low throttle a large moment shifts all motors up rather than clipping the low side, so
/// the full differential survives; at zero thrust nothing is raised.
#[test]
fn low_throttle_moments_shift_up_instead_of_clipping() {
    let mut mixer = create_mixer();
    // (thrust, moments, expected motors)
    let cases = [
        // Roll 1 at thrust 0.1: raw [0.19, -0.01, 0.19, -0.01], up 0.01.
        (0.1, Vec3::new(1.0, 0.0, 0.0), [0.2, 0.0, 0.2, 0.0]),
        // Pitch -1 at thrust 0.05: raw [-0.055, -0.055, 0.145, 0.145], up 0.055.
        (0.05, Vec3::new(0.0, -1.0, 0.0), [0.0, 0.0, 0.2, 0.2]),
        // Yaw 1 at thrust 0.2: raw [-0.07, 0.43, 0.43, -0.07], up 0.07.
        (0.2, Vec3::new(0.0, 0.0, 1.0), [0.0, 0.5, 0.5, 0.0]),
        // Zero thrust: the low side clips, nothing rises.
        (0.0, Vec3::new(1.0, 0.0, 0.0), [0.1, 0.0, 0.1, 0.0]),
    ];
    for (thrust, moments, expected) in cases {
        let output = mixer.mix(&ControlOutput::new(thrust, moments));
        for (i, e) in expected.iter().enumerate() {
            assert!(
                approx_eq(output.motors[i], *e),
                "thrust {thrust}, moments {moments:?}: motor {i} {} vs {e}",
                output.motors[i]
            );
        }
    }
}

/// The mixer reports what it gave up on its last call: collective when it shifted down,
/// moments when it clipped (or scaled); a clean mix clears the flags.
#[test]
fn reports_what_the_last_mix_gave_up() {
    // (thrust, moments, mixer_thrust, mixer_moments)
    let cases = [
        (0.5, Vec3::zeros(), false, false),
        (0.5, Vec3::new(1.0, 1.0, 1.0), false, false), // fits as is
        (1.0, Vec3::new(1.0, 1.0, 1.0), true, false),  // collective gives way
        (0.1, Vec3::new(1.0, 0.0, 0.0), false, false), // shifted up: nothing lost
        (0.0, Vec3::new(1.0, 0.0, 0.0), false, true),  // zero thrust: clipped
    ];
    let mut mixer = create_mixer();
    for (thrust, moments, mixer_thrust, mixer_moments) in cases {
        mixer.mix(&ControlOutput::new(thrust, moments));
        let saturation = mixer.saturation();
        assert!(
            (saturation.mixer_thrust, saturation.mixer_moments) == (mixer_thrust, mixer_moments),
            "thrust {thrust}, moments {moments:?}: {saturation:?}"
        );
        assert!(!saturation.tilt && !saturation.thrust && saturation.moments == [false; 3]);
    }
}

/// Where each motor of a motor-only preset sits: azimuth from the nose (degrees, clockwise
/// seen from above) and spin (+1 counterclockwise, whose drag torque yaws the nose right).
type Geometry = &'static [(f32, f32)];

/// One mix through a fresh preset.
type MixFn = fn(&ControlOutput) -> ActuatorCommand;

const CW: f32 = -1.0;
const CCW: f32 = 1.0;

/// Motor-only presets with their geometry, written from the diagrams, not the tables.
const MOTOR_PRESETS: [(&str, MixFn, Geometry); 7] = [
    (
        "quad_x",
        |c| TableMixer::quad_x().mix(c),
        &[(-45.0, CW), (45.0, CCW), (-135.0, CCW), (135.0, CW)],
    ),
    (
        "quad_plus",
        |c| TableMixer::quad_plus().mix(c),
        &[(0.0, CW), (90.0, CCW), (180.0, CW), (-90.0, CCW)],
    ),
    (
        "hex_x",
        |c| TableMixer::hex_x().mix(c),
        &[
            (-30.0, CW),
            (30.0, CCW),
            (90.0, CW),
            (150.0, CCW),
            (-150.0, CW),
            (-90.0, CCW),
        ],
    ),
    (
        "hex_plus",
        |c| TableMixer::hex_plus().mix(c),
        &[
            (0.0, CW),
            (60.0, CCW),
            (120.0, CW),
            (180.0, CCW),
            (-120.0, CW),
            (-60.0, CCW),
        ],
    ),
    (
        "octo_x",
        |c| TableMixer::octo_x().mix(c),
        &[
            (-22.5, CW),
            (22.5, CCW),
            (67.5, CW),
            (112.5, CCW),
            (157.5, CW),
            (-157.5, CCW),
            (-112.5, CW),
            (-67.5, CCW),
        ],
    ),
    (
        "y6",
        |c| TableMixer::y6().mix(c),
        &[
            (-60.0, CW),
            (60.0, CW),
            (180.0, CW),
            (-60.0, CCW),
            (60.0, CCW),
            (180.0, CCW),
        ],
    ),
    (
        "x8",
        |c| TableMixer::x8().mix(c),
        &[
            (-45.0, CW),
            (45.0, CCW),
            (135.0, CW),
            (-135.0, CCW),
            (-45.0, CCW),
            (45.0, CW),
            (135.0, CCW),
            (-135.0, CW),
        ],
    ),
];

/// The body moments (roll, pitch, yaw) a change in motor outputs makes on `geometry`.
fn moments_from(geometry: Geometry, delta: &[f32]) -> Vec3 {
    geometry
        .iter()
        .zip(delta)
        .fold(Vec3::zeros(), |sum, (&(azimuth_deg, spin), &d)| {
            let azimuth_rad = azimuth_deg.to_radians();
            // A motor left of center (negative sine) rolls right as it speeds up; one ahead of
            // center (positive cosine) raises the nose.
            sum + Vec3::new(-azimuth_rad.sin(), azimuth_rad.cos(), spin) * d
        })
}

/// Each motor preset hovers with equal motors, and turns a pure roll, pitch, or yaw request
/// into that moment alone, with the frame-contract sign (roll right, nose up, nose right).
#[test]
fn motor_presets_make_the_requested_moment_only() {
    for (name, mix, geometry) in MOTOR_PRESETS {
        let n = geometry.len();
        let hover = mix(&ControlOutput::new(0.5, Vec3::zeros())).motors;
        assert!(
            hover[..n].iter().all(|&m| approx_eq(m, 0.45)) && hover[n..].iter().all(|&m| m == 0.0),
            "{name}: hover {hover:?}"
        );
        for axis in 0..3 {
            let mut request = Vec3::zeros();
            request[axis] = 0.5;
            let output = mix(&ControlOutput::new(0.5, request)).motors;
            let delta: [f32; 8] = core::array::from_fn(|i| output[i] - hover[i]);
            let made = moments_from(geometry, &delta[..n]);
            for other in 0..3 {
                if other == axis {
                    assert!(made[other] > 0.01, "{name}: axis {axis} made {made:?}");
                } else {
                    assert!(
                        made[other].abs() < 1e-5,
                        "{name}: axis {axis} made {made:?}"
                    );
                }
            }
        }
    }
}

/// Tricopter, bicopter, and tailsitter hover: servos carry the moments their rows name, motors the rest; servo
/// outputs don't depend on thrust.
#[test]
fn servo_presets_move_the_right_outputs() {
    // (preset, mix, thrust, moments, expected motors[..3], expected servos[..2])
    type Case = (&'static str, MixFn, f32, Vec3, [f32; 3], [f32; 2]);
    let tri: MixFn = |c| TableMixer::tri().mix(c);
    let bi: MixFn = |c| TableMixer::bi().mix(c);
    let tailsitter: MixFn = |c| TableMixer::tailsitter_2p().mix(c);
    let cases: [Case; 13] = [
        // Tail servo yaws; motors stay level.
        (
            "tri",
            tri,
            0.5,
            Vec3::new(0.0, 0.0, 0.3),
            [0.45, 0.45, 0.45],
            [0.3, 0.0],
        ),
        // Roll: front-left up, front-right down, tail motor unchanged.
        (
            "tri",
            tri,
            0.5,
            Vec3::new(0.5, 0.0, 0.0),
            [0.5, 0.4, 0.45],
            [0.0, 0.0],
        ),
        // Pitch up: front pair up half as much as the tail goes down.
        (
            "tri",
            tri,
            0.5,
            Vec3::new(0.0, 0.5, 0.0),
            [0.475, 0.475, 0.4],
            [0.0, 0.0],
        ),
        // Roll: differential thrust only.
        (
            "bi",
            bi,
            0.5,
            Vec3::new(0.5, 0.0, 0.0),
            [0.5, 0.4, 0.0],
            [0.0, 0.0],
        ),
        // Pitch up: both rotors tilt aft.
        (
            "bi",
            bi,
            0.5,
            Vec3::new(0.0, 0.4, 0.0),
            [0.45, 0.45, 0.0],
            [0.4, 0.4],
        ),
        // Nose right: left rotor forward, right rotor aft.
        (
            "bi",
            bi,
            0.5,
            Vec3::new(0.0, 0.0, 0.4),
            [0.45, 0.45, 0.0],
            [-0.4, 0.4],
        ),
        // Servos ignore thrust.
        (
            "bi",
            bi,
            0.0,
            Vec3::new(0.0, 0.4, 0.4),
            [0.0, 0.0, 0.0],
            [0.0, 0.8],
        ),
        (
            "bi",
            bi,
            1.0,
            Vec3::new(0.0, 0.4, 0.4),
            [0.9, 0.9, 0.0],
            [0.0, 0.8],
        ),
        // Tailsitter, airplane frame. Roll right: left trailing edge down, right up.
        (
            "tailsitter",
            tailsitter,
            0.5,
            Vec3::new(0.4, 0.0, 0.0),
            [0.45, 0.45, 0.0],
            [-0.4, 0.4],
        ),
        // Nose up: both trailing edges up.
        (
            "tailsitter",
            tailsitter,
            0.5,
            Vec3::new(0.0, 0.4, 0.0),
            [0.45, 0.45, 0.0],
            [0.4, 0.4],
        ),
        // Nose right: more left thrust (yaw ratio 2.5 on 0.1 headroom).
        (
            "tailsitter",
            tailsitter,
            0.5,
            Vec3::new(0.0, 0.0, 0.4),
            [0.55, 0.35, 0.0],
            [0.0, 0.0],
        ),
        // Elevons ignore thrust; at full thrust the motors give way to keep the yaw split.
        (
            "tailsitter",
            tailsitter,
            0.0,
            Vec3::new(0.2, 0.3, 0.0),
            [0.0, 0.0, 0.0],
            [0.1, 0.5],
        ),
        (
            "tailsitter",
            tailsitter,
            1.0,
            Vec3::new(0.0, 0.0, 1.0),
            [1.0, 0.5, 0.0],
            [0.0, 0.0],
        ),
    ];
    for (name, mix, thrust, moments, motors, servos) in cases {
        let output = mix(&ControlOutput::new(thrust, moments));
        for (i, &e) in motors.iter().enumerate() {
            assert!(
                approx_eq(output.motors[i], e),
                "{name} {thrust} {moments:?}: motors {:?}",
                output.motors
            );
        }
        for (i, &e) in servos.iter().enumerate() {
            assert!(
                approx_eq(output.servos[i], e),
                "{name} {thrust} {moments:?}: servos {:?}",
                output.servos
            );
        }
    }
}

/// Plane: each surface carries its one moment, the motor only thrust, up to `thrust_factor`.
#[test]
fn plane_preset_puts_each_moment_on_its_surface() {
    // (thrust, moments, expected motor 0, expected servos[..3])
    let cases = [
        (0.5, Vec3::zeros(), 0.45, [0.0, 0.0, 0.0]),
        (1.0, Vec3::zeros(), 0.9, [0.0, 0.0, 0.0]),
        (0.5, Vec3::new(0.3, 0.0, 0.0), 0.45, [0.3, 0.0, 0.0]),
        (0.5, Vec3::new(0.0, -0.4, 0.0), 0.45, [0.0, -0.4, 0.0]),
        (0.0, Vec3::new(0.0, 0.0, 0.2), 0.0, [0.0, 0.0, 0.2]),
        (0.7, Vec3::new(-0.1, 0.2, -0.3), 0.63, [-0.1, 0.2, -0.3]),
    ];
    for (thrust, moments, motor, servos) in cases {
        let mut mixer = TableMixer::plane();
        let output = mixer.mix(&ControlOutput::new(thrust, moments));
        let label = format!("{thrust} {moments:?}: {output:?}");
        assert!(approx_eq(output.motors[0], motor), "{label}");
        assert!(output.motors[1..] == [0.0; 7], "{label}");
        for (servo, expected) in output.servos.iter().zip(servos) {
            assert!(approx_eq(*servo, expected), "{label}");
        }
        assert!(mixer.saturation() == Saturation::default(), "{label}");
    }
}

/// The quadplane preset flies its lift rotors on `thrust` and `moments` exactly as `quad_x`
/// does, and its pusher and surfaces on `cruise`: lift rotors idle in cruise, surfaces neutral
/// and the pusher stopped in a hover, and the pusher kept out of the lift rotors'
/// desaturation.
#[test]
fn quadplane_preset_splits_lift_and_cruise() {
    let cruise = |thrust, moments| Some(CruiseOutput { thrust, moments });
    let saturation = |mixer_thrust, mixer_moments| Saturation {
        mixer_thrust,
        mixer_moments,
        ..Saturation::default()
    };
    // (case, control, expected pusher, expected servos[..3], saturation)
    let cases = [
        (
            "hover: surfaces neutral, pusher stopped",
            ControlOutput {
                cruise: cruise(0.0, Vec3::zeros()),
                ..ControlOutput::new(0.5, Vec3::new(0.2, -0.1, 0.1))
            },
            0.0,
            [0.0; 3],
            saturation(false, false),
        ),
        (
            "cruise: lift rotors idle",
            ControlOutput {
                cruise: cruise(0.6, Vec3::new(0.3, -0.2, 0.1)),
                ..ControlOutput::default()
            },
            0.6,
            [0.3, -0.2, 0.1],
            saturation(false, false),
        ),
        (
            "transition: both sides fly",
            ControlOutput {
                cruise: cruise(0.8, Vec3::new(0.0, 0.3, 0.0)),
                ..ControlOutput::new(0.5, Vec3::new(0.1, 0.0, 0.0))
            },
            0.8,
            [0.0, 0.3, 0.0],
            saturation(false, false),
        ),
        (
            "no cruise output: servos take the moments, pusher stopped",
            ControlOutput::new(0.5, Vec3::new(0.1, 0.0, -0.2)),
            0.0,
            [0.1, 0.0, -0.2],
            saturation(false, false),
        ),
        (
            "lift saturated: collective gives way, pusher doesn't",
            ControlOutput {
                cruise: cruise(1.0, Vec3::zeros()),
                ..ControlOutput::new(1.0, Vec3::new(1.0, 1.0, 0.0))
            },
            1.0,
            [0.0; 3],
            saturation(true, false),
        ),
        (
            "out-of-range or NaN cruise saturates",
            ControlOutput {
                cruise: cruise(f32::NAN, Vec3::new(2.0, f32::NAN, -3.0)),
                ..ControlOutput::default()
            },
            0.0,
            [1.0, 0.0, -1.0],
            saturation(false, false),
        ),
    ];
    for (case, control, pusher, servos, saturation) in cases {
        let mut mixer = TableMixer::quadplane();
        let output = mixer.mix(&control);
        let lift = TableMixer::quad_x()
            .mix(&ControlOutput::new(control.thrust, control.moments))
            .motors;
        assert!(output.motors[..4] == lift[..4], "{case}: {output:?}");
        assert!(approx_eq(output.motors[4], pusher), "{case}: {output:?}");
        assert!(output.motors[5..] == [0.0; 3], "{case}: {output:?}");
        for (servo, expected) in output.servos.iter().zip(servos) {
            assert!(approx_eq(*servo, expected), "{case}: {output:?}");
        }
        assert!(output.servos[3..] == [0.0; 5], "{case}: {output:?}");
        assert!(
            mixer.saturation() == saturation,
            "{case}: {:?}",
            mixer.saturation()
        );
    }
}

/// The V-tail quadplane flies its lift rotors and pusher as the quadplane preset does, and
/// mixes cruise pitch and yaw onto the two ruddervators: pitch moves both alike, yaw opposite
/// ways (left trailing edge up turns the nose right), and a ruddervator asked past its range
/// clamps on its own.
#[test]
fn quadplane_vtail_mixes_ruddervators() {
    let cruise = |moments| ControlOutput {
        cruise: Some(CruiseOutput {
            thrust: 0.5,
            moments,
        }),
        ..ControlOutput::new(0.4, Vec3::new(0.1, -0.2, 0.3))
    };
    // (case, cruise moments, expected servos[..3], mixer_moments)
    let cases = [
        ("neutral", Vec3::zeros(), [0.0, 0.0, 0.0], false),
        (
            "roll: ailerons only",
            Vec3::new(0.4, 0.0, 0.0),
            [0.4, 0.0, 0.0],
            false,
        ),
        (
            "pitch up: both up",
            Vec3::new(0.0, 0.3, 0.0),
            [0.0, 0.3, 0.3],
            false,
        ),
        (
            "yaw right: left up, right down",
            Vec3::new(0.0, 0.0, 0.3),
            [0.0, 0.3, -0.3],
            false,
        ),
        (
            "pitch and yaw",
            Vec3::new(0.0, 0.5, -0.2),
            [0.0, 0.3, 0.7],
            false,
        ),
        (
            "full pitch and yaw clamp",
            Vec3::new(0.0, 0.8, 0.6),
            [0.0, 1.0, 0.2],
            true,
        ),
    ];
    for (case, moments, servos, mixer_moments) in cases {
        let control = cruise(moments);
        let mut mixer = TableMixer::quadplane_vtail();
        let output = mixer.mix(&control);
        let reference = TableMixer::quadplane().mix(&control);
        assert!(output.motors == reference.motors, "{case}: {output:?}");
        for (servo, expected) in output.servos.iter().zip(servos) {
            assert!(approx_eq(*servo, expected), "{case}: {output:?}");
        }
        assert!(output.servos[3..] == [0.0; 5], "{case}: {output:?}");
        assert!(
            mixer.saturation().mixer_moments == mixer_moments,
            "{case}: {:?}",
            mixer.saturation()
        );
    }
}

/// A servo asked past its range clamps on its own and reports the lost moment; the other
/// servo and the motors are untouched.
#[test]
fn servo_clamp_reports_lost_moments() {
    let mut mixer = TableMixer::bi();
    let output = mixer.mix(&ControlOutput::new(0.5, Vec3::new(0.0, 1.0, 1.0)));
    assert!(output.servos[..2] == [0.0, 1.0]);
    assert!(output.motors[..2] == [0.45, 0.45]);
    assert!(mixer.saturation().mixer_moments);
    assert!(!mixer.saturation().mixer_thrust);

    mixer.mix(&ControlOutput::new(0.5, Vec3::new(0.0, 0.5, 0.5)));
    assert!(mixer.saturation() == Saturation::default());
}

/// On six motors, too much thrust plus full roll and pitch still delivers the whole moment:
/// collective gives way, as on the quad.
#[test]
fn hex_desaturation_keeps_moments() {
    let mut mixer = TableMixer::hex_x();
    let moments = Vec3::new(1.0, 1.0, 0.0);
    let fits = mixer.mix(&ControlOutput::new(0.5, moments)).motors;
    assert!(mixer.saturation() == Saturation::default());
    let full = mixer.mix(&ControlOutput::new(1.0, moments)).motors;
    assert!(mixer.saturation().mixer_thrust && !mixer.saturation().mixer_moments);
    for i in 0..6 {
        assert!(
            approx_eq(full[i] - full[0], fits[i] - fits[0]),
            "motor {i}: {full:?} vs {fits:?}"
        );
    }
    assert!(full[..6].iter().all(|&m| (0.0..=1.0).contains(&m)));
}

/// A table of servos alone mixes with nothing to desaturate: motors stay off, servos follow
/// the moments, and only a clamped servo reports.
#[test]
fn servo_only_table_mixes_without_motors() {
    let rows = [
        MixRow::servo(0, 1.0, 0.0, 0.0),
        MixRow::servo(1, 0.0, 1.0, 1.0),
    ];
    let mut mixer = TableMixer::new(rows, MixerConfig::default()).expect("valid table");
    // (thrust, moments, expected servos[..2], mixer_moments)
    let cases = [
        (0.0, Vec3::new(0.3, -0.2, 0.1), [0.3, -0.1], false),
        (1.0, Vec3::new(-0.5, 0.0, 0.0), [-0.5, 0.0], false),
        (0.5, Vec3::new(0.0, 0.8, 0.8), [0.0, 1.0], true),
    ];
    for (thrust, moments, servos, mixer_moments) in cases {
        let output = mixer.mix(&ControlOutput::new(thrust, moments));
        assert!(output.motors == [0.0; ActuatorCommand::SLOTS]);
        assert!(
            output.servos[..2]
                .iter()
                .zip(servos)
                .all(|(&s, e)| approx_eq(s, e)),
            "{thrust} {moments:?}: {:?}",
            output.servos
        );
        assert!(
            mixer.saturation()
                == Saturation {
                    mixer_moments,
                    ..Saturation::default()
                }
        );
    }
}

/// Every preset passes the same validation a custom table gets.
#[test]
fn presets_are_valid_tables() {
    fn check<const N: usize>(name: &str, mixer: TableMixer<N>) {
        assert!(
            TableMixer::new(mixer.rows, mixer.config) == Ok(mixer),
            "{name}"
        );
    }
    check("quad_x", TableMixer::quad_x());
    check("quad_plus", TableMixer::quad_plus());
    check("tri", TableMixer::tri());
    check("bi", TableMixer::bi());
    check("tailsitter_2p", TableMixer::tailsitter_2p());
    check("plane", TableMixer::plane());
    check("hex_x", TableMixer::hex_x());
    check("hex_plus", TableMixer::hex_plus());
    check("y6", TableMixer::y6());
    check("octo_x", TableMixer::octo_x());
    check("x8", TableMixer::x8());
    check("quadplane", TableMixer::quadplane());
    check("quadplane_vtail", TableMixer::quadplane_vtail());
}

/// Invalid tables and configs are rejected at construction, naming the problem.
#[test]
fn rejects_invalid_tables_and_configs() {
    let motor = |i| MixRow::motor(i, 1.0, 0.0, 0.0);
    let good = MixerConfig::default();
    let cases: [([MixRow; 2], MixerConfig, MixerError); 10] = [
        (
            [motor(0), motor(2)],
            good,
            MixerError::MotorGap(Slot::Motor(2)),
        ),
        (
            [motor(0), motor(8)],
            good,
            MixerError::SlotOutOfRange(Slot::Motor(8)),
        ),
        (
            [motor(0), MixRow::pusher(2)],
            good,
            MixerError::MotorGap(Slot::Motor(2)),
        ),
        (
            [MixRow::pusher(0), motor(1)],
            good,
            MixerError::PusherBeforeMotor(Slot::Motor(0)),
        ),
        (
            [motor(0), MixRow::servo(8, 0.0, 0.0, 1.0)],
            good,
            MixerError::SlotOutOfRange(Slot::Servo(8)),
        ),
        (
            [motor(3), motor(3)],
            good,
            MixerError::DuplicateSlot(Slot::Motor(3)),
        ),
        (
            [motor(0), MixRow::motor(1, 0.0, f32::NAN, 0.0)],
            good,
            MixerError::NonFiniteFactor(Slot::Motor(1)),
        ),
        (
            [motor(0), motor(1)],
            MixerConfig {
                thrust_factor: 1.0,
                ..good
            },
            MixerError::ThrustFactor,
        ),
        (
            [motor(0), motor(1)],
            MixerConfig {
                thrust_factor: f32::NAN,
                ..good
            },
            MixerError::ThrustFactor,
        ),
        (
            [motor(0), motor(1)],
            MixerConfig {
                yaw_ratio: -0.5,
                ..good
            },
            MixerError::YawRatio,
        ),
    ];
    for (rows, config, error) in cases {
        assert!(
            TableMixer::new(rows, config) == Err(error),
            "{rows:?} {config:?}"
        );
    }
    assert!(TableMixer::<0>::new([], good) == Err(MixerError::Empty));
    // A pusher alone, or after the motors, is a valid table.
    assert!(TableMixer::new([MixRow::pusher(0)], good).is_ok());
    assert!(TableMixer::new([motor(0), MixRow::pusher(1)], good).is_ok());
    // A motor and a servo may share an index: they are different slots.
    assert!(TableMixer::new([motor(0), MixRow::servo(0, 0.0, 0.0, 1.0)], good).is_ok());
    assert!(
        TableMixer::quad_x().with_config(MixerConfig {
            yaw_ratio: f32::INFINITY,
            ..good
        }) == Err(MixerError::YawRatio)
    );
}

/// Config scales the motor split: a lower thrust factor leaves more headroom for moments.
#[test]
fn config_sets_the_thrust_and_moment_split() {
    let config = MixerConfig {
        thrust_factor: 0.8,
        yaw_ratio: 1.0,
    };
    let mut mixer = TableMixer::quad_x()
        .with_config(config)
        .expect("valid config");
    let output = mixer.mix(&ControlOutput::new(0.5, Vec3::new(0.0, 0.0, 1.0)));
    // Collective 0.4; yaw 1 · headroom 0.2 · ratio 1.
    assert!(
        output.motors[..4]
            .iter()
            .zip([0.2, 0.6, 0.6, 0.2])
            .all(|(&m, e)| approx_eq(m, e))
    );
}

/// Reversing yaw turns a yaw command the other way and leaves thrust, roll, and pitch as they
/// were.
#[test]
fn reversed_yaw_mirrors_only_yaw() {
    // (moments, motors expected to match the preset's (true) or swap with the opposite pair)
    let cases = [
        (Vec3::new(0.0, 0.0, 0.5), false),
        (Vec3::new(0.3, -0.2, 0.0), true),
    ];
    for (moments, same) in cases {
        let control = ControlOutput::new(0.5, moments);
        let preset = TableMixer::quad_x().mix(&control).motors;
        let reversed = TableMixer::quad_x()
            .with_reversed_yaw()
            .mix(&control)
            .motors;
        // quad_x's diagonals (0, 3) and (1, 2) spin the same way, so reversing yaw swaps them.
        let expected = if same {
            preset
        } else {
            let mut swapped = preset;
            swapped[..4].copy_from_slice(&[preset[1], preset[0], preset[3], preset[2]]);
            swapped
        };
        assert!(
            reversed.iter().zip(expected).all(|(&m, e)| approx_eq(m, e)),
            "{moments:?}: {reversed:?} vs {expected:?}"
        );
    }
}

/// A pusher carries no moments, so it flies the cruise thrust over its full range at any
/// thrust factor, while the lift motors keep their headroom (#448: at 0.8 the Alia's pusher
/// stopped at 0.8 and it couldn't climb on the wing).
#[test]
fn pusher_spans_its_full_range_at_any_thrust_factor() {
    // (thrust factor, cruise thrust, expected pusher)
    let cases = [
        (0.9, 1.0, 1.0),
        (0.8, 1.0, 1.0),
        (0.5, 1.0, 1.0),
        (0.8, 0.65, 0.65),
        (0.8, 1.5, 1.0),
        (0.8, f32::NAN, 0.0),
    ];
    for (thrust_factor, thrust, expected) in cases {
        let config = MixerConfig {
            thrust_factor,
            ..MixerConfig::default()
        };
        let mut mixer = TableMixer::quadplane()
            .with_config(config)
            .expect("valid config");
        let hover = ControlOutput::new(1.0, Vec3::zeros());
        let output = mixer.mix(&ControlOutput {
            cruise: Some(CruiseOutput {
                thrust,
                moments: Vec3::zeros(),
            }),
            ..hover
        });
        assert!(
            approx_eq(output.motors[4], expected),
            "{thrust_factor}, {thrust}: {output:?}"
        );
        // The lift motors keep the factor: full collective at `thrust_factor`.
        assert!(
            approx_eq(output.motors[0], thrust_factor),
            "{thrust_factor}: {output:?}"
        );
    }
}

/// Reversing a quadplane's props leaves its surfaces and pusher alone: the rudder turns the
/// nose the same way, in cruise and with a hover's moments alike (#392).
#[test]
fn reversed_yaw_keeps_the_surfaces() {
    let moments = Vec3::new(0.1, 0.2, 0.3);
    let cases = [
        ControlOutput::new(0.5, moments),
        ControlOutput {
            cruise: Some(CruiseOutput {
                thrust: 0.5,
                moments,
            }),
            ..Default::default()
        },
    ];
    for control in cases {
        let preset = TableMixer::quadplane().mix(&control);
        let reversed = TableMixer::quadplane().with_reversed_yaw().mix(&control);
        assert_eq!(reversed.servos, preset.servos, "{control:?}");
        assert_eq!(reversed.motors[4], preset.motors[4], "{control:?}");
    }
}