vzense-rust 0.4.2

High-level library for Vzense cameras
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
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
//! Basic routines to initialize or shut down a device and to set/get parameters.

use std::{ffi::CStr, os::raw::c_char, thread::sleep, time::Duration};

use sys::PsReturnStatus_PsRetOK as OK;
use vzense_sys::dcam560 as sys;

use crate::{
    ColorFormat, ColorResolution, DataMode, DepthMeasuringRange, Resolution, cyan, red, yellow,
};

use super::SESSION_INDEX;

/// The main interface to the camera.
pub struct Device {
    pub(super) handle: sys::PsDeviceHandle,
    pub(super) frame_ready: sys::PsFrameReady,
    pub(super) frame: sys::PsFrame,
    pub(super) color_resolution: ColorResolution,
    pub(super) color_is_mapped: bool,
    pub(super) depth_is_mapped: bool,
    pub(super) current_frame_is_depth: bool,
    pub(super) min_depth_mm: u16,
    pub(super) max_depth_mm: u16,
}
impl Device {
    /// Initializes the sytem and returns a device if it finds one. Make sure a Vzense camera is connected. `scan_time` should be at least one second to find a device. Set `scan_time = Duration::MAX` to scan until a device was found (useful to wait for reconnection after the connection to a device was interrupted).
    pub fn initialize(scan_time: Duration, verbose: bool) -> Result<Self, String> {
        initialize(verbose)?;

        let device_count = get_device_count(scan_time, verbose)?;

        let mut device = Device::open_device_by_ip(get_ip(device_count)?)?;

        if verbose {
            let info = device.get_device_info(device_count)?;
            println!(
                "{}",
                cyan!("model: {}, IP: {}, firmware: {}", info[0], info[1], info[2])
            );
        }

        device.start_stream(verbose)?;

        device.set_color_resolution(ColorResolution::Res640x480);

        Ok(device)
    }

    /// Choosing the min/max depth in mm for the color mapping of the depth output. These values also bound the depths used in the `util::TochDetector` to reduce measuring artifacts.
    pub fn set_depth_range(&mut self, min_depth_mm: u16, max_depth_mm: u16) -> Result<(), String> {
        if min_depth_mm >= max_depth_mm {
            return Err(red!("Input Error: min depth must be less than max depth"));
        }
        self.min_depth_mm = min_depth_mm;
        self.max_depth_mm = max_depth_mm;
        Ok(())
    }

    /// Get the current frame rate of the camera.
    pub fn get_frame_rate(&self) -> Result<u8, String> {
        let mut rate = 0;
        let status = unsafe { sys::Ps2_GetTofFrameRate(self.handle, SESSION_INDEX, &mut rate) };
        if status != OK {
            return Err(red!("get frame rate failed with status {}", status));
        }
        Ok(rate)
    }

    /// Set the ToF frame rate. Different devices have different maximum values. Please refer to the device specification.
    pub fn set_frame_rate(&self, rate: u8) -> Result<(), String> {
        let status = unsafe { sys::Ps2_SetTofFrameRate(self.handle, SESSION_INDEX, rate) };
        if status != OK {
            return Err(red!("set frame rate failed with status {}", status));
        }
        Ok(())
    }

    /// Get frame info like frame type, pixel format, width, height, etc.
    pub fn get_frame_info(&self) -> String {
        format!("{:?}", self.frame)
    }

    /// Checks if the number of pixels in `frame` equals `pixel_count`.
    pub fn check_pixel_count(&self, pixel_count: usize) {
        let w = self.frame.width as usize;
        let h = self.frame.height as usize;
        if w * h != pixel_count {
            println!("{}", red!("!!! pixel count is not equal to {} * {}", w, h))
        }
    }

    /// Set the color frame format to either RGB or BGR.
    pub fn set_color_format(&self, format: ColorFormat) {
        unsafe {
            match format {
                ColorFormat::Rgb => sys::Ps2_SetColorPixelFormat(
                    self.handle,
                    SESSION_INDEX,
                    sys::PsPixelFormat_PsPixelFormatRGB888,
                ),
                ColorFormat::Bgr => sys::Ps2_SetColorPixelFormat(
                    self.handle,
                    SESSION_INDEX,
                    sys::PsPixelFormat_PsPixelFormatBGR888,
                ),
            };
        }
    }

