Problem to subscrib with class

asked 2017-04-04 04:56:16 -0500

shenki gravatar image

updated 2017-04-04 05:02:12 -0500

Procópio gravatar image


I am currently in an internship and Im working on ROS with GAZEBO simulator. I made my own package but now im facing a problem. I have 10 pioneer3AT with a hokuyo laser on each, the plugin for the laser is working well, and I have a topic called "/hokuyo_laser" and I am trying to make my own plugin, using a C++ class, here is my code :

class ListenerPlugin : public ModelPlugin

        ros::NodeHandle n;
        ros::Subscriber sub

    ListenerPlugin() {}

    void scanCallback(const sensor_msgs::LaserScan::ConstPtr& scan){

         ROS_INFO("VALEUR SCAN: [%s]", scan->ranges);

    virtual void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf){

        std::cerr << "Le plugin listener a ete attache a[" << _model->GetName() << "]\n";

        sub = n.subscribe("hokuyo_laser", 1, &ListenerPlugin::scanCallback, this);
        std::cerr << sub;



And my problem is that my programm is never going in scanCallback, when I use std::cerr to display my Subscriber, there is only "0x1" that is showing.

Here is the result of the command "rostopic list -v" while simulator is running :

Published topics:

 * /hokuyo_laser [sensor_msgs/LaserScan] 1 publisher
 * /gazebo/link_states [gazebo_msgs/LinkStates] 1 publisher
 * /rosout [rosgraph_msgs/Log] 1 publisher
 * /rosout_agg [rosgraph_msgs/Log] 1 publisher
 * /gazebo/model_states [gazebo_msgs/ModelStates] 1 publisher
 * /clock [rosgraph_msgs/Clock] 1 publisher
 * /gazebo/parameter_descriptions [dynamic_reconfigure/ConfigDescription] 1 publisher
 * /gazebo/parameter_updates [dynamic_reconfigure/Config] 1 publisher

Subscribed topics:

 * /clock [rosgraph_msgs/Clock] 1 subscriber
 * /gazebo/set_model_state [gazebo_msgs/ModelState] 1 subscriber
 * /rosout [rosgraph_msgs/Log] 1 subscriber
 * /gazebo/set_link_state [gazebo_msgs/LinkState] 1 subscriber

I am using ros kinetic by the way, and I am sorry if my mistake is stupid, I litteraly can't find anything working on the Internet. Thank you for answering me !

edit retag flag offensive close merge delete



It's surprising if u can compile it, you have a syntax error here. A ; is missing after your subscribe definition. Furthermore make sure instant of the nodehandle is initialized properly..

mohsen1989m gravatar image mohsen1989m  ( 2017-04-04 05:11:48 -0500 )edit

Hello, i must have had a problem while recopying but the ";" is in the code. After your commentary I searched how does node initialise, and I just switch it on public and now it's working, thank you so much, i've been stuck on that for hours

shenki gravatar image shenki  ( 2017-04-04 05:45:18 -0500 )edit