• OrbbecSDK_ros关于imu数据的发布


    orbbec官方提供ros版本的sdk还不支持imu数据获取,貌似下个版本更新后就可以得到imu数据的话题。参考c++版本的sdk对ros的sdk进行修改,最后将imu数据以topic的形式发布。

    增加的代码都在setupDevices函数中

    1. void OBCameraNode::setupDevices() {
    2. auto sensor_list = device_->getSensorList();
    3. //std::cout<<"**********sensor list has "<count()<
    4. for (size_t i = 0; i < sensor_list->count(); i++) {
    5. auto sensor = sensor_list->getSensor(i);
    6. //std::cout<<"**********sensor list "<< i << " " <type(i) <
    7. auto profiles = sensor->getStreamProfileList();
    8. for (size_t j = 0; j < profiles->count(); j++) {
    9. auto profile = profiles->getProfile(j);
    10. stream_index_pair sip{profile->type(), 0};
    11. if (sensors_.find(sip) != sensors_.end()) {
    12. continue;
    13. }
    14. sensors_[sip] = std::make_shared(device_, sensor, stream_name_[sip]);
    15. }
    16. }
    17. for (const auto& item : enable_stream_) {
    18. auto stream_index = item.first;
    19. auto enable = item.second;
    20. if (enable && sensors_.find(stream_index) == sensors_.end()) {
    21. ROS_INFO_STREAM(stream_name_[stream_index]
    22. << "sensor isn't supported by current device! -- Skipping...");
    23. enable_stream_[stream_index] = false;
    24. }
    25. }
    26. if (enable_d2c_viewer_) {
    27. d2c_viewer_ = std::make_shared(nh_, nh_private_);
    28. }
    29. CHECK_NOTNULL(device_info_.get());
    30. if (enable_pipeline_) {
    31. pipeline_ = std::make_shared(device_);
    32. }
    33. try {
    34. if (enable_hardware_d2d_ && device_info_->pid() == GEMINI2_PID) {
    35. device_->setBoolProperty(OB_PROP_DISPARITY_TO_DEPTH_BOOL, true);
    36. }
    37. if (!depth_work_mode_.empty()) {
    38. device_->switchDepthWorkMode(depth_work_mode_.c_str());
    39. }
    40. if (sync_mode_ != OB_SYNC_MODE_CLOSE) {
    41. OBDeviceSyncConfig sync_config;
    42. sync_config.syncMode = sync_mode_;
    43. sync_config.irTriggerSignalInDelay = ir_trigger_signal_in_delay_;
    44. sync_config.rgbTriggerSignalInDelay = rgb_trigger_signal_in_delay_;
    45. sync_config.deviceTriggerSignalOutDelay = device_trigger_signal_out_delay_;
    46. device_->setSyncConfig(sync_config);
    47. if (device_->isPropertySupported(OB_PROP_SYNC_SIGNAL_TRIGGER_OUT_BOOL,
    48. OB_PERMISSION_READ_WRITE)) {
    49. device_->setBoolProperty(OB_PROP_SYNC_SIGNAL_TRIGGER_OUT_BOOL, sync_signal_trigger_out_);
    50. }
    51. }
    52. if (device_info_->pid() == GEMINI2_PID) {
    53. auto default_precision_level = device_->getIntProperty(OB_PROP_DEPTH_PRECISION_LEVEL_INT);
    54. if (default_precision_level != depth_precision_) {
    55. device_->setIntProperty(OB_PROP_DEPTH_PRECISION_LEVEL_INT, depth_precision_);
    56. }
    57. }
    58. device_->setBoolProperty(OB_PROP_DEPTH_SOFT_FILTER_BOOL, enable_soft_filter_);
    59. device_->setBoolProperty(OB_PROP_COLOR_AUTO_EXPOSURE_BOOL, enable_color_auto_exposure_);
    60. device_->setBoolProperty(OB_PROP_IR_AUTO_EXPOSURE_BOOL, enable_ir_auto_exposure_);
    61. auto default_soft_filter_max_diff = device_->getIntProperty(OB_PROP_DEPTH_MAX_DIFF_INT);
    62. if (soft_filter_max_diff_ != -1 && default_soft_filter_max_diff != soft_filter_max_diff_) {
    63. device_->setIntProperty(OB_PROP_DEPTH_MAX_DIFF_INT, soft_filter_max_diff_);
    64. }
    65. auto default_soft_filter_speckle_size =
    66. device_->getIntProperty(OB_PROP_DEPTH_MAX_SPECKLE_SIZE_INT);
    67. if (soft_filter_speckle_size_ != -1 &&
    68. default_soft_filter_speckle_size != soft_filter_speckle_size_) {
    69. device_->setIntProperty(OB_PROP_DEPTH_MAX_SPECKLE_SIZE_INT, soft_filter_speckle_size_);
    70. }
    71. //***************add imu*********************
    72. enable_stream_[ACCEL] = true;
    73. enable_stream_[GYRO] = true;
    74. for (const auto& stream_index : HID_STREAMS) {
    75. if (stream_index == GYRO)
    76. {
    77. auto profile_list = sensors_[stream_index]->getStreamProfileList();
    78. supported_profiles_[stream_index] = profile_list;
    79. auto selected_profile = profile_list->getGyroStreamProfile(OB_GYRO_FS_125dps, OB_SAMPLE_RATE_200_HZ);
    80. stream_profile_[stream_index] = selected_profile;
    81. }
    82. else if (stream_index == ACCEL)
    83. {
    84. auto profile_list = sensors_[stream_index]->getStreamProfileList();
    85. supported_profiles_[stream_index] = profile_list;
    86. auto selected_profile = profile_list->getAccelStreamProfile(OB_ACCEL_FS_4g, OB_SAMPLE_RATE_200_HZ);
    87. stream_profile_[stream_index] = selected_profile;
    88. }
    89. }
    90. // for (const auto& stream_index : HID_STREAMS) {
    91. // if (enable_stream_[stream_index]) {
    92. // pipeline_config_->enableStream(stream_profile_[stream_index]);
    93. // }
    94. // }
    95. imu_publisher_ = nh_.advertise("imu_data", 10);
    96. std::shared_ptr gyroSensor = nullptr;
    97. std::shared_ptr accelSensor = nullptr;
    98. gyroSensor = device_->getSensorList()->getSensor(OB_SENSOR_GYRO);
    99. if(gyroSensor){
    100. auto profiles = gyroSensor->getStreamProfileList();
    101. // auto profile = profiles->getProfile(0);
    102. gyroSensor->start(stream_profile_[GYRO], [this](std::shared_ptr frame) {
    103. std::unique_lock lk(printerMutex);
    104. //auto timeStamp = frame->timeStamp();
    105. gyr_timestamp = frame->timeStamp();
    106. auto gyroFrame = frame->as();
    107. if(gyroFrame != nullptr /*&& (timeStamp % 500) < 2*/) { //( timeStamp % 500 ) < 2: Reduce printing frequency
    108. auto value = gyroFrame->value();
    109. // std::cout << "Gyro Frame: \n\r{\n\r"
    110. // << " tsp = " << timeStamp << "\n\r"
    111. // << " temperature = " << gyroFrame->temperature() << "\n\r"
    112. // << " gyro.x = " << value.x << " rad/s"
    113. // << "\n\r"
    114. // << " gyro.y = " << value.y << " rad/s"
    115. // << "\n\r"
    116. // << " gyro.z = " << value.z << " rad/s"
    117. // << "\n\r"
    118. //sensor_msgs::Imu imu_msg = sensor_msgs::Imu();
    119. // std::cout<<"*******************************"<
    120. // std::cout<<"gyr_timestamp:"<< gyr_timestamp <
    121. imu_msg.header.stamp = ros::Time(gyr_timestamp);
    122. imu_msg.angular_velocity.x = value.x;
    123. imu_msg.angular_velocity.y = value.y;
    124. imu_msg.angular_velocity.z = value.z;
    125. // imu_publisher_.publish(imu_msg);
    126. }
    127. });
    128. }
    129. accelSensor = device_->getSensorList()->getSensor(OB_SENSOR_ACCEL);
    130. if(accelSensor) {
    131. auto profiles = accelSensor->getStreamProfileList();
    132. // auto profile = profiles->getProfile(0);
    133. accelSensor->start(stream_profile_[ACCEL], [this](std::shared_ptr frame) {
    134. std::unique_lock lk(printerMutex);
    135. //auto timeStamp = frame->timeStamp();
    136. acc_timestamp = frame->timeStamp();
    137. auto accelFrame = frame->as();
    138. if(accelFrame != nullptr) {
    139. auto value = accelFrame->value();
    140. // std::cout << "Accel Frame: \n\r{\n\r"
    141. // << " tsp = " << timeStamp << "\n\r"
    142. // << " temperature = " << accelFrame->temperature() << "\n\r"
    143. // << " accel.x = " << value.x << " m/s^2"
    144. // << "\n\r"
    145. // << " accel.y = " << value.y << " m/s^2"
    146. // << "\n\r"
    147. // << " accel.z = " << value.z << " m/s^2"
    148. // << "\n\r"
    149. // << "}\n\r" << std::endl;
    150. // std::cout<<"acc_timestamp:"<< acc_timestamp <
    151. // std::cout<<"*******************************"<
    152. imu_msg.linear_acceleration.x = value.x;
    153. imu_msg.linear_acceleration.y = value.y;
    154. imu_msg.linear_acceleration.z = value.z;
    155. imu_publisher_.publish(imu_msg);
    156. }
    157. });
    158. }
    159. //*******************************************
    160. } catch (const ob::Error& e) {
    161. ROS_ERROR_STREAM("Failed to setup devices: " << e.getMessage());
    162. } catch (const std::exception& e) {
    163. ROS_ERROR_STREAM("Failed to setup devices: " << e.what());
    164. }
    165. }

    参考的c++_sdk关于imu数据获取代码

    1. #include
    2. #include
    3. #include
    4. #include "utils.hpp"
    5. #define ESC 27
    6. std::mutex printerMutex;
    7. int main(int argc, char **argv) try {
    8. //打印SDK的版本号,SDK版本号分为主版本号,副版本号和修订版本号
    9. // Print the SDK version number, the SDK version number is divided into major version number, minor version number and revision number
    10. std::cout << "SDK version: " << ob::Version::getMajor() << "." << ob::Version::getMinor() << "." << ob::Version::getPatch() << std::endl;
    11. // Create a Context.
    12. //首先需要创建一个Context,用于获取设备信息列表和创建设备
    13. ob::Context ctx;
    14. // Query the list of connected devices
    15. //查询已经接入设备的列表
    16. auto devList = ctx.queryDeviceList();
    17. if(devList->deviceCount() == 0) {
    18. std::cerr << "Device not found!" << std::endl;
    19. return -1;
    20. }
    21. // Create a device, 0 represents the index of the first device
    22. auto dev = devList->getDevice(0);
    23. std::shared_ptr gyroSensor = nullptr;
    24. std::shared_ptr accelSensor = nullptr;
    25. try {
    26. //获取陀螺仪传感器
    27. gyroSensor = dev->getSensorList()->getSensor(OB_SENSOR_GYRO);
    28. if(gyroSensor) {
    29. //获取陀螺仪传感器的配置列表并选择第一个配置开流,在开流的回调里获取帧的数据
    30. // Get configuration list
    31. auto profiles = gyroSensor->getStreamProfileList();
    32. // Select the first profile to open stream
    33. auto profile = profiles->getProfile(0);
    34. gyroSensor->start(profile, [](std::shared_ptr frame) {
    35. std::unique_lock lk(printerMutex);
    36. auto timeStamp = frame->timeStamp();
    37. auto gyroFrame = frame->as();
    38. if(gyroFrame != nullptr ) { //( timeStamp % 500 ) < 2: Reduce printing frequency
    39. auto value = gyroFrame->value();
    40. std::cout << "Gyro Frame: \n\r{\n\r"
    41. << " timestamp = " << timeStamp << "\n\r"
    42. << " temperature = " << gyroFrame->temperature() << "\n\r"
    43. << " gyro.x = " << value.x << " rad/s"
    44. << "\n\r"
    45. << " gyro.y = " << value.y << " rad/s"
    46. << "\n\r"
    47. << " gyro.z = " << value.z << " rad/s"
    48. << "\n\r"
    49. << "}\n\r" << std::endl;
    50. }
    51. });
    52. }
    53. else {
    54. std::cout << "get gyro Sensor failed ! " << std::endl;
    55. }
    56. }
    57. catch(ob::Error &e) {
    58. std::cerr << "current device is not support imu!" << std::endl;
    59. exit(EXIT_FAILURE);
    60. }
    61. // Get Acceleration Sensor
    62. accelSensor = dev->getSensorList()->getSensor(OB_SENSOR_ACCEL);
    63. if(accelSensor) {
    64. // Get configuration list
    65. auto profiles = accelSensor->getStreamProfileList();
    66. // Select the first profile to open stream
    67. auto profile = profiles->getProfile(0);
    68. accelSensor->start(profile, [](std::shared_ptr frame) {
    69. std::unique_lock lk(printerMutex);
    70. auto timeStamp = frame->timeStamp();
    71. auto accelFrame = frame->as();
    72. if(accelFrame != nullptr ) {
    73. auto value = accelFrame->value();
    74. std::cout << "Accel Frame: \n\r{\n\r"
    75. << " timestamp = " << timeStamp << "\n\r"
    76. << " temperature = " << accelFrame->temperature() << "\n\r"
    77. << " accel.x = " << value.x << " m/s^2"
    78. << "\n\r"
    79. << " accel.y = " << value.y << " m/s^2"
    80. << "\n\r"
    81. << " accel.z = " << value.z << " m/s^2"
    82. << "\n\r"
    83. << "}\n\r" << std::endl;
    84. }
    85. });
    86. }
    87. else {
    88. std::cout << "get Accel Sensor failed ! " << std::endl;
    89. }
    90. std::cout << "Press ESC to exit! " << std::endl;
    91. while(true) {
    92. // Get the value of the pressed key, if it is the ESC key, exit the program
    93. int key = getch();
    94. if(key == ESC)
    95. break;
    96. }
    97. // turn off the flow
    98. if(gyroSensor) {
    99. gyroSensor->stop();
    100. }
    101. if(accelSensor) {
    102. accelSensor->stop();
    103. }
    104. return 0;
    105. }
    106. catch(ob::Error &e) {
    107. std::cerr << "function:" << e.getName() << "\nargs:" << e.getArgs() << "\nmessage:" << e.getMessage() << "\ntype:" << e.getExceptionType() << std::endl;
    108. exit(EXIT_FAILURE);
    109. }

  • 相关阅读:
    Grafana 开源了一款 eBPF 采集器 Beyla
    从Spring源码探究IOC初始化流程
    2.3.1 协程设计原理与汇编实现
    解决https页面加载http资源报错
    卡尔曼滤波:The Scaler Kalman Filter常量卡尔曼滤波器
    拥有游戏的一部分,写在我的世界禁用NFT之后
    【LeetCode热题100】--49.字母异位词分组
    Python数据分析与机器学习40-自然语言处理词向量模型-Word2Vec
    solidworks管道设计教程
    Can‘t locate Bio/SeqIO.pm in @INC
  • 原文地址:https://blog.csdn.net/qq_41451702/article/details/132888528