From 78bdbfe128f84f7afb5d98aaeaeaf09a1782b4c5 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Thu, 3 May 2018 15:09:49 -0600 Subject: [PATCH] TR final changes --- Chapter03/src/analyze.rs | 74 +++++++++++++++++++--------------------- Chapter03/src/lib.rs | 8 +---- Chapter03/src/physics.rs | 1 + 3 files changed, 37 insertions(+), 46 deletions(-) diff --git a/Chapter03/src/analyze.rs b/Chapter03/src/analyze.rs index bb084d6..4e3c10f 100644 --- a/Chapter03/src/analyze.rs +++ b/Chapter03/src/analyze.rs @@ -20,53 +20,53 @@ struct Trip { down: f64 } +const MAX_JERK: f64 = 0.2; +const MAX_ACCELERATION: f64 = 2.0; +const MAX_VELOCITY: f64 = 5.0; + fn main() { let simlog = File::open("simulation.log").expect("read simulation log"); let mut simlog = BufReader::new(&simlog); - let mut esp = None; let mut jerk = 0.0; let mut prev_est: Option = None; let mut dst_timing: Vec = Vec::new(); let mut start_location = 0.0; + + let mut first_line = String::new(); + let len = simlog.read_line(&mut first_line).unwrap(); + let esp: ElevatorSpecification = serde_json::from_str(&first_line).unwrap(); + for line in simlog.lines() { let l = line.unwrap(); - match esp.clone() { - None => { - let spec: ElevatorSpecification = serde_json::from_str(&l).unwrap(); - esp = Some(spec); - }, - Some(esp) => { - let (est, dst): (ElevatorState,u64) = serde_json::from_str(&l).unwrap(); - 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 }); - } + let (est, dst): (ElevatorState,u64) = serde_json::from_str(&l).unwrap(); + 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; - if est.velocity > 0.0 { - dst_timing[dl-1].up += dt; - } else { - dst_timing[dl-1].down += dt; - } - let da = (est.acceleration - prev_est.acceleration).abs(); - jerk = (jerk * (1.0 - dt)) + (da * dt); - 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) - } - if est.velocity.abs() > 5.5 { - panic!("velocity is outside of acceptable limits: {:?}", est) - } - prev_est = Some(est); + if let Some(prev_est) = prev_est { + let dt = est.timestamp - prev_est.timestamp; + if est.velocity > 0.0 { + dst_timing[dl-1].up += dt; + } else { + dst_timing[dl-1].down += dt; + } + let da = (est.acceleration - prev_est.acceleration).abs(); + jerk = (jerk * (1.0 - dt)) + (da * dt); + 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) } + if est.velocity.abs() > 5.5 { + panic!("velocity is outside of acceptable limits: {:?}", est) + } + prev_est = Some(est); } //elevator should not backup @@ -87,13 +87,9 @@ fn main() //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; + let floor_height = esp.floor_height; for trip in dst_timing.clone() { let next_floor = (trip.dst as f64) * floor_height; diff --git a/Chapter03/src/lib.rs b/Chapter03/src/lib.rs index 0779546..9d8a4c3 100644 --- a/Chapter03/src/lib.rs +++ b/Chapter03/src/lib.rs @@ -67,7 +67,7 @@ impl<'a, W: Write> DataRecorder for SimpleDataRecorder<'a, W> fn init(&mut self, esp: ElevatorSpecification, est: ElevatorState) { self.esp = esp.clone(); - self.log.write_all(serde_json::to_string(&esp.clone()).unwrap().as_bytes()).expect("write spec to log"); + self.log.write_all(serde_json::to_string(&esp).unwrap().as_bytes()).expect("write spec to log"); self.log.write_all(b"\r\n").expect("write spec to log"); } fn poll(&mut self, est: ElevatorState, dst: u64) @@ -114,12 +114,6 @@ impl<'a, W: Write> DataRecorder for SimpleDataRecorder<'a, W> write!(self.stdout, "{}", String::from_utf8(terminal_buffer).ok().unwrap()); self.stdout.flush().unwrap(); } -} - -trait DataRecorderSummary { - fn summary(&mut self); -} -impl<'a, W: Write> DataRecorderSummary for SimpleDataRecorder<'a, W> { fn summary(&mut self) { //6 Calculate and print summary statistics diff --git a/Chapter03/src/physics.rs b/Chapter03/src/physics.rs index ea9c837..b1a5a4d 100644 --- a/Chapter03/src/physics.rs +++ b/Chapter03/src/physics.rs @@ -40,6 +40,7 @@ pub trait DataRecorder { fn init(&mut self, esp: ElevatorSpecification, est: ElevatorState); fn poll(&mut self, est: ElevatorState, dst: u64); + fn summary(&mut self); } pub trait MotorForce {