forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rs-pose-predict.cpp
103 lines (94 loc) · 3.95 KB
/
rs-pose-predict.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// 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 <math.h>
#include <float.h>
#include "example-utils.hpp"
inline rs2_quaternion quaternion_exp(rs2_vector v)
{
float x = v.x/2, y = v.y/2, z = v.z/2, th2, th = sqrtf(th2 = x*x + y*y + z*z);
float c = cosf(th), s = th2 < sqrtf(120*FLT_EPSILON) ? 1-th2/6 : sinf(th)/th;
rs2_quaternion Q = { s*x, s*y, s*z, c };
return Q;
}
inline rs2_quaternion quaternion_multiply(rs2_quaternion a, rs2_quaternion b)
{
rs2_quaternion Q = {
a.x * b.w + a.w * b.x - a.z * b.y + a.y * b.z,
a.y * b.w + a.z * b.x + a.w * b.y - a.x * b.z,
a.z * b.w - a.y * b.x + a.x * b.y + a.w * b.z,
a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z,
};
return Q;
}
rs2_pose predict_pose(rs2_pose & pose, float dt_s)
{
rs2_pose P = pose;
P.translation.x = dt_s * (dt_s/2 * pose.acceleration.x + pose.velocity.x) + pose.translation.x;
P.translation.y = dt_s * (dt_s/2 * pose.acceleration.y + pose.velocity.y) + pose.translation.y;
P.translation.z = dt_s * (dt_s/2 * pose.acceleration.z + pose.velocity.z) + pose.translation.z;
rs2_vector W = {
dt_s * (dt_s/2 * pose.angular_acceleration.x + pose.angular_velocity.x),
dt_s * (dt_s/2 * pose.angular_acceleration.y + pose.angular_velocity.y),
dt_s * (dt_s/2 * pose.angular_acceleration.z + pose.angular_velocity.z),
};
P.rotation = quaternion_multiply(quaternion_exp(W), pose.rotation);
return P;
}
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);
// 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 mutex;
auto callback = [&](const rs2::frame& frame)
{
std::lock_guard<std::mutex> lock(mutex);
if (rs2::pose_frame fp = frame.as<rs2::pose_frame>()) {
rs2_pose pose_data = fp.get_pose_data();
auto now = std::chrono::system_clock::now().time_since_epoch();
double now_ms = static_cast<double>(std::chrono::duration_cast<std::chrono::milliseconds>(now).count());
double pose_time_ms = fp.get_timestamp();
float dt_s = static_cast<float>(std::max(0., (now_ms - pose_time_ms)/1000.));
rs2_pose predicted_pose = predict_pose(pose_data, dt_s);
std::cout << "Predicted " << std::fixed << std::setprecision(3) << dt_s*1000 << "ms " <<
"Confidence: " << pose_data.tracker_confidence << " T: " <<
predicted_pose.translation.x << " " <<
predicted_pose.translation.y << " " <<
predicted_pose.translation.z << " (meters) \r";
}
};
// Start streaming through the callback with default recommended configuration
rs2::pipeline_profile profiles = pipe.start(cfg, callback);
std::cout << "started thread\n";
while(true) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
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;
}