    /// Enable or disable the mapping of the color image to depth camera space.
    pub fn map_color_to_depth(&mut self, is_enabled: bool) {
        if self.color_resolution != ColorResolution::Res640x480 {
            self.set_color_resolution(ColorResolution::Res640x480);
            self.color_resolution = ColorResolution::Res640x480;
        }
        unsafe {
            // should actually be `Ps2_SetMapperEnabledRGBToDepth` but the names seem to be mixed up
            sys::Ps2_SetMapperEnabledDepthToRGB(
                self.handle,
                SESSION_INDEX,
                if is_enabled { 1 } else { 0 },
            );
        }
        self.color_is_mapped = is_enabled;
    }

    /// Enable or disable the mapping of the depth image to color camera space.
    pub fn map_depth_to_color(&mut self, is_enabled: bool) {
        if self.color_resolution != ColorResolution::Res640x480 {
            self.set_color_resolution(ColorResolution::Res640x480);
            self.color_resolution = ColorResolution::Res640x480;
        }
        unsafe {
            // should actually be `Ps2_SetMapperEnabledDepthToRGB` but the names seem to be mixed up
            sys::Ps2_SetMapperEnabledRGBToDepth(
                self.handle,
                SESSION_INDEX,
                if is_enabled { 1 } else { 0 },
            );
        }
        self.depth_is_mapped = is_enabled;
    }

    /// Sets the resolution of the color frame. Three resolutions are currently available: 640x480, 800x600, and 1600x1200.
    pub fn set_color_resolution(&mut self, resolution: ColorResolution) -> Resolution {
        if self.color_is_mapped || self.depth_is_mapped {
            println!(
                "{}",
                yellow!(
                    "setting of color resolution is ignored because color frame is mapped to depth or vice versa"
                )
            );
        } else {
            let res = match resolution {
                ColorResolution::Res640x480 => sys::PsResolution_PsRGB_Resolution_640_480,
                ColorResolution::Res800x600 => sys::PsResolution_PsRGB_Resolution_800_600,
                ColorResolution::Res1600x1200 => sys::PsResolution_PsRGB_Resolution_1600_1200,
            };
            unsafe {
                sys::Ps2_SetRGBResolution(self.handle, SESSION_INDEX, res);
            }
            self.color_resolution = resolution;
        }
        self.get_color_resolution()
    }

    /// Returns the resolution of the color frame.
    pub fn get_color_resolution(&self) -> Resolution {
        let mut resolution_type = 0;
        unsafe {
            sys::Ps2_GetRGBResolution(self.handle, SESSION_INDEX, &mut resolution_type);
        }
        match resolution_type {
            2 => Resolution::new(640, 480),
            5 => Resolution::new(800, 600),
            4 => Resolution::new(1600, 1200),
            _ => panic!("{}", red!("unknown rgb resolution")),
        }
    }

    /// Sets the depth range mode.
    pub fn set_depth_measuring_range(&self, depth_range: DepthMeasuringRange) {
        let depth_range = match depth_range {
            DepthMeasuringRange::Near => sys::PsDepthRange_PsNearRange,
            DepthMeasuringRange::Mid => sys::PsDepthRange_PsMidRange,
            DepthMeasuringRange::Far => sys::PsDepthRange_PsFarRange,
        };
        unsafe {
            sys::Ps2_SetDepthRange(self.handle, SESSION_INDEX, depth_range);
        }
    }

    /// Returns the current depth measuring range `(min, max)` of the camera in mm.
    pub fn get_depth_measuring_range(&self) -> (u16, u16) {
        let mut depth_range = sys::PsDepthRange::default();

        unsafe {
            sys::Ps2_GetDepthRange(self.handle, SESSION_INDEX, &mut depth_range);
        }

        let mut mr = sys::PsMeasuringRange::default();

        unsafe {
            sys::Ps2_GetMeasuringRange(self.handle, SESSION_INDEX, depth_range, &mut mr);
        }

        match depth_range {
            sys::PsDepthRange_PsNearRange => (mr.effectDepthMinNear, mr.effectDepthMaxNear),
            sys::PsDepthRange_PsMidRange => (mr.effectDepthMinMid, mr.effectDepthMaxMid),
            sys::PsDepthRange_PsFarRange => (mr.effectDepthMinFar, mr.effectDepthMaxFar),
            _ => panic!("{}", red!("unknown measuring range")),
        }
    }

    /// Set the wait time for the call to `read_next_frame` in ms.
    pub fn set_wait_time(&self, time: u16) {
        unsafe {
            sys::Ps2_SetWaitTimeOfReadNextFrame(self.handle, SESSION_INDEX, time);
        }
    }

    /// Set data mode
    pub fn set_data_mode(&self, mode: DataMode) {
        let mode = match mode {
            DataMode::DepthAndRGB => sys::PsDataMode_PsDepthAndRGB_30,
            DataMode::IRAndRGB => sys::PsDataMode_PsIRAndRGB_30,
            DataMode::DepthAndIRAndRGB => sys::PsDataMode_PsDepthAndIRAndRGB_30,
        };

        unsafe {
            sys::Ps2_SetDataMode(self.handle, SESSION_INDEX, mode);
        }
    }

    /// Current data mode.
    pub fn get_data_mode(&self) -> Result<DataMode, String> {
        let mut data_mode = sys::PsDataMode::default();
        let status = unsafe { sys::Ps2_GetDataMode(self.handle, SESSION_INDEX, &mut data_mode) };
        if status != OK {
            return Err(red!("get data mode failed with status {}", status));
        }
        match data_mode {
            sys::PsDataMode_PsDepthAndRGB_30 => Ok(DataMode::DepthAndRGB),
            sys::PsDataMode_PsIRAndRGB_30 => Ok(DataMode::IRAndRGB),
            sys::PsDataMode_PsDepthAndIRAndRGB_30 => Ok(DataMode::DepthAndIRAndRGB),
            _ => Err(red!("unknown data mode")),
        }
    }

    /// Stops the stream, closes the device, and clears all resources.
    pub fn shut_down(&mut self, verbose: bool) {
        unsafe {
            sys::Ps2_StopStream(self.handle, SESSION_INDEX);
            sys::Ps2_CloseDevice(&mut self.handle);

            let status = sys::Ps2_Shutdown();
            if status != OK {
                println!("{}", red!("shut down failed with status: {}", status));
            } else if verbose {
                println!("shut down device successfully");
            }
        }
    }

    /// Returns device info as an array of Strings: \[model, IP, firmware, serial number\]
    pub fn get_device_info(&self, device_count: u32) -> Result<[String; 4], String> {
        if device_count == 0 {
            return Err(red!("no device to get info for"));
        }

        let mut device_info = sys::PsDeviceInfo::default();

        unsafe { sys::Ps2_GetDeviceListInfo(&mut device_info, device_count) };

        let ip = device_info.ip.as_ptr();
        let uri = device_info.uri.as_ptr(); // model_name:serial_number
        let serial = device_info.alias.as_ptr(); // serial number

        let firmware = self
            .get_firmware_version()
            .expect(&red!("Cannot get firmware version"));

        Ok([
            unsafe { CStr::from_ptr(uri) }
                .to_str()
                .unwrap()
                .split(":")
                .collect::<Vec<&str>>()[0]
                .to_string(),
            unsafe { CStr::from_ptr(ip) }.to_string_lossy().into_owned(),
            firmware,
            unsafe { CStr::from_ptr(serial) }
                .to_string_lossy()
                .into_owned(),
        ])
    }

    // private functions_______________________________________________________

    fn get_firmware_version(&self) -> Result<String, String> {
        let mut buffer = [0; 64];
        match get_firmware_version(self.handle, &mut buffer) {
            OK => Ok(CStr::from_bytes_until_nul(&buffer)
                .unwrap()
                .to_string_lossy()
                .into_owned()),
            error_code => Err(format!("{}", error_code)),
        }
    }

