forked from jerry73204/realsense-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor.rs
405 lines (351 loc) · 11.3 KB
/
sensor.rs
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
//! Defines the sensor type.
use crate::{
common::*,
device::Device,
error::{ErrorChecker, Result},
kind::{CameraInfo, Rs2Option},
options::ToOptions,
processing_block_list::ProcessingBlockList,
sensor_kind,
stream_profile_list::StreamProfileList,
};
/// The enumeration of extended sensor type returned by [Sensor::try_extend](Sensor::try_extend).
#[derive(Debug)]
pub enum ExtendedSensor {
Color(ColorSensor),
Depth(DepthSensor),
DepthStereo(DepthStereoSensor),
L500Depth(L500DepthSensor),
Motion(MotionSensor),
FishEye(FishEyeSensor),
Software(SoftwareSensor),
Pose(PoseSensor),
Tm2(Tm2Sensor),
Other(AnySensor),
}
impl ExtendedSensor {
pub fn color(self) -> Option<ColorSensor> {
match self {
Self::Color(sensor) => Some(sensor),
_ => None,
}
}
pub fn depth(self) -> Option<DepthSensor> {
match self {
Self::Depth(sensor) => Some(sensor),
_ => None,
}
}
pub fn depth_stereo(self) -> Option<DepthStereoSensor> {
match self {
Self::DepthStereo(sensor) => Some(sensor),
_ => None,
}
}
pub fn l500_depth(self) -> Option<L500DepthSensor> {
match self {
Self::L500Depth(sensor) => Some(sensor),
_ => None,
}
}
pub fn motion(self) -> Option<MotionSensor> {
match self {
Self::Motion(sensor) => Some(sensor),
_ => None,
}
}
pub fn fish_eye(self) -> Option<FishEyeSensor> {
match self {
Self::FishEye(sensor) => Some(sensor),
_ => None,
}
}
pub fn software(self) -> Option<SoftwareSensor> {
match self {
Self::Software(sensor) => Some(sensor),
_ => None,
}
}
pub fn pose(self) -> Option<PoseSensor> {
match self {
Self::Pose(sensor) => Some(sensor),
_ => None,
}
}
pub fn tm2(self) -> Option<Tm2Sensor> {
match self {
Self::Tm2(sensor) => Some(sensor),
_ => None,
}
}
pub fn other(self) -> Option<AnySensor> {
match self {
Self::Other(sensor) => Some(sensor),
_ => None,
}
}
}
/// Represents a sensor on device.
#[derive(Debug)]
pub struct Sensor<Kind>
where
Kind: sensor_kind::SensorKind,
{
pub(crate) ptr: NonNull<sys::rs2_sensor>,
_phantom: PhantomData<Kind>,
}
// type aliases
pub type ColorSensor = Sensor<sensor_kind::Color>;
pub type DepthSensor = Sensor<sensor_kind::Depth>;
pub type DepthStereoSensor = Sensor<sensor_kind::DepthStereo>;
pub type L500DepthSensor = Sensor<sensor_kind::L500Depth>;
pub type MotionSensor = Sensor<sensor_kind::Motion>;
pub type FishEyeSensor = Sensor<sensor_kind::FishEye>;
pub type SoftwareSensor = Sensor<sensor_kind::Software>;
pub type PoseSensor = Sensor<sensor_kind::Pose>;
pub type Tm2Sensor = Sensor<sensor_kind::Tm2>;
pub type AnySensor = Sensor<sensor_kind::Any>;
impl<Kind> Sensor<Kind>
where
Kind: sensor_kind::SensorKind,
{
/// Gets the corresponding device for sensor.
pub fn device(&self) -> Result<Device> {
let device = unsafe {
let mut checker = ErrorChecker::new();
let ptr =
sys::rs2_create_device_from_sensor(self.ptr.as_ptr(), checker.inner_mut_ptr());
checker.check()?;
Device::from_raw(ptr)
};
Ok(device)
}
/// Gets an attribute on sensor.
///
/// It will return error if the attribute is not available on sensor.
pub fn get_option(&self, option: Rs2Option) -> Result<f32> {
unsafe {
let mut checker = ErrorChecker::new();
let val = sys::rs2_get_option(
self.ptr.as_ptr().cast::<sys::rs2_options>(),
option as sys::rs2_option,
checker.inner_mut_ptr(),
);
checker.check()?;
Ok(val)
}
}
// pub fn set_option(&mut self, option: Rs2Option, value: f32) -> Result<()> {
// unsafe {
// let mut checker = ErrorChecker::new();
// let val = sys::rs2_set_option(
// self.ptr.as_ptr().cast::<sys::rs2_options>(),
// option as sys::rs2_option,
// value,
// checker.inner_mut_ptr(),
// );
// checker.check()?;
// }
// Ok(())
// }
/// List stream profiles on sensor.
pub fn stream_profiles(&self) -> Result<StreamProfileList> {
let list = unsafe {
let mut checker = ErrorChecker::new();
let ptr = sys::rs2_get_stream_profiles(self.ptr.as_ptr(), checker.inner_mut_ptr());
checker.check()?;
StreamProfileList::from_raw(ptr)
};
Ok(list)
}
/// Retrieves list of recommended processing blocks.
pub fn recommended_processing_blocks(&self) -> Result<ProcessingBlockList> {
let list = unsafe {
let mut checker = ErrorChecker::new();
let ptr = sys::rs2_get_recommended_processing_blocks(
self.ptr.as_ptr(),
checker.inner_mut_ptr(),
);
checker.check()?;
ProcessingBlockList::from_raw(ptr)
};
Ok(list)
}
pub fn name(&self) -> Result<&str> {
self.info(CameraInfo::Name)
}
pub fn serial_number(&self) -> Result<&str> {
self.info(CameraInfo::SerialNumber)
}
pub fn recommended_firmware_version(&self) -> Result<&str> {
self.info(CameraInfo::RecommendedFirmwareVersion)
}
pub fn physical_port(&self) -> Result<&str> {
self.info(CameraInfo::PhysicalPort)
}
pub fn debug_op_code(&self) -> Result<&str> {
self.info(CameraInfo::DebugOpCode)
}
pub fn advanced_mode(&self) -> Result<&str> {
self.info(CameraInfo::AdvancedMode)
}
pub fn product_id(&self) -> Result<&str> {
self.info(CameraInfo::ProductId)
}
pub fn camera_locked(&self) -> Result<&str> {
self.info(CameraInfo::CameraLocked)
}
pub fn usb_type_descriptor(&self) -> Result<&str> {
self.info(CameraInfo::UsbTypeDescriptor)
}
pub fn product_line(&self) -> Result<&str> {
self.info(CameraInfo::ProductLine)
}
pub fn asic_serial_number(&self) -> Result<&str> {
self.info(CameraInfo::AsicSerialNumber)
}
pub fn firmware_update_id(&self) -> Result<&str> {
self.info(CameraInfo::FirmwareUpdateId)
}
pub fn count(&self) -> Result<&str> {
self.info(CameraInfo::Count)
}
pub fn info(&self, kind: CameraInfo) -> Result<&str> {
let ptr = unsafe {
let mut checker = ErrorChecker::new();
let ptr = sys::rs2_get_sensor_info(
self.ptr.as_ptr(),
kind as sys::rs2_camera_info,
checker.inner_mut_ptr(),
);
checker.check()?;
ptr
};
// TODO: deallicate this CStr?
let string = unsafe { CStr::from_ptr(ptr).to_str().unwrap() };
Ok(string)
}
pub fn is_info_supported(&self, kind: CameraInfo) -> Result<bool> {
let val = unsafe {
let mut checker = ErrorChecker::new();
let val = sys::rs2_supports_sensor_info(
self.ptr.as_ptr(),
kind as sys::rs2_camera_info,
checker.inner_mut_ptr(),
);
checker.check()?;
val
};
Ok(val != 0)
}
pub fn into_raw(self) -> *mut sys::rs2_sensor {
let ptr = self.ptr;
mem::forget(self);
ptr.as_ptr()
}
pub unsafe fn from_raw(ptr: *mut sys::rs2_sensor) -> Self {
Self {
ptr: NonNull::new(ptr).unwrap(),
_phantom: PhantomData,
}
}
}
impl AnySensor {
pub fn is_extendable_to<Kind>(&self) -> Result<bool>
where
Kind: sensor_kind::NonAnySensorKind,
{
unsafe {
let mut checker = ErrorChecker::new();
let val = sys::rs2_is_sensor_extendable_to(
self.ptr.as_ptr(),
Kind::EXTENSION as sys::rs2_extension,
checker.inner_mut_ptr(),
);
checker.check()?;
Ok(val != 0)
}
}
/// Extends to a specific sensor subtype.
pub fn try_extend_to<Kind>(self) -> Result<result::Result<Sensor<Kind>, Self>>
where
Kind: sensor_kind::NonAnySensorKind,
{
if self.is_extendable_to::<Kind>()? {
let ptr = self.into_raw();
let sensor = Sensor {
ptr: NonNull::new(ptr).unwrap(),
_phantom: PhantomData,
};
Ok(Ok(sensor))
} else {
Ok(Err(self))
}
}
/// Extends to one of a sensor subtype.
pub fn try_extend(self) -> Result<ExtendedSensor> {
let sensor_any = self;
let sensor_any = match sensor_any.try_extend_to::<sensor_kind::DepthStereo>()? {
Ok(sensor) => return Ok(ExtendedSensor::DepthStereo(sensor)),
Err(sensor) => sensor,
};
let sensor_any = match sensor_any.try_extend_to::<sensor_kind::Depth>()? {
Ok(sensor) => return Ok(ExtendedSensor::Depth(sensor)),
Err(sensor) => sensor,
};
let sensor_any = match sensor_any.try_extend_to::<sensor_kind::L500Depth>()? {
Ok(sensor) => return Ok(ExtendedSensor::L500Depth(sensor)),
Err(sensor) => sensor,
};
let sensor_any = match sensor_any.try_extend_to::<sensor_kind::Color>()? {
Ok(sensor) => return Ok(ExtendedSensor::Color(sensor)),
Err(sensor) => sensor,
};
let sensor_any = match sensor_any.try_extend_to::<sensor_kind::Motion>()? {
Ok(sensor) => return Ok(ExtendedSensor::Motion(sensor)),
Err(sensor) => sensor,
};
let sensor_any = match sensor_any.try_extend_to::<sensor_kind::FishEye>()? {
Ok(sensor) => return Ok(ExtendedSensor::FishEye(sensor)),
Err(sensor) => sensor,
};
let sensor_any = match sensor_any.try_extend_to::<sensor_kind::Software>()? {
Ok(sensor) => return Ok(ExtendedSensor::Software(sensor)),
Err(sensor) => sensor,
};
let sensor_any = match sensor_any.try_extend_to::<sensor_kind::Pose>()? {
Ok(sensor) => return Ok(ExtendedSensor::Pose(sensor)),
Err(sensor) => sensor,
};
let sensor_any = match sensor_any.try_extend_to::<sensor_kind::Tm2>()? {
Ok(sensor) => return Ok(ExtendedSensor::Tm2(sensor)),
Err(sensor) => sensor,
};
Ok(ExtendedSensor::Other(sensor_any))
}
}
impl DepthSensor {
/// Gets the depth units of depth sensor.
pub fn depth_units(&self) -> Result<f32> {
self.get_option(Rs2Option::DepthUnits)
}
}
impl<Kind> ToOptions for Sensor<Kind>
where
Kind: sensor_kind::SensorKind,
{
fn options_ptr(&self) -> NonNull<sys::rs2_options> {
self.ptr.cast::<sys::rs2_options>()
}
}
unsafe impl<Kind> Send for Sensor<Kind> where Kind: sensor_kind::SensorKind {}
impl<Kind> Drop for Sensor<Kind>
where
Kind: sensor_kind::SensorKind,
{
fn drop(&mut self) {
unsafe {
sys::rs2_delete_sensor(self.ptr.as_ptr());
}
}
}