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
//! Frame-conformance kit for state sources: simulator backends and estimators.
//!
//! Every simulator or sensor stack reports state in its own frames and units; a
//! [`StateSource`](crate::StateSource) converts to the
//! [`VehicleState` frame contract](crate::VehicleState#frame-contract) at its boundary. This
//! module is a table of physical [`Scenario`]s with the state each must produce. A backend's
//! tests build the simulator's *native* report for each scenario (a mock-client fixture), run
//! it through the backend, and [`Scenario::check`] the result. That catches a swapped axis, a
//! flipped sign, a wrong rotation order, or a unit slip before the backend flies.
//!
//! [`conformance_tests!`](crate::conformance_tests) generates one test per scenario and a
//! test that fails when a scenario has none, so new scenarios reach every backend.
//!
//! # Examples
//!
//! ```
//! use stable_flight::testing::conformance::{LEVEL, ROLL_RIGHT_30, SCENARIOS};
//!
//! // A backend that got the frames right produces the scenario's expected state.
//! for scenario in SCENARIOS {
//!     assert!(scenario.check(&scenario.expected_state()).is_ok());
//! }
//!
//! // Level flight is not a 30° right bank.
//! let error = ROLL_RIGHT_30.check(&LEVEL.expected_state()).unwrap_err();
//! assert_eq!(error.mismatches()[0].field, "roll_rad");
//! ```

use core::fmt;

use crate::Vec3;
use nalgebra::UnitQuaternion;

use crate::VehicleState;
use crate::math::wrap_angle;

#[cfg(test)]
mod tests;

/// Largest difference accepted for an angle (rad) or body rate (rad/s).
pub const ANGLE_TOLERANCE: f32 = 1e-3;

/// Largest difference accepted for a position (m), velocity (m/s), or acceleration (m/s²).
pub const LINEAR_TOLERANCE: f32 = 1e-3;

/// Altitude every scenario flies at, in meters. Nonzero so a dropped or negated altitude
/// shows up.
const SCENARIO_ALTITUDE: f32 = 100.0;

/// One physical situation and the [`VehicleState`] it must map to.
///
/// Fields give the physical truth in the
/// [frame contract's](crate::VehicleState#frame-contract) axes and signs, with angles in
/// degrees (as simulators usually report them), so a backend can build its native fixture
/// from them.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Scenario {
    /// Short name used in failure messages.
    pub name: &'static str,
    /// Roll angle in degrees.
    pub roll_deg: f32,
    /// Pitch angle in degrees.
    pub pitch_deg: f32,
    /// Heading in degrees.
    pub heading_deg: f32,
    /// Body rates (roll, pitch, yaw) in degrees per second.
    pub body_rates_degps: [f32; 3],
    /// World position (north, east, up) in meters.
    pub position_neu_m: [f32; 3],
    /// World velocity (north, east, up) in m/s.
    pub velocity_neu_mps: [f32; 3],
    /// World acceleration (north, east, up) in m/s².
    pub acceleration_neu_mps2: [f32; 3],
}

/// At rest, wings level, nose north.
pub const LEVEL: Scenario = Scenario {
    name: "level",
    roll_deg: 0.0,
    pitch_deg: 0.0,
    heading_deg: 0.0,
    body_rates_degps: [0.0; 3],
    position_neu_m: [0.0, 0.0, SCENARIO_ALTITUDE],
    velocity_neu_mps: [0.0; 3],
    acceleration_neu_mps2: [0.0; 3],
};

/// Banked 30° right (right wing down).
pub const ROLL_RIGHT_30: Scenario = Scenario {
    name: "roll right 30°",
    roll_deg: 30.0,
    ..LEVEL
};

/// Nose 15° up.
pub const PITCH_UP_15: Scenario = Scenario {
    name: "pitch up 15°",
    pitch_deg: 15.0,
    ..LEVEL
};

/// Level, nose pointing east.
pub const HEADING_EAST: Scenario = Scenario {
    name: "heading east",
    heading_deg: 90.0,
    ..LEVEL
};

/// Nose east, 10° up, banked 30° right. Distinct angles on every axis catch a wrong rotation
/// order or swapped Euler angles.
pub const BANKED_CLIMB_EAST: Scenario = Scenario {
    name: "banked climb east",
    roll_deg: 30.0,
    pitch_deg: 10.0,
    heading_deg: 90.0,
    ..LEVEL
};

/// Level, climbing at 2 m/s and accelerating upward at 1 m/s².
pub const CLIMBING: Scenario = Scenario {
    name: "climbing",
    velocity_neu_mps: [0.0, 0.0, 2.0],
    acceleration_neu_mps2: [0.0, 0.0, 1.0],
    ..LEVEL
};