    fn open_device_by_ip(ip: *const c_char) -> Result<Self, String> {
        let mut handle = 0 as sys::PsDeviceHandle;
        let status = unsafe { sys::Ps2_OpenDeviceByIP(ip, &mut handle) };
        if status != OK {
            return Err(red!("open device failed with status {}", status));
        }
        if !handle.is_null() {
            Ok(Device {
                handle,
                frame_ready: sys::PsFrameReady::default(),
                frame: sys::PsFrame::default(),
                color_resolution: ColorResolution::Res640x480,
                color_is_mapped: false,
                depth_is_mapped: false,
                current_frame_is_depth: false,
                min_depth_mm: 500,  // default value
                max_depth_mm: 1000, // default value
            })
        } else {
            Err(red!("device ptr is null"))
        }
    }

    fn start_stream(&self, verbose: bool) -> Result<(), String> {
        let status = unsafe { sys::Ps2_StartStream(self.handle, SESSION_INDEX) };
        if status != OK {
            return Err(red!("start stream failed with status {}", status));
        }
        if verbose {
            println!("stream started")
        }
        Ok(())
    }

    #[deprecated(since = "0.3.0", note = "Use initialize(scan_time, verbose) instead.")]
    pub fn init() -> Result<Self, String> {
        Err("Deprecated, use initialize(scan_time, verbose) instead".to_string())
    }
}

/// `Data` trait to allow use of `Device` in `util::TouchDetector`.
impl crate::util::touch_detector::Data for Device {
    fn get_frame_p_frame_data(&self) -> *mut u8 {
        self.frame.pFrameData
    }
    fn get_frame_data_len(&self) -> usize {
        self.frame.dataLen as usize
    }
    fn get_min_depth_mm(&self) -> u16 {
        self.min_depth_mm
    }
    fn get_max_depth_mm(&self) -> u16 {
        self.max_depth_mm
    }
    fn current_frame_is_depth(&self) -> bool {
        self.current_frame_is_depth
    }
}

fn get_firmware_version(handle: sys::PsDeviceHandle, buffer: &mut [u8]) -> sys::PsReturnStatus {
    let len = buffer.len().try_into().unwrap();
    let ptr: *mut c_char = buffer.as_mut_ptr().cast();
    unsafe { sys::Ps2_GetFirmwareVersionNumber(handle, SESSION_INDEX, ptr, len) }
}

fn initialize(verbose: bool) -> Result<(), String> {
    if verbose {
        println!("initializing...");
    }
    let status = unsafe { sys::Ps2_Initialize() };

    // status -101 is reinitialization
    if status == -101 {
        if verbose {
            println!("reinitializing...");
        }
    } else if status != OK {
        return Err(red!("initialization failed with status {}", status));
    }
    Ok(())
}

/// Tries to find devices every 200 ms for duration `scan_time`.
fn get_device_count(scan_time: Duration, verbose: bool) -> Result<u32, String> {
    if scan_time < Duration::from_secs(1) {
        println!(
            "{}",
            yellow!("vzense-rust warning: scan time might be too short to detect a device")
        );
    }
    let sleep_interval = Duration::from_millis(200);
    let try_count = scan_time.div_duration_f64(sleep_interval).ceil() as u64;

    let mut device_count = 0;
    let mut times_tried = 0;
    let mut status;
    if verbose {
        println!("searching for device...");
    }
    loop {
        status = unsafe { sys::Ps2_GetDeviceCount(&mut device_count) };

        if status != OK {
            return Err(red!("get device count failed with status {}", status));
        } else {
            if device_count > 0 {
                if verbose {
                    println!("{}", cyan!("device found"));
                }
                break;
            }
            times_tried += 1;
            if times_tried >= try_count {
                return Err(red!("no device found"));
            }
            sleep(sleep_interval);
        }
    }
    Ok(device_count)
}

fn get_ip(device_count: u32) -> Result<*const c_char, String> {
    let mut device_info = sys::PsDeviceInfo::default();
    unsafe {
        let status = sys::Ps2_GetDeviceListInfo(&mut device_info, device_count);
        if status != OK {
            return Err(red!("get device list info failed with status {}", status));
        }
    }
    Ok(device_info.ip.as_ptr())
}