stable_flight/params/metadata.rs
1//! Descriptions, units, grouping, and valid ranges for params, for ground stations
2//! (`param-metadata` feature): a host-side companion to each [`ParamTable`], so flight-controller
3//! firmware, which never enables the feature, carries none of it.
4//!
5//! A config that implements [`ParamMetadata`] describes each of its params in a
6//! [`METADATA`](ParamMetadata::METADATA) table with the same names in the same order as its
7//! [`PARAMS`](ParamTable::PARAMS). Ranges restate what the config's `validate` accepts, in the
8//! param's unit; the core's tests check each against `validate`, so a range can't drift from
9//! validation unnoticed.
10//!
11//! # Examples
12//!
13//! ```
14//! use stable_flight::params::metadata::ParamMetadata;
15//! use stable_flight::stabilizers::copter::CopterStabilizerConfig;
16//!
17//! let rate_imax = CopterStabilizerConfig::METADATA
18//! .iter()
19//! .find(|m| m.name() == "STAB_RATE_IMAX")
20//! .expect("described");
21//! assert_eq!(rate_imax.units(), "rad");
22//! assert_eq!(rate_imax.min().map(|b| b.value()), Some(0.0));
23//! assert_eq!(rate_imax.max(), None);
24//! ```
25
26use super::ParamTable;
27
28#[cfg(test)]
29mod tests;
30
31/// One end of a param's valid range, in the param's unit.
32#[derive(Debug, Clone, Copy, PartialEq)]
33pub struct Bound {
34 value: f32,
35 inclusive: bool,
36}
37
38impl Bound {
39 /// The bound's value.
40 pub const fn value(&self) -> f32 {
41 self.value
42 }
43
44 /// Whether `value` itself is valid (`[` rather than `(`).
45 pub const fn inclusive(&self) -> bool {
46 self.inclusive
47 }
48}
49
50/// What a ground station shows for one param: its description, unit, where it is grouped, and
51/// its valid range. Built from a per-group template:
52///
53/// ```
54/// use stable_flight::params::metadata::ParamMeta;
55///
56/// const LIMITS: ParamMeta = ParamMeta::in_group("Guidance", "Limits");
57/// const TILT: ParamMeta = LIMITS
58/// .param("GUID_TILT_MAX", "Largest tilt waypoint flight may request")
59/// .with_units("deg")
60/// .above(0.0)
61/// .below(90.0);
62///
63/// assert_eq!(TILT.group(), "Limits");
64/// assert!(!TILT.max().expect("bounded").inclusive());
65/// ```
66#[derive(Debug, Clone, Copy, PartialEq)]
67pub struct ParamMeta {
68 name: &'static str,
69 description: &'static str,
70 units: &'static str,
71 category: &'static str,
72 group: &'static str,
73 min: Option<Bound>,
74 max: Option<Bound>,
75}
76
77impl ParamMeta {
78 /// A template for the params of one `group` (e.g. `"Rate"`) within a `category` (the
79 /// component, e.g. `"Stabilizer"`): no name, unit, or range yet. Name it with
80 /// [`param`](Self::param).
81 pub const fn in_group(category: &'static str, group: &'static str) -> Self {
82 Self {
83 name: "",
84 description: "",
85 units: "",
86 category,
87 group,
88 min: None,
89 max: None,
90 }
91 }
92
93 /// This template as the param `name`, described by a one-line `description`.
94 pub const fn param(self, name: &'static str, description: &'static str) -> Self {
95 Self {
96 name,
97 description,
98 ..self
99 }
100 }
101
102 /// With `units` as a ground station shows them, e.g. `"deg/s"`; empty for a dimensionless
103 /// param (gains, fractions).
104 pub const fn with_units(self, units: &'static str) -> Self {
105 Self { units, ..self }
106 }
107
108 /// Valid from `value` up, `value` included.
109 pub const fn at_least(self, value: f32) -> Self {
110 self.with_min(value, true)
111 }
112
113 /// Valid above `value`, `value` excluded.
114 pub const fn above(self, value: f32) -> Self {
115 self.with_min(value, false)
116 }
117
118 /// Valid up to `value`, `value` included.
119 pub const fn at_most(self, value: f32) -> Self {
120 self.with_max(value, true)
121 }
122
123 /// Valid below `value`, `value` excluded.
124 pub const fn below(self, value: f32) -> Self {
125 self.with_max(value, false)
126 }
127
128 const fn with_min(self, value: f32, inclusive: bool) -> Self {
129 Self {
130 min: Some(Bound { value, inclusive }),
131 ..self
132 }
133 }
134
135 const fn with_max(self, value: f32, inclusive: bool) -> Self {
136 Self {
137 max: Some(Bound { value, inclusive }),
138 ..self
139 }
140 }
141
142 /// The param's name, as in its [`ParamTable`].
143 pub const fn name(&self) -> &'static str {
144 self.name
145 }
146
147 /// One-line description.
148 pub const fn description(&self) -> &'static str {
149 self.description
150 }
151
152 /// Units, e.g. `"deg/s"`; empty when dimensionless.
153 pub const fn units(&self) -> &'static str {
154 self.units
155 }
156
157 /// The component the param belongs to, e.g. `"Stabilizer"`.
158 pub const fn category(&self) -> &'static str {
159 self.category
160 }
161
162 /// The loop or feature within the component, e.g. `"Rate"`.
163 pub const fn group(&self) -> &'static str {
164 self.group
165 }
166
167 /// The lower end of the valid range; `None` when unbounded below.
168 pub const fn min(&self) -> Option<Bound> {
169 self.min
170 }
171
172 /// The upper end of the valid range; `None` when unbounded above.
173 pub const fn max(&self) -> Option<Bound> {
174 self.max
175 }
176}
177
178/// A [`ParamTable`] whose params are described for ground stations.
179pub trait ParamMetadata: ParamTable {
180 /// One entry per param: the same names, in the same order, as
181 /// [`PARAMS`](ParamTable::PARAMS).
182 const METADATA: &'static [ParamMeta];
183}