Simple Subscribing plugin
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
You're missing a semicolon at the end of the typedef statement.
fixed that but only 1 less error !
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.
You probably also need to move the typedef before the definition of the subs function, otherwise the compiler won't know what CustomPtr means.
Here's another example plugin that uses gazebo topics for communication: collision_map_creator, from this tutorial.
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.
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.
Even if I publish as a Vector2d message how Do I still subscribing to a TOPIC.