stable-flight 0.1.0

no_std flight-control core for Stable Flight: pipeline traits, copter guidance, cascaded-PID stabilizers, 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
use crate::Vec3;

use super::*;

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

/// Helper function to create a mixer instance.
fn create_mixer() -> QuadCopterMixer {
    QuadCopterMixer::default()
}

/// 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 (#112); 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]);
    }
}