diff --git a/Chapter03/src/lib.rs b/Chapter03/src/lib.rs index 1e7fc06..f75dc34 100644 --- a/Chapter03/src/lib.rs +++ b/Chapter03/src/lib.rs @@ -68,11 +68,13 @@ impl<'a, W: Write> DataRecorder for SimpleDataRecorder<'a, W> { 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(b"\r\n").expect("write spec to log"); } fn poll(&mut self, est: ElevatorState, dst: u64) { let datum = (est.clone(), dst); self.log.write_all(serde_json::to_string(&datum).unwrap().as_bytes()).expect("write state to log"); + self.log.write_all(b"\r\n").expect("write state to log"); //5.4. Print realtime statistics print!("{}{}{}", clear::All, cursor::Goto(1, 1), cursor::Hide); @@ -135,14 +137,17 @@ pub fn run_simulation() location: 0.0, velocity: 0.0, acceleration: 0.0, - motor_input: MotorInput::Up { voltage: 0.0 } + motor_input: MotorInput::Up { + //zero is positive force to counter gravity + voltage: 9.8 * (120000.0 / 8.0) + } }; //3. Store input building description and floor requests let mut esp = ElevatorSpecification { floor_count: 0, floor_height: 0.0, - carriage_weight: 0.0 + carriage_weight: 120000.0 }; let mut floor_requests = Vec::new(); diff --git a/Chapter03/src/motor.rs b/Chapter03/src/motor.rs index 2ab7509..aacd2a1 100644 --- a/Chapter03/src/motor.rs +++ b/Chapter03/src/motor.rs @@ -165,7 +165,11 @@ impl MotorController for SmoothMotorController let gravity_adjusted_acceleration = target_acceleration + 9.8; let target_force = gravity_adjusted_acceleration * self.esp.carriage_weight; let target_voltage = target_force / 8.0; - if target_voltage > 0.0 { + if !target_voltage.is_finite() { + //divide by zero etc. + //may happen if time delta underflows + MotorInput::Up { voltage: 0.0 } + } else if target_voltage > 0.0 { MotorInput::Up { voltage: target_voltage } } else { MotorInput::Down { voltage: target_voltage.abs() } diff --git a/Chapter03/src/physics.rs b/Chapter03/src/physics.rs index 0e0cff5..8dba511 100644 --- a/Chapter03/src/physics.rs +++ b/Chapter03/src/physics.rs @@ -82,6 +82,7 @@ pub fn simulate_elevator(esp: ElevatorSpe //5. Loop while there are remaining floor requests let original_ts = Instant::now(); + thread::sleep(time::Duration::from_millis(10)); while req.len() > 0 { //5.1. Update location, velocity, and acceleration @@ -108,12 +109,12 @@ pub fn simulate_elevator(esp: ElevatorSpe req.remove(0); } - //5.3. Adjust motor control to process next floor request - est.motor_input = mc.poll(est.clone(), next_floor); - //5.4. Print realtime statistics dr.poll(est.clone(), next_floor); + //5.3. Adjust motor control to process next floor request + est.motor_input = mc.poll(est.clone(), next_floor); + thread::sleep(time::Duration::from_millis(10)); } }