cpp-motion-module.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
3 
5 // librealsense motion-module sample - accessing IMU data //
7 
8 // First include the librealsense C++ header file
9 #include <librealsense/rs.hpp>
10 #include <cstdio>
11 #include <mutex>
12 #include <vector>
13 #include <chrono>
14 #include <iostream>
15 #include <iomanip>
16 #include <string>
17 #include <thread>
18 
19 std::mutex mtx;
20 std::vector<std::string> logs;
21 
22 void log(const char* msg)
23 {
24  std::lock_guard<std::mutex> lock(mtx);
25  logs.push_back(msg);
26 }
27 
28 int main() try
29 {
31 
32  // Create a context object. This object owns the handles to all connected realsense devices.
34 
35  std::cout << "There are "<< ctx.get_device_count() << " connected RealSense devices.\n";
36 
37  if (ctx.get_device_count() == 0) return EXIT_FAILURE;
38 
39  // This tutorial will access only a single device, but it is trivial to extend to multiple devices
40  rs::device * dev = ctx.get_device(0);
41  printf("\nUsing device 0, an %s\n", dev->get_name());
42  printf(" Serial number: %s\n", dev->get_serial());
43  printf(" Firmware version: %s\n", dev->get_firmware_version());
44 
46  {
47  printf("This device does not support motion tracking!");
48  return EXIT_FAILURE;
49  }
50 
51  auto motion_callback = [](rs::motion_data entry)
52  {
53  auto now = std::chrono::system_clock::now().time_since_epoch();
54  auto sys_time = std::chrono::duration_cast<std::chrono::milliseconds>(now).count();
55  std::stringstream ss;
56  ss << "Motion,\t host time " << sys_time
57  << "\ttimestamp: " << std::setprecision(8) << entry.timestamp_data.timestamp
58  << "\tsource: " << (rs::event)entry.timestamp_data.source_id
59  << "\tframe_num: " << entry.timestamp_data.frame_number
60  << "\tx: " << std::setprecision(5) << entry.axes[0] << "\ty: " << entry.axes[1] << "\tz: " << entry.axes[2];
61  log(ss.str().c_str());
62  };
63 
64  // ... and the timestamp packets (DS4.1/FishEye Frame, GPIOS...)
65  auto timestamp_callback = [](rs::timestamp_data entry)
66  {
67  auto now = std::chrono::system_clock::now().time_since_epoch();
68  auto sys_time = std::chrono::duration_cast<std::chrono::milliseconds>(now).count();
69  std::stringstream ss;
70  ss << "TimeEvt, host time " << sys_time
71  << "\ttimestamp: " << std::setprecision(8) << entry.timestamp
72  << "\tsource: " << (rs::event)entry.source_id
73  << "\tframe_num: " << entry.frame_number;
74  log(ss.str().c_str());
75  };
76  for (int ii = 0; ii < 100; ii++)
77  {
78  std::cout << "\n\nIteration " << ii+1 << " started\n\n" << std::endl;
79 
80  // 1. Make motion-tracking available
82  {
84  }
85 
86  // 2. Optional - configure motion module
87  //dev->set_options(mm_cfg_list.data(), mm_cfg_list.size(), mm_cfg_params.data());
88 
89  dev->enable_stream(rs::stream::depth, 640, 480, rs::format::z16, 60);
94 
96 
97  auto frame_callback = [](rs::frame frame){
98  auto now = std::chrono::system_clock::now().time_since_epoch();
99  auto sys_time = std::chrono::duration_cast<std::chrono::milliseconds>(now).count();
100  std::stringstream ss;
101  ss << "Frame data, time " << sys_time
102  << "\ttimestamp: " << std::setprecision(8) << frame.get_timestamp()
103  << "\tsource: " << std::setw(13) << frame.get_stream_type()
104  << "\tframe_num: " << frame.get_frame_number();
105  log(ss.str().c_str());
106  };
107 
108  for (int i = (int)(rs::stream::depth); i <= (int)(rs::stream::fisheye); i++)
110 
111  logs.clear();
112  logs.reserve(10000);
113 
114  // 3. Start generating motion-tracking data
116 
117  printf("\nThe application is collecting data for next 10sec...\n");
118  std::this_thread::sleep_for(std::chrono::milliseconds(10000));
119 
120  // 4. stop data acquisition
122 
123  for (auto entry : logs) std::cout << entry << std::endl;
124 
125  // 5. reset previous settings formotion data handlers
127  }
128 
129  return EXIT_SUCCESS;
130 }
131 catch(const rs::error & e)
132 {
133  // Method calls against librealsense objects may throw exceptions of type rs::error
134  printf("rs::error was thrown when calling %s(%s):\n", e.get_failed_function().c_str(), e.get_failed_args().c_str());
135  printf(" %s\n", e.what());
136  return EXIT_FAILURE;
137 }
138 catch(...)
139 {
140  printf("Unhandled excepton occured'n");
141  return EXIT_FAILURE;
142 }
void log_to_console(log_severity min_severity)
Definition: rs.hpp:1104
Provides convenience methods relating to devices.
Definition: rs.hpp:567
void timestamp_callback(rs_device *, rs_timestamp_data, void *)
void enable_stream(stream stream, int width, int height, format format, int framerate, output_buffer_format output_buffer_type=output_buffer_format::continous)
Enables specific stream and requests specific properties.
Definition: rs.hpp:704
const std::string & get_failed_args() const
Definition: rs.hpp:315
std::vector< std::string > logs
rs_error * e
const char * get_firmware_version() const
Retrieves version of firmware currently installed on device.
Definition: rs.hpp:608
std::mutex mtx
void motion_callback(rs_device *, rs_motion_data, void *)
Motion data from gyroscope and accelerometer from the microcontroller.
Definition: rs.hpp:295
Exposes librealsense functionality for C++ compilers.
event
Source device that triggered specific timestamp event from the motion module.
Definition: rs.hpp:227
void stop(rs::source source=rs::source::video)
Ends streaming on all streams for this device.
Definition: rs.hpp:887
stream
Streams are different types of data provided by RealSense devices.
Definition: rs.hpp:24
void frame_callback(rs_device *dev, rs_frame_ref *frame, void *user)
GLuint GLuint GLsizei count
Definition: glext.h:111
Context.
Definition: rs.hpp:319
void disable_motion_tracking(void)
Disables events polling.
Definition: rs.hpp:861
const char * get_name() const
Retrieves human-readable device model string.
Definition: rs.hpp:578
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
void start(rs::source source=rs::source::video)
Begins streaming on all enabled streams for this device.
Definition: rs.hpp:879
void enable_motion_tracking(std::function< void(motion_data)> motion_handler, std::function< void(timestamp_data)> timestamp_handler)
Sets callback for motion module event.
Definition: rs.hpp:841
int main()
auto ctx
const char * get_serial() const
Retrieves unique serial number of device.
Definition: rs.hpp:588
Frame.
Definition: rs.hpp:392
rs_device * dev
void set_frame_callback(rs::stream stream, std::function< void(frame)> frame_handler)
Sets callback for frame arrival event.
Definition: rs.hpp:817
bool supports(capabilities capability) const
Determines device capabilities.
Definition: rs.hpp:1005
device * get_device(int index)
Definition: rs.hpp:354
void set_option(option option, double value)
Sets current value of single option.
Definition: rs.hpp:976
const std::string & get_failed_function() const
Definition: rs.hpp:314
Timestamp data from the motion microcontroller.
Definition: rs.hpp:288
int get_device_count() const
Definition: rs.hpp:343
void log(const char *msg)


librealsense
Author(s): Sergey Dorodnicov , Mark Horn , Reagan Lopez
autogenerated on Fri Mar 13 2020 03:16:17