common: Make Logger calculate and publish pace.
authorTilman Sauerbeck <tilman@code-monkey.de>
Sun, 5 Jul 2020 16:35:09 +0000 (18:35 +0200)
committerTilman Sauerbeck <tilman@code-monkey.de>
Sun, 6 Sep 2020 17:57:23 +0000 (19:57 +0200)
Every 1000m, pace is set to the time in seconds it took to travel
that distance.

src/common/logger.rs
test/logger_test.rs

index ea84bc23d4e23ad71d73f6ec6859977e1f913a6b..98a857a660ed1cbcebdbecb06cf5c8f7738f34ca 100644 (file)
@@ -119,6 +119,10 @@ pub struct Logger<'a> {
     // The total distance logged of the currently running recording.
     pub total_distance_cm: u32,
 
+    split_distance_cm: u32,
+    split_duration_ms: u32,
+    pub pace_s: u32,
+
     // The number of slots filled in num_flight.
     num_in_flight: usize,
 
@@ -294,6 +298,9 @@ impl<'a> Logger<'a> {
             first_sector: 0,
             recording_started: 0,
             total_distance_cm: 0,
+            split_distance_cm: 0,
+            split_duration_ms: 0,
+            pace_s: 0,
             num_in_flight: 0,
 
             in_flight: [InFlight::new(); 7],
@@ -362,6 +369,9 @@ impl<'a> Logger<'a> {
         self.sectors_written = 0;
         self.recording_started = tap.unix_time;
         self.total_distance_cm = 0;
+        self.split_distance_cm = 0;
+        self.split_duration_ms = 0;
+        self.pace_s = 0;
         self.num_in_flight = 0;
 
         self.prepare_write_buffer(true);
@@ -372,8 +382,6 @@ impl<'a> Logger<'a> {
     }
 
     pub fn log(&mut self, prev_tap: &TimeAndPos, tap: &TimeAndPos) {
-        self.total_distance_cm += tap.distance_cm(&prev_tap) as u32;
-
         let d_time_ms = elapsed_ms(tap.system_time, prev_tap.system_time);
 
         // We know that our hardware cannot deliver updates more often
@@ -390,6 +398,20 @@ impl<'a> Logger<'a> {
         if self.write_packet(d_time_s, d_lat, d_lon) {
             self.flush_in_flight(false);
         }
+
+        let distance_cm = tap.distance_cm(&prev_tap) as u32;
+
+        self.total_distance_cm += distance_cm;
+        self.split_distance_cm += distance_cm;
+
+        self.split_duration_ms += d_time_ms;
+
+        if self.split_distance_cm >= 100_000 {
+            self.split_distance_cm -= 100_000;
+
+            self.pace_s = self.split_duration_ms / 1000;
+            self.split_duration_ms = 0;
+        }
     }
 
     pub fn stop_recording(&mut self, tap: &TimeAndPos) -> u16 {
index 784c04955cc567938e852abfbd7d3b8e8b0bb43c..18b0fbb9921854a4ce876a9f5da0abe4631de32e 100644 (file)
@@ -572,3 +572,42 @@ fn remove_recording1() {
 
     assert_eq!(fake_storage.expected, fake_storage.actual);
 }
+
+#[test]
+fn pace0() {
+    let mut fake_storage = FakeStorage::new();
+
+    let mut logger = Logger::new(&mut fake_storage);
+    logger.init();
+
+    let lat_lon = [
+        (49.372117, 8.820219),
+        (49.372229, 8.821636),
+    ];
+
+    let template = gps::TimeAndPos {
+        system_time: 0,
+        unix_time: 0,
+        latitude_deg: 0,
+        longitude_deg: 0,
+        latitude_rad: Fixed::from_f32(0.0),
+        longitude_rad: Fixed::from_f32(0.0),
+    };
+
+    let mut taps = [template; 11];
+
+    for i in 0..taps.len() {
+        taps[i].system_time = (i as u32) * 100;
+        taps[i].unix_time = i as u32;
+        taps[i].latitude_rad = Fixed::from_f32(lat_lon[i % lat_lon.len()].0).to_radians();
+        taps[i].longitude_rad = Fixed::from_f32(lat_lon[i % lat_lon.len()].1).to_radians();
+    }
+
+    logger.start_recording(&taps[0]);
+
+    for i in 0..(taps.len() - 1) {
+        logger.log(&taps[i], &taps[i + 1]);
+    }
+
+    assert_eq!(logger.pace_s, 10);
+}