0
$\begingroup$

Gazebo Answers logo

I am writing a simple subscriber program that I want which I want to subscribe to a topic that I publish. I have the publisher writtten and It works. But I am not able to even compile the subscriber : here is the subscriber program :

#include <gazebo.hh>
#include <transport/transport.hh>
#include <msgs/msgs.hh>
#include <iostream>

#include "custom.pb.h"
void subs(CustomPtr &msg)
{
  std::cout << "Recieved { " << msg->my_data << "}" ;
}
int main(int _argc, char **_argv)
{

  typedef const boost::shared_ptr<const my_msgs::msgs::Custom> CustomPtr

  // loading gazebo
  gazebo::load(_argc,_argv);
  int i;
  gazebo::run();
  
  gazebo::transport::NodePtr node(new gazebo::transport::Node());
  node->Init ();
  // start transportation
  gazebo::transport::run();

  // Subscribe to a topic
  gazebo::transport::SubscriberPtr sub = node->Subscribe("~/testing_example", subs);
  
  //publishing loop
  while (true)
  {


  }

  // shutting down
  gazebo::transport::fini();
}

This is the error I get :

/home/bharadwajramesh/Gazebo_tests/subscriber/subscriber.cc:7:11: error: variable or field ‘subs’ declared void
/home/bharadwajramesh/Gazebo_tests/subscriber/subscriber.cc:7:11: error: ‘CustomPtr’ was not declared in this scope
/home/bharadwajramesh/Gazebo_tests/subscriber/subscriber.cc:7:22: error: ‘msg’ was not declared in this scope
/home/bharadwajramesh/Gazebo_tests/subscriber/subscriber.cc: In function ‘int main(int, char**)’:
/home/bharadwajramesh/Gazebo_tests/subscriber/subscriber.cc:14:54: error: ISO C++ forbids declaration of ‘type name’ with no type [-fpermissive]
/home/bharadwajramesh/Gazebo_tests/subscriber/subscriber.cc:14:62: error: template argument 1 is invalid
/home/bharadwajramesh/Gazebo_tests/subscriber/subscriber.cc:17:3: error: expected initializer before ‘gazebo’
/home/bharadwajramesh/Gazebo_tests/subscriber/subscriber.cc:27:79: error: ‘subs’ was not declared in this scope
make[2]: *** [CMakeFiles/subscriber.dir/subscriber.cc.o] Error 1
make[1]: *** [CMakeFiles/subscriber.dir/all] Error 2
make: *** [all] Error 2

"testing_example" is the node that I publish on.

I do now know how to access the data that is publish over the node, how do I write a simple function that will do this for me. Please help.

-Thanks Bharadwaj


Originally posted by Bharadwaj Ramesh on Gazebo Answers with karma: 51 on 2013-05-03

Post score: 0


Original comments

Comment by scpeters on 2013-05-05:
You're missing a semicolon at the end of the typedef statement.

Comment by Bharadwaj Ramesh on 2013-05-06:
fixed that but only 1 less error !

Comment by Bharadwaj Ramesh on 2013-05-06:
basically I need the code that will help me extract data from a topic and use this data. like stor them in variables for use. Do I need to create a separate function for this ? I would be greatly helpfull if I can get a sample code that does this.

Comment by scpeters on 2013-05-07:
You probably also need to move the typedef before the definition of the subs function, otherwise the compiler won't know what CustomPtr means.

Comment by scpeters on 2013-05-07:
Here's another example plugin that uses gazebo topics for communication: collision_map_creator, from this tutorial.

Comment by Bharadwaj Ramesh on 2013-05-07:
I did try moving the type def outside but that does not help.

The above tutorial you shared seems too complicated for me since I am pretty much a beginner with c++.

I try follwoing this tutorial here

https://bitbucket.org/osrf/gazebo/src/256f98f2318bf2078e708f069367f1b71549ffb6/examples/stand_alone/listener/listener.cc?at=default

