forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rs-pose-and-image.cpp
87 lines (78 loc) · 2.9 KB
/
rs-pose-and-image.cpp
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
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2019 Intel Corporation. All Rights Reserved.
#include <librealsense2/rs.hpp>
#include <iostream>
#include <iomanip>
#include <chrono>
#include <thread>
#include <mutex>
#include "example-utils.hpp"
int main(int argc, char * argv[]) try
{
std::string serial;
if (!device_with_streams({ RS2_STREAM_POSE }, serial))
return EXIT_SUCCESS;
// Declare RealSense pipeline, encapsulating the actual device and sensors
rs2::pipeline pipe;
// Create a configuration for configuring the pipeline with a non default profile
rs2::config cfg;
if (!serial.empty())
cfg.enable_device(serial);
// Add pose stream
cfg.enable_stream(RS2_STREAM_POSE, RS2_FORMAT_6DOF);
// Enable both image streams
// Note: It is not currently possible to enable only one
cfg.enable_stream(RS2_STREAM_FISHEYE, 1, RS2_FORMAT_Y8);
cfg.enable_stream(RS2_STREAM_FISHEYE, 2, RS2_FORMAT_Y8);
// Define frame callback
// The callback is executed on a sensor thread and can be called simultaneously from multiple sensors
// Therefore any modification to common memory should be done under lock
std::mutex data_mutex;
uint64_t pose_counter = 0;
uint64_t frame_counter = 0;
bool first_data = true;
auto last_print = std::chrono::system_clock::now();
auto callback = [&](const rs2::frame& frame)
{
std::lock_guard<std::mutex> lock(data_mutex);
// Only start measuring time elapsed once we have received the
// first piece of data
if (first_data) {
first_data = false;
last_print = std::chrono::system_clock::now();
}
if (auto fp = frame.as<rs2::pose_frame>()) {
pose_counter++;
}
else if (auto fs = frame.as<rs2::frameset>()) {
frame_counter++;
}
// Print the approximate pose and image rates once per second
auto now = std::chrono::system_clock::now();
if (now - last_print >= std::chrono::seconds(1)) {
std::cout << "\r" << std::setprecision(0) << std::fixed
<< "Pose rate: " << pose_counter << " "
<< "Image rate: " << frame_counter << std::flush;
pose_counter = 0;
frame_counter = 0;
last_print = now;
}
};
// Start streaming through the callback
rs2::pipeline_profile profiles = pipe.start(cfg, callback);
// Sleep this thread until we are done
while(true) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
return EXIT_SUCCESS;
}
catch (const rs2::error & e)
{
std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
return EXIT_FAILURE;
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}