From 4d67bf5533296423d3598cb174758ad42bca95dd Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Fri, 6 Apr 2018 18:48:15 -0600 Subject: [PATCH] simulation driver --- Chapter03/src/lib.rs | 32 +++++++++++++++++--------------- Chapter03/src/motor.rs | 6 +++--- Chapter03/src/physics.rs | 13 +++++++++++++ 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/Chapter03/src/lib.rs b/Chapter03/src/lib.rs index e9f96a2..2a4dcca 100644 --- a/Chapter03/src/lib.rs +++ b/Chapter03/src/lib.rs @@ -1,7 +1,8 @@ mod physics; mod motor; -use physics::{ElevatorSpecification, ElevatorState, MotorInput, simulate_elevator, DataRecorder, MotorController}; +use physics::{ElevatorSpecification, ElevatorState, MotorInput, simulate_elevator, DataRecorder, MotorController, MotorVoltage}; +use motor::{SmoothMotorController}; extern crate floating_duration; use std::time::Instant; @@ -17,12 +18,12 @@ use termion::input::TermRead; use termion::event::Key; use std::cmp; -fn variable_summary(stdout: &mut raw::RawTerminal, vname: String, data: Vec) { +fn variable_summary(stdout: &mut raw::RawTerminal, vname: String, data: &Vec) { let (avg, dev) = variable_summary_stats(data); variable_summary_print(stdout, vname, avg, dev); } -fn variable_summary_stats(data: Vec) -> (f64, f64) +fn variable_summary_stats(data: &Vec) -> (f64, f64) { //calculate statistics let N = data.len(); @@ -46,18 +47,18 @@ fn variable_summary_print(stdout: &mut raw::RawTerminal, vname: Str write!(stdout, "\r\n"); } -struct SimpleDataRecorder +struct SimpleDataRecorder<'a, W: 'a + Write> { esp: ElevatorSpecification, termwidth: u64, termheight: u64, - stdout: &'static raw::RawTerminal, + stdout: &'a mut raw::RawTerminal, record_location: Vec, record_velocity: Vec, record_acceleration: Vec, record_voltage: Vec, } -impl DataRecorder for SimpleDataRecorder +impl<'a, W: Write> DataRecorder for SimpleDataRecorder<'a, W> { fn init(&mut self, esp: ElevatorSpecification, est: ElevatorState) { @@ -103,15 +104,15 @@ impl DataRecorder for SimpleDataRecorder trait DataRecorderSummary { fn summary(&mut self); } -impl DataRecorderSummary for SimpleDataRecorder { +impl<'a, W: Write> DataRecorderSummary for SimpleDataRecorder<'a, W> { fn summary(&mut self) { //6 Calculate and print summary statistics write!(self.stdout, "{}{}{}", clear::All, cursor::Goto(1, 1), cursor::Show).unwrap(); - variable_summary(&mut self.stdout, "location".to_string(), self.record_location); - variable_summary(&mut self.stdout, "velocity".to_string(), self.record_velocity); - variable_summary(&mut self.stdout, "acceleration".to_string(), self.record_acceleration); - variable_summary(&mut self.stdout, "voltage".to_string(), self.record_voltage); + variable_summary(&mut self.stdout, "location".to_string(), &self.record_location); + variable_summary(&mut self.stdout, "velocity".to_string(), &self.record_velocity); + variable_summary(&mut self.stdout, "acceleration".to_string(), &self.record_acceleration); + variable_summary(&mut self.stdout, "voltage".to_string(), &self.record_voltage); self.stdout.flush().unwrap(); } } @@ -192,21 +193,22 @@ pub fn run_simulation() } let termsize = termion::terminal_size().ok(); - let dr = SimpleDataRecorder { + let mut dr = SimpleDataRecorder { esp: esp.clone(), termwidth: termsize.map(|(w,_)| w-2).expect("termwidth") as u64, termheight: termsize.map(|(_,h)| h-2).expect("termheight") as u64, - stdout: io::stdout().into_raw_mode().unwrap(), + stdout: &mut io::stdout().into_raw_mode().unwrap(), record_location: Vec::new(), record_velocity: Vec::new(), record_acceleration: Vec::new(), record_voltage: Vec::new() }; - let mc = SimpleMotorController { + let mut mc = SmoothMotorController { + timestamp: Instant::now(), esp: esp.clone() }; - simulate_elevator(esp, est, floor_requests, mc, dr); + simulate_elevator(esp, est, floor_requests, &mut mc, &mut dr); dr.summary(); } diff --git a/Chapter03/src/motor.rs b/Chapter03/src/motor.rs index a87e292..23eac07 100644 --- a/Chapter03/src/motor.rs +++ b/Chapter03/src/motor.rs @@ -5,7 +5,7 @@ use physics::{ElevatorSpecification, ElevatorState, MotorInput, MotorController} pub struct SimpleMotorController { - esp: ElevatorSpecification + pub esp: ElevatorSpecification } impl MotorController for SimpleMotorController @@ -75,8 +75,8 @@ impl MotorController for SimpleMotorController pub struct SmoothMotorController { - esp: ElevatorSpecification, - timestamp: Instant + pub esp: ElevatorSpecification, + pub timestamp: Instant } impl MotorController for SmoothMotorController diff --git a/Chapter03/src/physics.rs b/Chapter03/src/physics.rs index d80bd41..1080a11 100644 --- a/Chapter03/src/physics.rs +++ b/Chapter03/src/physics.rs @@ -55,6 +55,19 @@ impl MotorForce for MotorInput { } } +pub trait MotorVoltage { + fn voltage(&self) -> f64; +} +impl MotorVoltage for MotorInput { + fn voltage(&self) -> f64 + { + match *self { + MotorInput::Up { voltage: v } => { v } + MotorInput::Down { voltage: v } => { -v } + } + } +} + pub fn simulate_elevator(esp: ElevatorSpecification, est: ElevatorState, req: FloorRequests, mc: &mut MC, dr: &mut DR) {