Home | Tutorials | Wiki | Issues
Ask Your Question
0

Simple Subscribing plugin

asked 2013-05-03 23:28:10 -0500

Bharadwaj Ramesh gravatar image

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

edit retag flag offensive close merge delete

Comments

You're missing a semicolon at the end of the typedef statement.

scpeters gravatar imagescpeters ( 2013-05-05 02:25:32 -0500 )edit

fixed that but only 1 less error !

Bharadwaj Ramesh gravatar imageBharadwaj Ramesh ( 2013-05-06 22:22:26 -0500 )edit

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.

Bharadwaj Ramesh gravatar imageBharadwaj Ramesh ( 2013-05-06 22:25:00 -0500 )edit

You probably also need to move the typedef before the definition of the subs function, otherwise the compiler won't know what CustomPtr means.

scpeters gravatar imagescpeters ( 2013-05-07 01:10:18 -0500 )edit

Here's another example plugin that uses gazebo topics for communication: collision_map_creator, from this tutorial.

scpeters gravatar imagescpeters ( 2013-05-07 01:13:39 -0500 )edit

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.

Bharadwaj Ramesh gravatar imageBharadwaj Ramesh ( 2013-05-07 15:30:40 -0500 )edit

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.

scpeters gravatar imagescpeters ( 2013-05-07 19:21:34 -0500 )edit

Even if I publish as a Vector2d message how Do I still subscribing to a TOPIC.

Bharadwaj Ramesh gravatar imageBharadwaj Ramesh ( 2013-05-07 21:43:10 -0500 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2013-05-12 19:30:29 -0500

Bharadwaj Ramesh gravatar image

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...

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.

edit flag offensive delete link more

Comments

I'm glad the Vector2d message worked for you. Using custom messages can be tricky to get set up properly.

scpeters gravatar imagescpeters ( 2013-05-12 23:00:24 -0500 )edit

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.

Bharadwaj Ramesh gravatar imageBharadwaj Ramesh ( 2013-05-13 21:53:22 -0500 )edit

thanks for sharing your code, this will help me...

djou07 gravatar imagedjou07 ( 2015-01-13 02:43:23 -0500 )edit

*note for gazebo5 -> use #include <gazebo math="" gzmath.hh=""> instead of #include <math gzmath.hh="">

m4k gravatar imagem4k ( 2016-06-05 17:03:55 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-05-03 23:28:10 -0500

Seen: 3,356 times

Last updated: May 12 '13