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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//! Small numeric helpers shared by guidance, stabilizers, controllers, and mixers:
//! [`saturate_unit`], [`saturate_symmetric`], [`saturate_throttle`], [`desaturate`],
//! [`cap_magnitude`], [`clamp_symmetric`] and its per-component [`clamp_symmetric_each`],
//! [`limit_or_zero`], [`all_finite`], [`assert_dt`], [`tilt_boost`], and [`wrap_angle`] and
//! [`euler_angles`] (both public at the crate root).
//!
//! They implement the per-tick input policy (saturate, NaN → neutral, never let a non-finite
//! error into controller state, wrap angles), which is specified in `architecture.md`,
//! "Input Policy (no panics in the tick)". [`assert_dt`] is the policy's one per-tick panic.
use ;
use ;
use crateVec2;
/// Panics unless `dt` is positive and finite: the one per-tick assert the input policy allows.
/// Runners guarantee it; a zero, negative, NaN, or infinite `dt` is a caller bug, and an
/// infinite one would poison integrals (`0 · ∞ = NaN`).
pub
/// How far [`desaturate`] may shift outputs up, per unit of collective: up to twice the
/// collective. Keeps nearly the full moment split at flying throttle (a hover collective of
/// ~0.55 allows 1.1, more than any split needs) while fading airmode out toward idle.
const MAX_RAISE_PER_COLLECTIVE: f32 = 2.0;
/// Fits mixer outputs into `[0, 1]`, keeping the moments (attitude over thrust), like PX4's
/// airmode. Each output is `collective` plus its moment share.
///
/// 1. If the outputs span more than 1, the moment shares are scaled down about `collective`
/// until they span exactly 1, keeping their ratios.
/// 2. All outputs then shift by the smallest amount that fits them in `[0, 1]`: down if one
/// is above 1, up if one is below 0. Collective thrust gives way.
///
/// Shifting up is limited to [`MAX_RAISE_PER_COLLECTIVE`] times `collective`, so it fades in
/// from zero throttle instead of switching on at the first bit of it: at zero (or negative)
/// collective nothing rises and the shares below 0 are clipped, so an armed vehicle at idle
/// on the ground isn't lifted, or tipped, by wound-up moments. Outputs are then clamped to
/// `[0, 1]`. Inputs are expected finite (the mixer saturates them); a NaN output stays NaN.
///
/// Returns what it gave up, for saturation reporting.
pub
/// How far past `[0, 1]` an output may land after [`desaturate`]'s shift before it counts as
/// clipped rather than float rounding.
const CLIP_TOLERANCE: f32 = 1e-6;
/// What [`desaturate`] gave up to fit the outputs.
pub
/// Scales `v` down, keeping its direction, so its length is at most `max`. Unlike per-axis
/// saturation, a diagonal request stays diagonal. A non-finite `v` has no direction, so it
/// becomes zero (neutral), per the input policy.
pub
/// Saturates a normalized command to [-1, 1]; NaN becomes 0 (neutral).
pub
/// Saturates `value` to `[-limit, limit]`; NaN becomes 0 (neutral), and a negative or NaN
/// `limit` acts as 0, so neither a bad input nor a bad config panics the loop.
pub
/// Clamps `value` to `[-limit, limit]`; NaN `value` stays NaN, like `f32::clamp`.
///
/// Unlike `f32::clamp`, it never panics: a negative or NaN `limit` acts as 0 (neutral output).
/// Limits come from config passed in every tick, so a bad config must not panic the loop.
pub
/// A config limit as given when it is non-negative, else 0 (neutral); NaN also becomes 0.
pub
/// [`clamp_symmetric`] applied to each component.
pub
/// True when every component is finite.
pub
/// ZYX Euler angles `(roll, pitch, yaw)` in radians of an attitude quaternion, in the
/// [frame contract](crate::VehicleState#frame-contract)'s convention (the same as
/// `UnitQuaternion::euler_angles`). Use this rather than nalgebra's method to read Euler angles
/// from [`VehicleState::attitude`](crate::VehicleState::attitude).
///
/// It is a closed form on the quaternion. nalgebra's goes through a rotation matrix and `cos`
/// (more flash on target), and its gimbal-lock branch reports a finite pitch of -π/2 and yaw 0
/// for a NaN attitude. Here NaN in gives NaN out, which the controllers treat as neutral. Near
/// ±90° pitch the `asin` argument is clamped, so float rounding can't turn a valid attitude
/// into NaN.
///
/// # Examples
///
/// ```
/// use stable_flight::euler_angles;
/// use stable_flight::nalgebra::UnitQuaternion;
///
/// let attitude = UnitQuaternion::from_euler_angles(0.1, -0.2, 1.5);
/// let (roll, pitch, yaw) = euler_angles(&attitude);
/// assert!((roll - 0.1).abs() < 1e-6 && (pitch + 0.2).abs() < 1e-6 && (yaw - 1.5).abs() < 1e-6);
/// ```
/// Floor on the tilt cosine [`tilt_boost`] divides by: past 60° of tilt the boost stops
/// growing (2×), so a steep estimate can't demand unbounded thrust.
const MIN_TILT_COSINE: f32 = 0.5;
/// Tilt cosine below which [`tilt_boost`] fades to zero (about 84°–90° of tilt), so an
/// inverted vehicle gets no thrust from the boost, as ArduPilot's angle boost does.
const INVERTED_FADE_COSINE: f32 = 0.1;
/// Thrust multiplier that keeps a level thrust's vertical component while tilted:
/// `1 / cos(tilt)`, with `cos(tilt) = cos(roll) · cos(pitch)` (yaw doesn't change it).
///
/// Floored at [`MIN_TILT_COSINE`] (at most 2× past 60°), and faded linearly to 0 as the
/// cosine falls from [`INVERTED_FADE_COSINE`] to 0: at 90° and beyond (inverted) the
/// multiplier is 0, so thrust never pushes an upside-down vehicle toward the ground. A
/// non-finite attitude counts as level (1, no boost).
///
/// The cosine is read from the quaternion directly (`1 − 2(x² + y²)`, the rotation matrix's
/// `(2, 2)` entry): no trig, no matrix.
pub
/// Saturates a throttle or thrust command to [0, 1]; NaN becomes 0 (no thrust).
pub
/// Wraps an angle in radians to [-π, π); non-finite input gives NaN.
///
/// Float rounding can return exactly `+π` for inputs just below an odd multiple of π, so
/// treat the range as closed, [-π, π], when that matters.
///
/// # Examples
///
/// ```
/// use core::f32::consts::{FRAC_PI_2, PI};
/// use stable_flight::wrap_angle;
///
/// assert!((wrap_angle(3.0 * FRAC_PI_2) + FRAC_PI_2).abs() < 1e-6); // 270° → -90°
/// assert_eq!(wrap_angle(PI), -PI);
/// assert!(wrap_angle(f32::NAN).is_nan());
/// ```
/// `f32::rem_euclid` for a positive `divisor`, which `core` lacks: the remainder in
/// [0, divisor). `%` truncates toward zero, so a negative remainder is shifted up.