Reputation: 11
I'm trying to read a geometry_msgs::PoseArray and convert it to a geometry_msgs::Pose in c++ for ROS. It should only convert the data when there is data on topic /tag_detections_pose. When I run this node( see code), I get a segmentation fault (core dumped) at the marked location when there is an empty array on /tag_detections_pose. I expect this to be an issue with pointers, but cannot find where exactly. I think I have to initialize my pointer, but I don't see where exactly. (b.t.w. The code is to convert apriltag_ros messages to be readable by 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;
}
EDIT: some extra information. Whenever I read the next data from /tag_detections_pose, the segmentation fault occurs.
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: []
---
Whenever I see an apriltag, there is no segmentation fault. Then the following code comes from /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
---
Upvotes: 1
Views: 4602
Reputation: 1
Here's how I solved this problem.
Add if(msg->detections.size()>0)
before data = msg->detections[0].pose.pose;
Upvotes: 0
Reputation: 574
maybe seg fault
happening cause You are declaring a Non-Pointer type in subscriber Constructor(i haven't tested This).
U Don't Have to Initilize a geometry_msgs::PoseArray::ConstPtr&
u can just do geometry_msgs::PoseArray
and be rid of pointer
Upvotes: 0
Reputation: 3476
Instead of checking if (input)
, you should check for what you actually care about: whether the poses field is non-empty.
In ROS messages, the arrays are stored as C++ vector
s. To check if the poses
vector has any elements, you can replace your condition by if (!input->poses.empty())
Upvotes: 1