Sure, just initialize the ROS node inside the load function
if (!ros::isInitialized())
{
int argc = 0;
char **argv = NULL;
ros::init(argc, argv, node_name,
ros::init_options::NoSigintHandler);
}
// Create ROS node.
this->rosNode.reset(new ros::NodeHandle( node_name ));
and create a publisher
this->rosPub = this->rosNode->advertise<std_msgs::Float64>(topic_name,1);
Then you can access the class variable this->rosPub
from within the plugin and send the messages.
EDIT
Include ROS at the top of the header file gazebo_motor_model.h
:
#include "ros/ros.h"
Declare the class variables inside the header file
namespace gazebo {
...
class GazeboMotorModel : public MotorModel, public ModelPlugin {
...
private: std::unique_ptr<ros::NodeHandle> rosNode;
private: ros::Publisher rosPub;
initialize the ROS node inside the Load function of the plugin inside the gazebo_motor_model.cpp
file
if (!ros::isInitialized())
{
int argc = 0;
char **argv = NULL;
ros::init(argc, argv, "my_ros_node",
ros::init_options::NoSigintHandler);
}
// Create ROS node.
this->rosNode.reset(new ros::NodeHandle( "my_ros_node" ));
initialize the ros publisher right under the ROS node initialization
this->rosPub = this->rosNode->advertise<std_msgs::Float64>("my_ros_topic",1);
note that the message type (here std_msgs::Float64
) must be edited to match your topics message type.
Now you can send the message anywhere in your update function
std_msgs::Float64 msg;
msg.data = 15.3;
this->rosPub.publish(msg);