mirror of
https://github.com/andrew-johnson-4/Hands-On-Functional-Programming-in-Rust
synced 2024-11-18 03:25:33 +00:00
fix bugs in new simulation and motor controller
This commit is contained in:
parent
b42c902ef8
commit
7d9159cd97
@ -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();
|
||||
|
||||
|
@ -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() }
|
||||
|
@ -82,6 +82,7 @@ pub fn simulate_elevator<MC: MotorController, DR: DataRecorder>(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<MC: MotorController, DR: DataRecorder>(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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user