cpp-motion-module.cpp
Go to the documentation of this file.
00001 // License: Apache 2.0. See LICENSE file in root directory.
00002 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
00003 
00005 // librealsense motion-module sample - accessing IMU data //
00007 
00008 // First include the librealsense C++ header file
00009 #include <librealsense/rs.hpp>
00010 #include <cstdio>
00011 #include <mutex>
00012 #include <vector>
00013 #include <chrono>
00014 #include <iostream>
00015 #include <iomanip>
00016 #include <string>
00017 #include <thread>
00018 
00019 std::mutex mtx;
00020 std::vector<std::string> logs;
00021 
00022 void log(const char* msg)
00023 {
00024     std::lock_guard<std::mutex> lock(mtx);
00025     logs.push_back(msg);
00026 }
00027 
00028 int main() try
00029 {
00030     rs::log_to_console(rs::log_severity::error);
00031 
00032     // Create a context object. This object owns the handles to all connected realsense devices.
00033     rs::context ctx;
00034 
00035     std::cout << "There are "<< ctx.get_device_count() << " connected RealSense devices.\n";
00036 
00037     if (ctx.get_device_count() == 0) return EXIT_FAILURE;
00038 
00039     // This tutorial will access only a single device, but it is trivial to extend to multiple devices
00040     rs::device * dev = ctx.get_device(0);
00041     printf("\nUsing device 0, an %s\n", dev->get_name());
00042     printf("    Serial number: %s\n", dev->get_serial());
00043     printf("    Firmware version: %s\n", dev->get_firmware_version());
00044 
00045     if (!dev->supports(rs::capabilities::motion_events))
00046     {
00047         printf("This device does not support motion tracking!");
00048         return EXIT_FAILURE;
00049     }
00050 
00051     auto motion_callback = [](rs::motion_data entry)
00052     {
00053         auto now = std::chrono::system_clock::now().time_since_epoch();
00054         auto sys_time = std::chrono::duration_cast<std::chrono::milliseconds>(now).count();
00055         std::stringstream ss;
00056         ss << "Motion,\t host time " << sys_time
00057             << "\ttimestamp: " << std::setprecision(8) << entry.timestamp_data.timestamp
00058             << "\tsource: " << (rs::event)entry.timestamp_data.source_id
00059             << "\tframe_num: " << entry.timestamp_data.frame_number
00060             << "\tx: " << std::setprecision(5) <<  entry.axes[0] << "\ty: " << entry.axes[1] << "\tz: " << entry.axes[2];
00061         log(ss.str().c_str());
00062     };
00063 
00064     // ... and the timestamp packets (DS4.1/FishEye Frame, GPIOS...)
00065     auto timestamp_callback = [](rs::timestamp_data entry)
00066     {
00067         auto now = std::chrono::system_clock::now().time_since_epoch();
00068         auto sys_time = std::chrono::duration_cast<std::chrono::milliseconds>(now).count();
00069         std::stringstream ss;
00070         ss << "TimeEvt, host time "  << sys_time
00071             << "\ttimestamp: " << std::setprecision(8) << entry.timestamp
00072             << "\tsource: " << (rs::event)entry.source_id
00073             << "\tframe_num: " << entry.frame_number;
00074         log(ss.str().c_str());
00075     };
00076     for (int ii = 0; ii < 100; ii++)
00077     {
00078         std::cout << "\n\nIteration " << ii+1 << " started\n\n" << std::endl;
00079 
00080         // 1. Make motion-tracking available
00081         if (dev->supports(rs::capabilities::motion_events))
00082         {
00083             dev->enable_motion_tracking(motion_callback, timestamp_callback);
00084         }
00085 
00086         // 2. Optional - configure motion module
00087         //dev->set_options(mm_cfg_list.data(), mm_cfg_list.size(), mm_cfg_params.data());
00088 
00089         dev->enable_stream(rs::stream::depth, 640, 480, rs::format::z16, 60);
00090         dev->enable_stream(rs::stream::color, 640, 480, rs::format::rgb8, 60);
00091         dev->enable_stream(rs::stream::infrared, 640, 480, rs::format::y8, 60);
00092         dev->enable_stream(rs::stream::infrared2, 640, 480, rs::format::y8, 60);
00093         dev->enable_stream(rs::stream::fisheye, 640, 480, rs::format::raw8, 60);
00094 
00095         dev->set_option(rs::option::fisheye_strobe, 1);
00096 
00097         auto frame_callback = [](rs::frame frame){
00098             auto now = std::chrono::system_clock::now().time_since_epoch();
00099             auto sys_time = std::chrono::duration_cast<std::chrono::milliseconds>(now).count();
00100             std::stringstream ss;
00101             ss << "Frame data,   time " << sys_time
00102                 << "\ttimestamp: " << std::setprecision(8) << frame.get_timestamp()
00103                 << "\tsource: " << std::setw(13) << frame.get_stream_type()
00104                 << "\tframe_num: " << frame.get_frame_number();
00105             log(ss.str().c_str());
00106         };
00107 
00108         for (int i = (int)(rs::stream::depth); i <= (int)(rs::stream::fisheye); i++)
00109             dev->set_frame_callback((rs::stream)i, frame_callback);
00110 
00111         logs.clear();
00112         logs.reserve(10000);
00113 
00114         // 3. Start generating motion-tracking data
00115         dev->start(rs::source::all_sources);
00116 
00117         printf("\nThe application is collecting data for next 10sec...\n");
00118         std::this_thread::sleep_for(std::chrono::milliseconds(10000));
00119 
00120         // 4. stop data acquisition
00121         dev->stop(rs::source::all_sources);
00122 
00123         for (auto entry : logs)   std::cout << entry << std::endl;
00124 
00125         // 5. reset previous settings formotion data handlers
00126         dev->disable_motion_tracking();
00127     }
00128 
00129     return EXIT_SUCCESS;
00130 }
00131 catch(const rs::error & e)
00132 {
00133     // Method calls against librealsense objects may throw exceptions of type rs::error
00134     printf("rs::error was thrown when calling %s(%s):\n", e.get_failed_function().c_str(), e.get_failed_args().c_str());
00135     printf("    %s\n", e.what());
00136     return EXIT_FAILURE;
00137 }
00138 catch(...)
00139 {
00140     printf("Unhandled excepton occured'n");
00141     return EXIT_FAILURE;
00142 }


librealsense
Author(s): Sergey Dorodnicov , Mark Horn , Reagan Lopez
autogenerated on Tue Jun 25 2019 19:54:38