phoxal 0.34.2

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};

/// Simulation-specific parameters for every component defined in the architecture.
/// These extend the physical (URDF) and component/model parameters with simulator-specific noise, resolutions, and properties.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum Capability {
    Motor(Motor),
    Encoder(Encoder),
    Accelerometer(Accelerometer),
    Gyroscope(Gyroscope),
    Magnetometer(Magnetometer),
    Imu(Imu),
    Gnss(Gnss),
    Camera(Camera),
    Depth(Depth),
    Range(Range),
    Lidar(Lidar),
    Mmwave(Mmwave),
    Microphone(Microphone),
    Speaker,
    Battery,
    Led,
    EmergencyStop(EmergencyStop),
}

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

    pub fn validate(&self, field: &str, errors: &mut Vec<String>) {
        match self {
            Self::Motor(config) => {
                validate_optional_finite(
                    config.acceleration_radps2,
                    field,
                    "acceleration_radps2",
                    errors,
                );
                validate_optional_positive(
                    config.sampling_period_torque_hz,
                    field,
                    "sampling_period_torque_hz",
                    errors,
                );
                if let Some(pid) = &config.control_pid {
                    if pid.len() != 3 {
                        errors.push(format!("{field}.control_pid must contain exactly 3 terms"));
                    }
                    for value in pid {
                        if !value.is_finite() {
                            errors.push(format!("{field}.control_pid values must be finite"));
                            break;
                        }
                    }
                }
            }
            Self::Encoder(config) => {
                validate_sampling(config.sampling_period_hz, field, errors);
                validate_optional_finite(config.resolution, field, "resolution", errors);
                validate_optional_finite(config.noise, field, "noise", errors);
            }
            Self::Accelerometer(config) => {
                validate_sampling(config.sampling_period_hz, field, errors);
                validate_optional_finite(config.resolution, field, "resolution", errors);
                validate_table(
                    config.lookup_table.as_deref(),
                    field,
                    "lookup_table",
                    errors,
                );
            }
            Self::Gyroscope(config) => {
                validate_sampling(config.sampling_period_hz, field, errors);
                validate_optional_finite(config.resolution, field, "resolution", errors);
                validate_table(
                    config.lookup_table.as_deref(),
                    field,
                    "lookup_table",
                    errors,
                );
            }
            Self::Magnetometer(config) => {
                validate_sampling(config.sampling_period_hz, field, errors);
                validate_optional_finite(config.resolution, field, "resolution", errors);
                validate_table(
                    config.lookup_table.as_deref(),
                    field,
                    "lookup_table",
                    errors,
                );
            }
            Self::Imu(config) => {
                validate_sampling(config.sampling_period_hz, field, errors);
                validate_optional_finite(config.resolution, field, "resolution", errors);
                validate_optional_finite(config.noise, field, "noise", errors);
            }
            Self::Gnss(config) => {
                validate_sampling(config.sampling_period_hz, field, errors);
                validate_optional_finite(config.resolution, field, "resolution", errors);
                validate_optional_finite(config.accuracy, field, "accuracy", errors);
                validate_optional_finite(
                    config.noise_correlation,
                    field,
                    "noise_correlation",
                    errors,
                );
                validate_optional_finite(
                    config.speed_resolution,
                    field,
                    "speed_resolution",
                    errors,
                );
                validate_optional_finite(config.speed_noise, field, "speed_noise", errors);
            }
            Self::Camera(config) => {
                validate_sampling(config.sampling_period_hz, field, errors);
                validate_optional_finite(config.near, field, "near", errors);
                validate_optional_finite(config.far, field, "far", errors);
                validate_optional_finite(config.exposure, field, "exposure", errors);
                validate_optional_finite(
                    config.ambient_occlusion_radius,
                    field,
                    "ambient_occlusion_radius",
                    errors,
                );
                validate_optional_finite(config.bloom_threshold, field, "bloom_threshold", errors);
                validate_optional_finite(config.noise, field, "noise", errors);
                validate_optional_finite(config.motion_blur, field, "motion_blur", errors);
            }
            Self::Depth(config) => {
                validate_sampling(config.sampling_period_hz, field, errors);
                validate_optional_finite(config.noise, field, "noise", errors);
                validate_optional_finite(config.resolution, field, "resolution", errors);
                validate_optional_finite(config.motion_blur, field, "motion_blur", errors);
            }
            Self::Range(config) => {
                validate_sampling(config.sampling_period_hz, field, errors);
                validate_optional_finite(config.noise, field, "noise", errors);
                validate_optional_finite(config.resolution, field, "resolution", errors);
            }
            Self::Lidar(config) => {
                validate_sampling(config.sampling_period_hz, field, errors);
                validate_optional_finite(config.noise, field, "noise", errors);
                validate_optional_finite(config.resolution, field, "resolution", errors);
            }
            Self::Mmwave(config) => {
                validate_sampling(config.sampling_period_hz, field, errors);
                validate_optional_finite(config.noise, field, "noise", errors);
                validate_optional_finite(config.resolution, field, "resolution", errors);
                validate_table(
                    config.lookup_table.as_deref(),
                    field,
                    "lookup_table",
                    errors,
                );
            }
            Self::Microphone(config) => {
                validate_sampling(config.sampling_period_hz, field, errors);
                validate_optional_finite(config.aperture, field, "aperture", errors);
            }
            Self::Speaker | Self::Battery | Self::Led | Self::EmergencyStop(_) => {}
        }
    }
}

