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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//! A flat name → `f32` view over the config tree, for live tuning from a ground station.
//!
//! The config is the only store: [`Params::param`] reads a field of the component's config,
//! and [`Params::set_param`] writes a copy and commits it through
//! [`Configurable::set_config`], which validates it (an invalid value is rejected and the
//! config unchanged) and applies the integrator policy. Any [`Configurable`] whose config
//! implements [`ParamTable`] gets [`Params`]; [`FlightLoop`](crate::FlightLoop) combines its
//! guidance's and stabilizer's.
//!
//! Names are uppercase ASCII of at most [`MAX_NAME_LEN`] bytes (MAVLink's `param_id`),
//! prefixed by component (`STAB_`, `GUID_`) and loop. Vector components end in `_X`/`_Y`/`_Z`
//! (roll/pitch/yaw for angles and rates). Angles and angular rates are in degrees and deg/s,
//! as ground stations show them, and convert to the config's radians at this boundary;
//! everything else is in the config field's own unit.
//!
//! Params are addressed by index, `0..param_count()`, in a stable table order; name lookup
//! scans the names. Index access is what paced output uses (a cursor that sends a few params
//! per tick).
//!
//! Units, descriptions, groups, and valid ranges are in the `metadata` module (feature
//! `param-metadata`), one table per config next to its [`ParamTable`]. The tables below map
//! names to config fields.
//!
//! # Copter stabilizer ([`CopterStabilizerConfig`](crate::stabilizers::copter::CopterStabilizerConfig))
//!
//! | Name | Field |
//! |---|---|
//! | `STAB_RATE_P`, `_I`, `_D` | `body_rate.gains` |
//! | `STAB_RATE_YAW_P`, `_I`, `_D` | `body_rate.yaw_gains` |
//! | `STAB_RATE_IMAX` | `body_rate.max_integral` |
//! | `STAB_ATT_P`, `_I`, `_D` | `attitude.gains` |
//! | `STAB_ATT_AMAX_X`, `_Y` | `attitude.max_angles_rad` |
//! | `STAB_ATT_IMAX` | `attitude.max_integral` |
//! | `STAB_ATT_RMAX_X`, `_Y` | `attitude.max_rate_radps` |
//! | `STAB_YAW_P`, `_I`, `_D` | `yaw.gains` |
//! | `STAB_YAW_IMAX` | `yaw.max_integral` |
//! | `STAB_YAW_RMAX` | `yaw.max_rate_radps` |
//! | `STAB_FS_RATE_X`, `_Y`, `_Z` | `full_scale_rates_radps` |
//! | `STAB_THR_EXPO` | `thrust_curve_exponent` |
//!
//! # Copter guidance ([`CopterGuidanceConfig`](crate::guidance::copter::CopterGuidanceConfig))
//!
//! | Name | Field |
//! |---|---|
//! | `GUID_ALT_P`, `_I`, `_D` | `altitude.gains` |
//! | `GUID_ALT_IMAX` | `altitude.max_integral` |
//! | `GUID_ALT_HOVER` | `altitude.hover_thrust` |
//! | `GUID_LAT_POS_P` | `lateral.position_p` |
//! | `GUID_LAT_VEL_P`, `_I`, `_D` | `lateral.velocity` |
//! | `GUID_LAT_IMAX` | `lateral.max_integral` |
//! | `GUID_NAV_PERIOD` | `navigation_period_s` |
//! | `GUID_TILT_MAX` | `max_tilt_rad` |
//! | `GUID_DESC_MAX` | `max_descent_mps` |
//! | `GUID_LAND_SPD` | `land_speed_mps` |
//! | `GUID_STICK_X`, `_Y` | `max_stick_angles_rad` |
//!
//! # Examples
//!
//! ```
//! use stable_flight::params::Params;
//! use stable_flight::stabilizers::copter::{CopterStabilizer, CopterStabilizerConfig};
//! use stable_flight::Configurable;
//!
//! let mut stabilizer = CopterStabilizer::new(CopterStabilizerConfig::default());
//!
//! stabilizer.set_param("STAB_RATE_P", 0.2).expect("valid value");
//! assert_eq!(stabilizer.param("STAB_RATE_P"), Some(0.2));
//! assert_eq!(stabilizer.config().body_rate.gains.p, 0.2);
//!
//! stabilizer.set_param("STAB_ATT_AMAX_X", 30.0).expect("valid value"); // degrees
//! assert!((stabilizer.config().attitude.max_angles_rad.x - 30f32.to_radians()).abs() < 1e-6);
//!
//! assert!(stabilizer.set_param("STAB_RATE_IMAX", -1.0).is_err()); // fails validation
//! assert!(stabilizer.set_param("NO_SUCH_PARAM", 1.0).is_err());
//!
//! let mut count = 0;
//! stabilizer.visit_params(&mut |_name, _value| count += 1);
//! assert_eq!(count, 24);
//! ```
use fmt;
use crateConfigurable;
use crateInvalidConfig;
/// Longest param name in bytes: MAVLink's `param_id` field.
pub const MAX_NAME_LEN: usize = 16;
/// One named `f32` field of a config `C`: how to read it and how to write it, unvalidated.
/// A config that exposes its fields as named params.
///
/// Names must be unique within a table and across the components combined into one view
/// (e.g. a [`FlightLoop`](crate::FlightLoop)'s guidance and stabilizer), and at most
/// [`MAX_NAME_LEN`] bytes.
/// Read, write, and list a component's tunable values by index or by name.
///
/// Implementors provide the four index methods; name lookup, [`param`](Params::param),
/// [`set_param`](Params::set_param), and [`visit_params`](Params::visit_params) are built on
/// them.
///
/// **Contract: a successful write stores the value as given**, up to the rounding of a unit
/// conversion (degrees ↔ radians); an out-of-range value is rejected, never clamped or
/// adjusted. Implementors must uphold it (for the blanket impl: each [`Param`] reads back what
/// it wrote, and `set_config` commits the config unchanged or rejects it), because a link
/// confirms a write by echoing the value it was sent.
/// Why a param write ([`Params::set_param`], [`Params::set_param_at`]) was rejected.
/// A [`Param`] for a config field path: `param!("NAME", a.b)`, or `param!(deg "NAME", a.b)` to
/// expose a radian field in degrees.
pub use param;