Home | Tutorials | Wiki | Issues
Ask Your Question
0

Gazebo node cannot see Gazebo Topic, need help understanding why

asked 2017-01-25 16:11:59 -0500

Francis123 gravatar image

updated 2017-01-27 11:08:28 -0500

I used this tutorial to get me going http://gazebosim.org/tutorials?tut=gu....

It gave me enough information to create a gazebo model plugin, and a standalone gazebo node that would use a gazebo transport node to publish commands to a Gazebo topic.

My problem is that my gazebo model plugin is unable to see when a message is sent to the gazebo topic ("/topic/a") even though I have a gazebo subscriber callback function for "/topic/a". To confirm that I was publishing and subscribing to the gazebo topic I opened a terminal and used the command

"gz topic -i /topic/a"

The response was

Type: gazebo.msgs.Vector3d

Publishers:
    Same IP:43457

Subscribers:
    Same IP:45557

They had the same IP but different ports. Is that normal? How do I get both gazebo nodes to look at the same Gazebo topic?

My code for my standalone is below

#include <gazebo/gazebo_client.hh>
#include <gazebo/common/common.hh>
#include <gazebo/gazebo_config.h>
#include <gazebo/physics/physics.hh>
#include <gazebo/transport/transport.hh>
#include <gazebo/msgs/msgs.hh>


void callback_from_gazebo(ConstVector3dPtr &_msg) {   std::cout << "Callback received () \n"; }


int main(int _argc, char **_argv) {     std::cout << "Gazebo Initialization \n";
    gazebo::transport::PublisherPtr pub1,pub2;
    gazebo::transport::SubscriberPtr sub1;

    gazebo::client::setup(_argc, _argv);

    gazebo::transport::init();
    gazebo::transport::run();
    gazebo::transport::NodePtr node(new gazebo::transport::Node());
    node->Init("default");
    std::cout << "End Gazebo Initialization \n";

    std::cout << "Define Gazebo Publishers and Subscribers \n";
    pub1 = node->Advertise<gazebo::msgs::Vector3d>("/topic/a");
    pub2 = node->Advertise<gazebo::msgs::WorldControl>("/gazebo/torqueprofile/world_control");
    sub1 = node->Subscribe("/topic/b", callback_from_gazebo);

    std::cout << "Waiting for connection\n";
    pub1->WaitForConnection();
    pub2->WaitForConnection();
    std::cout << "Connections confirmed\n";

    while(1)
    {
        gazebo::msgs::Vector3d cmd;
        cmd.set_x(1);
        cmd.set_y(2);
        cmd.set_z(3);
        pub1->WaitForConnection();
        pub1->Publish(cmd);

        gazebo::msgs::WorldControl world_msg;
        world_msg.set_step(true);
        pub2->Publish(world_msg);

        gazebo::common::Time::MSleep(1000);
    }

    gazebo::transport::fini();
    gazebo::client::shutdown();
    return 0; }

My code for my plugin is below

#include <boost/bind.hpp>
#include <gazebo/transport/transport.hh>
#include <gazebo/msgs/msgs.hh>
#include <gazebo/physics/physics.hh>
#include <gazebo/common/common.hh>
#include <gazebo/math/Angle.hh>
#include <gazebo/gazebo_client.hh>
#include <gazebo/gazebo_config.h>
#include <stdio.h>
#include <fstream>


namespace gazebo
{
  /// \brief A plugin to control two cubes strictly in Gazebo
  class gazebo_simple_plugin : public ModelPlugin
  {
    /// \brief Constructor
    public: gazebo_simple_plugin() 
    {
    }


    public: virtual void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
    {
        model = _model;

        if (model->GetJointCount() == 0)
        {
            std::cerr << "\n no joints \n";
            return;
        }
        else
            std::cerr << "\n joints are there \n";

        //set joint handle to joints in the _model
        joint1 = model->GetJoints()[0]; 

        // Create the Gazebo node
        node = transport::NodePtr(new transport::Node());
        node->Init(this->model->GetWorld()->GetName());

        pub1 = node->Advertise<gazebo::msgs::Vector3d>("/topic/b");
        sub1 = node->Subscribe("/topic/a", &gazebo_simple_plugin::callback_from_standalone, this);

        //Listen to the update event. This event is broadcast every
        updateConnection = event::Events::ConnectWorldUpdateBegin(boost::bind(&gazebo_simple_plugin::OnUpdate, this, _1));

    }


