Gazebo | Ignition | Community
Ask Your Question

GAZEBO plugin subscribe to ROS topic

asked 2016-08-05 04:47:50 -0500

Hanteus gravatar image

Greetings, I have some problems making a GAZEBO plugin and a ROS node communicate. The ROS node pubblishes correctly the message on the topic, but the plugin isn't able to acces them.

This is the code of the plugin:

// Initialize node
node = transport::NodePtr(new transport::Node());

// Subscribe to the control node
std::cout << "Subscribing to " << "~/quetzalcoatl/cmdVelocity" << " topic" << std::endl;
commandSubscriber = node->Subscribe("~/quetzalcoatl/cmdVelocity", &SkidSteer::updateVelocity, this);
std::cout << "Subscribed to " << commandSubscriber << std::endl;

// Listen to the update event, this event is broadcast every simulation iteration
this->updateConnection = event::Events::ConnectWorldUpdateBegin(
boost::bind(&SkidSteer::OnUpdate, this, _1));

This is the code of the ROS node which pubblishes the message:

while (ros::ok())
    ROS_INFO("Node '%s' sending the %d message", ros::this_node::getName().c_str(), count);

    // Update the velocities
    linearVelocity = 10 + count / 100;
    angularVelocity = 0;

    // Fill the message
    cmdVelocityMsg.linearVelocity = linearVelocity;
    cmdVelocityMsg.angularVelocity = angularVelocity;
    cmdVelocityMsg.moveUp = moveUp;
    cmdVelocityMsg.moveDown = moveDown;
    cmdVelocityMsg.moveLeft = moveLeft;
    cmdVelocityMsg.moveRigth = moveRigth;


    // Sleep untill next loop, Gazebo must be running or it won't work

    // ROS_INFO("Sleeped");


This is the message used by the plugin:

package quetzalcoatlMsgs.msgs;

message cmdVelocity
    required float linearVelocity = 1;
    required float angularVelocity = 2;
    required bool moveUp = 3;
    required bool moveDown = 4;
    required bool moveLeft = 5;
    required bool moveRigth = 6;

This is the message used by the node:

float32 linearVelocity
float32 angularVelocity
bool moveUp
bool moveDown
bool moveLeft
bool moveRigth

What I'm doing wrong? Also it is correct to declare two different messages, one for the node and one for the plugin, which refer to the same topic?

edit retag flag offensive close merge delete


You probably need to use a single message description. Maybe this tutorial will help:

chapulina gravatar imagechapulina ( 2016-08-05 12:09:48 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2016-08-07 10:42:41 -0500

winston gravatar image

You could not directly subscribe to ROS topics and use ROS messages by a transport node. You have 2 solutions to choose:
1. Declare a ROS node in your plugin and use it to subscribe to your ROS messages. And then fill your transport messages one field by one field.
2. Declare a transport node in your ROS node and publish your transport messages.

The second solution seems more convenient. I have not tried this way but I suggestion you tried it and told us whether it is feasible or not.

edit flag offensive delete link more


Thank you for your advice. I've choosen the first solution, since it's the one that also explained by this tutorial ( Now I need to find a way to force the plugin to use my ros custom message.

Hanteus gravatar imageHanteus ( 2016-08-20 02:32:36 -0500 )edit

Question Tools



Asked: 2016-08-05 04:47:50 -0500

Seen: 5,580 times

Last updated: Aug 07 '16