Segmentation fault using sensor_msgs/LaserScan Message
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);
}
}