update mini simulator #214

Merged
muellerr merged 2 commits from update-minisim into main 2025-01-30 18:57:28 +01:00
17 changed files with 123 additions and 87 deletions

View File

@ -1,5 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
"""Example client for the sat-rs example application""" """Example client for the sat-rs example application"""
import logging import logging
import sys import sys
import time import time

View File

@ -12,7 +12,7 @@ authors = [
{name = "Robin Mueller", email = "robin.mueller.m@gmail.com"}, {name = "Robin Mueller", email = "robin.mueller.m@gmail.com"},
] ]
dependencies = [ dependencies = [
"tmtccmd~=8.0", "tmtccmd~=8.1",
"pydantic~=2.7" "pydantic~=2.7"
] ]

View File

@ -0,0 +1,11 @@
from tmtccmd.config import CmdTreeNode
def create_acs_node(mode_node: CmdTreeNode, hk_node: CmdTreeNode) -> CmdTreeNode:
acs_node = CmdTreeNode("acs", "ACS Subsystem Node")
mgm_node = CmdTreeNode("mgms", "MGM devices node")
mgm_node.add_child(mode_node)
mgm_node.add_child(hk_node)
acs_node.add_child(mgm_node)
return acs_node

View File

@ -12,7 +12,7 @@ from pytmtc.pus_tc import create_cmd_definition_tree
class SatrsConfigHook(HookBase): class SatrsConfigHook(HookBase):
def __init__(self, json_cfg_path: str): 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]: def get_communication_interface(self, com_if_key: str) -> Optional[ComInterface]:
from tmtccmd.config.com import ( from tmtccmd.config.com import (

View File

View File

@ -4,7 +4,7 @@ from spacepackets.ecss.pus_3_hk import Subservice
from spacepackets.ecss import PusTm from spacepackets.ecss import PusTm
from pytmtc.common import AcsId, Apid from pytmtc.common import AcsId, Apid
from pytmtc.mgms import handle_mgm_hk_report from pytmtc.acs.mgms import handle_mgm_hk_report
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)

View File

@ -17,8 +17,9 @@ from tmtccmd.tmtc import (
) )
from tmtccmd.pus.s11_tc_sched import create_time_tagged_cmd from tmtccmd.pus.s11_tc_sched import create_time_tagged_cmd
from pytmtc.acs import create_acs_node
from pytmtc.common import Apid from pytmtc.common import Apid
from pytmtc.mgms import create_mgm_cmds from pytmtc.acs.mgms import create_mgm_cmds
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -67,7 +68,6 @@ class TcHandler(TcHandlerBase):
def create_cmd_definition_tree() -> CmdTreeNode: def create_cmd_definition_tree() -> CmdTreeNode:
root_node = CmdTreeNode.root_node() root_node = CmdTreeNode.root_node()
hk_node = CmdTreeNode("hk", "Housekeeping Node", hide_children_for_print=True) hk_node = CmdTreeNode("hk", "Housekeeping Node", hide_children_for_print=True)
@ -101,15 +101,7 @@ def create_cmd_definition_tree() -> CmdTreeNode:
) )
) )
root_node.add_child(scheduler_node) root_node.add_child(scheduler_node)
root_node.add_child(create_acs_node(mode_node, hk_node))
acs_node = CmdTreeNode("acs", "ACS Subsystem Node")
mgm_node = CmdTreeNode("mgms", "MGM devices node")
mgm_node.add_child(mode_node)
mgm_node.add_child(hk_node)
acs_node.add_child(mgm_node)
root_node.add_child(acs_node)
return root_node return root_node

View File

@ -14,11 +14,12 @@ fern = "0.7"
strum = { version = "0.26", features = ["derive"] } strum = { version = "0.26", features = ["derive"] }
num_enum = "0.7" num_enum = "0.7"
humantime = "2" humantime = "2"
tai-time = { version = "0.3", features = ["serde"] }
[dependencies.asynchronix] [dependencies.nexosim]
version = "0.2.2" version = "0.3.1"
# git = "https://github.com/asynchronics/asynchronix.git" git = "https://github.com/us-irs/nexosim.git"
# branch = "main" branch = "explicit-serde-feature"
features = ["serde"] features = ["serde"]
[dependencies.satrs] [dependencies.satrs]

