From 33aa52ca4d0f1043c3492c5939bc40c8d457f064 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 30 Jan 2025 18:33:22 +0100 Subject: [PATCH] update mini simulator --- satrs-example/pytmtc/pyproject.toml | 2 +- satrs-example/pytmtc/pytmtc/config.py | 2 +- satrs-minisim/Cargo.toml | 9 +-- satrs-minisim/src/acs.rs | 17 +++-- satrs-minisim/src/controller.rs | 100 +++++++++++++++++--------- satrs-minisim/src/eps.rs | 21 +++--- satrs-minisim/src/lib.rs | 2 +- satrs-minisim/src/main.rs | 23 +++--- satrs-minisim/src/time.rs | 2 +- satrs/Cargo.toml | 4 +- 10 files changed, 107 insertions(+), 75 deletions(-) diff --git a/satrs-example/pytmtc/pyproject.toml b/satrs-example/pytmtc/pyproject.toml index fcb4a32..bcbb0ab 100644 --- a/satrs-example/pytmtc/pyproject.toml +++ b/satrs-example/pytmtc/pyproject.toml @@ -12,7 +12,7 @@ authors = [ {name = "Robin Mueller", email = "robin.mueller.m@gmail.com"}, ] dependencies = [ - "tmtccmd~=8.0", + "tmtccmd~=8.1", "pydantic~=2.7" ] diff --git a/satrs-example/pytmtc/pytmtc/config.py b/satrs-example/pytmtc/pytmtc/config.py index 6647769..150b9c1 100644 --- a/satrs-example/pytmtc/pytmtc/config.py +++ b/satrs-example/pytmtc/pytmtc/config.py @@ -12,7 +12,7 @@ from pytmtc.pus_tc import create_cmd_definition_tree class SatrsConfigHook(HookBase): def __init__(self, json_cfg_path: str): - super().__init__(json_cfg_path=json_cfg_path) + super().__init__(json_cfg_path) def get_communication_interface(self, com_if_key: str) -> Optional[ComInterface]: from tmtccmd.config.com import ( diff --git a/satrs-minisim/Cargo.toml b/satrs-minisim/Cargo.toml index 0842844..ea1dc08 100644 --- a/satrs-minisim/Cargo.toml +++ b/satrs-minisim/Cargo.toml @@ -14,11 +14,12 @@ fern = "0.7" strum = { version = "0.26", features = ["derive"] } num_enum = "0.7" humantime = "2" +tai-time = { version = "0.3", features = ["serde"] } -[dependencies.asynchronix] -version = "0.2.2" -# git = "https://github.com/asynchronics/asynchronix.git" -# branch = "main" +[dependencies.nexosim] +version = "0.3.1" +git = "https://github.com/us-irs/nexosim.git" +branch = "explicit-serde-feature" features = ["serde"] [dependencies.satrs] diff --git a/satrs-minisim/src/acs.rs b/satrs-minisim/src/acs.rs index af6566e..0b1a987 100644 --- a/satrs-minisim/src/acs.rs +++ b/satrs-minisim/src/acs.rs @@ -1,8 +1,8 @@ use std::{f32::consts::PI, sync::mpsc, time::Duration}; -use asynchronix::{ - model::{Model, Output}, - time::Scheduler, +use nexosim::{ + model::{Context, Model}, + ports::Output, }; use satrs::power::SwitchStateBinary; use satrs_minisim::{ @@ -55,7 +55,7 @@ impl MagnetometerModel { self.switch_state = switch_state; } - pub async fn send_sensor_values(&mut self, _: (), scheduler: &Scheduler) { + pub async fn send_sensor_values(&mut self, _: (), scheduler: &mut Context) { self.reply_sender .send(ReplyProvider::create_mgm_reply(MgmReplyCommon { switch_state: self.switch_state, @@ -114,11 +114,11 @@ impl MagnetorquerModel { pub async fn apply_torque( &mut self, duration_and_dipole: (Duration, MgtDipole), - scheduler: &Scheduler, + cx: &mut Context, ) { self.torque_dipole = duration_and_dipole.1; self.torquing = true; - if scheduler + if cx .schedule_event(duration_and_dipole.0, Self::clear_torque, ()) .is_err() { @@ -138,12 +138,11 @@ impl MagnetorquerModel { self.generate_magnetic_field(()).await; } - pub async fn request_housekeeping_data(&mut self, _: (), scheduler: &Scheduler) { + pub async fn request_housekeeping_data(&mut self, _: (), cx: &mut Context) { if self.switch_state != SwitchStateBinary::On { return; } - scheduler - .schedule_event(Duration::from_millis(15), Self::send_housekeeping_data, ()) + cx.schedule_event(Duration::from_millis(15), Self::send_housekeeping_data, ()) .expect("requesting housekeeping data failed") } diff --git a/satrs-minisim/src/controller.rs b/satrs-minisim/src/controller.rs index 4fe1495..e3f1544 100644 --- a/satrs-minisim/src/controller.rs +++ b/satrs-minisim/src/controller.rs @@ -1,7 +1,7 @@ use std::{sync::mpsc, time::Duration}; -use asynchronix::{ - simulation::{Address, Simulation}, +use nexosim::{ + simulation::{Address, Scheduler, Simulation}, time::{Clock, MonotonicTime, SystemClock}, }; use satrs_minisim::{ @@ -23,35 +23,52 @@ const MGM_REQ_WIRETAPPING: bool = false; const PCDU_REQ_WIRETAPPING: bool = false; const MGT_REQ_WIRETAPPING: bool = false; +pub struct ModelAddrWrapper { + mgm_addr: Address>, + pcdu_addr: Address, + mgt_addr: Address, +} + // The simulation controller processes requests and drives the simulation. +#[allow(dead_code)] pub struct SimController { pub sys_clock: SystemClock, pub request_receiver: mpsc::Receiver, pub reply_sender: mpsc::Sender, pub simulation: Simulation, - pub mgm_addr: Address>, - pub pcdu_addr: Address, - pub mgt_addr: Address, + pub scheduler: Scheduler, + pub addr_wrapper: ModelAddrWrapper, } +impl ModelAddrWrapper { + pub fn new( + mgm_addr: Address>, + pcdu_addr: Address, + mgt_addr: Address, + ) -> Self { + Self { + mgm_addr, + pcdu_addr, + mgt_addr, + } + } +} impl SimController { pub fn new( sys_clock: SystemClock, request_receiver: mpsc::Receiver, reply_sender: mpsc::Sender, simulation: Simulation, - mgm_addr: Address>, - pcdu_addr: Address, - mgt_addr: Address, + scheduler: Scheduler, + addr_wrapper: ModelAddrWrapper, ) -> Self { Self { sys_clock, request_receiver, reply_sender, simulation, - mgm_addr, - pcdu_addr, - mgt_addr, + scheduler, + addr_wrapper, } } @@ -62,7 +79,7 @@ impl SimController { // Check for UDP requests every millisecond. Shift the simulator ahead here to prevent // replies lying in the past. t += Duration::from_millis(udp_polling_interval_ms); - self.sys_clock.synchronize(t); + let _synch_status = self.sys_clock.synchronize(t); self.handle_sim_requests(t_old); self.simulation .step_until(t) @@ -118,11 +135,13 @@ impl SimController { } match mgm_request { MgmRequestLis3Mdl::RequestSensorData => { - self.simulation.send_event( - MagnetometerModel::send_sensor_values, - (), - &self.mgm_addr, - ); + self.simulation + .process_event( + MagnetometerModel::send_sensor_values, + (), + &self.addr_wrapper.mgm_addr, + ) + .expect("event execution error for mgm"); } } Ok(()) @@ -136,14 +155,21 @@ impl SimController { match pcdu_request { PcduRequest::RequestSwitchInfo => { self.simulation - .send_event(PcduModel::request_switch_info, (), &self.pcdu_addr); + .process_event( + PcduModel::request_switch_info, + (), + &self.addr_wrapper.pcdu_addr, + ) + .unwrap(); } PcduRequest::SwitchDevice { switch, state } => { - self.simulation.send_event( - PcduModel::switch_device, - (switch, state), - &self.pcdu_addr, - ); + self.simulation + .process_event( + PcduModel::switch_device, + (switch, state), + &self.addr_wrapper.pcdu_addr, + ) + .unwrap(); } } Ok(()) @@ -155,17 +181,23 @@ impl SimController { log::info!("received MGT request: {:?}", mgt_request); } match mgt_request { - MgtRequest::ApplyTorque { duration, dipole } => self.simulation.send_event( - MagnetorquerModel::apply_torque, - (duration, dipole), - &self.mgt_addr, - ), - MgtRequest::RequestHk => self.simulation.send_event( - MagnetorquerModel::request_housekeeping_data, - (), - &self.mgt_addr, - ), - } + MgtRequest::ApplyTorque { duration, dipole } => self + .simulation + .process_event( + MagnetorquerModel::apply_torque, + (duration, dipole), + &self.addr_wrapper.mgt_addr, + ) + .unwrap(), + MgtRequest::RequestHk => self + .simulation + .process_event( + MagnetorquerModel::request_housekeeping_data, + (), + &self.addr_wrapper.mgt_addr, + ) + .unwrap(), + }; Ok(()) } diff --git a/satrs-minisim/src/eps.rs b/satrs-minisim/src/eps.rs index c07e290..53745af 100644 --- a/satrs-minisim/src/eps.rs +++ b/satrs-minisim/src/eps.rs @@ -1,8 +1,8 @@ use std::{sync::mpsc, time::Duration}; -use asynchronix::{ - model::{Model, Output}, - time::Scheduler, +use nexosim::{ + model::{Context, Model}, + ports::Output, }; use satrs::power::SwitchStateBinary; use satrs_minisim::{ @@ -29,14 +29,13 @@ impl PcduModel { } } - pub async fn request_switch_info(&mut self, _: (), scheduler: &Scheduler) { - scheduler - .schedule_event( - Duration::from_millis(SWITCH_INFO_DELAY_MS), - Self::send_switch_info, - (), - ) - .expect("requesting switch info failed"); + pub async fn request_switch_info(&mut self, _: (), cx: &mut Context) { + cx.schedule_event( + Duration::from_millis(SWITCH_INFO_DELAY_MS), + Self::send_switch_info, + (), + ) + .expect("requesting switch info failed"); } pub fn send_switch_info(&mut self) { diff --git a/satrs-minisim/src/lib.rs b/satrs-minisim/src/lib.rs index b3b7d44..b97cd5b 100644 --- a/satrs-minisim/src/lib.rs +++ b/satrs-minisim/src/lib.rs @@ -1,4 +1,4 @@ -use asynchronix::time::MonotonicTime; +use nexosim::time::MonotonicTime; use num_enum::{IntoPrimitive, TryFromPrimitive}; use serde::{de::DeserializeOwned, Deserialize, Serialize}; diff --git a/satrs-minisim/src/main.rs b/satrs-minisim/src/main.rs index 4b6c240..73f3462 100644 --- a/satrs-minisim/src/main.rs +++ b/satrs-minisim/src/main.rs @@ -1,8 +1,8 @@ use acs::{MagnetometerModel, MagnetorquerModel}; -use asynchronix::simulation::{Mailbox, SimInit}; -use asynchronix::time::{MonotonicTime, SystemClock}; -use controller::SimController; +use controller::{ModelAddrWrapper, SimController}; use eps::PcduModel; +use nexosim::simulation::{Mailbox, SimInit}; +use nexosim::time::{MonotonicTime, SystemClock}; use satrs_minisim::udp::SIM_CTRL_PORT; use satrs_minisim::{SimReply, SimRequest}; use std::sync::mpsc; @@ -63,19 +63,20 @@ fn create_sim_controller( } else { SimInit::new() }; - let simulation = sim_init - .add_model(mgm_model, mgm_mailbox) - .add_model(pcdu_model, pcdu_mailbox) - .add_model(mgt_model, mgt_mailbox) - .init(start_time); + let addrs = ModelAddrWrapper::new(mgm_addr, pcdu_addr, mgt_addr); + let (simulation, scheduler) = sim_init + .add_model(mgm_model, mgm_mailbox, "MGM model") + .add_model(pcdu_model, pcdu_mailbox, "PCDU model") + .add_model(mgt_model, mgt_mailbox, "MGT model") + .init(start_time) + .unwrap(); SimController::new( sys_clock, request_receiver, reply_sender, simulation, - mgm_addr, - pcdu_addr, - mgt_addr, + scheduler, + addrs, ) } diff --git a/satrs-minisim/src/time.rs b/satrs-minisim/src/time.rs index 63ae327..2d78d2b 100644 --- a/satrs-minisim/src/time.rs +++ b/satrs-minisim/src/time.rs @@ -1,4 +1,4 @@ -use asynchronix::time::MonotonicTime; +use nexosim::time::MonotonicTime; pub fn current_millis(time: MonotonicTime) -> u64 { (time.as_secs() as u64 * 1000) + (time.subsec_nanos() as u64 / 1_000_000) diff --git a/satrs/Cargo.toml b/satrs/Cargo.toml index a57c335..36925d7 100644 --- a/satrs/Cargo.toml +++ b/satrs/Cargo.toml @@ -31,9 +31,9 @@ version = "0.13" default-features = false [dependencies.cobs] -git = "https://github.com/robamu/cobs.rs.git" +git = "https://github.com/jamesmunns/cobs.rs.git" version = "0.2.3" -branch = "all_features" +branch = "main" default-features = false [dependencies.num-traits]