Advertisement
Guest User

ROS Gazebo

a guest
Nov 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <gazebo/transport/transport.hh>
  2. #include <gazebo/msgs/msgs.hh>
  3. #include <gazebo/gazebo_client.hh>
  4. #include "ros/ros.h"
  5. #include "sensor_msgs.msg.Image"
  6.  
  7. #include <iostream>
  8.  
  9. class readGazeboTopic
  10. {
  11. public:
  12.   readGazeboTopic(arguments);
  13.   virtual ~readGazeboTopic();
  14.  
  15.   void readGazeboTopic::cb(sensor_msgs::Image &img)
  16.   {
  17.     colorimage = *img;
  18.   }
  19.  
  20.   sensor_msgs::Image readGazeboTopic::readImage()
  21.   {
  22.     return colorimage;
  23.   }
  24.  
  25. private:
  26.   /* data */
  27.   sensor_msgs::Image colorimage;
  28. };
  29.  
  30.  
  31. /////////////////////////////////////////////////
  32. // Function is called everytime a message is received.
  33. void cb(ConstWorldStatisticsPtr &_msg)
  34. {
  35.   // Dump the message contents to stdout.
  36.   std::cout << _msg->DebugString();
  37. }
  38.  
  39. /////////////////////////////////////////////////
  40. int main(int _argc, char **_argv)
  41. {
  42.   // Load gazebo
  43.   gazebo::client::setup(_argc, _argv);
  44.  
  45.   //Load ROS
  46.   ros::init(argc, argv, "ImagePublisher");
  47.  
  48.   ros::NodeHandle n;
  49.  
  50.   // Create our node for communication
  51.   gazebo::transport::NodePtr node(new gazebo::transport::Node());
  52.   node->Init();
  53.  
  54.   //Create our class
  55.   readGazeboTopic rGT;
  56.  
  57.   // Listen to Gazebo world_stats topic
  58.   gazebo::transport::SubscriberPtr sub = node->Subscribe("~/world_stats", rGT::cb);
  59.  
  60.   // Ros publisher
  61.   ros::Publisher ImagePub = n.advertise<sensor_msgs::Image>("color", 1000);
  62.   ros::Rate loop_rate(10);
  63.  
  64.   // Busy wait loop...replace with your own code as needed.
  65.   while (ros::ok())  {
  66.     ImagePub.publish(rGT::readImage());
  67.     ros::spinOnce();
  68.     loop_rate.sleep();
  69.   }
  70.  
  71.   // Make sure to shut everything down.
  72.   gazebo::client::shutdown();
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement