phoxal 0.43.0

Phoxal - production-oriented autonomous robot framework: the runtime engine and model (the api contract tree lives in phoxal-api, the typed bus in phoxal-bus).
Documentation
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Capability {
    Motor(Motor),
    Encoder(Encoder),
    Accelerometer(Accelerometer),
    Gyroscope(Gyroscope),
    Magnetometer(Magnetometer),
    Imu(Imu),
    Gnss(Gnss),
    Camera(Camera),
    Depth(Depth),
    EmergencyStop(EmergencyStop),
    Range(Range),
    Lidar(Lidar),
    Mmwave(Mmwave),
    Microphone(Microphone),
    Speaker(Speaker),
    Battery(Battery),
    Led(Led),
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum EncoderType {
    Incremental,
    Absolute,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum MotorCommand {
    Position,
    Velocity,
    Torque,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum StructuralTarget {
    Joint { id: String },
    Link { id: String },
}

pub const MODULE_INSTANCE_SEPARATOR: &str = "__";

impl StructuralTarget {
    #[must_use]
    pub fn namespaced(&self, component_id: &str) -> Self {
        match self {
            Self::Joint { id } => Self::Joint {
                id: format!("{component_id}{MODULE_INSTANCE_SEPARATOR}{id}"),
            },
            Self::Link { id } => Self::Link {
                id: format!("{component_id}{MODULE_INSTANCE_SEPARATOR}{id}"),
            },
        }
    }
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum LidarOutput {
    Ranges,
    Points,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum CameraMode {
    Mono,
    Rgb,
}

#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum GnssCoordinateSystem {
    #[default]
    Local,
    Wgs84,
}

impl Capability {
    const fn default_encoder_type() -> EncoderType {
        EncoderType::Incremental
    }

    const fn default_counts_per_revolution() -> u32 {
        1024
    }

    const fn default_gear_ratio() -> f64 {
        1.0
    }

    #[must_use]
    pub fn kind_name(&self) -> &'static str {
        match self {
            Self::Motor { .. } => "motor",
            Self::Encoder { .. } => "encoder",
            Self::Imu { .. } => "imu",
            Self::Accelerometer { .. } => "accelerometer",
            Self::Gyroscope { .. } => "gyroscope",
            Self::Magnetometer { .. } => "magnetometer",
            Self::Gnss { .. } => "gnss",
            Self::Camera { .. } => "camera",
            Self::Depth { .. } => "depth",
            Self::EmergencyStop { .. } => "emergency_stop",
            Self::Range { .. } => "range",
            Self::Lidar { .. } => "lidar",
            Self::Mmwave { .. } => "mmwave",
            Self::Microphone { .. } => "microphone",
            Self::Speaker { .. } => "speaker",
            Self::Battery { .. } => "battery",
            Self::Led { .. } => "led",
        }
    }

    #[must_use]
    pub fn target(&self) -> &StructuralTarget {
        match self {
            Self::Motor(nm) => &nm.target,
            Self::Encoder(nm) => &nm.target,
            Self::Accelerometer(nm) => &nm.target,
            Self::Gyroscope(nm) => &nm.target,
            Self::Magnetometer(nm) => &nm.target,
            Self::Imu(nm) => &nm.target,
            Self::Gnss(nm) => &nm.target,
            Self::Camera(nm) => &nm.target,
            Self::Depth(nm) => &nm.target,
            Self::EmergencyStop(nm) => &nm.target,
            Self::Range(nm) => &nm.target,
            Self::Lidar(nm) => &nm.target,
            Self::Mmwave(nm) => &nm.target,
            Self::Microphone(nm) => &nm.target,
            Self::Speaker(nm) => &nm.target,
            Self::Battery(nm) => &nm.target,
            Self::Led(nm) => &nm.target,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Motor {
    pub target: StructuralTarget,
    pub command: MotorCommand,
    #[serde(default = "Capability::default_gear_ratio")]
    pub gear_ratio: f64,
    #[serde(default)]
    pub max_torque_nm: Option<f64>,
    #[serde(default)]
    pub max_velocity_radps: Option<f64>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Encoder {
    pub target: StructuralTarget,
    pub publish_rate_hz: f64,
    #[serde(default = "Capability::default_gear_ratio")]
    pub gear_ratio: f64,
    #[serde(default = "Capability::default_encoder_type")]
    pub encoder_type: EncoderType,
    #[serde(default = "Capability::default_counts_per_revolution")]
    pub counts_per_revolution: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Accelerometer {
    /// Publishes raw accelerometer samples in the sensor-local frame in m/s^2.
    ///
    /// This capability does not imply gravity compensation, zero-bias removal,
    /// or motion-state filtering. Small non-zero readings while stationary are
    /// valid unless a producer-specific contract says otherwise.
    pub target: StructuralTarget,
    pub publish_rate_hz: f64,
    #[serde(default)]
    pub axes: Option<[bool; 3]>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Gyroscope {
    /// Publishes raw angular velocity samples in the sensor-local frame in rad/s.
    ///
    /// This capability does not imply zero-bias removal or rest-state filtering.
    /// Small non-zero readings while stationary are valid unless a producer-specific
    /// contract says otherwise.
    pub target: StructuralTarget,
    pub publish_rate_hz: f64,
    #[serde(default)]
    pub axes: Option<[bool; 3]>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Magnetometer {
    pub target: StructuralTarget,
    pub publish_rate_hz: f64,
    #[serde(default)]
    pub axes: Option<[bool; 3]>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Imu {
    /// Publishes orientation samples in the sensor-local frame.
    ///
    /// Orientation is reported independently of the raw accelerometer and gyroscope
    /// streams; consumers must not assume those streams are de-biased, filtered, or
    /// fused to match this orientation estimate exactly.
    pub target: StructuralTarget,
    pub publish_rate_hz: f64,
    #[serde(default)]
    pub axes: Option<[bool; 3]>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Gnss {
    pub target: StructuralTarget,
    pub publish_rate_hz: f64,
    #[serde(default)]
    pub coordinate_system: GnssCoordinateSystem,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Camera {
    pub target: StructuralTarget,
    pub mode: CameraMode,
    pub publish_rate_hz: f64,
    pub width_px: u32,
    pub height_px: u32,
    #[serde(default)]
    pub field_of_view_rad: Option<f64>,
}

/// Shared depth capability.
///
/// Contract:
/// - payload data stores unsigned 16-bit millimeter samples
/// - `width_px` and `height_px` are static metadata and are not repeated in
///   each payload
/// - published payloads contain complete grids with valid non-zero samples
/// - pixels represent forward-axis depth, not radial range
/// - columns increase to sensor-right and rows increase downward
///
/// Any simulation or hardware driver that publishes this capability should
/// follow that same geometry rule.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Depth {
    pub target: StructuralTarget,
    pub publish_rate_hz: f64,
    pub width_px: u32,
    pub height_px: u32,
    #[serde(default)]
    pub field_of_view_rad: Option<f64>,
    #[serde(default)]
    pub min_range_m: Option<f64>,
    #[serde(default)]
    pub max_range_m: Option<f64>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Range {
    pub target: StructuralTarget,
    pub publish_rate_hz: f64,
    pub min_range_m: f64,
    pub max_range_m: f64,
    pub field_of_view_rad: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct EmergencyStop {
    pub target: StructuralTarget,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Lidar {
    pub target: StructuralTarget,
    pub publish_rate_hz: f64,
    pub output: LidarOutput,
    #[serde(default)]
    pub min_range_m: Option<f64>,
    #[serde(default)]
    pub max_range_m: Option<f64>,
    #[serde(default)]
    pub horizontal_fov_rad: Option<f64>,
    #[serde(default)]
    pub horizontal_resolution_rad: Option<f64>,
    #[serde(default)]
    pub vertical_fov_rad: Option<f64>,
    #[serde(default)]
    pub vertical_resolution_rad: Option<f64>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Mmwave {
    pub target: StructuralTarget,
    pub publish_rate_hz: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Microphone {
    pub target: StructuralTarget,
    pub publish_rate_hz: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Speaker {
    pub target: StructuralTarget,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Battery {
    pub target: StructuralTarget,
    pub publish_rate_hz: f64,
    /// Nominal pack voltage. Required: it is the only thing that turns an
    /// energy reading into the reported voltage and current.
    pub voltage_v: f64,
    /// Pack capacity. Required: it is the denominator of `charge_ratio`.
    pub capacity_ah: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Led {
    pub target: StructuralTarget,
}

#[cfg(test)]
mod tests {
    use super::{Capability, GnssCoordinateSystem, StructuralTarget};

    #[test]
    fn namespaces_structural_targets_with_component_instance_id() {
        assert_eq!(
            StructuralTarget::Joint {
                id: "motor_joint".to_string()
            }
            .namespaced("left_drive"),
            StructuralTarget::Joint {
                id: "left_drive__motor_joint".to_string()
            }
        );
        assert_eq!(
            StructuralTarget::Link {
                id: "sensor_link".to_string()
            }
            .namespaced("front_sensor"),
            StructuralTarget::Link {
                id: "front_sensor__sensor_link".to_string()
            }
        );
    }

    #[test]
    fn gnss_coordinate_system_defaults_to_local_in_source_schema() {
        let yaml = r#"
kind: gnss
publish_rate_hz: 10.0
target:
  kind: link
  id: sensor_link
"#;

        let capability: Capability = serde_yaml::from_str(yaml).expect("valid GNSS capability");
        let Capability::Gnss(gnss) = capability else {
            panic!("expected GNSS capability");
        };

        assert_eq!(gnss.coordinate_system, GnssCoordinateSystem::Local);
    }

    #[test]
    fn gnss_coordinate_system_accepts_wgs84_in_source_schema() {
        let yaml = r#"
kind: gnss
publish_rate_hz: 10.0
coordinate_system: wgs84
target:
  kind: link
  id: sensor_link
"#;

        let capability: Capability = serde_yaml::from_str(yaml).expect("valid GNSS capability");
        let Capability::Gnss(gnss) = capability else {
            panic!("expected GNSS capability");
        };

        assert_eq!(gnss.coordinate_system, GnssCoordinateSystem::Wgs84);
    }
}