分段错误(核心转储)ROS C++ 箭头

segmentation fault (core dumped) ROS c++ arrow

本文关键字:ROS C++ 箭头 转储 错误 核心 分段      更新时间:2023-10-16

我正在尝试读取一个geometry_msgs::P oseArray并将其转换为用于ROS的c ++中的geometry_msgs::P ose。它应该只在主题/tag_detections_pose 上有数据时转换数据。当我运行这个节点(见代码(时,当/tag_detections_pose 上有一个空数组时,我在标记的位置出现分段错误(核心转储(。我希望这是指针的问题,但找不到确切的位置。我想我必须初始化我的指针,但我看不到确切的位置。(B.T.W.代码是将apriltag_ros消息转换为可由 simulink 读取(

#include "ros/ros.h"
#include <geometry_msgs/Pose.h>
#include <geometry_msgs/PoseArray.h>
#include <std_msgs/Bool.h>
#include <array>

class SubscribeAndPublish
{
public:
  SubscribeAndPublish()
  {
    //Topic you want to publish
    pub_ = n_.advertise<geometry_msgs::Pose>("/tag_pose", 1);
    //Topic you want to subscribe
    sub_ = n_.subscribe<geometry_msgs::PoseArray>("/tag_detections_pose", 1, &SubscribeAndPublish::callback, this);
  }
  void callback(const geometry_msgs::PoseArray::ConstPtr& input)
  {
    if (input) {
      geometry_msgs::Pose output;
      output.position.x = input->poses[0].position.x; //Here I get the segmentation fault
      output.position.y = input->poses[0].position.y;
      output.position.z = input->poses[0].position.z;
      output.orientation.x = input->poses[0].orientation.x;
      output.orientation.y = input->poses[0].orientation.y;
      output.orientation.z = input->poses[0].orientation.z;
      output.orientation.w = input->poses[0].orientation.w;
      pub_.publish(output);
    }
  }
private:
  ros::NodeHandle n_;
  ros::Publisher pub_;
  ros::Subscriber sub_;
};//End of class SubscribeAndPublish
int main(int argc, char **argv)
{
  //Initiate ROS
  ros::init(argc, argv, "apriltag_to_simu_V2");
  SubscribeAndPublish SAPObject;
  ros::spin();
  return 0;
}

编辑:一些额外的信息。每当我从/tag_detections_pose 读取下一个数据时,都会发生分段错误。

header: 
  seq: 14192
  stamp: 
    secs: 1519941974
    nsecs: 650975624
  frame_id: usb_cam
poses: []
---
header: 
  seq: 14193
  stamp: 
    secs: 1519941974
    nsecs: 995796728
  frame_id: usb_cam
poses: []
---

每当我看到一个四月标签时,都没有分段错误。然后以下代码来自/tag_detections_pose:

---
header: 
  seq: 80
  stamp: 
    secs: 1519981664
    nsecs: 100302767
  frame_id: usb_cam
poses: 
  - 
    position: 
      x: 0.110443956833
      y: -0.080843233518
      z: 0.753152633488
    orientation: 
      x: 0.706039345202
      y: 0.705726892216
      z: -0.0580458686961
      w: 0.00941667438916
---
header: 
  seq: 81
  stamp: 
    secs: 1519981664
    nsecs: 368606478
  frame_id: usb_cam
poses: 
  - 
    position: 
      x: 0.110435802307
      y: -0.0808251086937
      z: 0.753089835655
    orientation: 
      x: 0.706068882885
      y: 0.705719138117
      z: -0.0577643573596
      w: 0.0095136604333
---

与其检查if (input),不如检查你真正关心的是什么:姿势字段是否为非空。

在 ROS 消息中,数组存储为 C++ vector s。要检查poses向量是否有任何元素,您可以通过if (!input->poses.empty()) <</p>

div class="answers"替换您的条件>

也许seg fault发生,因为您正在订阅者构造函数中声明非指针类型(我还没有测试过这个(。

你不必启动一个geometry_msgs::PoseArray::ConstPtr&你可以做geometry_msgs::PoseArray并摆脱指针

这是我解决这个问题的方法。在data = msg->detections[0].pose.pose;之前添加if(msg->detections.size()>0)