How to publish data to ROS from Gazebo plugin
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!
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 ?
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.
And if you add the following line : this->pub_.publish(msg);
Hi, I try to use your publisher, but i got some errors. which headers did you add?
Hi, unlight, did you find the answer? I am trying to do the same and have not had good result so far.