    private: void callback_from_standalone(ConstVector3dPtr &_msg)
    {
      cmd=(_msg->x());
      printf("cmd received ...
(more)
edit retag flag offensive close merge delete

Comments

Make sure there is no typo in the topic name for your subscribe call. Can you provide code and what Gazebo version you are using?

hugomatic gravatar imagehugomatic ( 2017-01-26 12:53:01 -0500 )edit

I made sure there was no typo in the topic name. Also i am able to see both the publisher and the subscriber to the topic so that confirms that the gazebo nodes are looking at the topic. I did find this page to be helpful (http://gazebosim.org/tutorials?tut=custom_messages#CodeExplained13). It made it clear that in my gazebo node executable (that uses int main()) I needed to make a call to these two transport functions: gazebo::transport::init(); gazebo::transport::run();

Francis123 gravatar imageFrancis123 ( 2017-01-27 10:20:06 -0500 )edit

I also added the call pub1->WaitForConnection(); before each message that I publish over the gazebo transport layer, before I just called it after defining the gazebo publisher handle. I am using Gazebo 7, and cannot show code due to employment restrictions.

Francis123 gravatar imageFrancis123 ( 2017-01-27 10:22:41 -0500 )edit

It is normal that the publisher and subscriber have different ports. The output of gz topic indicates that there is one publisher on /topic/a and one subscriber. If you are running a model plugin, then you shouldn't call gazebo::transport::init nor gazebo::transport::run. The documentation you referenced is for a standalone program. Can you post your code?

nkoenig gravatar imagenkoenig ( 2017-01-27 10:26:21 -0500 )edit

I can show small portions though. Here is my subscriber for topic a in one gazebo node sub1 = node->Subscribe("/gazebo/torqueprofile/feedback", callback_from_gazebo); Here is my code for the publisher for "topic a" in the gazebo control plugin pub1 = node->Advertise<gazebo::msgs::vector3d>("/gazebo/torqueprofile/feedback"); They are identical name definition

Francis123 gravatar imageFrancis123 ( 2017-01-27 10:26:41 -0500 )edit

Can you create a simple plugin that demonstrates your problem, but doesn't violate your employment restrictions. Creating a simple plugin will also help you self-debug the problem.

nkoenig gravatar imagenkoenig ( 2017-01-27 10:28:00 -0500 )edit

I added the code to the question now. So basically what I see now is the stand alone can publish to topic a, but the gazebo plugin cannot publish to topic b, any ideas?

Francis123 gravatar imageFrancis123 ( 2017-01-27 11:50:13 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2017-01-31 13:50:00 -0500

Francis123 gravatar image

updated 2017-01-31 13:51:13 -0500

I was able to figure out why my standalone gazebo program and gazebo plugin were having problems communicating with each other.

It was all on the the standalone gazebo program side, and I used this link to help me understand where I went wrong. http://gazebosim.org/tutorials?tut=cu...

For the problem of publishing messages to a gazebo topic Basically I was missing these two lines before initializing the gazebo transport node (something that is not used in another tutorial I used)

gazebo::transport::init();
gazebo::transport::run();

For the problem of subscribing to a gazebo topic through the standalone gazebo program, I had to create a shared boost ptr to the object that is passed as a parameter to the callback function

typedef const boost::shared_ptr<gazebo::msgs::Vector3d> ConstVector3dPtr;

In the future it might be helpful if there were tutorials that are soley about creating standalone gazebo programs, I did find the answers but through a lot of assumptions.

edit flag offensive delete link more
0

answered 2017-01-27 10:31:24 -0500

chapulina gravatar image

If I understood well, you have a model plugin and a standalone program talking to each other over a Gazebo topic, right?

You could try setting the GAZEBO_MASTER_URI environment variable before launching Gazebo with your plugin and before launching the standalone program in an attempt to force them to use the same port, for example:

GAZEBO_MASTER_URI=http://localhost:43457 gazebo your.world
GAZEBO_MASTER_URI=http://localhost:43457 ./standalone_program
edit flag offensive delete link more

Comments

I tired using the command you suggested, but I keep getting an error saying your.world is a invalid type of identifier. Obviosuly I replace your.world with my world. Aside from that I dont think that the port is what is causing this problem I created a simple version of standalone program and gazebo plugin and posted it above. What I found was that the standalone program was able to publish to the topic a, and the gazebo plugin was able to see see the message

Francis123 gravatar imageFrancis123 ( 2017-01-27 13:12:11 -0500 )edit

However the gazebo plugin cannot publish to topic B, and I have no idea why,

Francis123 gravatar imageFrancis123 ( 2017-01-27 13:12:49 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-01-25 16:11:59 -0500

Seen: 1,750 times

Last updated: Jan 31 '17