How to publish data to ROS from Gazebo plugin

asked 2016-03-31 23:09:42 -0500

unlight gravatar image

Hi guys,

I am trying to learn how to publish data over a ROS topic from a Gazebo plugin class. I have attempted to reuse other people's code, but without any luck. I have included the following code that for now, is only attempting to initialise ROS and then publish a String over a custom topic.

class VelodynePlugin : public ModelPlugin
{
    private: ros::NodeHandle* rosnode_;
    private: ros::Publisher pub_;
    private: PubQueue<std_msgs::String>::Ptr pub_Queue;
    private: PubMultiQueue pmq;

    public: virtual void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
    {   
        if (!ros::isInitialized()) 
        {
          int argc = 0;
          char **argv = NULL;
          ros::init(argc, argv, "TestSpace", ros::init_options::NoSigintHandler);
        }

        this->rosnode_ = new ros::NodeHandle("TestSpace");
        this->pmq.startServiceThread();
        this->pub_Queue = this->pmq.addPub<std_msgs::String>();
        this->pub_ = this->rosnode_->advertise<std_msgs::String>("/custom", 100);

        std_msgs::String msg;
        msg.data = "hello";
        this->pub_Queue->push(msg, this->pub_);
    }
};

GZ_REGISTER_MODEL_PLUGIN(VelodynePlugin);

When I run this plugin through Gazebo, I can see that the "/custom" topic has been created under "rostopic list", however, when I subscribe to the topic via "rostopic echo /custom", I do not get the "hello" message.

Is anyone able to guide me on what I am missing to properly publish data from the Gazebo plugin so that I can subscribe to it in a ROS program?

Many thanks!

edit retag flag offensive close merge delete

Comments

I'm not super familiar with this but shouldn't it be a `Sensor` rather than a `ModelPlugin`? Also why not base your code off of gazebo_ros_laser in https://github.com/ros-simulation/gazebo_ros_pkgs ?

Lucas Walter gravatar imageLucas Walter ( 2016-04-01 15:25:30 -0500 )edit

You might be right about the SensorPlugin, this class was the result of following the Velodyne Lidar tutorial and was supposed to Spin the Lidar's revolute joint. Ultimately I just want to broadcast the sensor measurements within the plugin as well. Thank you for the link, I will see if I can figure it out from the code there. If anyone notices that I am missing something simple, I would really appreciate the help.

unlight gravatar imageunlight ( 2016-04-01 17:17:15 -0500 )edit

And if you add the following line : this->pub_.publish(msg);

Brosseau.F gravatar imageBrosseau.F ( 2016-04-04 01:52:57 -0500 )edit

Hi, I try to use your publisher, but i got some errors. which headers did you add?

VikoX gravatar imageVikoX ( 2018-12-11 12:54:08 -0500 )edit