dexcontrol 0.7.7-rc.4

Safe Rust API for the precompiled DexControl robot runtime
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// Copyright (C) 2026 Dexmate Inc.
//
// This software is dual-licensed:
//
// 1. GNU Affero General Public License v3.0 (AGPL-3.0)
//    See LICENSE-AGPL for details
//
// 2. Commercial License
//    For commercial licensing terms, contact: contact@dexmate.ai
//! Sensor snapshots own their native storage; borrowed channels cannot outlive them.
use crate::*;
pub use ffi::{
    dex_frame_info_t as FrameInfo, dex_imu_t as Imu, dex_lidar_2d_info_t as Lidar2dInfo,
    dex_lidar_3d_info_t as Lidar3dInfo, dex_stream_stats_t as StreamStats,
    dex_ultrasonic_t as Ultrasonic,
};
#[derive(Debug)]
pub struct Sample<T> {
    pub value: T,
    pub age: Duration,
}
#[derive(Debug)]
pub struct Wrench {
    pub values: [f64; 6],
    pub timestamp_ns: u64,
}
impl Robot {
    /// None accepts any sample age. Some requires a positive millisecond bound.
    pub fn battery_with_age(&self, max_age: Option<Duration>) -> Result<Sample<Battery>> {
        let mut value = Battery {
            struct_size: std::mem::size_of::<Battery>() as u32,
            ..Default::default()
        };
        let mut age = 0;
        let bound = sensor_age(max_age)?;
        call(|e| unsafe {
            ffi::dex_robot_battery_ex(self.0.as_ptr(), bound, &mut value, &mut age, e)
        })?;
        Ok(Sample {
            value,
            age: Duration::from_millis(age),
        })
    }
    pub fn wrench(&self, name: &str, max_age: Option<Duration>) -> Result<Sample<Wrench>> {
        let name = string(name)?;
        let bound = sensor_age(max_age)?;
        let mut values = [0.; 6];
        let mut time = 0;
        let mut age = 0;
        call(|e| unsafe {
            ffi::dex_robot_wrench_ex(
                self.0.as_ptr(),
                name.as_ptr(),
                bound,
                values.as_mut_ptr(),
                &mut time,
                &mut age,
                e,
            )
        })?;
        Ok(Sample {
            value: Wrench {
                values,
                timestamp_ns: time,
            },
            age: Duration::from_millis(age),
        })
    }
    pub fn temperature_json(&self, name: &str) -> Result<String> {
        let name = string(name)?;
        text(|out, e| unsafe {
            ffi::dex_robot_temperature_json(self.0.as_ptr(), name.as_ptr(), out, e)
        })
    }
    pub fn sensor_json(&self, name: &str, role: Option<&str>) -> Result<String> {
        let name = string(name)?;
        let role = role.map(string).transpose()?;
        text(|out, e| unsafe {
            ffi::dex_robot_sensor_json(
                self.0.as_ptr(),
                name.as_ptr(),
                role.as_ref().map_or(ptr::null(), |s| s.as_ptr()),
                out,
                e,
            )
        })
    }
    pub fn camera<'a>(&'a self, name: &str) -> Result<Camera<'a>> {
        Ok(Camera {
            robot: self,
            name: string(name)?,
        })
    }
    pub fn imu(&self, name: &str, max_age: Option<Duration>) -> Result<Sample<Imu>> {
        let name = string(name)?;
        let bound = sensor_age(max_age)?;
        let mut value = Imu {
            struct_size: std::mem::size_of::<Imu>() as u32,
            ..Default::default()
        };
        let mut age = 0;
        call(|e| unsafe {
            ffi::dex_robot_imu_ex(
                self.0.as_ptr(),
                name.as_ptr(),
                bound,
                &mut value,
                &mut age,
                e,
            )
        })?;
        Ok(Sample {
            value,
            age: Duration::from_millis(age),
        })
    }
    pub fn ultrasonic(&self, name: &str, max_age: Option<Duration>) -> Result<Sample<Ultrasonic>> {
        let name = string(name)?;
        let bound = sensor_age(max_age)?;
        let mut value = Ultrasonic {
            struct_size: std::mem::size_of::<Ultrasonic>() as u32,
            ..Default::default()
        };
        let mut age = 0;
        call(|e| unsafe {
            ffi::dex_robot_ultrasonic_ex(
                self.0.as_ptr(),
                name.as_ptr(),
                bound,
                &mut value,
                &mut age,
                e,
            )
        })?;
        Ok(Sample {
            value,
            age: Duration::from_millis(age),
        })
    }
    pub fn lidar_2d(&self, name: &str, max_age: Option<Duration>) -> Result<Lidar2d> {
        let name = string(name)?;
        let bound = sensor_age(max_age)?;
        let mut out = ptr::null_mut();
        let result = call(|e| unsafe {
            ffi::dex_robot_lidar_2d_take(self.0.as_ptr(), name.as_ptr(), bound, &mut out, e)
        });
        let handle = NonNull::new(out).map(Lidar2d);
        result?;
        handle.ok_or_else(|| invalid("Runtime returned no sensor snapshot"))
    }
    pub fn lidar_3d(&self, name: &str, max_age: Option<Duration>) -> Result<Lidar3d> {
        let name = string(name)?;
        let bound = sensor_age(max_age)?;
        let mut out = ptr::null_mut();
        let result = call(|e| unsafe {
            ffi::dex_robot_lidar_3d_take(self.0.as_ptr(), name.as_ptr(), bound, &mut out, e)
        });
        let handle = NonNull::new(out).map(Lidar3d);
        result?;
        handle.ok_or_else(|| invalid("Runtime returned no sensor snapshot"))
    }
}
/// An owning immutable scan. Borrowed channels stay valid until the scan is dropped.
/// ```compile_fail
/// fn ranges(scan: dexcontrol::Lidar2d) -> &'static [f64] {
///     scan.ranges().unwrap()
/// }
/// ```
pub struct Lidar2d(NonNull<ffi::dex_lidar_2d_t>);
impl Lidar2d {
    pub fn info(&self) -> Result<Lidar2dInfo> {
        let mut value = Lidar2dInfo {
            struct_size: std::mem::size_of::<Lidar2dInfo>() as u32,
            ..Default::default()
        };
        call(|e| unsafe { ffi::dex_lidar_2d_info(self.0.as_ptr(), &mut value, e) })?;
        Ok(value)
    }
    pub fn age(&self) -> Result<Duration> {
        let mut value = 0;
        call(|_| unsafe { ffi::dex_lidar_2d_age_ms(self.0.as_ptr(), &mut value) })?;
        Ok(Duration::from_millis(value))
    }
    pub fn ranges(&self) -> Result<&[f64]> {
        let mut data = ptr::null();
        let mut count = 0;
        call(|_| unsafe {
            ffi::dex_lidar_2d_channel_f64(self.0.as_ptr(), 0, &mut data, &mut count)
        })?;
        // SAFETY: the immutable snapshot owns this channel; the result borrows self.
        unsafe { borrowed(data, count) }
    }
    pub fn angles(&self) -> Result<&[f64]> {
        let mut data = ptr::null();
        let mut count = 0;
        call(|_| unsafe {
            ffi::dex_lidar_2d_channel_f64(self.0.as_ptr(), 1, &mut data, &mut count)
        })?;
        // SAFETY: the immutable snapshot owns this channel; the result borrows self.
        unsafe { borrowed(data, count) }
    }
    pub fn intensities(&self) -> Result<&[u32]> {
        let mut data = ptr::null();
        let mut count = 0;
        call(|_| unsafe {
            ffi::dex_lidar_2d_channel_u32(self.0.as_ptr(), 0, &mut data, &mut count)
        })?;
        // SAFETY: the immutable snapshot owns this channel; the result borrows self.
        unsafe { borrowed(data, count) }
    }
}
impl Drop for Lidar2d {
    fn drop(&mut self) {
        unsafe { ffi::dex_lidar_2d_release(self.0.as_ptr()) }
    }
}
// SAFETY: snapshot data is immutable; its owning handle is kept alive by Rust borrows.
unsafe impl Send for Lidar2d {}
unsafe impl Sync for Lidar2d {}
pub struct Lidar3d(NonNull<ffi::dex_lidar_3d_t>);
impl Lidar3d {
    pub fn info(&self) -> Result<Lidar3dInfo> {
        let mut value = Lidar3dInfo {
            struct_size: std::mem::size_of::<Lidar3dInfo>() as u32,
            ..Default::default()
        };
        call(|e| unsafe { ffi::dex_lidar_3d_info(self.0.as_ptr(), &mut value, e) })?;
        Ok(value)
    }
    pub fn age(&self) -> Result<Duration> {
        let mut value = 0;
        call(|_| unsafe { ffi::dex_lidar_3d_age_ms(self.0.as_ptr(), &mut value) })?;
        Ok(Duration::from_millis(value))
    }
    pub fn x(&self) -> Result<&[f64]> {
        let mut data = ptr::null();
        let mut count = 0;
        call(|_| unsafe {
            ffi::dex_lidar_3d_channel_f64(self.0.as_ptr(), 0, &mut data, &mut count)
        })?;
        // SAFETY: the immutable snapshot owns this channel; the result borrows self.
        unsafe { borrowed(data, count) }
    }
    pub fn y(&self) -> Result<&[f64]> {
        let mut data = ptr::null();
        let mut count = 0;
        call(|_| unsafe {
            ffi::dex_lidar_3d_channel_f64(self.0.as_ptr(), 1, &mut data, &mut count)
        })?;
        // SAFETY: the immutable snapshot owns this channel; the result borrows self.
        unsafe { borrowed(data, count) }
    }
    pub fn z(&self) -> Result<&[f64]> {
        let mut data = ptr::null();
        let mut count = 0;
        call(|_| unsafe {
            ffi::dex_lidar_3d_channel_f64(self.0.as_ptr(), 2, &mut data, &mut count)
        })?;
        // SAFETY: the immutable snapshot owns this channel; the result borrows self.
        unsafe { borrowed(data, count) }
    }
    pub fn intensity(&self) -> Result<&[u32]> {
        let mut data = ptr::null();
        let mut count = 0;
        call(|_| unsafe {
            ffi::dex_lidar_3d_channel_u32(self.0.as_ptr(), 0, &mut data, &mut count)
        })?;
        // SAFETY: the immutable snapshot owns this channel; the result borrows self.
        unsafe { borrowed(data, count) }
    }
    pub fn ring(&self) -> Result<&[u32]> {
        let mut data = ptr::null();
        let mut count = 0;
        call(|_| unsafe {
            ffi::dex_lidar_3d_channel_u32(self.0.as_ptr(), 1, &mut data, &mut count)
        })?;
        // SAFETY: the immutable snapshot owns this channel; the result borrows self.
        unsafe { borrowed(data, count) }
    }
    pub fn point_timestamps_ns(&self) -> Result<&[u32]> {
        let mut data = ptr::null();
        let mut count = 0;
        call(|_| unsafe {
            ffi::dex_lidar_3d_channel_u32(self.0.as_ptr(), 2, &mut data, &mut count)
        })?;
        // SAFETY: the immutable snapshot owns this channel; the result borrows self.
        unsafe { borrowed(data, count) }
    }
}
impl Drop for Lidar3d {
    fn drop(&mut self) {
        unsafe { ffi::dex_lidar_3d_release(self.0.as_ptr()) }
    }
}
// SAFETY: snapshot data is immutable; its owning handle is kept alive by Rust borrows.
unsafe impl Send for Lidar3d {}
unsafe impl Sync for Lidar3d {}

