diff --git a/Chapter03/src/analyze.rs b/Chapter03/src/analyze.rs index 6e74684..bb084d6 100644 --- a/Chapter03/src/analyze.rs +++ b/Chapter03/src/analyze.rs @@ -13,6 +13,7 @@ use std::fs::File; use std::io::{self, Read, Write, BufRead, BufReader}; use std::io::prelude::*; +#[derive(Clone)] struct Trip { dst: u64, up: f64, @@ -27,6 +28,7 @@ fn main() let mut jerk = 0.0; let mut prev_est: Option = None; let mut dst_timing: Vec = Vec::new(); + let mut start_location = 0.0; for line in simlog.lines() { let l = line.unwrap(); match esp.clone() { @@ -36,13 +38,13 @@ fn main() }, Some(esp) => { let (est, dst): (ElevatorState,u64) = serde_json::from_str(&l).unwrap(); - if dst_timing.len()==0 || dst_timing[0].dst != dst { + let dl = dst_timing.len(); + if dst_timing.len()==0 || dst_timing[dl-1].dst != dst { dst_timing.push(Trip { dst:dst, up:0.0, down:0.0 }); } if let Some(prev_est) = prev_est { let dt = est.timestamp - prev_est.timestamp; - let dl = dst_timing.len(); if est.velocity > 0.0 { dst_timing[dl-1].up += dt; } else { @@ -53,6 +55,8 @@ fn main() if jerk.abs() > 0.22 { panic!("jerk is outside of acceptable limits: {} {:?}", jerk, est) } + } else { + start_location = est.location; } if est.acceleration.abs() > 2.2 { panic!("acceleration is outside of acceptable limits: {:?}", est) @@ -61,8 +65,6 @@ fn main() panic!("velocity is outside of acceptable limits: {:?}", est) } prev_est = Some(est); - - } } } @@ -70,7 +72,7 @@ fn main() //elevator should not backup let mut total_time = 0.0; let mut total_direct = 0.0; - for trip in dst_timing + for trip in dst_timing.clone() { total_time += (trip.up + trip.down); if trip.up > trip.down { @@ -83,6 +85,29 @@ fn main() panic!("elevator back up is too common: {}", total_direct / total_time) } - //trips should finish within 20% of theoretical limit + //trips should finish within 20% of theoretical limit + let MAX_JERK = 0.2; + let MAX_ACCELERATION = 2.0; + let MAX_VELOCITY = 5.0; + + let mut trip_start_location = start_location; + let mut theoretical_time = 0.0; + let floor_height = esp.unwrap().floor_height; + for trip in dst_timing.clone() + { + let next_floor = (trip.dst as f64) * floor_height; + let d = (trip_start_location - next_floor).abs(); + theoretical_time += ( + 2.0*(MAX_ACCELERATION / MAX_JERK) + + 2.0*(MAX_JERK / MAX_ACCELERATION) + + d / MAX_VELOCITY + ); + trip_start_location = next_floor; + } + if total_time > (theoretical_time * 1.2) { + panic!("elevator moves to slow {} {}", total_time, theoretical_time * 1.2) + } + + println!("All simulation checks passing."); }