Guest User

Untitled

a guest
May 21st, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. #include <ros/ros.h>
  2.  
  3. class SubscribeAndPublish
  4. {
  5. public:
  6. SubscribeAndPublish()
  7. {
  8. //Topic you want to publish
  9. pub_ = n_.advertise<PUBLISHED_MESSAGE_TYPE>("/published_topic", 1);
  10.  
  11. //Topic you want to subscribe
  12. sub_ = n_.subscribe("/subscribed_topic", 1, &SubscribeAndPublish::callback, this);
  13. }
  14.  
  15. void callback(const SUBSCRIBED_MESSAGE_TYPE& input)
  16. {
  17. PUBLISHED_MESSAGE_TYPE output;
  18. //.... do something with the input and generate the output...
  19. pub_.publish(output);
  20. }
  21.  
  22. private:
  23. ros::NodeHandle n_;
  24. ros::Publisher pub_;
  25. ros::Subscriber sub_;
  26.  
  27. };//End of class SubscribeAndPublish
  28.  
  29. int main(int argc, char **argv)
  30. {
  31. //Initiate ROS
  32. ros::init(argc, argv, "subscribe_and_publish");
  33.  
  34. //Create an object of class SubscribeAndPublish that will take care of everything
  35. SubscribeAndPublish SAPObject;
  36.  
  37. ros::spin();
  38.  
  39. return 0;
  40. }
Add Comment
Please, Sign In to add comment