fn sensor_age(age: Option<Duration>) -> Result<u64> {
    age.map(millis).transpose().map(|v| v.unwrap_or(0))
}
/// Caller must tie the returned slice's lifetime to the native buffer's owner.
unsafe fn borrowed<'a, T>(data: *const T, count: usize) -> Result<&'a [T]> {
    if count == 0 {
        return Ok(&[]);
    }
    if data.is_null() || count > isize::MAX as usize / std::mem::size_of::<T>() {
        return Err(invalid("Invalid native channel"));
    }
    // SAFETY: caller guarantees initialized storage valid for the owner's borrow.
    Ok(unsafe { std::slice::from_raw_parts(data, count) })
}
pub struct Camera<'a> {
    robot: &'a Robot,
    name: CString,
}
impl Camera<'_> {
    pub fn streams(&self) -> Result<Vec<String>> {
        let mut n = 0;
        call(|e| unsafe {
            ffi::dex_robot_camera_stream_count(self.robot.0.as_ptr(), self.name.as_ptr(), &mut n, e)
        })?;
        (0..n)
            .map(|i| {
                text(|out, e| unsafe {
                    ffi::dex_robot_camera_stream_name(
                        self.robot.0.as_ptr(),
                        self.name.as_ptr(),
                        i,
                        out,
                        e,
                    )
                })
            })
            .collect()
    }
    /// history=0 keeps only the latest frame; positive values retain bounded history.
    pub fn subscribe(&self, stream: &str, history: usize) -> Result<()> {
        let stream = string(stream)?;
        call(|e| unsafe {
            ffi::dex_robot_camera_subscribe(
                self.robot.0.as_ptr(),
                self.name.as_ptr(),
                stream.as_ptr(),
                history,
                e,
            )
        })
    }
    pub fn unsubscribe(&self, stream: &str) -> Result<()> {
        let stream = string(stream)?;
        call(|e| unsafe {
            ffi::dex_robot_camera_unsubscribe(
                self.robot.0.as_ptr(),
                self.name.as_ptr(),
                stream.as_ptr(),
                e,
            )
        })
    }
    pub fn latest_frame(&self, stream: &str) -> Result<Option<Frame>> {
        let stream = string(stream)?;
        let mut out = ptr::null_mut();
        let result = call(|e| unsafe {
            ffi::dex_robot_camera_latest_frame(
                self.robot.0.as_ptr(),
                self.name.as_ptr(),
                stream.as_ptr(),
                &mut out,
                e,
            )
        });
        let frame = NonNull::new(out).map(Frame);
        result?;
        Ok(frame)
    }
    pub fn stats(&self, stream: &str) -> Result<StreamStats> {
        let stream = string(stream)?;
        let mut out = StreamStats {
            struct_size: std::mem::size_of::<StreamStats>() as u32,
            ..Default::default()
        };
        call(|e| unsafe {
            ffi::dex_robot_camera_stats(
                self.robot.0.as_ptr(),
                self.name.as_ptr(),
                stream.as_ptr(),
                &mut out,
                e,
            )
        })?;
        Ok(out)
    }
    pub fn is_active(&self, stream: Option<&str>, window: Duration) -> Result<bool> {
        let stream = stream.map(string).transpose()?;
        let window = duration_ms(window)?;
        let mut active = false;
        call(|e| unsafe {
            ffi::dex_robot_camera_is_active(
                self.robot.0.as_ptr(),
                self.name.as_ptr(),
                stream.as_ref().map_or(ptr::null(), |s| s.as_ptr()),
                window,
                &mut active,
                e,
            )
        })?;
        Ok(active)
    }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FrameEncoding {
    Rgb8,
    Bgr8,
    Gray8,
    Depth32F,
    Unknown(u32),
}
/// An owning immutable image. Pixel slices cannot outlive this frame.
/// ```compile_fail
/// fn pixels(frame: dexcontrol::Frame) -> &'static [u8] {
///     frame.data().unwrap()
/// }
/// ```
pub struct Frame(NonNull<ffi::dex_frame_t>);
impl Frame {
    pub fn info(&self) -> Result<FrameInfo> {
        let mut out = FrameInfo {
            struct_size: std::mem::size_of::<FrameInfo>() as u32,
            ..Default::default()
        };
        call(|e| unsafe { ffi::dex_frame_info(self.0.as_ptr(), &mut out, e) })?;
        Ok(out)
    }
    pub fn encoding(&self) -> Result<FrameEncoding> {
        Ok(match self.info()?.encoding {
            0 => FrameEncoding::Rgb8,
            1 => FrameEncoding::Bgr8,
            2 => FrameEncoding::Gray8,
            3 => FrameEncoding::Depth32F,
            n => FrameEncoding::Unknown(n),
        })
    }
    pub fn data(&self) -> Result<&[u8]> {
        let mut out = ptr::null();
        let mut n = 0;
        call(|_| unsafe { ffi::dex_frame_data(self.0.as_ptr(), &mut out, &mut n) })?;
        // SAFETY: the frame owns immutable pixels; the returned slice borrows self.
        unsafe { borrowed(out, n) }
    }
}
impl Drop for Frame {
    fn drop(&mut self) {
        unsafe { ffi::dex_frame_release(self.0.as_ptr()) }
    }
}
// SAFETY: frames are immutable native snapshots with ownership tied to Rust borrows.
unsafe impl Send for Frame {}
unsafe impl Sync for Frame {}