分段错误(核心转储)ROS C++ 箭头
segmentation fault (core dumped) ROS c++ arrow
我正在尝试读取一个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>
也许seg fault
发生,因为您正在订阅者构造函数中声明非指针类型(我还没有测试过这个(。
你不必启动一个geometry_msgs::PoseArray::ConstPtr&
你可以做geometry_msgs::PoseArray
并摆脱指针
这是我解决这个问题的方法。在data = msg->detections[0].pose.pose;
之前添加if(msg->detections.size()>0)
- C++(和 ROS) - 包含与前向声明引用,设置默认值和类型定义
- 如何通过ROS将realsense数据传输到其他设备
- 如何使用realsense摄像头调试ROS错误消息
- 如何使用 ROS 从 yaml 文件中读取带有元组的数组?
- ROS/C++:无法在 eclipse 中修复未解析的包含
- ROS:谷歌制图师/地图
- 如何在ROS中使用PCL可视化动力学数据的表面法线
- ROS 订阅回调 - 使用 boost::绑定成员函数
- ROS CPP 相当于 Python subprocess.call() 和 shell=True
- 使用 Python、ROS 和 C++ 进行日志记录
- ROS:在nodehandle.subscribe中使用lambda作为回调
- 在单个 Docker 容器中开发 ROS 节点?
- 在ROS中,C++和Python都是必需的吗?
- 如何消除ROS打开CV中的内存不足错误?
- 您将如何在现有的QT Creator GUI项目中实现ROS发布者和订阅者?
- ROS中未定义的参考误差到OpenCV图像转换器
- 创建并编译基类以在其他 C++ 项目 (ROS) 中使用
- 将 JPG 编码数组从 ROS sensor_msgs/压缩图像保存到 roscpp 中的文件
- ROS C++项目"include"文件夹?
- ROS创建移动的类