1
0
forked from ROMEO/nexosim

Add instance name to model contexts

This commit is contained in:
Jaŭhien Piatlicki
2024-05-10 10:54:45 +02:00
parent 4b5195f981
commit 195bcdceba
13 changed files with 131 additions and 44 deletions

View File

@ -79,8 +79,8 @@ impl Model for MotorAssembly {
// clones.
motor.position = self.position.clone();
setup_context.add_model(driver, driver_mbox);
setup_context.add_model(motor, motor_mbox);
setup_context.add_model(driver, driver_mbox, "driver");
setup_context.add_model(motor, motor_mbox, "motor");
}
}
@ -105,7 +105,9 @@ fn main() {
let t0 = MonotonicTime::EPOCH;
// Assembly and initialization.
let mut simu = SimInit::new().add_model(assembly, assembly_mbox).init(t0);
let mut simu = SimInit::new()
.add_model(assembly, assembly_mbox, "assembly")
.init(t0);
// ----------
// Simulation.

View File

@ -368,9 +368,9 @@ fn main() {
// Assembly and initialization.
let mut simu = SimInit::new()
.add_model(controller, controller_mbox)
.add_model(pump, pump_mbox)
.add_model(tank, tank_mbox)
.add_model(controller, controller_mbox, "controller")
.add_model(pump, pump_mbox, "pump")
.add_model(tank, tank_mbox, "tank")
.init(t0);
// ----------

View File

@ -140,10 +140,10 @@ fn main() {
// Assembly and initialization.
let mut simu = SimInit::new()
.add_model(psu, psu_mbox)
.add_model(load1, load1_mbox)
.add_model(load2, load2_mbox)
.add_model(load3, load3_mbox)
.add_model(psu, psu_mbox, "psu")
.add_model(load1, load1_mbox, "load1")
.add_model(load2, load2_mbox, "load2")
.add_model(load3, load3_mbox, "load3")
.init(t0);
// ----------

View File

@ -53,8 +53,15 @@ impl Motor {
/// For the sake of simplicity, we do as if the rotor rotates
/// instantaneously. If the current is too weak to overcome the load or when
/// attempting to move to an opposite phase, the position remains unchanged.
pub async fn current_in(&mut self, current: (f64, f64)) {
pub async fn current_in(&mut self, current: (f64, f64), context: &Context<Self>) {
assert!(!current.0.is_nan() && !current.1.is_nan());
println!(
"Model instance {} at time {}: setting currents: {:.2} and {:.2}",
context.name(),
context.time(),
current.0,
current.1
);
let (target_phase, abs_current) = match (current.0 != 0.0, current.1 != 0.0) {
(false, false) => return,
@ -78,9 +85,16 @@ impl Motor {
}
/// Torque applied by the load [N·m] -- input port.
pub fn load(&mut self, torque: f64) {
pub fn load(&mut self, torque: f64, context: &Context<Self>) {
assert!(torque >= 0.0);
println!(
"Model instance {} at time {}: setting load: {:.2}",
context.name(),
context.time(),
torque
);
self.torque = torque;
}
}
@ -124,6 +138,13 @@ impl Driver {
/// Sets the pulse rate (sign = direction) [Hz] -- input port.
pub async fn pulse_rate(&mut self, pps: f64, context: &Context<Self>) {
println!(
"Model instance {} at time {}: setting pps: {:.2}",
context.name(),
context.time(),
pps
);
let pps = pps.signum() * pps.abs().clamp(Self::MIN_PPS, Self::MAX_PPS);
if pps == self.pps {
return;
@ -148,6 +169,12 @@ impl Driver {
_: (),
context: &'a Context<Self>,
) -> impl Future<Output = ()> + Send + 'a {
println!(
"Model instance {} at time {}: sending pulse",
context.name(),
context.time()
);
async move {
let current_out = match self.next_phase {
0 => (self.current, 0.0),
@ -205,8 +232,8 @@ fn main() {
// Assembly and initialization.
let mut simu = SimInit::new()
.add_model(driver, driver_mbox)
.add_model(motor, motor_mbox)
.add_model(driver, driver_mbox, "driver")
.add_model(motor, motor_mbox, "motor")
.init(t0);
// ----------