stable_flight/composition.rs
1//! Combinators that build one pipeline component out of several.
2//!
3//! - [`ComposedVehicle`] joins an independent [`StateSource`] and [`Actuator`] into a
4//! [`Vehicle`], e.g. an IMU/estimator and an ESC driver on hardware. The two halves don't see
5//! each other, so it doesn't fit a state source that needs the actuator's data (such as a
6//! simulator bridge's truth state); that case needs a different combinator.
7//! - `(A, B)` is a [`TelemetryPublisher`] that fans each record out to both sinks; nest tuples
8//! for more. `()` is the no-op sink.
9//!
10//! Both are allocation-free and add no dynamic dispatch.
11//!
12//! ```
13//! # use std::time::Duration;
14//! # use stable_flight::{ActuatorCommand, Command, ControlOutput, Saturation, Setpoint, VehicleState};
15//! use stable_flight::{TelemetryPublisher, TelemetryRecord};
16//!
17//! #[derive(Default)]
18//! struct Counter(u32);
19//! impl TelemetryPublisher for Counter {
20//! fn publish(&mut self, _record: &TelemetryRecord) {
21//! self.0 += 1;
22//! }
23//! }
24//!
25//! // A ground-station sink, a log sink, and `()` where a slot needs no sink.
26//! let mut sinks = (Counter::default(), (Counter::default(), ()));
27//! # let (command, setpoint, state, control, actuators) = (Command::default(), Setpoint::Attitude(Default::default()), VehicleState::default(), ControlOutput::default(), ActuatorCommand::default());
28//! # let record = TelemetryRecord { dt: Duration::from_millis(4), command: &command, setpoint: &setpoint, state: &state, control: &control, actuators: &actuators, saturation: Saturation::default(), failsafe: None, flight_mode: Default::default(), wind: None };
29//! sinks.publish(&record);
30//! assert_eq!((sinks.0.0, sinks.1.0.0), (1, 1));
31//! ```
32//!
33//! [`Vehicle`]: crate::Vehicle
34
35use core::time::Duration;
36
37use crate::{
38 Actuator, ActuatorCommand, StateSource, TelemetryPublisher, TelemetryRecord, VehicleState,
39};
40
41#[cfg(test)]
42mod tests;
43
44/// A [`Vehicle`](crate::Vehicle) made of a separate state source and actuator.
45///
46/// `state` goes to `S`, `actuate` to `A`; `Vehicle` itself comes from the blanket
47/// impl. Use it when sensing and actuation are different components, which is the normal
48/// case on hardware.
49///
50/// # Examples
51///
52/// ```
53/// use std::time::Duration;
54/// use stable_flight::{
55/// Actuator, ActuatorCommand, ComposedVehicle, StateSource, Vehicle, VehicleState,
56/// };
57///
58/// struct Imu;
59/// impl StateSource for Imu {
60/// fn state(&mut self, _dt: Duration) -> VehicleState {
61/// VehicleState::default()
62/// }
63/// }
64///
65/// struct Escs {
66/// last: ActuatorCommand,
67/// }
68/// impl Actuator for Escs {
69/// fn actuate(&mut self, command: &ActuatorCommand) {
70/// self.last = *command;
71/// }
72/// }
73///
74/// fn fly(vehicle: &mut impl Vehicle) {
75/// let _state = vehicle.state(Duration::from_millis(4));
76/// vehicle.actuate(&ActuatorCommand { motors: [0.5; 8], ..Default::default() });
77/// }
78///
79/// let mut vehicle = ComposedVehicle::new(Imu, Escs { last: ActuatorCommand::default() });
80/// fly(&mut vehicle);
81/// assert_eq!(vehicle.actuator().last.motors[0], 0.5);
82/// ```
83#[derive(Debug, Clone, Default)]
84pub struct ComposedVehicle<S, A> {
85 source: S,
86 actuator: A,
87}
88
89impl<S, A> ComposedVehicle<S, A> {
90 /// Pairs a state source with an actuator.
91 pub fn new(source: S, actuator: A) -> Self {
92 ComposedVehicle { source, actuator }
93 }
94
95 /// The state-source half.
96 pub fn source(&self) -> &S {
97 &self.source
98 }
99
100 /// The actuator half.
101 pub fn actuator(&self) -> &A {
102 &self.actuator
103 }
104
105 /// Splits back into `(source, actuator)`.
106 pub fn into_parts(self) -> (S, A) {
107 (self.source, self.actuator)
108 }
109}
110
111impl<S: StateSource, A> StateSource for ComposedVehicle<S, A> {
112 fn state(&mut self, dt: Duration) -> VehicleState {
113 self.source.state(dt)
114 }
115}
116
117impl<S, A: Actuator> Actuator for ComposedVehicle<S, A> {
118 fn actuate(&mut self, actuator_command: &ActuatorCommand) {
119 self.actuator.actuate(actuator_command);
120 }
121}
122
123/// The no-op sink: discards every record. Use it where a loop needs a publisher but nothing
124/// should be published, or as a placeholder slot in a tuple fan-out.
125impl TelemetryPublisher for () {
126 fn publish(&mut self, _record: &TelemetryRecord) {}
127}
128
129/// Fans each record out to both publishers, first `A` then `B`.
130impl<A: TelemetryPublisher, B: TelemetryPublisher> TelemetryPublisher for (A, B) {
131 fn publish(&mut self, record: &TelemetryRecord) {
132 self.0.publish(record);
133 self.1.publish(record);
134 }
135}