melvin_ob/mode_control/mode_context.rs
1use crate::flight_control::{
2 orbit::OrbitCharacteristics,
3 Supervisor,
4};
5use crate::objective::{BeaconController, BeaconControllerState, KnownImgObjective};
6use crate::util::KeychainWithOrbit;
7use std::{collections::BinaryHeap, sync::Arc};
8use tokio::sync::{Mutex, RwLock, mpsc::Receiver, watch};
9
10/// [`ModeContext`] is a central context container used by `GlobalMode` in the onboard software.
11/// It provides shared access to key mission-critical resources such as orbit state,
12/// supervisory control, objective channels, and internal buffers.
13///
14/// This structure enables modular FSM behavior by providing thread-safe, concurrent access
15/// to both live and buffered data, allowing `GlobalMode` to remain more or less stateless and focused
16/// solely on decision logic.
17pub(crate) struct ModeContext {
18 /// Shared keychain containing the various controllers and the orbit configuration.
19 k: Arc<KeychainWithOrbit>,
20 /// Orbit characteristics, updated during operation (e.g., after burns).
21 o_ch: Arc<RwLock<OrbitCharacteristics>>,
22 /// Supervisor instance providing new observation data, objective data, etc.
23 super_v: Arc<Supervisor>,
24 /// Receiver for new Known Image Objectives (Zoned Objectives).
25 zo_mon: RwLock<Receiver<KnownImgObjective>>,
26 /// Watch receiver for the current state of the Beacon Controller.
27 bo_mon: RwLock<watch::Receiver<BeaconControllerState>>,
28 /// Priority buffer for scheduled image objectives, used by internal planners.
29 k_buffer: Mutex<BinaryHeap<KnownImgObjective>>,
30 /// Shared access to the Beacon Controller for retrieval logic and updates.
31 beac_cont: Arc<BeaconController>,
32}
33
34impl ModeContext {
35
36 /// Constructs a new [`ModeContext`], initializing all internal references.
37 ///
38 /// # Arguments
39 /// - `key`: The mission keychain, including the basic controllers and the orbit.
40 /// - `o_char`: Initial orbital characteristics.
41 /// - `zo_mon_un`: Receiver for incoming Known Image Objectives.
42 /// - `bo_mon_un`: Watch receiver for beacon controller state updates.
43 /// - `super_v`: Shared [`Supervisor`] handle.
44 /// - `beac_cont`: Shared [`BeaconController`] for beacon objective management.
45 pub(crate) fn new(
46 key: KeychainWithOrbit,
47 o_char: OrbitCharacteristics,
48 zo_mon_un: Receiver<KnownImgObjective>,
49 bo_mon_un: watch::Receiver<BeaconControllerState>,
50 super_v: Arc<Supervisor>,
51 beac_cont: Arc<BeaconController>,
52 ) -> Arc<Self> {
53 let k = Arc::new(key);
54 let o_ch = Arc::new(RwLock::new(o_char));
55 let zo_mon = RwLock::new(zo_mon_un);
56 let bo_mon = RwLock::new(bo_mon_un);
57 Arc::new(Self {
58 k,
59 o_ch,
60 super_v,
61 zo_mon,
62 bo_mon,
63 k_buffer: Mutex::new(BinaryHeap::new()),
64 beac_cont,
65 })
66 }
67
68 /// Provides a reference to the [`KeychainWithOrbit`].
69 pub(super) fn k(&self) -> &Arc<KeychainWithOrbit> { &self.k }
70 /// Provides a copy of the current [`OrbitCharacteristics`].
71 pub(crate) async fn o_ch_clone(&self) -> OrbitCharacteristics { *self.o_ch.read().await }
72 /// Provides a reference to the shared and locked [`OrbitCharacteristics`].
73 pub(super) fn o_ch_lock(&self) -> &RwLock<OrbitCharacteristics> { &self.o_ch }
74 /// Provides a reference to the locked Zoned Objective Event Receiver.
75 pub(super) fn zo_mon(&self) -> &RwLock<Receiver<KnownImgObjective>> { &self.zo_mon }
76 /// Provides a reference to the watch resembling the current state of the Beacon controller.
77 pub(super) fn bo_mon(&self) -> &RwLock<watch::Receiver<BeaconControllerState>> { &self.bo_mon }
78 /// Provides a shared reference to the [`Supervisor`].
79 pub(super) fn super_v(&self) -> &Arc<Supervisor> { &self.super_v }
80 /// Provides a reference to the locked Zoned Objective Buffer implemented as a [`BinaryHeap`].
81 pub(super) fn k_buffer(&self) -> &Mutex<BinaryHeap<KnownImgObjective>> { &self.k_buffer }
82 /// Provides a shared reference to the [`BeaconController`].
83 pub(super) fn beac_cont(&self) -> &Arc<BeaconController> { &self.beac_cont }
84}