1use core::fmt;
30
31use crate::Vec3;
32use nalgebra::UnitQuaternion;
33
34use crate::VehicleState;
35use crate::math::wrap_angle;
36
37#[cfg(test)]
38mod tests;
39
40pub const ANGLE_TOLERANCE: f32 = 1e-3;
42
43pub const LINEAR_TOLERANCE: f32 = 1e-3;
45
46const SCENARIO_ALTITUDE: f32 = 100.0;
49
50#[derive(Debug, Clone, Copy, PartialEq)]
57pub struct Scenario {
58 pub name: &'static str,
60 pub roll_deg: f32,
62 pub pitch_deg: f32,
64 pub heading_deg: f32,
66 pub body_rates_degps: [f32; 3],
68 pub position_neu_m: [f32; 3],
70 pub velocity_neu_mps: [f32; 3],
72 pub acceleration_neu_mps2: [f32; 3],
74}
75
76pub const LEVEL: Scenario = Scenario {
78 name: "level",
79 roll_deg: 0.0,
80 pitch_deg: 0.0,
81 heading_deg: 0.0,
82 body_rates_degps: [0.0; 3],
83 position_neu_m: [0.0, 0.0, SCENARIO_ALTITUDE],
84 velocity_neu_mps: [0.0; 3],
85 acceleration_neu_mps2: [0.0; 3],
86};
87
88pub const ROLL_RIGHT_30: Scenario = Scenario {
90 name: "roll right 30°",
91 roll_deg: 30.0,
92 ..LEVEL
93};
94
95pub const PITCH_UP_15: Scenario = Scenario {
97 name: "pitch up 15°",
98 pitch_deg: 15.0,
99 ..LEVEL
100};
101
102pub const HEADING_EAST: Scenario = Scenario {
104 name: "heading east",
105 heading_deg: 90.0,
106 ..LEVEL
107};
108
109pub const BANKED_CLIMB_EAST: Scenario = Scenario {
112 name: "banked climb east",
113 roll_deg: 30.0,
114 pitch_deg: 10.0,
115 heading_deg: 90.0,
116 ..LEVEL
117};
118
119pub const CLIMBING: Scenario = Scenario {
121 name: "climbing",
122 velocity_neu_mps: [0.0, 0.0, 2.0],
123 acceleration_neu_mps2: [0.0, 0.0, 1.0],
124 ..LEVEL
125};
126
127pub const MOVING_NORTHEAST: Scenario = Scenario {
130 name: "moving northeast",
131 position_neu_m: [30.0, 40.0, SCENARIO_ALTITUDE],
132 velocity_neu_mps: [3.0, 4.0, 0.0],
133 acceleration_neu_mps2: [0.5, 1.5, 0.0],
134 ..LEVEL
135};
136
137pub const YAW_RIGHT_RATE: Scenario = Scenario {
139 name: "yaw right rate",
140 body_rates_degps: [0.0, 0.0, 30.0],
141 ..LEVEL
142};
143
144pub const COMBINED_RATES: Scenario = Scenario {
147 name: "combined rates",
148 body_rates_degps: [10.0, 20.0, 30.0],
149 ..LEVEL
150};
151
152pub const SCENARIOS: &[Scenario] = &[
154 LEVEL,
155 ROLL_RIGHT_30,
156 PITCH_UP_15,
157 HEADING_EAST,
158 BANKED_CLIMB_EAST,
159 CLIMBING,
160 MOVING_NORTHEAST,
161 YAW_RIGHT_RATE,
162 COMBINED_RATES,
163];
164
165impl Scenario {
166 pub fn expected_state(&self) -> VehicleState {
168 let [roll, pitch, heading] =
169 [self.roll_deg, self.pitch_deg, self.heading_deg].map(f32::to_radians);
170 let [p, q, r] = self.body_rates_degps.map(f32::to_radians);
171 let [x, y, z] = self.position_neu_m;
172 let [vx, vy, vz] = self.velocity_neu_mps;
173 let [ax, ay, az] = self.acceleration_neu_mps2;
174
175 VehicleState {
176 position_m: Vec3::new(x, y, z),
177 velocity_mps: Vec3::new(vx, vy, vz),
178 acceleration_mps2: Vec3::new(ax, ay, az),
179 attitude: UnitQuaternion::from_euler_angles(roll, pitch, heading),
180 angular_velocity_radps: Vec3::new(p, q, r),
181 roll_rad: roll,
182 pitch_rad: pitch,
183 heading_rad: heading,
184 airspeed_mps: Some(Vec3::new(vx, vy, vz).norm()),
186 }
187 }
188
189 #[allow(
201 clippy::result_large_err,
202 reason = "mismatches are stored inline so the kit needs no allocator; test-only"
203 )]
204 pub fn check(&self, state: &VehicleState) -> Result<(), ConformanceError> {
205 let e = self.expected_state();
206 let mut error = ConformanceError::new(self.name);
207
208 let linear = [
209 ("position_m.x", e.position_m.x, state.position_m.x),
210 ("position_m.y", e.position_m.y, state.position_m.y),
211 ("position_m.z", e.position_m.z, state.position_m.z),
212 ("velocity_mps.x", e.velocity_mps.x, state.velocity_mps.x),
213 ("velocity_mps.y", e.velocity_mps.y, state.velocity_mps.y),
214 ("velocity_mps.z", e.velocity_mps.z, state.velocity_mps.z),
215 (
216 "acceleration_mps2.x",
217 e.acceleration_mps2.x,
218 state.acceleration_mps2.x,
219 ),
220 (
221 "acceleration_mps2.y",
222 e.acceleration_mps2.y,
223 state.acceleration_mps2.y,
224 ),
225 (
226 "acceleration_mps2.z",
227 e.acceleration_mps2.z,
228 state.acceleration_mps2.z,
229 ),
230 ];
231 let rates = [
232 (
233 "angular_velocity_radps.x",
234 e.angular_velocity_radps.x,
235 state.angular_velocity_radps.x,
236 ),
237 (
238 "angular_velocity_radps.y",
239 e.angular_velocity_radps.y,
240 state.angular_velocity_radps.y,
241 ),
242 (
243 "angular_velocity_radps.z",
244 e.angular_velocity_radps.z,
245 state.angular_velocity_radps.z,
246 ),
247 ];
248 let (q_roll, q_pitch, q_yaw) = crate::euler_angles(&state.attitude);
249 let angles = [
250 ("roll_rad", e.roll_rad, state.roll_rad),
251 ("pitch_rad", e.pitch_rad, state.pitch_rad),
252 ("heading_rad", e.heading_rad, state.heading_rad),
253 ("attitude roll", e.roll_rad, q_roll),
254 ("attitude pitch", e.pitch_rad, q_pitch),
255 ("attitude yaw", e.heading_rad, q_yaw),
256 ];
257
258 for (field, expected, actual) in linear {
259 error.check(field, expected, actual, expected - actual, LINEAR_TOLERANCE);
260 }
261 if let (Some(expected), Some(actual)) = (e.airspeed_mps, state.airspeed_mps) {
262 error.check(
263 "airspeed_mps",
264 expected,
265 actual,
266 expected - actual,
267 LINEAR_TOLERANCE,
268 );
269 }
270 for (field, expected, actual) in rates {
271 error.check(field, expected, actual, expected - actual, ANGLE_TOLERANCE);
272 }
273 for (field, expected, actual) in angles {
274 let difference = wrap_angle(expected - actual);
275 error.check(field, expected, actual, difference, ANGLE_TOLERANCE);
276 }
277
278 if error.mismatches().is_empty() {
279 Ok(())
280 } else {
281 Err(error)
282 }
283 }
284}
285
286const CHECKED_FIELDS: usize = 19;
288
289#[derive(Debug, Clone, Copy, PartialEq)]
293pub struct ConformanceError {
294 pub scenario: &'static str,
296 mismatches: [Mismatch; CHECKED_FIELDS],
297 len: usize,
298}
299
300impl ConformanceError {
301 fn new(scenario: &'static str) -> Self {
302 Self {
303 scenario,
304 mismatches: [Mismatch::EMPTY; CHECKED_FIELDS],
305 len: 0,
306 }
307 }
308
309 pub fn mismatches(&self) -> &[Mismatch] {
311 &self.mismatches[..self.len]
312 }
313
314 fn check(
316 &mut self,
317 field: &'static str,
318 expected: f32,
319 actual: f32,
320 difference: f32,
321 tolerance: f32,
322 ) {
323 if difference.abs() <= tolerance || self.len == CHECKED_FIELDS {
324 return;
325 }
326 self.mismatches[self.len] = Mismatch {
327 field,
328 expected,
329 actual,
330 };
331 self.len += 1;
332 }
333}
334
335impl fmt::Display for ConformanceError {
336 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
337 write!(f, "scenario `{}` off contract", self.scenario)?;
338 for (i, mismatch) in self.mismatches().iter().enumerate() {
339 let separator = if i == 0 { ": " } else { "; " };
340 write!(f, "{separator}{mismatch}")?;
341 }
342 Ok(())
343 }
344}
345
346impl core::error::Error for ConformanceError {}
347
348#[derive(Debug, Clone, Copy, PartialEq)]
350pub struct Mismatch {
351 pub field: &'static str,
353 pub expected: f32,
355 pub actual: f32,
357}
358
359impl Mismatch {
360 const EMPTY: Mismatch = Mismatch {
361 field: "",
362 expected: 0.0,
363 actual: 0.0,
364 };
365}
366
367impl fmt::Display for Mismatch {
368 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
369 write!(
370 f,
371 "{} expected {} got {}",
372 self.field, self.expected, self.actual
373 )
374 }
375}
376
377#[macro_export]
404macro_rules! conformance_tests {
405 ($state_for:path; $($test:ident: $scenario:ident $(, ignore = $reason:literal)?;)+) => {
406 $(
407 #[test]
408 $(#[ignore = $reason])?
409 fn $test() {
410 let scenario = $crate::testing::conformance::$scenario;
411 if let Err(error) = scenario.check(&$state_for(&scenario)) {
412 panic!("{error}");
413 }
414 }
415 )+
416
417 #[test]
418 fn conformance_covers_every_scenario() {
419 let covered = [$($crate::testing::conformance::$scenario),+];
420 for scenario in $crate::testing::conformance::SCENARIOS {
421 assert!(
422 covered.contains(scenario),
423 "no conformance test for scenario `{}`",
424 scenario.name
425 );
426 }
427 }
428 };
429}