/// Northeast of the origin, flying northeast and speeding up. Distinct north and east values
/// catch a swapped horizontal axis (e.g. ENU vs NED).
pub const MOVING_NORTHEAST: Scenario = Scenario {
    name: "moving northeast",
    position_neu_m: [30.0, 40.0, SCENARIO_ALTITUDE],
    velocity_neu_mps: [3.0, 4.0, 0.0],
    acceleration_neu_mps2: [0.5, 1.5, 0.0],
    ..LEVEL
};

/// Level, yawing right (clockwise from above) at 30°/s.
pub const YAW_RIGHT_RATE: Scenario = Scenario {
    name: "yaw right rate",
    body_rates_degps: [0.0, 0.0, 30.0],
    ..LEVEL
};

/// Rolling right, pitching up, and yawing right at distinct rates, which catches swapped
/// rate axes.
pub const COMBINED_RATES: Scenario = Scenario {
    name: "combined rates",
    body_rates_degps: [10.0, 20.0, 30.0],
    ..LEVEL
};

/// Every scenario; a backend covers each one (see [`conformance_tests!`](crate::conformance_tests)).
pub const SCENARIOS: &[Scenario] = &[
    LEVEL,
    ROLL_RIGHT_30,
    PITCH_UP_15,
    HEADING_EAST,
    BANKED_CLIMB_EAST,
    CLIMBING,
    MOVING_NORTHEAST,
    YAW_RIGHT_RATE,
    COMBINED_RATES,
];

impl Scenario {
    /// The [`VehicleState`] a conforming backend reports for this scenario.
    pub fn expected_state(&self) -> VehicleState {
        let [roll, pitch, heading] =
            [self.roll_deg, self.pitch_deg, self.heading_deg].map(f32::to_radians);
        let [p, q, r] = self.body_rates_degps.map(f32::to_radians);
        let [x, y, z] = self.position_neu_m;
        let [vx, vy, vz] = self.velocity_neu_mps;
        let [ax, ay, az] = self.acceleration_neu_mps2;

        VehicleState {
            position_m: Vec3::new(x, y, z),
            velocity_mps: Vec3::new(vx, vy, vz),
            acceleration_mps2: Vec3::new(ax, ay, az),
            attitude: UnitQuaternion::from_euler_angles(roll, pitch, heading),
            angular_velocity_radps: Vec3::new(p, q, r),
            roll_rad: roll,
            pitch_rad: pitch,
            heading_rad: heading,
            // Still air: airspeed is ground speed.
            airspeed_mps: Some(Vec3::new(vx, vy, vz).norm()),
        }
    }

    /// Checks `state` against this scenario: position, velocity, acceleration, the Euler
    /// angles, the attitude quaternion's Euler angles, the body rates, and airspeed when the
    /// backend reports one (`None` passes: no air-data source).
    ///
    /// Angles are compared modulo a full turn, so a heading in [0, 2π) or [-π, π) both pass.
    /// A NaN field always fails.
    ///
    /// # Errors
    ///
    /// [`ConformanceError`] listing every field outside [`ANGLE_TOLERANCE`] or
    /// [`LINEAR_TOLERANCE`].
    #[allow(
        clippy::result_large_err,
        reason = "mismatches are stored inline so the kit needs no allocator; test-only"
    )]
    pub fn check(&self, state: &VehicleState) -> Result<(), ConformanceError> {
        let e = self.expected_state();
        let mut error = ConformanceError::new(self.name);

        let linear = [
            ("position_m.x", e.position_m.x, state.position_m.x),
            ("position_m.y", e.position_m.y, state.position_m.y),
            ("position_m.z", e.position_m.z, state.position_m.z),
            ("velocity_mps.x", e.velocity_mps.x, state.velocity_mps.x),
            ("velocity_mps.y", e.velocity_mps.y, state.velocity_mps.y),
            ("velocity_mps.z", e.velocity_mps.z, state.velocity_mps.z),
            (
                "acceleration_mps2.x",
                e.acceleration_mps2.x,
                state.acceleration_mps2.x,
            ),
            (
                "acceleration_mps2.y",
                e.acceleration_mps2.y,
                state.acceleration_mps2.y,
            ),
            (
                "acceleration_mps2.z",
                e.acceleration_mps2.z,
                state.acceleration_mps2.z,
            ),
        ];
        let rates = [
            (
                "angular_velocity_radps.x",
                e.angular_velocity_radps.x,
                state.angular_velocity_radps.x,
            ),
            (
                "angular_velocity_radps.y",
                e.angular_velocity_radps.y,
                state.angular_velocity_radps.y,
            ),
            (
                "angular_velocity_radps.z",
                e.angular_velocity_radps.z,
                state.angular_velocity_radps.z,
            ),
        ];
        let (q_roll, q_pitch, q_yaw) = crate::euler_angles(&state.attitude);
        let angles = [
            ("roll_rad", e.roll_rad, state.roll_rad),
            ("pitch_rad", e.pitch_rad, state.pitch_rad),
            ("heading_rad", e.heading_rad, state.heading_rad),
            ("attitude roll", e.roll_rad, q_roll),
            ("attitude pitch", e.pitch_rad, q_pitch),
            ("attitude yaw", e.heading_rad, q_yaw),
        ];

        for (field, expected, actual) in linear {
            error.check(field, expected, actual, expected - actual, LINEAR_TOLERANCE);
        }
        if let (Some(expected), Some(actual)) = (e.airspeed_mps, state.airspeed_mps) {
            error.check(
                "airspeed_mps",
                expected,
                actual,
                expected - actual,
                LINEAR_TOLERANCE,
            );
        }
        for (field, expected, actual) in rates {
            error.check(field, expected, actual, expected - actual, ANGLE_TOLERANCE);
        }
        for (field, expected, actual) in angles {
            let difference = wrap_angle(expected - actual);
            error.check(field, expected, actual, difference, ANGLE_TOLERANCE);
        }

        if error.mismatches().is_empty() {
            Ok(())
        } else {
            Err(error)
        }
    }
}

