【发布时间】:2018-03-01 22:05:32
【问题描述】:
我正在尝试读取 geometry_msgs::PoseArray 并将其转换为用于 ROS 的 c++ 中的 geometry_msgs::Pose。它应该只在主题/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: []
---
每当我看到 apriltag 时,都没有分段错误。那么下面的代码来自/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
---
【问题讨论】:
-
在哪一行出现段错误?使用调试器运行。
-
@DBedrenko 跟评论
//Here I get the segmentation fault就行了 -
你怎么知道
poses的第一个元素确实存在?
标签: c++ pointers segmentation-fault simulink ros