ros service crash with no message [closed]

asked 2017-01-17 04:11:19 -0500

yueweiliang gravatar image

updated 2017-01-17 04:17:42 -0500

gvdhoorn gravatar image

i wanna use a serve to subscribe the "/scan" topic and in the other node i creat a client to request the lasersan data from the serve.But it sometimes crashes when running and no meessage showed in the terminal,in fact the two nodes are just exit directly.i donot know why ,here is the code ,thanks for looking and helping me slove it:

-----------------server

#include <vector>
#include "ros/ros.h"
 #include <boost/thread/mutex.hpp>
#include "vfh_to_goal/get_Laser.h"
#include "sensor_msgs/LaserScan.h" 
  boost::mutex scan_mutex_;
sensor_msgs::LaserScan scan;
bool flag = false;
void scanCallback(const sensor_msgs::LaserScan::ConstPtr &scan1)
{
    scan = *scan1;
    flag = true;
    ROS_INFO("get laser");
}
bool getLaser(vfh_to_goal::get_Laser::Request &req,
                           vfh_to_goal::get_Laser::Response &res  )
{
      if(req.get && flag)
      {
         scan_mutex_.lock();
            res.scan = scan;
            ROS_INFO("copy laser to srv");
            flag = false;
        scan_mutex_.unlock();
      }
        return true;
}

-------client

bool getlaser()
{
    int num_laser=0;
       ros::Rate loop_rate(20);
    while(num_laser==0)
    {
        cout<<"0"<<num_laser<<endl;
        vfh_to_goal::get_Laser srv1;
        srv1.request.get=true;
        if(client_srv.call(srv1))
        {
            num_laser=srv1.response.scan.ranges.size() ;
             cout<<"1::"<<num_laser<<endl;
            if(num_laser==0)
                continue;
            else
            {
                 cout<<"2"<<endl;
                laserdata.clear();
                vector<float>::reverse_iterator it=srv1.response.scan.ranges.rbegin();
                while(it!=srv1.response.scan.ranges.rend())
                {
                    laserdata.push_back(*it);
                    ++it;
                }
                explore_value=(double)srv1.response.scan.range_max-0.1;
                break;
            }
        }
        else
        {
                cout<<"laser_serve can not connect!"<<endl;
        }
        loop_rate.sleep();
    }

    return true;
}

int main(int argc, char** argv)
{
    ros::init(argc, argv, "get_laser_serv");
    ros::NodeHandle n;
    ros::ServiceServer service = n.advertiseService("get_laser_gy", getLaser);
    ros::Subscriber sub = n.subscribe<sensor_msgs::LaserScan>("/scan", 50, &scanCallback);
    ros::spin();
}
edit retag flag offensive reopen merge delete

Closed for the following reason Question does not follow our guidelines for questions. Please see: http://wiki.ros.org/Support for more details. by tfoote
close date 2017-02-15 20:29:59.615037

Comments

Please use the Preformatted Text button (the one with 101010 on it) to properly format code and console copy/pastes next time. Without it, code is unreadable.

gvdhoorn gravatar image gvdhoorn  ( 2017-01-17 04:18:21 -0500 )edit

Please provide at least the console output for your execution and consider putting in more debugging prints to find out where it's going through your code. Or else use gdb to step through execution. Without more information we won't be able to help you.

tfoote gravatar image tfoote  ( 2017-02-15 20:29:51 -0500 )edit