/// Number of fields [`Scenario::check`] compares.
const CHECKED_FIELDS: usize = 19;

/// A backend's state for a [`Scenario`] broke the frame contract.
///
/// Holds its mismatches inline (no allocation), so it works in `no_std` tests.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ConformanceError {
    /// The scenario's name.
    pub scenario: &'static str,
    mismatches: [Mismatch; CHECKED_FIELDS],
    len: usize,
}

impl ConformanceError {
    fn new(scenario: &'static str) -> Self {
        Self {
            scenario,
            mismatches: [Mismatch::EMPTY; CHECKED_FIELDS],
            len: 0,
        }
    }

    /// Every field that was off, in check order.
    pub fn mismatches(&self) -> &[Mismatch] {
        &self.mismatches[..self.len]
    }

    /// Records `field` unless `|difference| <= tolerance` (false for NaN, so NaN fails).
    fn check(
        &mut self,
        field: &'static str,
        expected: f32,
        actual: f32,
        difference: f32,
        tolerance: f32,
    ) {
        if difference.abs() <= tolerance || self.len == CHECKED_FIELDS {
            return;
        }
        self.mismatches[self.len] = Mismatch {
            field,
            expected,
            actual,
        };
        self.len += 1;
    }
}

impl fmt::Display for ConformanceError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "scenario `{}` off contract", self.scenario)?;
        for (i, mismatch) in self.mismatches().iter().enumerate() {
            let separator = if i == 0 { ": " } else { "; " };
            write!(f, "{separator}{mismatch}")?;
        }
        Ok(())
    }
}

impl core::error::Error for ConformanceError {}

/// One field of a [`ConformanceError`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Mismatch {
    /// The `VehicleState` field, e.g. `velocity_mps.z`.
    pub field: &'static str,
    /// The contract's value (SI: m, m/s, m/s², rad, rad/s).
    pub expected: f32,
    /// The backend's value.
    pub actual: f32,
}

impl Mismatch {
    const EMPTY: Mismatch = Mismatch {
        field: "",
        expected: 0.0,
        actual: 0.0,
    };
}

impl fmt::Display for Mismatch {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} expected {} got {}",
            self.field, self.expected, self.actual
        )
    }
}

/// Generates a backend's conformance tests: one `#[test]` per listed scenario, plus
/// `conformance_covers_every_scenario`, which fails if any scenario in
/// [`SCENARIOS`](crate::testing::conformance::SCENARIOS) has no test.
///
/// `$state_for` is a `fn(&Scenario) -> VehicleState` that builds the simulator's native report
/// for the scenario and runs it through the backend. Each row is `test_name: SCENARIO`,
/// optionally followed by `, ignore = "reason"` for a known failure.
///
/// # Examples
///
/// ```
/// use stable_flight::VehicleState;
/// use stable_flight::testing::conformance::Scenario;
///
/// // A real backend builds its simulator's native report here.
/// fn state_for(scenario: &Scenario) -> VehicleState {
///     scenario.expected_state()
/// }
///
/// stable_flight::conformance_tests! {
///     state_for;
///     conforms_level: LEVEL;
///     conforms_roll_right_30: ROLL_RIGHT_30;
///     conforms_pitch_up_15: PITCH_UP_15, ignore = "pitch sign not converted yet";
/// }
/// ```
#[macro_export]
macro_rules! conformance_tests {
    ($state_for:path; $($test:ident: $scenario:ident $(, ignore = $reason:literal)?;)+) => {
        $(
            #[test]
            $(#[ignore = $reason])?
            fn $test() {
                let scenario = $crate::testing::conformance::$scenario;
                if let Err(error) = scenario.check(&$state_for(&scenario)) {
                    panic!("{error}");
                }
            }
        )+

        #[test]
        fn conformance_covers_every_scenario() {
            let covered = [$($crate::testing::conformance::$scenario),+];
            for scenario in $crate::testing::conformance::SCENARIOS {
                assert!(
                    covered.contains(scenario),
                    "no conformance test for scenario `{}`",
                    scenario.name
                );
            }
        }
    };
}