d400-motion.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2016 Intel Corporation. All Rights Reserved.
3 
4 #include "d400-motion.h"
5 
6 #include <mutex>
7 #include <chrono>
8 #include <vector>
9 #include <map>
10 #include <iterator>
11 #include <cstddef>
12 
13 #include <src/backend.h>
15 #include <src/metadata.h>
16 #include "ds/ds-timestamp.h"
17 #include "d400-options.h"
18 #include "d400-info.h"
19 #include "stream.h"
20 #include "proc/motion-transform.h"
22 #include <src/fourcc.h>
23 #include <src/metadata-parser.h>
24 #include <src/hid-sensor.h>
25 using namespace librealsense;
26 namespace librealsense
27 {
28  // D457 development
29  const std::map<uint32_t, rs2_format> motion_fourcc_to_rs2_format = {
30  {rs_fourcc('G','R','E','Y'), RS2_FORMAT_MOTION_XYZ32F},
31  };
32  const std::map<uint32_t, rs2_stream> motion_fourcc_to_rs2_stream = {
33  {rs_fourcc('G','R','E','Y'), RS2_STREAM_ACCEL},
34  };
35 
37  {
38  return _ds_motion_common->get_motion_intrinsics(stream);
39  }
40 
41  std::shared_ptr<synthetic_sensor> d400_motion_uvc::create_uvc_device(std::shared_ptr<context> ctx,
42  const std::vector<platform::uvc_device_info>& all_uvc_infos,
43  const firmware_version& camera_fw_version)
44  {
45  if (all_uvc_infos.empty())
46  {
47  LOG_WARNING("No UVC info provided, IMU is disabled");
48  return nullptr;
49  }
50 
51  std::vector<std::shared_ptr<platform::uvc_device>> imu_devices;
52  for (auto&& info : filter_by_mi(all_uvc_infos, 4)) // Filter just mi=4, IMU
53  imu_devices.push_back( get_backend()->create_uvc_device( info ) );
54 
55  std::unique_ptr< frame_timestamp_reader > timestamp_reader_backup( new ds_timestamp_reader() );
56  std::unique_ptr<frame_timestamp_reader> timestamp_reader_metadata(new ds_timestamp_reader_from_metadata_mipi_motion(std::move(timestamp_reader_backup)));
57 
58  auto enable_global_time_option = std::shared_ptr<global_time_option>(new global_time_option());
59 
60  auto raw_motion_ep = std::make_shared<uvc_sensor>("Raw IMU Sensor", std::make_shared<platform::multi_pins_uvc_device>(imu_devices),
61  std::unique_ptr<frame_timestamp_reader>(new global_timestamp_reader(std::move(timestamp_reader_metadata), _tf_keeper, enable_global_time_option)), this);
62 
63  auto motion_ep = std::make_shared<ds_motion_sensor>("Motion Module", raw_motion_ep, this,
65 
66  motion_ep->register_option(RS2_OPTION_GLOBAL_TIME_ENABLED, enable_global_time_option);
67 
68  // register pre-processing
69  std::shared_ptr<enable_motion_correction> mm_correct_opt = nullptr;
70 
71  // Motion intrinsic calibration presents is a prerequisite for motion correction.
72  try
73  {
74  if (_mm_calib)
75  {
76  mm_correct_opt = std::make_shared<enable_motion_correction>(motion_ep.get(),
77  option_range{ 0, 1, 1, 1 });
78  motion_ep->register_option(RS2_OPTION_ENABLE_MOTION_CORRECTION, mm_correct_opt);
79  }
80  }
81  catch (...) {}
82 
83  // For FW >=5.16 the scale factor changed to 0.0001 to support higher resolution (diff between two adjacent samples)
84  double gyro_scale_factor = _fw_version >= firmware_version( 5, 16, 0, 0 ) ? 0.0001 : 0.1 ;
85 
86  motion_ep->register_processing_block(
89  [&, mm_correct_opt, gyro_scale_factor]()
90  { return std::make_shared< motion_to_accel_gyro >( _mm_calib, mm_correct_opt, gyro_scale_factor );
91  });
92 
93  return motion_ep;
94  }
95 
96 
97  std::shared_ptr<synthetic_sensor> d400_motion::create_hid_device(std::shared_ptr<context> ctx,
98  const std::vector<platform::hid_device_info>& all_hid_infos,
99  const firmware_version& camera_fw_version)
100  {
101  return _ds_motion_common->create_hid_device(ctx, all_hid_infos, camera_fw_version, _tf_keeper);
102  }
103 
104  d400_motion_base::d400_motion_base( std::shared_ptr< const d400_info > const & dev_info )
105  : device(dev_info),
106  d400_device(dev_info),
107  _accel_stream(new stream(RS2_STREAM_ACCEL)),
108  _gyro_stream(new stream(RS2_STREAM_GYRO))
109  {
110  _ds_motion_common = std::make_shared<ds_motion_common>(this, _fw_version,
112  }
113 
114  d400_motion::d400_motion( std::shared_ptr< const d400_info > const & dev_info )
115  : device(dev_info),
116  d400_device(dev_info),
117  d400_motion_base(dev_info)
118  {
119  using namespace ds;
120 
121  std::vector<platform::hid_device_info> hid_infos = dev_info->get_group().hid_devices;
122 
123  _ds_motion_common->init_motion(hid_infos.empty(), *_depth_stream);
124 
125  initialize_fisheye_sensor( dev_info->get_context(), dev_info->get_group() );
126 
127  // Try to add HID endpoint
128  auto hid_ep = create_hid_device(dev_info->get_context(), dev_info->get_group().hid_devices, _fw_version);
129  if (hid_ep)
130  {
131  _motion_module_device_idx = static_cast<uint8_t>(add_sensor(hid_ep));
132 
133  // HID metadata attributes
134  hid_ep->get_raw_sensor()->register_metadata(RS2_FRAME_METADATA_FRAME_TIMESTAMP, make_hid_header_parser(&hid_header::timestamp));
135  }
136  //for FW >=5.16 the scale factor changes to 1000.0 since FW sends 32bit
137  if (_fw_version >= firmware_version( 5, 15, 1, 224))
138  get_raw_motion_sensor()->set_gyro_scale_factor( 10000.0 );
139 
140  }
141 
142 
144  {
145  return dynamic_cast< ds_motion_sensor & >( get_sensor( _motion_module_device_idx.value() ) );
146  }
147 
148  std::shared_ptr<hid_sensor> d400_motion::get_raw_motion_sensor()
149  {
150  auto raw_sensor = get_motion_sensor().get_raw_sensor();
151  return std::dynamic_pointer_cast< hid_sensor >( raw_sensor );
152  }
153 
154  d400_motion_uvc::d400_motion_uvc( std::shared_ptr< const d400_info > const & dev_info )
155  : device(dev_info),
156  d400_device(dev_info),
157  d400_motion_base(dev_info)
158  {
159  using namespace ds;
160 
161  std::vector<platform::uvc_device_info> uvc_infos = dev_info->get_group().uvc_devices;
162 
163  _ds_motion_common->init_motion(uvc_infos.empty(), *_depth_stream);
164 
165  if (!uvc_infos.empty())
166  {
167  // product id - D457 dev - check - must not be the front of uvc_infos vector
168  _pid = uvc_infos.front().pid;
169  }
170 
171  // Try to add HID endpoint
172  std::shared_ptr<synthetic_sensor> sensor_ep;
173  sensor_ep = create_uvc_device(dev_info->get_context(), dev_info->get_group().uvc_devices, _fw_version);
174  if (sensor_ep)
175  {
176  _motion_module_device_idx = static_cast<uint8_t>(add_sensor(sensor_ep));
177 
178  // HID metadata attributes - D457 dev - check metadata parser
179  sensor_ep->get_raw_sensor()->register_metadata(RS2_FRAME_METADATA_FRAME_TIMESTAMP, make_hid_header_parser(&hid_header::timestamp));
180  }
181  }
182 
183  void d400_motion::initialize_fisheye_sensor(std::shared_ptr<context> ctx, const platform::backend_device_group& group)
184  {
185  using namespace ds;
186 
187  bool is_fisheye_avaialable = false;
188  auto fisheye_infos = _ds_motion_common->init_fisheye(group, is_fisheye_avaialable);
189  if (!is_fisheye_avaialable)
190  return;
191 
192  std::unique_ptr< frame_timestamp_reader > ds_timestamp_reader_backup( new ds_timestamp_reader() );
193  std::unique_ptr<frame_timestamp_reader> ds_timestamp_reader_metadata(new ds_timestamp_reader_from_metadata(std::move(ds_timestamp_reader_backup)));
194  auto enable_global_time_option = std::shared_ptr<global_time_option>(new global_time_option());
195  auto raw_fisheye_ep
196  = std::make_shared< uvc_sensor >( "FishEye Sensor",
197  get_backend()->create_uvc_device( fisheye_infos.front() ),
198  std::unique_ptr< frame_timestamp_reader >( new global_timestamp_reader(
199  std::move( ds_timestamp_reader_metadata ),
200  _tf_keeper,
201  enable_global_time_option ) ),
202  this );
203  auto fisheye_ep = std::make_shared<ds_fisheye_sensor>(raw_fisheye_ep, this);
204 
205  _ds_motion_common->assign_fisheye_ep(raw_fisheye_ep, fisheye_ep, enable_global_time_option);
206 
207  register_fisheye_options();
208 
209  register_fisheye_metadata();
210 
211  // Add fisheye endpoint
212  _fisheye_device_idx = add_sensor(fisheye_ep);
213  }
214 
216  {
217  _ds_motion_common->register_fisheye_options();
218  }
219 
221  {
222  _ds_motion_common->register_fisheye_metadata();
223  }
224 
226  {
228  }
229 }
d400-motion.h
librealsense
Definition: algo.h:18
RS2_FRAME_METADATA_FRAME_TIMESTAMP
@ RS2_FRAME_METADATA_FRAME_TIMESTAMP
Definition: rs_frame.h:32
librealsense::d400_motion::get_raw_motion_sensor
std::shared_ptr< hid_sensor > get_raw_motion_sensor()
Definition: d400-motion.cpp:148
uint8_t
unsigned char uint8_t
Definition: stdint.h:78
platform-utils.h
rsutils::version
Definition: version.h:20
librealsense::d400_device::_depth_stream
std::shared_ptr< stream_interface > _depth_stream
Definition: d400-device.h:99
librealsense::motion_fourcc_to_rs2_stream
const std::map< uint32_t, rs2_stream > motion_fourcc_to_rs2_stream
Definition: d400-motion.cpp:32
librealsense::synthetic_sensor::get_raw_sensor
const std::shared_ptr< raw_sensor_base > & get_raw_sensor() const
Definition: sensor.h:243
librealsense::d400_motion_base::_motion_module_device_idx
optional_value< uint8_t > _motion_module_device_idx
Definition: d400-motion.h:39
librealsense::d400_motion::register_stream_to_extrinsic_group
void register_stream_to_extrinsic_group(const stream_interface &stream, uint32_t group_index)
Definition: d400-motion.cpp:225
RS2_OPTION_GLOBAL_TIME_ENABLED
@ RS2_OPTION_GLOBAL_TIME_ENABLED
Definition: rs_option.h:81
librealsense::make_hid_header_parser
std::shared_ptr< md_attribute_parser_base > make_hid_header_parser(Attribute St::*attribute, attrib_modifyer mod=nullptr)
A utility function to create HID metadata header parser.
Definition: metadata-parser.h:264
librealsense::hid_header::timestamp
uint64_t timestamp
Definition: src/metadata.h:799
librealsense::device::register_stream_to_extrinsic_group
void register_stream_to_extrinsic_group(const stream_interface &stream, uint32_t groupd_index)
Definition: device.cpp:163
librealsense::platform::backend_device_group
Definition: backend-device-group.h:50
librealsense::d400_motion::register_fisheye_metadata
void register_fisheye_metadata()
Definition: d400-motion.cpp:220
librealsense::rs_fourcc
uint32_t rs_fourcc(const T a, const T b, const T c, const T d)
Definition: fourcc.h:13
LOG_WARNING
#define LOG_WARNING(...)
Definition: easyloggingpp.h:72
librealsense::d400_device::_hw_monitor
std::shared_ptr< hw_monitor > _hw_monitor
Definition: d400-device.h:94
librealsense::platform::filter_by_mi
std::vector< uvc_device_info > filter_by_mi(const std::vector< uvc_device_info > &devices, uint32_t mi)
Definition: platform-utils.cpp:134
hid-sensor.h
librealsense::option_range
Definition: option-interface.h:14
librealsense::optional_value::value
T & value() &
Definition: src/types.h:129
librealsense::device::add_sensor
int add_sensor(const std::shared_ptr< sensor_interface > &sensor_base)
Definition: device.cpp:76
rs2_motion_device_intrinsic
Motion device intrinsics: scale, bias, and variances.
Definition: rs_types.h:71
uint32_t
unsigned int uint32_t
Definition: stdint.h:80
librealsense::d400_motion::register_fisheye_options
void register_fisheye_options()
Definition: d400-motion.cpp:215
motion-transform.h
librealsense::d400_device::_fw_version
firmware_version _fw_version
Definition: d400-device.h:95
RS2_STREAM_GYRO
@ RS2_STREAM_GYRO
Definition: rs_sensor.h:50
librealsense::d400_device::_device_capabilities
ds::ds_caps _device_capabilities
Definition: d400-device.h:97
librealsense::stream_interface
Definition: stream-interface.h:13
d400-options.h
librealsense::ds_timestamp_reader_from_metadata
Definition: ds-timestamp.h:13
librealsense::global_time_interface::_tf_keeper
std::shared_ptr< time_diff_keeper > _tf_keeper
Definition: global_timestamp_reader.h:114
librealsense::motion_fourcc_to_rs2_format
const std::map< uint32_t, rs2_format > motion_fourcc_to_rs2_format
Definition: d400-motion.cpp:29
librealsense::d400_motion_base::get_motion_intrinsics
rs2_motion_device_intrinsic get_motion_intrinsics(rs2_stream) const
Definition: d400-motion.cpp:36
librealsense::d400_motion_base::d400_motion_base
d400_motion_base(std::shared_ptr< const d400_info > const &dev_info)
Definition: d400-motion.cpp:104
d400-info.h
librealsense::firmware_version
rsutils::version firmware_version
Definition: src/firmware-version.h:11
librealsense::d400_motion::initialize_fisheye_sensor
void initialize_fisheye_sensor(std::shared_ptr< context > ctx, const platform::backend_device_group &group)
Definition: d400-motion.cpp:183
backend.h
librealsense::d400_motion::_motion_module_device_idx
optional_value< uint8_t > _motion_module_device_idx
Definition: d400-motion.h:68
librealsense::stream
Definition: src/stream.h:30
fps.info
info
Definition: fps.py:50
librealsense::d400_motion::d400_motion
d400_motion(std::shared_ptr< const d400_info > const &dev_info)
Definition: d400-motion.cpp:114
librealsense::d400_motion_base
Definition: d400-motion.h:12
auto-exposure-processor.h
RS2_STREAM_ACCEL
@ RS2_STREAM_ACCEL
Definition: rs_sensor.h:51
test-fps.ds
ds
Definition: test-fps.py:77
librealsense::d400_motion_base::_pid
uint16_t _pid
Definition: d400-motion.h:37
librealsense::ds_timestamp_reader
Definition: ds-timestamp.h:68
RS2_FORMAT_MOTION_XYZ32F
@ RS2_FORMAT_MOTION_XYZ32F
Definition: rs_sensor.h:79
librealsense::backend_device::get_backend
std::shared_ptr< platform::backend > get_backend()
Definition: backend-device-factory.cpp:118
librealsense::d400_motion_base::_mm_calib
std::shared_ptr< mm_calib_handler > _mm_calib
Definition: d400-motion.h:38
librealsense::d400_motion_uvc::d400_motion_uvc
d400_motion_uvc(std::shared_ptr< const d400_info > const &)
Definition: d400-motion.cpp:154
test-projection-from-recording.ctx
ctx
Definition: test-projection-from-recording.py:16
librealsense::global_time_option
Definition: global_timestamp_reader.h:13
metadata.h
librealsense::ds_timestamp_reader_from_metadata_mipi_motion
Definition: ds-timestamp.h:56
librealsense::d400_motion_base::_ds_motion_common
std::shared_ptr< ds_motion_common > _ds_motion_common
Definition: d400-motion.h:32
librealsense::global_timestamp_reader
Definition: global_timestamp_reader.h:91
librealsense::d400_motion::create_hid_device
std::shared_ptr< synthetic_sensor > create_hid_device(std::shared_ptr< context > ctx, const std::vector< platform::hid_device_info > &all_hid_infos, const firmware_version &camera_fw_version)
Definition: d400-motion.cpp:97
librealsense::d400_motion_uvc::create_uvc_device
std::shared_ptr< synthetic_sensor > create_uvc_device(std::shared_ptr< context > ctx, const std::vector< platform::uvc_device_info > &all_uvc_infos, const firmware_version &camera_fw_version)
Definition: d400-motion.cpp:41
librealsense::device
Definition: device.h:35
ds-timestamp.h
rs2_stream
rs2_stream
Streams are different types of data provided by RealSense devices.
Definition: rs_sensor.h:43
metadata-parser.h
librealsense::device::get_sensor
sensor_interface & get_sensor(size_t subdevice) override
Definition: device.cpp:101
librealsense::d400_motion::get_motion_sensor
ds_motion_sensor & get_motion_sensor()
Definition: d400-motion.cpp:143
librealsense::ds_motion_sensor
Definition: ds-motion-common.h:81
fourcc.h
RS2_OPTION_ENABLE_MOTION_CORRECTION
@ RS2_OPTION_ENABLE_MOTION_CORRECTION
Definition: rs_option.h:57
librealsense::d400_device
Definition: d400-device.h:26


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Fri Aug 2 2024 08:30:02