ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Segmentation fault using sensor_msgs/LaserScan Message

asked 2014-05-05 03:29:59 -0500

Hlift gravatar image

I am trying to use 2 hokuyo lasers which publish data on 2 topics. I managed to subscribe to both topics and send them all to 2 new topics. However, in my main part of the code, I get a segmentation fault when I try to print the ranges of the LaserScan Message, while I am able to print the other data. In my callback funtion, it was possible to print the data of the ranges of the LaserScan Message!

Why is it not possible for me to get acces to the ranges of the LaserScan Message in the main part of my code and do I get a segmentation fault?

Thanks for your help!

    #include <ros/ros.h>
    #include <iostream>
    #include <std_msgs/String.h>
    #include <math.h>       
    #include <sstream>
    #include <sensor_msgs/LaserScan.h>
    #include <std_msgs/Float32.h>

sensor_msgs::LaserScan l_laser;
sensor_msgs::LaserScan r_laser;

void Callback_left_laser(const sensor_msgs::LaserScan::ConstPtr& scan_left)
{
    l_laser=*scan_left; 
}

void Callback_right_laser(const sensor_msgs::LaserScan::ConstPtr& scan_right)
{
    r_laser=*scan_right;
    ROS_INFO("point_of_ranges=[%f] \n", r_laser.ranges[0]); // this one works
}

int main(int argc, char** argv)
{
    ros::init(argc, argv, "object_detection"); 

    ros::NodeHandle n;

    ros::Subscriber left_laser_subscriber;
    ros::Publisher left_laser_publisher;

    left_laser_subscriber = n.subscribe<sensor_msgs::LaserScan>("/hlift/left_scan", 100, Callback_left_laser);
    left_laser_publisher = n.advertise<sensor_msgs::LaserScan>("/hlift/left_scan_publish", 100);

    ros::Subscriber right_laser_subscriber;
    ros::Publisher right_laser_publisher;

    right_laser_subscriber = n.subscribe<sensor_msgs::LaserScan>("/hlift/right_scan", 100, Callback_right_laser);
    right_laser_publisher = n.advertise<sensor_msgs::LaserScan>("/hlift/right_scan_publish", 100);

    while (ros::ok())
    {    
        ros::Rate loop_rate(10); 

        ros::spinOnce(); 

        loop_rate.sleep();

        ROS_INFO("min_angle=[%f] \n", r_laser.angle_min); //this one works also
        ROS_INFO("point_of_ranges=[%f] \n", r_laser.ranges[0]); //this one gives a segmentation fault!

        left_laser_publisher.publish(l_laser);//all the data which are called are published on a new topic
        right_laser_publisher.publish(r_laser);
    }
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2014-05-05 06:33:25 -0500

Angus gravatar image

updated 2014-05-05 06:35:02 -0500

Your main "while" loop will start running immediately after launching the node, but the values for "r_laser" will not be set until the callback is made, so when you try to access the first element, you get a segfault. Maybe try changing the code in your main loop to

if (!r_laser.ranges.empty())
  ROS_INFO("point_of_ranges=[%f] \n", r_laser.ranges[0])
edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-05-05 03:29:59 -0500

Seen: 1,022 times

Last updated: May 05 '14