View File

@ -1,8 +1,8 @@
use std::{f32::consts::PI, sync::mpsc, time::Duration}; use std::{f32::consts::PI, sync::mpsc, time::Duration};
use asynchronix::{ use nexosim::{
model::{Model, Output}, model::{Context, Model},
time::Scheduler, ports::Output,
}; };
use satrs::power::SwitchStateBinary; use satrs::power::SwitchStateBinary;
use satrs_minisim::{ use satrs_minisim::{
@ -55,7 +55,7 @@ impl<ReplyProvider: MgmReplyProvider> MagnetometerModel<ReplyProvider> {
self.switch_state = switch_state; self.switch_state = switch_state;
} }
pub async fn send_sensor_values(&mut self, _: (), scheduler: &Scheduler<Self>) { pub async fn send_sensor_values(&mut self, _: (), scheduler: &mut Context<Self>) {
self.reply_sender self.reply_sender
.send(ReplyProvider::create_mgm_reply(MgmReplyCommon { .send(ReplyProvider::create_mgm_reply(MgmReplyCommon {
switch_state: self.switch_state, switch_state: self.switch_state,
@ -114,11 +114,11 @@ impl MagnetorquerModel {
pub async fn apply_torque( pub async fn apply_torque(
&mut self, &mut self,
duration_and_dipole: (Duration, MgtDipole), duration_and_dipole: (Duration, MgtDipole),
scheduler: &Scheduler<Self>, cx: &mut Context<Self>,
) { ) {
self.torque_dipole = duration_and_dipole.1; self.torque_dipole = duration_and_dipole.1;
self.torquing = true; self.torquing = true;
if scheduler if cx
.schedule_event(duration_and_dipole.0, Self::clear_torque, ()) .schedule_event(duration_and_dipole.0, Self::clear_torque, ())
.is_err() .is_err()
{ {
@ -138,12 +138,11 @@ impl MagnetorquerModel {
self.generate_magnetic_field(()).await; self.generate_magnetic_field(()).await;
} }
pub async fn request_housekeeping_data(&mut self, _: (), scheduler: &Scheduler<Self>) { pub async fn request_housekeeping_data(&mut self, _: (), cx: &mut Context<Self>) {
if self.switch_state != SwitchStateBinary::On { if self.switch_state != SwitchStateBinary::On {
return; return;
} }
scheduler cx.schedule_event(Duration::from_millis(15), Self::send_housekeeping_data, ())
.schedule_event(Duration::from_millis(15), Self::send_housekeeping_data, ())
.expect("requesting housekeeping data failed") .expect("requesting housekeeping data failed")
} }

View File

@ -1,7 +1,7 @@
use std::{sync::mpsc, time::Duration}; use std::{sync::mpsc, time::Duration};
use asynchronix::{ use nexosim::{
simulation::{Address, Simulation}, simulation::{Address, Scheduler, Simulation},
time::{Clock, MonotonicTime, SystemClock}, time::{Clock, MonotonicTime, SystemClock},
}; };
use satrs_minisim::{ use satrs_minisim::{
@ -23,35 +23,52 @@ const MGM_REQ_WIRETAPPING: bool = false;
const PCDU_REQ_WIRETAPPING: bool = false; const PCDU_REQ_WIRETAPPING: bool = false;
const MGT_REQ_WIRETAPPING: bool = false; const MGT_REQ_WIRETAPPING: bool = false;
pub struct ModelAddrWrapper {
mgm_addr: Address<MagnetometerModel<MgmLis3MdlReply>>,
pcdu_addr: Address<PcduModel>,
mgt_addr: Address<MagnetorquerModel>,
}
// The simulation controller processes requests and drives the simulation. // The simulation controller processes requests and drives the simulation.
#[allow(dead_code)]
pub struct SimController { pub struct SimController {
pub sys_clock: SystemClock, pub sys_clock: SystemClock,
pub request_receiver: mpsc::Receiver<SimRequest>, pub request_receiver: mpsc::Receiver<SimRequest>,
pub reply_sender: mpsc::Sender<SimReply>, pub reply_sender: mpsc::Sender<SimReply>,
pub simulation: Simulation, pub simulation: Simulation,
pub mgm_addr: Address<MagnetometerModel<MgmLis3MdlReply>>, pub scheduler: Scheduler,
pub pcdu_addr: Address<PcduModel>, pub addr_wrapper: ModelAddrWrapper,
pub mgt_addr: Address<MagnetorquerModel>,
} }
impl ModelAddrWrapper {
pub fn new(
mgm_addr: Address<MagnetometerModel<MgmLis3MdlReply>>,
pcdu_addr: Address<PcduModel>,
mgt_addr: Address<MagnetorquerModel>,
) -> Self {
Self {
mgm_addr,
pcdu_addr,
mgt_addr,
}
}
}
impl SimController { impl SimController {
pub fn new( pub fn new(
sys_clock: SystemClock, sys_clock: SystemClock,
request_receiver: mpsc::Receiver<SimRequest>, request_receiver: mpsc::Receiver<SimRequest>,
reply_sender: mpsc::Sender<SimReply>, reply_sender: mpsc::Sender<SimReply>,
simulation: Simulation, simulation: Simulation,
mgm_addr: Address<MagnetometerModel<MgmLis3MdlReply>>, scheduler: Scheduler,
pcdu_addr: Address<PcduModel>, addr_wrapper: ModelAddrWrapper,
mgt_addr: Address<MagnetorquerModel>,
) -> Self { ) -> Self {
Self { Self {
sys_clock, sys_clock,
request_receiver, request_receiver,
reply_sender, reply_sender,
simulation, simulation,
mgm_addr, scheduler,
pcdu_addr, addr_wrapper,
mgt_addr,
} }
} }
@ -62,7 +79,7 @@ impl SimController {
// Check for UDP requests every millisecond. Shift the simulator ahead here to prevent // Check for UDP requests every millisecond. Shift the simulator ahead here to prevent
// replies lying in the past. // replies lying in the past.
t += Duration::from_millis(udp_polling_interval_ms); 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.handle_sim_requests(t_old);
self.simulation self.simulation
.step_until(t) .step_until(t)
@ -118,11 +135,13 @@ impl SimController {
} }
match mgm_request { match mgm_request {
MgmRequestLis3Mdl::RequestSensorData => { MgmRequestLis3Mdl::RequestSensorData => {
self.simulation.send_event( self.simulation
MagnetometerModel::send_sensor_values, .process_event(
(), MagnetometerModel::send_sensor_values,
&self.mgm_addr, (),
); &self.addr_wrapper.mgm_addr,
)
.expect("event execution error for mgm");
} }
} }
Ok(()) Ok(())
@ -136,14 +155,21 @@ impl SimController {
match pcdu_request { match pcdu_request {
PcduRequest::RequestSwitchInfo => { PcduRequest::RequestSwitchInfo => {
self.simulation 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 } => { PcduRequest::SwitchDevice { switch, state } => {
self.simulation.send_event( self.simulation
PcduModel::switch_device, .process_event(
(switch, state), PcduModel::switch_device,
&self.pcdu_addr, (switch, state),
); &self.addr_wrapper.pcdu_addr,
)
.unwrap();
} }
} }
Ok(()) Ok(())
@ -155,17 +181,23 @@ impl SimController {
log::info!("received MGT request: {:?}", mgt_request); log::info!("received MGT request: {:?}", mgt_request);
} }
match mgt_request { match mgt_request {
MgtRequest::ApplyTorque { duration, dipole } => self.simulation.send_event( MgtRequest::ApplyTorque { duration, dipole } => self
MagnetorquerModel::apply_torque, .simulation
(duration, dipole), .process_event(
&self.mgt_addr, MagnetorquerModel::apply_torque,
), (duration, dipole),
MgtRequest::RequestHk => self.simulation.send_event( &self.addr_wrapper.mgt_addr,
MagnetorquerModel::request_housekeeping_data, )
(), .unwrap(),
&self.mgt_addr, MgtRequest::RequestHk => self
), .simulation
} .process_event(
MagnetorquerModel::request_housekeeping_data,
(),
&self.addr_wrapper.mgt_addr,
)
.unwrap(),
};
Ok(()) Ok(())
} }

View File

@ -1,8 +1,8 @@
use std::{sync::mpsc, time::Duration}; use std::{sync::mpsc, time::Duration};
use asynchronix::{ use nexosim::{
model::{Model, Output}, model::{Context, Model},
time::Scheduler, ports::Output,
}; };
use satrs::power::SwitchStateBinary; use satrs::power::SwitchStateBinary;
use satrs_minisim::{ use satrs_minisim::{
@ -29,14 +29,13 @@ impl PcduModel {
} }
} }
pub async fn request_switch_info(&mut self, _: (), scheduler: &Scheduler<Self>) { pub async fn request_switch_info(&mut self, _: (), cx: &mut Context<Self>) {
scheduler cx.schedule_event(
.schedule_event( Duration::from_millis(SWITCH_INFO_DELAY_MS),
Duration::from_millis(SWITCH_INFO_DELAY_MS), Self::send_switch_info,
Self::send_switch_info, (),
(), )
) .expect("requesting switch info failed");
.expect("requesting switch info failed");
} }
pub fn send_switch_info(&mut self) { pub fn send_switch_info(&mut self) {

View File

@ -1,4 +1,4 @@
use asynchronix::time::MonotonicTime; use nexosim::time::MonotonicTime;
use num_enum::{IntoPrimitive, TryFromPrimitive}; use num_enum::{IntoPrimitive, TryFromPrimitive};
use serde::{de::DeserializeOwned, Deserialize, Serialize}; use serde::{de::DeserializeOwned, Deserialize, Serialize};

View File

@ -1,8 +1,8 @@
use acs::{MagnetometerModel, MagnetorquerModel}; use acs::{MagnetometerModel, MagnetorquerModel};
use asynchronix::simulation::{Mailbox, SimInit}; use controller::{ModelAddrWrapper, SimController};
use asynchronix::time::{MonotonicTime, SystemClock};
use controller::SimController;
use eps::PcduModel; use eps::PcduModel;
use nexosim::simulation::{Mailbox, SimInit};
use nexosim::time::{MonotonicTime, SystemClock};
use satrs_minisim::udp::SIM_CTRL_PORT; use satrs_minisim::udp::SIM_CTRL_PORT;
use satrs_minisim::{SimReply, SimRequest}; use satrs_minisim::{SimReply, SimRequest};
use std::sync::mpsc; use std::sync::mpsc;
@ -63,19 +63,20 @@ fn create_sim_controller(
} else { } else {
SimInit::new() SimInit::new()
}; };
let simulation = sim_init let addrs = ModelAddrWrapper::new(mgm_addr, pcdu_addr, mgt_addr);
.add_model(mgm_model, mgm_mailbox) let (simulation, scheduler) = sim_init
.add_model(pcdu_model, pcdu_mailbox) .add_model(mgm_model, mgm_mailbox, "MGM model")
.add_model(mgt_model, mgt_mailbox) .add_model(pcdu_model, pcdu_mailbox, "PCDU model")
.init(start_time); .add_model(mgt_model, mgt_mailbox, "MGT model")
.init(start_time)
.unwrap();
SimController::new( SimController::new(
sys_clock, sys_clock,
request_receiver, request_receiver,
reply_sender, reply_sender,
simulation, simulation,
mgm_addr, scheduler,
pcdu_addr, addrs,
mgt_addr,
) )
} }

View File

@ -1,4 +1,4 @@
use asynchronix::time::MonotonicTime; use nexosim::time::MonotonicTime;
pub fn current_millis(time: MonotonicTime) -> u64 { pub fn current_millis(time: MonotonicTime) -> u64 {
(time.as_secs() as u64 * 1000) + (time.subsec_nanos() as u64 / 1_000_000) (time.as_secs() as u64 * 1000) + (time.subsec_nanos() as u64 / 1_000_000)

View File

@ -31,9 +31,9 @@ version = "0.13"
default-features = false default-features = false
[dependencies.cobs] [dependencies.cobs]
git = "https://github.com/robamu/cobs.rs.git" git = "https://github.com/jamesmunns/cobs.rs.git"
version = "0.2.3" version = "0.2.3"
branch = "all_features" branch = "main"
default-features = false default-features = false
[dependencies.num-traits] [dependencies.num-traits]