/// Deterministic emergency-stop input for a simulated component.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct EmergencyStop {
    #[serde(default)]
    pub engaged: bool,
}

fn validate_sampling(value: f64, field: &str, errors: &mut Vec<String>) {
    if !value.is_finite() || value <= f64::EPSILON {
        errors.push(format!("{field}.sampling_period_hz must be finite and > 0"));
    }
}

fn validate_optional_finite(value: Option<f64>, field: &str, name: &str, errors: &mut Vec<String>) {
    if let Some(value) = value
        && !value.is_finite()
    {
        errors.push(format!("{field}.{name} must be finite"));
    }
}

fn validate_optional_positive(
    value: Option<f64>,
    field: &str,
    name: &str,
    errors: &mut Vec<String>,
) {
    if let Some(value) = value
        && (!value.is_finite() || value <= f64::EPSILON)
    {
        errors.push(format!("{field}.{name} must be finite and > 0"));
    }
}

fn validate_table(table: Option<&[Vec<f64>]>, field: &str, name: &str, errors: &mut Vec<String>) {
    let Some(table) = table else {
        return;
    };
    for row in table {
        if row.iter().any(|value| !value.is_finite()) {
            errors.push(format!("{field}.{name} values must be finite"));
            return;
        }
    }
}

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

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub enum CameraProjection {
    Planar,
    Cylindrical,
    Spherical,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Motor {
    #[serde(default)]
    pub actuator_type: ActuatorType,
    #[serde(default)]
    pub acceleration_radps2: Option<f64>,
    #[serde(default)]
    pub control_pid: Option<Vec<f64>>,
    #[serde(default)]
    pub sampling_period_torque_hz: Option<f64>,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Encoder {
    pub sampling_period_hz: f64,
    #[serde(default)]
    pub resolution: Option<f64>,
    #[serde(default)]
    pub noise: Option<f64>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Accelerometer {
    pub sampling_period_hz: f64,
    #[serde(default)]
    pub resolution: Option<f64>,
    #[serde(default)]
    pub lookup_table: Option<Vec<Vec<f64>>>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Gyroscope {
    pub sampling_period_hz: f64,
    #[serde(default)]
    pub resolution: Option<f64>,
    #[serde(default)]
    pub lookup_table: Option<Vec<Vec<f64>>>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Magnetometer {
    pub sampling_period_hz: f64,
    #[serde(default)]
    pub resolution: Option<f64>,
    #[serde(default)]
    pub lookup_table: Option<Vec<Vec<f64>>>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Imu {
    pub sampling_period_hz: f64,
    #[serde(default)]
    pub resolution: Option<f64>,
    #[serde(default)]
    pub noise: Option<f64>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Gnss {
    pub sampling_period_hz: f64,
    #[serde(default)]
    pub resolution: Option<f64>,
    #[serde(default)]
    pub accuracy: Option<f64>,
    #[serde(default)]
    pub noise_correlation: Option<f64>,
    #[serde(default)]
    pub speed_resolution: Option<f64>,
    #[serde(default)]
    pub speed_noise: Option<f64>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Camera {
    pub sampling_period_hz: f64,
    #[serde(default)]
    pub projection: Option<CameraProjection>,
    #[serde(default)]
    pub near: Option<f64>,
    #[serde(default)]
    pub far: Option<f64>,
    #[serde(default)]
    pub exposure: Option<f64>,
    #[serde(default)]
    pub anti_aliasing: Option<bool>,
    #[serde(default)]
    pub ambient_occlusion_radius: Option<f64>,
    #[serde(default)]
    pub bloom_threshold: Option<f64>,
    #[serde(default)]
    pub noise: Option<f64>,
    #[serde(default)]
    pub motion_blur: Option<f64>,
    #[serde(default)]
    pub noise_mask_url: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Depth {
    pub sampling_period_hz: f64,
    #[serde(default)]
    pub noise: Option<f64>,
    #[serde(default)]
    pub resolution: Option<f64>,
    #[serde(default)]
    pub motion_blur: Option<f64>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Range {
    pub sampling_period_hz: f64,
    #[serde(default)]
    pub noise: Option<f64>,
    #[serde(default)]
    pub resolution: Option<f64>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Lidar {
    pub sampling_period_hz: f64,
    #[serde(default)]
    pub noise: Option<f64>,
    #[serde(default)]
    pub resolution: Option<f64>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Mmwave {
    pub sampling_period_hz: f64,
    #[serde(default)]
    pub noise: Option<f64>,
    #[serde(default)]
    pub resolution: Option<f64>,
    #[serde(default)]
    pub lookup_table: Option<Vec<Vec<f64>>>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct Microphone {
    pub sampling_period_hz: f64,
    #[serde(default)]
    pub aperture: Option<f64>,
}