From 504971a163e7d436e25329bd27c92a8fd2845652 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Thu, 3 May 2018 12:23:14 -0600 Subject: [PATCH] update with TR edits (ongoing) --- Chapter03/src/lib.rs | 43 +++++++++++++----------------------------- Chapter03/src/motor.rs | 20 ++++++++++++-------- 2 files changed, 25 insertions(+), 38 deletions(-) diff --git a/Chapter03/src/lib.rs b/Chapter03/src/lib.rs index a63197a..0779546 100644 --- a/Chapter03/src/lib.rs +++ b/Chapter03/src/lib.rs @@ -157,21 +157,12 @@ pub fn run_simulation() let mut floor_requests = Vec::new(); //4. Parse input and store as building description and floor requests - match env::args().nth(1) { + let buffer = match env::args().nth(1) { Some(ref fp) if *fp == "-".to_string() => { let mut buffer = String::new(); io::stdin().read_to_string(&mut buffer) .expect("read_to_string failed"); - - for (li,l) in buffer.lines().enumerate() { - if li==0 { - esp.floor_count = l.parse::().unwrap(); - } else if li==1 { - esp.floor_height = l.parse::().unwrap(); - } else { - floor_requests.push(l.parse::().unwrap()); - } - } + buffer }, None => { let fp = "test1.txt"; @@ -180,16 +171,7 @@ pub fn run_simulation() .expect("File::open failed") .read_to_string(&mut buffer) .expect("read_to_string failed"); - - for (li,l) in buffer.lines().enumerate() { - if li==0 { - esp.floor_count = l.parse::().unwrap(); - } else if li==1 { - esp.floor_height = l.parse::().unwrap(); - } else { - floor_requests.push(l.parse::().unwrap()); - } - } + buffer }, Some(fp) => { let mut buffer = String::new(); @@ -197,16 +179,17 @@ pub fn run_simulation() .expect("File::open failed") .read_to_string(&mut buffer) .expect("read_to_string failed"); + buffer + } + }; - for (li,l) in buffer.lines().enumerate() { - if li==0 { - esp.floor_count = l.parse::().unwrap(); - } else if li==1 { - esp.floor_height = l.parse::().unwrap(); - } else { - floor_requests.push(l.parse::().unwrap()); - } - } + for (li,l) in buffer.lines().enumerate() { + if li==0 { + esp.floor_count = l.parse::().unwrap(); + } else if li==1 { + esp.floor_height = l.parse::().unwrap(); + } else { + floor_requests.push(l.parse::().unwrap()); } } diff --git a/Chapter03/src/motor.rs b/Chapter03/src/motor.rs index c7667a5..3520328 100644 --- a/Chapter03/src/motor.rs +++ b/Chapter03/src/motor.rs @@ -26,16 +26,19 @@ impl MotorController for SimpleMotorController //at an average velocity of v/2 before stopping let d = t * (est.velocity/2.0); + let dst_height = (dst as f64) * self.esp.floor_height; + //l = distance to next floor - let l = (est.location - (dst as f64)*self.esp.floor_height).abs(); + let l = (est.location - dst_height).abs(); let target_acceleration = { //are we going up? - let going_up = est.location < (dst as f64)*self.esp.floor_height; + let going_up = est.location < dst_height; //Do not exceed maximum velocity if est.velocity.abs() >= 5.0 { - if going_up==(est.velocity>0.0) { + if (going_up && est.velocity>0.0) + || (!going_up && est.velocity<0.0) { 0.0 //decelerate if going in wrong direction } else if going_up { @@ -45,7 +48,8 @@ impl MotorController for SimpleMotorController } //if within comfortable deceleration range and moving in right direction, decelerate - } else if l < d && going_up==(est.velocity>0.0) { + } else if l < d && ((going_up && est.velocity>0.0) + || (!going_up && est.velocity<0.0)) { if going_up { -1.0 } else { @@ -73,6 +77,10 @@ impl MotorController for SimpleMotorController } } +const MAX_JERK: f64 = 0.2; +const MAX_ACCELERATION: f64 = 2.0; +const MAX_VELOCITY: f64 = 5.0; + pub struct SmoothMotorController { pub esp: ElevatorSpecification, @@ -91,10 +99,6 @@ impl MotorController for SmoothMotorController { //5.3. Adjust motor control to process next floor request - let MAX_JERK = 0.2; - let MAX_ACCELERATION = 2.0; - let MAX_VELOCITY = 5.0; - //it will take t seconds to reach max from max let t_accel = MAX_ACCELERATION / MAX_JERK; let t_veloc = MAX_VELOCITY / MAX_ACCELERATION;