1use std::collections::BTreeMap;
24
25use hpr_flightdata::log::FlightLog;
26use hpr_flightdata::readings::{self, Clip, Reading, Reason};
27use serde::{Deserialize, Serialize};
28
29use super::logger::{
30 LOWER_RANGE_G, Recording, Written, accelerometer_log, accelerometer_log_set_lower,
31 barometer_log,
32};
33use super::{Flight, SyntheticError};
34
35const FOOT_M: f64 = 0.3048;
37
38pub const GRID_RATES_HZ: [f64; 6] = [500.0, 100.0, 20.0, 5.0, 1.0, 0.5];
41
42pub const GRID_WRITTEN: [Written; 6] = [
46 Written::Altitude { step_m: 0.0 },
47 Written::Altitude { step_m: FOOT_M },
48 Written::Altitude { step_m: 10.0 },
49 Written::PressureAlone { step_pa: 1.0 },
50 Written::PressureAlone { step_pa: 10.0 },
51 Written::PressureAlone { step_pa: 100.0 },
52];
53
54pub const ACCELEROMETER_RATES_HZ: [f64; 3] = [500.0, 100.0, 20.0];
58
59pub const CROPPED: [Recording; 2] = [
63 Recording {
64 climbing_hz: 20.0,
65 descending_hz: 20.0,
66 written: Written::Altitude { step_m: FOOT_M },
67 },
68 Recording {
69 climbing_hz: 20.0,
70 descending_hz: 20.0,
71 written: Written::PressureAlone { step_pa: 10.0 },
72 },
73];
74
75#[derive(Debug, Clone, Copy, PartialEq, Eq)]
77pub enum Crop {
78 BeforeLiftoff,
80 AfterLiftoff,
82 ZeroFirst,
85}
86
87impl Crop {
88 pub const ALL: [Self; 3] = [Self::BeforeLiftoff, Self::AfterLiftoff, Self::ZeroFirst];
90
91 pub fn name(self) -> &'static str {
93 match self {
94 Self::BeforeLiftoff => "from 0.3 s before liftoff",
95 Self::AfterLiftoff => "from 0.1 s after liftoff",
96 Self::ZeroFirst => "from 0.5 s after liftoff, its zero first",
97 }
98 }
99
100 pub fn cut(self, log: &FlightLog, liftoff_s: f64) -> FlightLog {
104 let from_s = match self {
105 Self::BeforeLiftoff => liftoff_s - 0.3,
106 Self::AfterLiftoff => liftoff_s + 0.1,
107 Self::ZeroFirst => liftoff_s + 0.5,
108 };
109 let cut_track = |time_s: &[f64], values: &[f64]| -> (Vec<f64>, Vec<f64>) {
110 let first = time_s.partition_point(|t| *t < from_s);
111 let mut time: Vec<f64> = time_s[first..].to_vec();
112 let mut kept: Vec<f64> = values[first..].to_vec();
113 if let (Self::ZeroFirst, Some(&zero), [t0, t1, ..]) =
114 (self, values.first(), time.as_slice())
115 {
116 let back = t0 - (t1 - t0);
117 time.insert(0, back);
118 kept.insert(0, zero);
119 }
120 (time, kept)
121 };
122 let mut cut = log.clone();
123 (cut.time_s, cut.altitude_m) = cut_track(&log.time_s, &log.altitude_m);
124 for channel in &mut cut.channels {
125 (channel.time_s, channel.values) = cut_track(&channel.time_s, &channel.values);
126 }
127 cut
128 }
129}
130
131pub fn grid() -> Vec<Recording> {
134 GRID_RATES_HZ
135 .iter()
136 .flat_map(|&hz| {
137 GRID_WRITTEN.iter().map(move |&written| Recording {
138 climbing_hz: hz,
139 descending_hz: hz,
140 written,
141 })
142 })
143 .collect()
144}
145
146pub fn name(recording: &Recording) -> String {
148 let rate = if recording.climbing_hz == recording.descending_hz {
149 format!("{} Hz", recording.climbing_hz)
150 } else {
151 format!(
152 "{} Hz up, {} Hz down",
153 recording.climbing_hz, recording.descending_hz
154 )
155 };
156 let written = match recording.written {
157 Written::Altitude { step_m: 0.0 } => "altitude, no step".to_owned(),
158 Written::Altitude { step_m } => format!("altitude in {step_m} m steps"),
159 Written::Pressure { step_pa } => format!("pressure in {step_pa} Pa steps and altitude"),
160 Written::PressureAlone { step_pa } => format!("pressure alone in {step_pa} Pa steps"),
161 };
162 format!("{rate}, {written}")
163}
164
165#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
167#[serde(rename_all = "snake_case")]
168pub enum Quantity {
169 Liftoff,
171 ApogeeHeight,
173 ApogeeTime,
175 TopSpeed,
177 Landing,
179 FlightTime,
181 DescentRate,
183 TopAcceleration,
185 Burnout,
187 DrogueRate,
189 MainOpening,
191 MainRate,
193}
194
195impl Quantity {
196 pub const ALL: [Self; 12] = [
198 Self::Liftoff,
199 Self::ApogeeHeight,
200 Self::ApogeeTime,
201 Self::TopSpeed,
202 Self::Landing,
203 Self::FlightTime,
204 Self::DescentRate,
205 Self::TopAcceleration,
206 Self::Burnout,
207 Self::DrogueRate,
208 Self::MainOpening,
209 Self::MainRate,
210 ];
211
212 pub fn name(self) -> &'static str {
214 match self {
215 Self::Liftoff => "liftoff",
216 Self::ApogeeHeight => "apogee height",
217 Self::ApogeeTime => "apogee time",
218 Self::TopSpeed => "top speed",
219 Self::Landing => "landing",
220 Self::FlightTime => "flight time",
221 Self::DescentRate => "descent rate",
222 Self::TopAcceleration => "top acceleration",
223 Self::Burnout => "burnout",
224 Self::DrogueRate => "drogue rate",
225 Self::MainOpening => "main opening",
226 Self::MainRate => "main rate",
227 }
228 }
229
230 pub fn unit(self) -> &'static str {
232 match self {
233 Self::ApogeeHeight => "m",
234 Self::TopSpeed | Self::DescentRate | Self::DrogueRate | Self::MainRate => "m/s",
235 Self::TopAcceleration => "m/s²",
236 _ => "s",
237 }
238 }
239}
240
241#[derive(Debug, Clone, Copy, PartialEq)]
243pub enum Outcome {
244 Given {
246 error: f64,
248 bound: f64,
250 },
251 Floor {
254 error: f64,
256 bound: f64,
258 clip: Option<Clip>,
261 },
262 Withheld(Reason),
264 Absent,
267}
268
269pub fn outcomes(flight: &Flight, log: &FlightLog) -> Vec<(Quantity, Outcome)> {
271 let read = readings::read(log);
272 let truth = &flight.truth;
273 let events = &truth.events;
274 let pad_s = flight.draw.logger.pad_s;
275 let given = |error: f64, bound: f64| Outcome::Given { error, bound };
276 fn of<T>(reading: &Reading<T>, take: impl Fn(&T) -> Outcome) -> Outcome {
277 match reading {
278 Reading::Read(value) => take(value),
279 Reading::Withheld(withheld) => Outcome::Withheld(withheld.reason),
280 }
281 }
282 let descent_rate = events.apogee_m / (events.landing_s - events.apogee_s);
283 let height = |t_log: f64| truth.at(t_log - pad_s).map_or(f64::NAN, |x| x.height_m);
285 let chord = |from_s: f64, to_s: f64| (height(from_s) - height(to_s)) / (to_s - from_s);
286 let top_acceleration = top_specific_force(flight);
287 let floor_or_given = |is_floor: bool, error: f64, bound: f64, clip: Option<Clip>| {
288 if is_floor {
289 Outcome::Floor { error, bound, clip }
290 } else {
291 Outcome::Given { error, bound }
292 }
293 };
294 let main_opening = match &read.main_descent {
295 Reading::Read(main) => match (&main.opening, events.deployments.len()) {
296 (Some(opening), n) if n >= 2 => {
297 let opened = pad_s + events.deployments[n - 1].1;
298 given(opening.time_s - opened, opening.bound_s)
299 }
300 (Some(opening), _) => given(f64::INFINITY, opening.bound_s),
303 (None, n) if n >= 2 => given(f64::INFINITY, 0.0),
304 (None, _) => Outcome::Absent,
305 },
306 Reading::Withheld(withheld) => Outcome::Withheld(withheld.reason),
307 };
308 let two_canopies = events.deployments.len() >= 2;
309 let drogue_rate = match &read.drogue_descent {
310 Reading::Read(d) => given(d.rate_m_s - chord(d.from_s, d.to_s), d.bound_m_s),
311 Reading::Withheld(withheld) if withheld.reason == Reason::OneCanopy && !two_canopies => {
312 Outcome::Absent
313 }
314 Reading::Withheld(withheld) => Outcome::Withheld(withheld.reason),
315 };
316 vec![
317 (
318 Quantity::Liftoff,
319 of(&read.liftoff, |l| {
320 given(l.time_s - (pad_s + events.liftoff_s), l.bound_s)
321 }),
322 ),
323 (
324 Quantity::ApogeeHeight,
325 of(&read.apogee, |a| {
326 given(a.height_m - truth.apogee_above_pad_m(), a.bound_m)
327 }),
328 ),
329 (
330 Quantity::ApogeeTime,
331 of(&read.apogee, |a| {
332 given(a.time_s - (pad_s + events.apogee_s), a.time_bound_s)
333 }),
334 ),
335 (
336 Quantity::TopSpeed,
337 of(&read.max_speed, |s| {
338 floor_or_given(
339 s.is_floor,
340 s.speed_m_s - truth.top_speed().0,
341 s.bound_m_s,
342 None,
343 )
344 }),
345 ),
346 (
347 Quantity::Landing,
348 of(&read.landing, |l| {
349 given(l.time_s - (pad_s + events.landing_s), l.bound_s)
350 }),
351 ),
352 (
353 Quantity::FlightTime,
354 of(&read.flight_time, |f| {
355 given(f.time_s - (events.landing_s - events.liftoff_s), f.bound_s)
356 }),
357 ),
358 (
359 Quantity::DescentRate,
360 of(&read.landing, |l| {
361 given(
362 l.mean_descent_rate_m_s - descent_rate,
363 l.mean_descent_rate_bound_m_s,
364 )
365 }),
366 ),
367 (
368 Quantity::TopAcceleration,
369 of(&read.max_acceleration, |a| {
370 floor_or_given(
371 a.is_floor,
372 a.acceleration_m_s2 - top_acceleration,
373 a.bound_m_s2,
374 a.clip,
375 )
376 }),
377 ),
378 (
379 Quantity::Burnout,
380 of(&read.burnout, |b| {
381 floor_or_given(
382 b.is_floor,
383 b.time_s - (pad_s + events.burnout_s),
384 b.bound_s,
385 None,
386 )
387 }),
388 ),
389 (Quantity::DrogueRate, drogue_rate),
390 (Quantity::MainOpening, main_opening),
391 (
392 Quantity::MainRate,
393 of(&read.main_descent, |d| {
394 given(d.rate_m_s - chord(d.from_s, d.to_s), d.bound_m_s)
395 }),
396 ),
397 ]
398}
399
400pub(super) fn top_specific_force(flight: &Flight) -> f64 {
403 let events = &flight.truth.events;
404 flight
405 .truth
406 .samples
407 .iter()
408 .filter(|x| x.time_s >= events.liftoff_s && x.time_s <= events.apogee_s)
409 .map(|x| x.axial_specific_force_m_s2)
410 .fold(f64::NEG_INFINITY, f64::max)
411}
412
413pub fn top_over_range(flight: &Flight) -> Option<f64> {
417 flight
418 .draw
419 .logger
420 .profile
421 .accelerometer_range_m_s2()
422 .map(|range| top_specific_force(flight) / range)
423}
424
425pub fn top_over_lower_range(flight: &Flight) -> Option<f64> {
428 flight
429 .draw
430 .logger
431 .profile
432 .accelerometer_lower_range_m_s2()
433 .map(|range| top_specific_force(flight) / range)
434}
435
436pub fn lower_range_name() -> String {
438 format!("its accelerometer set to ±{LOWER_RANGE_G} g")
439}
440
441pub fn logs(
453 flight: &Flight,
454 seed: u64,
455 index: u64,
456) -> Result<Vec<(String, FlightLog)>, SyntheticError> {
457 let mut logs = vec![(
458 flight.draw.logger.profile.name().to_owned(),
459 flight.log.clone(),
460 )];
461 for recording in grid() {
462 if let Some(log) =
463 barometer_log(&flight.draw.logger, &recording, &flight.truth, seed, index)?
464 {
465 logs.push((name(&recording), log));
466 }
467 }
468 for rate_hz in ACCELEROMETER_RATES_HZ {
469 if let Some(log) =
470 accelerometer_log(&flight.draw.logger, rate_hz, &flight.truth, seed, index)?
471 {
472 logs.push((format!("its accelerometer at {rate_hz} Hz"), log));
473 }
474 }
475 if let Some(log) = accelerometer_log_set_lower(&flight.draw.logger, &flight.truth, seed, index)?
476 {
477 logs.push((lower_range_name(), log));
478 }
479 let liftoff_s = flight.draw.logger.pad_s + flight.truth.events.liftoff_s;
480 for recording in CROPPED {
481 if let Some(log) =
482 barometer_log(&flight.draw.logger, &recording, &flight.truth, seed, index)?
483 {
484 for crop in Crop::ALL {
485 let name = format!("{}, {}", name(&recording), crop.name());
486 logs.push((name, crop.cut(&log, liftoff_s)));
487 }
488 }
489 }
490 Ok(logs)
491}
492
493#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
495pub struct Worst {
496 pub flight: u64,
498 pub error: f64,
500 pub bound: f64,
502 pub share: f64,
504}
505
506#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
508pub struct Tally {
509 pub log: String,
511 pub quantity: Quantity,
513 pub given: u64,
515 pub withheld: BTreeMap<Reason, u64>,
517 #[serde(default)]
519 pub absent: u64,
520 pub worst: Option<Worst>,
522}
523
524impl Tally {
525 pub fn holds(&self) -> bool {
527 self.worst.as_ref().is_none_or(|worst| worst.share <= 1.0)
528 }
529}
530
531pub type FlightOutcomes = (u64, Vec<(String, Vec<(Quantity, Outcome)>)>);
534
535pub fn tally(flights: &[FlightOutcomes]) -> Vec<Tally> {
538 let mut order: Vec<String> = Vec::new();
539 let mut tallies: BTreeMap<(String, Quantity), Tally> = BTreeMap::new();
540 for (flight, logs) in flights {
541 for (log, results) in logs {
542 if !order.contains(log) {
543 order.push(log.clone());
544 }
545 for (quantity, outcome) in results {
546 let tally = tallies
547 .entry((log.clone(), *quantity))
548 .or_insert_with(|| Tally {
549 log: log.clone(),
550 quantity: *quantity,
551 given: 0,
552 withheld: BTreeMap::new(),
553 absent: 0,
554 worst: None,
555 });
556 match *outcome {
557 Outcome::Given { error, bound } | Outcome::Floor { error, bound, .. } => {
558 tally.given += 1;
559 let size = match outcome {
562 Outcome::Floor { .. } if error.is_nan() => f64::INFINITY,
563 Outcome::Floor { .. } => error.max(0.0),
564 _ => error.abs(),
565 };
566 let share = if bound > 0.0 {
568 size / bound
569 } else if size == 0.0 {
570 0.0
571 } else {
572 f64::INFINITY
573 };
574 let share = if share.is_nan() { f64::INFINITY } else { share };
575 if tally.worst.as_ref().is_none_or(|w| share > w.share) {
576 tally.worst = Some(Worst {
577 flight: *flight,
578 error,
579 bound,
580 share,
581 });
582 }
583 }
584 Outcome::Withheld(reason) => {
585 *tally.withheld.entry(reason).or_insert(0) += 1;
586 }
587 Outcome::Absent => tally.absent += 1,
588 }
589 }
590 }
591 }
592 order
593 .iter()
594 .flat_map(|log| {
595 Quantity::ALL
596 .iter()
597 .filter_map(|quantity| tallies.remove(&(log.clone(), *quantity)))
598 .collect::<Vec<_>>()
599 })
600 .collect()
601}
602
603#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
606pub struct Errors {
607 pub quantity: Quantity,
609 pub floor: bool,
611 pub given: u64,
613 pub spread: Option<Spread>,
615}
616
617#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
619pub struct Spread {
620 pub mean_error: f64,
622 pub least_error: f64,
624 pub most_error: f64,
626 pub mean_bound: f64,
628}
629
630pub fn errors(flights: &[FlightOutcomes]) -> Vec<Errors> {
635 #[derive(Default)]
636 struct Sum {
637 given: u64,
638 finite: u64,
639 error: f64,
640 bound: f64,
641 least: Option<f64>,
642 most: Option<f64>,
643 }
644 let mut sums: BTreeMap<(Quantity, bool), Sum> = BTreeMap::new();
645 for (_, logs) in flights {
646 for (_, results) in logs {
647 for (quantity, outcome) in results {
648 let (error, bound, floor) = match *outcome {
649 Outcome::Given { error, bound } => (error, bound, false),
650 Outcome::Floor { error, bound, .. } => (error, bound, true),
651 Outcome::Withheld(_) | Outcome::Absent => continue,
652 };
653 let sum = sums.entry((*quantity, floor)).or_default();
654 sum.given += 1;
655 if error.is_finite() && bound.is_finite() {
656 sum.finite += 1;
657 sum.error += error;
658 sum.bound += bound;
659 sum.least = Some(sum.least.map_or(error, |least| least.min(error)));
660 sum.most = Some(sum.most.map_or(error, |most| most.max(error)));
661 }
662 }
663 }
664 }
665 sums.into_iter()
666 .map(|((quantity, floor), sum)| {
667 #[expect(
668 clippy::cast_precision_loss,
669 reason = "a count of readings converts exactly"
670 )]
671 let count = sum.finite as f64;
672 Errors {
673 quantity,
674 floor,
675 given: sum.given,
676 spread: sum.least.zip(sum.most).map(|(least, most)| Spread {
677 mean_error: sum.error / count,
678 least_error: least,
679 most_error: most,
680 mean_bound: sum.bound / count,
681 }),
682 }
683 })
684 .collect()
685}
686
687#[cfg(test)]
688mod tests {
689 use super::*;
690 use crate::synthetic::designs::DesignFiles;
691 use crate::synthetic::{Flyer, Set};
692
693 #[test]
696 fn the_errors_keep_floors_apart() {
697 let mut sorted = Quantity::ALL;
698 sorted.sort();
699 assert_eq!(sorted, Quantity::ALL, "the errors' map orders by `Ord`");
700 let given = |error: f64| Outcome::Given { error, bound: 1.0 };
701 let flights: Vec<FlightOutcomes> = vec![
702 (
703 0,
704 vec![(
705 "a".to_owned(),
706 vec![
707 (Quantity::MainRate, given(0.5)),
708 (Quantity::Burnout, given(-0.25)),
709 (Quantity::MainOpening, given(f64::INFINITY)),
710 ],
711 )],
712 ),
713 (
714 1,
715 vec![(
716 "b".to_owned(),
717 vec![
718 (
719 Quantity::Burnout,
720 Outcome::Floor {
721 error: -0.75,
722 bound: 2.0,
723 clip: None,
724 },
725 ),
726 (Quantity::Burnout, given(0.75)),
727 (Quantity::Liftoff, Outcome::Withheld(Reason::TooShort)),
728 (Quantity::DrogueRate, Outcome::Absent),
729 ],
730 )],
731 ),
732 ];
733 let drogue = tally(&flights)
735 .into_iter()
736 .find(|t| t.quantity == Quantity::DrogueRate)
737 .unwrap();
738 assert_eq!((drogue.given, drogue.absent), (0, 1));
739 assert!(drogue.withheld.is_empty());
740 let errors = errors(&flights);
741 let rows: Vec<(Quantity, bool, u64)> = errors
742 .iter()
743 .map(|row| (row.quantity, row.floor, row.given))
744 .collect();
745 assert_eq!(
746 rows,
747 [
748 (Quantity::Burnout, false, 2),
749 (Quantity::Burnout, true, 1),
750 (Quantity::MainOpening, false, 1),
751 (Quantity::MainRate, false, 1),
752 ]
753 );
754 assert_eq!(
755 errors[0].spread,
756 Some(Spread {
757 mean_error: 0.25,
758 least_error: -0.25,
759 most_error: 0.75,
760 mean_bound: 1.0
761 })
762 );
763 assert_eq!(errors[1].spread.map(|s| s.mean_bound), Some(2.0));
764 assert_eq!(errors[2].spread, None);
765 }
766
767 #[test]
769 fn the_grid_names_each_recording_apart() {
770 let names: Vec<String> = grid().iter().map(name).collect();
771 let mut unique = names.clone();
772 unique.sort();
773 unique.dedup();
774 assert_eq!(unique.len(), GRID_RATES_HZ.len() * GRID_WRITTEN.len());
775 assert!(names.contains(&"20 Hz, altitude in 0.3048 m steps".to_owned()));
776 assert!(names.contains(&"0.5 Hz, pressure alone in 100 Pa steps".to_owned()));
777 }
778
779 #[test]
782 fn a_floor_holds_one_side_and_never_on_nan() {
783 let floor = |error: f64| Outcome::Floor {
784 error,
785 bound: 1.0,
786 clip: None,
787 };
788 let holds = |outcome: Outcome| {
789 tally(&[(
790 0,
791 vec![("a".to_owned(), vec![(Quantity::Burnout, outcome)])],
792 )])[0]
793 .holds()
794 };
795 assert!(holds(floor(-100.0)));
796 assert!(holds(floor(1.0)));
797 assert!(!holds(floor(1.01)));
798 assert!(!holds(floor(f64::NAN)));
799 assert!(!holds(Outcome::Given {
800 error: f64::NAN,
801 bound: 1.0
802 }));
803 }
804
805 #[test]
808 fn a_top_is_measured_against_its_parts_range() {
809 let mut flyer = Flyer::new(DesignFiles::committed());
810 let mut flights = (0..40).filter_map(|k| flyer.flight(Set::Tuning, k).ok());
811 let with = flights
812 .by_ref()
813 .find(|f| f.draw.logger.profile.accelerometer_range_m_s2().is_some())
814 .unwrap();
815 let range = with.draw.logger.profile.accelerometer_range_m_s2().unwrap();
816 let share = top_over_range(&with).unwrap();
817 assert_eq!(share, top_specific_force(&with) / range);
818 assert!(share > 0.0 && share < 1.0, "{share}");
820 let without = flights
821 .find(|f| f.draw.logger.profile.accelerometer_range_m_s2().is_none())
822 .unwrap();
823 assert_eq!(top_over_range(&without), None);
824 }
825
826 #[test]
828 fn a_log_cut_on_the_climb_counts_its_top_speed_as_a_floor() {
829 let mut flyer = Flyer::new(DesignFiles::committed());
830 let flight = (0..40)
832 .filter_map(|k| flyer.flight(Set::Tuning, k).ok())
833 .find(|f| readings::read(&f.log).max_speed.value().is_some())
834 .unwrap();
835 let events = &flight.truth.events;
836 let cut_s = flight.draw.logger.pad_s + 0.5 * (events.liftoff_s + events.apogee_s);
837 let mut log = flight.log.clone();
838 let keep = log.time_s.partition_point(|t| *t < cut_s);
839 log.time_s.truncate(keep);
840 log.altitude_m.truncate(keep);
841 for channel in &mut log.channels {
842 let keep = channel.time_s.partition_point(|t| *t < cut_s);
843 channel.time_s.truncate(keep);
844 channel.values.truncate(keep);
845 }
846 let outcome = outcomes(&flight, &log)
847 .into_iter()
848 .find(|(quantity, _)| *quantity == Quantity::TopSpeed)
849 .unwrap()
850 .1;
851 assert!(matches!(outcome, Outcome::Floor { .. }), "{outcome:?}");
852 }
853
854 #[test]
858 fn a_part_set_to_its_lower_range_clips_there() {
859 let mut flyer = Flyer::new(DesignFiles::committed());
860 let mut flights = (0..).filter_map(|k| flyer.flight(Set::Tuning, k).ok().map(|f| (k, f)));
861 let (index, imu) = flights
862 .by_ref()
863 .take(40)
864 .find(|(_, f)| top_over_lower_range(f).is_some_and(|share| share > 1.1))
865 .unwrap();
866 let lower = imu.draw.logger.profile.accelerometer_lower_range_m_s2();
867 assert_eq!(
868 lower,
869 Some(LOWER_RANGE_G * hpr_core::gravity::STANDARD_GRAVITY_MPS2)
870 );
871 let log =
872 accelerometer_log_set_lower(&imu.draw.logger, &imu.truth, Set::Tuning.seed(), index)
873 .unwrap()
874 .unwrap();
875 let read = readings::read(&log);
876 let top = read.max_acceleration.value().unwrap();
877 assert_eq!(top.range_m_s2, lower, "{top:?}");
878 assert_eq!(top.clip, Some(Clip::Range), "{top:?}");
879 assert!(top.is_floor, "{top:?}");
880 let outcome = outcomes(&imu, &log)
881 .into_iter()
882 .find(|(quantity, _)| *quantity == Quantity::TopAcceleration)
883 .map(|(_, outcome)| outcome);
884 let Some(Outcome::Floor { error, bound, clip }) = outcome else {
885 panic!("{outcome:?}");
886 };
887 assert_eq!(clip, Some(Clip::Range));
888 assert!(error < 0.0 && error <= bound, "{error}, {bound}");
889 let (index, altos) = flights
890 .take(40)
891 .find(|(_, f)| {
892 f.draw.logger.profile.accelerometer_range_m_s2().is_some()
893 && top_over_lower_range(f).is_none()
894 })
895 .unwrap();
896 assert!(
897 accelerometer_log_set_lower(
898 &altos.draw.logger,
899 &altos.truth,
900 Set::Tuning.seed(),
901 index
902 )
903 .unwrap()
904 .is_none()
905 );
906 }
907
908 #[test]
911 fn a_tuning_flight_holds_and_a_miss_is_caught() {
912 let mut flyer = Flyer::new(DesignFiles::committed());
913 let (index, flight) = (0..)
915 .map(|k| (k, flyer.flight(Set::Tuning, k)))
916 .find_map(|(k, flight)| {
917 flight
918 .ok()
919 .filter(|f| f.draw.logger.profile.recording().is_some())
920 .map(|f| (k, f))
921 })
922 .unwrap();
923 let logs = logs(&flight, Set::Tuning.seed(), index).unwrap();
924 let accelerometers = if flight
925 .log
926 .channels
927 .iter()
928 .any(|channel| channel.quantity == hpr_flightdata::log::Quantity::Acceleration)
929 {
930 ACCELEROMETER_RATES_HZ.len()
931 } else {
932 0
933 } + usize::from(
934 flight
935 .draw
936 .logger
937 .profile
938 .accelerometer_lower_range_m_s2()
939 .is_some(),
940 );
941 assert_eq!(
942 logs.len(),
943 1 + grid().len() + accelerometers + CROPPED.len() * Crop::ALL.len()
944 );
945 let results: Vec<(String, Vec<(Quantity, Outcome)>)> = logs
946 .iter()
947 .map(|(name, log)| (name.clone(), outcomes(&flight, log)))
948 .collect();
949 let tallies = tally(&[(index, results.clone())]);
950 for tally in &tallies {
951 assert!(tally.holds(), "{tally:?}");
952 }
953 let mut missed = results;
955 for (quantity, outcome) in &mut missed[0].1 {
956 if let (Quantity::ApogeeHeight, Outcome::Given { error, bound }) = (quantity, outcome) {
957 *error = 1.01 * *bound;
958 }
959 }
960 let tallies = tally(&[(index, missed)]);
961 assert!(!tallies[1].holds(), "{:?}", tallies[1]);
962 assert_eq!(tallies[1].quantity, Quantity::ApogeeHeight);
963 }
964}