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