RGBDCameraPlugin.hh
Go to the documentation of this file.
1 
10 #ifndef _RGBD_CAMERA_PLUGIN_HH_
11 #define _RGBD_CAMERA_PLUGIN_HH_
12 
13 // Gazebo
14 #include "gazebo/common/Plugin.hh"
15 #include <gazebo/msgs/msgs.hh>
16 #include <gazebo/physics/physics.hh>
17 #include <gazebo/rendering/DepthCamera.hh>
18 #include <gazebo/sensors/sensors.hh>
19 #include <gazebo/transport/transport.hh>
20 #include <gazebo/common/Events.hh>
21 // OGRE
22 #include "gazebo/rendering/ogre_gazebo.h"
23 // Custom messages
24 #include "camera_request.pb.h"
25 #include "camera_response.pb.h"
26 // Queue for threaded access
27 #include "ConcurrentQueue.hh"
28 
29 
30 // TODO - Profiling
31 #include <chrono>
32 
33 namespace RGBDCameraPlugin {
34 
35  // SDF parameters
36 
38  #define PARAM_CAMERA "cameraName"
39  #define PARAM_QUEUE_SIZE "renderQueueSize"
41  #define PARAM_OUTPUT_DIR "outputDir"
43  #define PARAM_EXTENSION "imageFormat"
45  #define PARAM_REQ_TOPIC "requestTopic"
47  #define PARAM_RES_TOPIC "responseTopic"
49 
50  // Default values for SDF parameters
51 
53  #define DEFAULT_OUTPUT_DIR "/tmp/RGBDCameraPlugin"
54  #define DEFAULT_EXTENSION "png"
56  #define DEFAULT_REQ_TOPIC "~/grasp/rgbd"
58  #define DEFAULT_RES_TOPIC "~/grasp/rgbd/response"
60 
61  // Message enums
62 
64  #define CAPTURE_REQUEST grasp::msgs::CameraRequest::CAPTURE
65  #define MOVE_REQUEST grasp::msgs::CameraRequest::MOVE
67  #define PREFIX_REQUEST grasp::msgs::CameraRequest::PREFIX
69  #define CAPTURE_RESPONSE grasp::msgs::CameraResponse::CAPTURE
71  #define MOVE_RESPONSE grasp::msgs::CameraResponse::MOVE
73  #define PREFIX_RESPONSE grasp::msgs::CameraResponse::PREFIX
75 }
76 
77 namespace gazebo {
78 
80  typedef const boost::shared_ptr<const grasp::msgs::CameraRequest>
83  typedef const boost::shared_ptr<const grasp::msgs::CameraResponse>
85 
86  // Forward declaration of private data class
88 
89  // TODO
90  class RGBDCameraPlugin : public ModelPlugin
91  {
92  // Private attributes
93 
95  private: std::unique_ptr<RGBDCameraPluginPrivate> data_ptr;
96 
98  private: physics::ModelPtr model;
100  private: physics::WorldPtr world;
101 
103  private: rendering::DepthCameraPtr camera;
105  private: event::ConnectionPtr updateConn;
107  private: event::ConnectionPtr newRGBFrameConn;
109  private: event::ConnectionPtr newDepthFrameConn;
110 
112  private: std::thread thread_rgb;
114  private: std::thread thread_depth;
116  private: bool stop_thread {false};
121 
123  private: std::string output_dir;
125  private: std::string output_prefix {""};
127  private: std::string output_ext;
128 
130  private: bool capture {false};
132  private: bool update_pose {false};
134  private: ignition::math::Pose3d new_pose;
135 
137  private: bool rgb_captured {false};
139  private: bool depth_captured {false};
140 
141  // Public methods
142 
144  public: RGBDCameraPlugin();
145 
147  public: virtual ~RGBDCameraPlugin();
148 
152  public: virtual void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf);
153 
160  public: void onNewRGBFrame(
161  const unsigned char *_image,
162  unsigned int _width,
163  unsigned int _height,
164  unsigned int _depth,
165  const std::string &_format);
166 
173  public: void onNewDepthFrame(
174  const float *_image,
175  unsigned int _width,
176  unsigned int _height,
177  unsigned int _depth,
178  const std::string &_format);
179 
180  // Private methods
181 
183  private: void onUpdate();
184 
187  private: void onRequest(CameraRequestPtr &_msg);
188 
190  private: void saveRenderRGB();
191 
193  private: void saveRenderDepth();
194 
196  private: void saveDepthFrame(
197  const float *_image,
198  unsigned int _width,
199  unsigned int _height,
200  unsigned int _depth,
201  const std::string &_format,
202  const std::string &_filename);
203 
205  private: void clearQueues();
206 
211  private: static int OgrePixelFormat(const std::string &_format);
212  };
213 }
214 
215 #endif
std::string output_ext
Rendered output format.
Definition: RGBDCameraPlugin.hh:127
event::ConnectionPtr newRGBFrameConn
Pointer to RGB camera callback connection.
Definition: RGBDCameraPlugin.hh:107
Queue data structure for concurrent access.
Definition: ContactSensorPlugin.cc:12
std::string output_dir
Render output directory.
Definition: RGBDCameraPlugin.hh:123
ConcurrentQueue< unsigned char * > * rgb_queue
Multithread safe queue for RGB data.
Definition: RGBDCameraPlugin.hh:118
Definition: RGBDCameraPlugin.hh:33
std::thread thread_rgb
Auxiliar thread for saving RGB frames to disk.
Definition: RGBDCameraPlugin.hh:112
rendering::DepthCameraPtr camera
Pointer to depth camera renderer.
Definition: RGBDCameraPlugin.hh:103
physics::ModelPtr model
Camera model.
Definition: RGBDCameraPlugin.hh:98
std::thread thread_depth
Auxiliar thread for saving depth frames to disk.
Definition: RGBDCameraPlugin.hh:114
Class for private Target plugin data.
Definition: RGBDCameraPlugin.cc:17
const boost::shared_ptr< const grasp::msgs::CameraResponse > CameraResponsePtr
Shared pointer declaration for response message type.
Definition: RGBDCameraPlugin.hh:84
ignition::math::Pose3d new_pose
New desired pose.
Definition: RGBDCameraPlugin.hh:134
physics::WorldPtr world
World.
Definition: RGBDCameraPlugin.hh:100
ConcurrentQueue< float * > * depth_queue
Multithread safe queue for depth data.
Definition: RGBDCameraPlugin.hh:120
const boost::shared_ptr< const grasp::msgs::CameraRequest > CameraRequestPtr
Shared pointer declaration for request message type.
Definition: RGBDCameraPlugin.hh:81
event::ConnectionPtr newDepthFrameConn
Pointer to depth camera callback connection.
Definition: RGBDCameraPlugin.hh:109
std::unique_ptr< RGBDCameraPluginPrivate > data_ptr
Class with private attributes.
Definition: RGBDCameraPlugin.hh:95
event::ConnectionPtr updateConn
Pointer to world update callback connection.
Definition: RGBDCameraPlugin.hh:105