Does a subscribed need to defined inside a class ? In the above ex. I shared they do not use a class. Can I write up such a code that will subscribe.

Comment by scpeters on 2013-05-07:
If you want a simple solution and you're just trying to pass a single float, then use a Vector2d or Vector3d message, like I suggested in your other question.

Comment by Bharadwaj Ramesh on 2013-05-07:
Even if I publish as a Vector2d message how Do I still subscribing to a TOPIC.

$\endgroup$

1 Answer 1

0
$\begingroup$

Gazebo Answers logo

I have it fixed now. I am using a Vector2D type of message to transmit. here is the subscriber plugin:

#include <gazebo/transport/transport.hh>
#include <gazebo/msgs/msgs.hh>
#include <gazebo/gazebo.hh>

#include <iostream>

/////////////////////////////////////////////////
// Function is called everytime a message is received.
typedef const boost::shared_ptr<const gazebo::msgs::Vector2d> VectorTwoDPtr;
void cb(VectorTwoDPtr &_msg)
{
  // Dump the message contents to stdout.
  std::cout << "x : " <<_msg->x() << "\n";
  std::cout << "y : " <<_msg->y() << "\n";
}

/////////////////////////////////////////////////
int main(int _argc, char **_argv)
{
  // Load gazebo
  gazebo::load(_argc, _argv);

  gazebo::run();

  // Create our node for communication
  gazebo::transport::NodePtr node(new gazebo::transport::Node());
  node->Init();

  // Listen to Gazebo world_stats topic
  gazebo::transport::SubscriberPtr sub = node->Subscribe("~/pose_example1", cb);

  // Busy wait loop...replace with your own code as needed.
  while (true)
    gazebo::common::Time::MSleep(10);

  // Make sure to shut everything down.
  gazebo::transport::fini();
}

And my publisher looks like :

#include <gazebo/gazebo.hh>
#include <gazebo/transport/transport.hh>
#include <gazebo/msgs/msgs.hh>
#include <math/gzmath.hh>

#include <iostream>


/////////////////////////////////////////////////
int main(int _argc, char **_argv)
{
  // Load gazebo
  gazebo::load(_argc, _argv);

  // Create our node for communication
  gazebo::transport::NodePtr node(new gazebo::transport::Node());
  node->Init();

  // Start transport
  gazebo::transport::run();

  // Publish to a Gazebo topic
  gazebo::transport::PublisherPtr pub =
    node->Advertise<gazebo::msgs::Vector2d>("~/pose_example1");

  // Wait for a subscriber to connect
  pub->WaitForConnection();

  // Publisher loop...replace with your own code.
  while (true)
  {
    gazebo::common::Time::MSleep(100);
    gazebo::math::Vector2d vect(5, 7);
    gazebo::msgs::Vector2d msg;
    gazebo::msgs::Set(&msg, vect);
    pub->Publish(msg);
  }

  // Make sure to shut everything down.
  gazebo::transport::fini();
}

This is not a program I completely wrote but it was a program that I slightly modified based on the example given in this link :

https://bitbucket.org/osrf/gazebo/src/c21a92eb17f9/examples/stand_alone?at=default

This was just a simple program that I wanted to implement so that I understand how to communicate.

Thank you scpeters for all your patient replies and your suggestions which is what I finally planned to use.


Originally posted by Bharadwaj Ramesh with karma: 51 on 2013-05-12

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by scpeters on 2013-05-12:
I'm glad the Vector2d message worked for you. Using custom messages can be tricky to get set up properly.

Comment by Bharadwaj Ramesh on 2013-05-13:
Yeah, I dint know that I did not need to create a custom message. It was my lack of C++ knowledge that made it difficult to understand.

Comment by djou07 on 2015-01-13:
thanks for sharing your code, this will help me...

Comment by m4k on 2016-06-05:
*note for gazebo5 -> use #include <gazebo/math/gzmath.hh> instead of #include <math/gzmath.hh>

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.