YARP
Yet Another Robot Platform
RGBDSensorWrapper.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef YARP_DEV_RGBDSENSORWRAPPER_RGBDSENSORWRAPPER_H
10 #define YARP_DEV_RGBDSENSORWRAPPER_RGBDSENSORWRAPPER_H
11 
12 #include <vector>
13 #include <iostream>
14 #include <string>
15 #include <sstream>
16 
17 #include <yarp/os/Port.h>
18 #include <yarp/os/Time.h>
19 #include <yarp/os/Stamp.h>
20 #include <yarp/os/Bottle.h>
21 #include <yarp/os/Network.h>
22 #include <yarp/os/Property.h>
23 #include <yarp/os/PeriodicThread.h>
24 #include <yarp/os/BufferedPort.h>
25 
26 
27 #include <yarp/sig/Vector.h>
28 
29 #include <yarp/dev/IWrapper.h>
31 #include <yarp/dev/PolyDriver.h>
32 #include <yarp/dev/IRGBDSensor.h>
35 
36 // ROS stuff
37 #include <yarp/os/Node.h>
38 #include <yarp/os/Publisher.h>
39 #include <yarp/os/Subscriber.h>
40 #include <yarp/rosmsg/TickTime.h>
43 
44 
45 namespace RGBDImpl
46 {
47  const std::string frameId_param = "ROS_frame_Id";
48  const std::string nodeName_param = "ROS_nodeName";
49  const std::string colorTopicName_param = "ROS_colorTopicName";
50  const std::string depthTopicName_param = "ROS_depthTopicName";
51  const std::string depthInfoTopicName_param = "ROS_depthInfoTopicName";
52  const std::string colorInfoTopicName_param = "ROS_colorInfoTopicName";
53  class RGBDSensorParser;
54 }
55 
56 #define DEFAULT_THREAD_PERIOD 0.03 // s
57 
58 // Following three definitions would fit better in a header file
59 // shared between client and server ... where to place it?
61 #define RGBD_WRAPPER_PROTOCOL_VERSION_MAJOR 1
62 #define RGBD_WRAPPER_PROTOCOL_VERSION_MINOR 0
63 
64 
65 
68 {
69 private:
70  yarp::dev::IRGBDSensor *iRGBDSensor;
74 
75 public:
77  ~RGBDSensorParser() override = default;
78  bool configure(yarp::dev::IRGBDSensor *interface);
79  bool configure(yarp::dev::IRgbVisualParams *rgbInterface, yarp::dev::IDepthVisualParams *depthInterface);
81  bool respond(const yarp::os::Bottle& cmd, yarp::os::Bottle& response) override;
82 };
83 
84 
127  public yarp::dev::IWrapper,
130 {
131 private:
137  typedef unsigned int UInt;
138 
139  enum SensorType{COLOR_SENSOR, DEPTH_SENSOR};
140 
141  template <class T>
142  struct param
143  {
144  param(T& inVar, std::string inName)
145  {
146  var = &inVar;
147  parname = inName;
148  }
149  T* var;
150  std::string parname;
151  };
152 
153  std::string colorFrame_StreamingPort_Name;
154  std::string depthFrame_StreamingPort_Name;
155  ImagePortType colorFrame_StreamingPort;
156  DepthPortType depthFrame_StreamingPort;
157 
158  // One RPC port should be enough for the wrapper in all cases
159  yarp::os::Port rpcPort;
160  std::string rpcPort_Name;
161  ImageTopicType rosPublisherPort_color;
162  ImageTopicType rosPublisherPort_depth;
163  DepthTopicType rosPublisherPort_colorCaminfo;
164  DepthTopicType rosPublisherPort_depthCaminfo;
165  yarp::os::Node* rosNode;
166  std::string nodeName;
167  std::string depthTopicName;
168  std::string colorTopicName;
169  std::string dInfoTopicName;
170  std::string cInfoTopicName;
171  std::string rosFrameId;
172  yarp::sig::FlexImage colorImage;
173  DepthImage depthImage;
174  UInt nodeSeq;
175 
176  // It should be possible to attach this guy to more than one port, try to see what
177  // will happen when receiving 2 calls at the same time (receive one calls while serving
178  // another one, it will result in concurrent thread most probably) and buffering issues.
179 // sensor::depth::RGBDSensor_RPCMgsParser RPC_parser;
180 
181  //Helper class for RPCs
182  RGBDImpl::RGBDSensorParser rgbdParser;
183 
184  // Image data specs
185  // int hDim, vDim;
186  double period;
187  std::string sensorId;
188  yarp::dev::IRGBDSensor* sensor_p;
191  int verbose;
192  bool use_YARP;
193  bool use_ROS;
194  bool forceInfoSync;
195  bool initialize_YARP(yarp::os::Searchable& config);
196  bool initialize_ROS(yarp::os::Searchable& config);
197  bool read(yarp::os::ConnectionReader& connection);
198 
199  // Open the wrapper only, the attach method needs to be called before using it
200  // Typical usage: yarprobotinterface
201  bool openDeferredAttach(yarp::os::Searchable& prop);
202 
203  // If a subdevice parameter is given, the wrapper will open it and attach to immediately.
204  // Typical usage: simulator or command line
205  bool isSubdeviceOwned;
206  yarp::dev::PolyDriver* subDeviceOwned;
207  bool openAndAttachSubDevice(yarp::os::Searchable& prop);
208 
209  // Synch
210  yarp::os::Stamp colorStamp;
211  yarp::os::Stamp depthStamp;
212  yarp::os::Property m_conf;
213 
214  void shallowCopyImages(const yarp::sig::FlexImage& src, yarp::sig::FlexImage& dest);
215  void shallowCopyImages(const DepthImage& src, DepthImage& dest);
216  bool writeData();
217  void deepCopyImages(const yarp::sig::FlexImage& src,
219  const std::string& frame_id,
220  const yarp::rosmsg::TickTime& timeStamp,
221  const UInt& seq);
222 
223  void deepCopyImages(const DepthImage& src,
225  const std::string& frame_id,
226  const yarp::rosmsg::TickTime& timeStamp,
227  const UInt& seq);
228 
229  bool setCamInfo(yarp::rosmsg::sensor_msgs::CameraInfo& cameraInfo,
230  const std::string& frame_id,
231  const UInt& seq,
232  const SensorType& sensorType);
233 
234  static std::string yarp2RosPixelCode(int code);
235 
236 public:
242  ~RGBDSensorWrapper() override;
243 
244  bool open(yarp::os::Searchable &params) override;
245  bool fromConfig(yarp::os::Searchable &params);
246  bool close() override;
247 
248  void setId(const std::string &id);
249  std::string getId();
250 
254  bool attachAll(const yarp::dev::PolyDriverList &p) override;
255  bool detachAll() override;
256 
257  bool attach(yarp::dev::PolyDriver *poly) override;
259  bool detach() override;
260 
261  bool threadInit() override;
262  void threadRelease() override;
263  void run() override;
264 };
265 
266 #endif // YARP_DEV_RGBDSENSORWRAPPER_RGBDSENSORWRAPPER_H
constexpr yarp::conf::vocab32_t VOCAB_PROTOCOL_VERSION
contains the definition of a Vector type
bool configure(yarp::dev::IRGBDSensor *interface)
bool respond(const yarp::os::Bottle &cmd, yarp::os::Bottle &response) override
Respond to a message.
~RGBDSensorParser() override=default
bool detach() override
Detach the object (you must have first called attach).
bool attachAll(const yarp::dev::PolyDriverList &p) override
Specify which sensor this thread has to read from.
RGBDSensorWrapper & operator=(const RGBDSensorWrapper &)=delete
bool close() override
Close the DeviceDriver.
RGBDSensorWrapper & operator=(RGBDSensorWrapper &&)=delete
bool fromConfig(yarp::os::Searchable &params)
void run() override
Loop function.
RGBDSensorWrapper(RGBDSensorWrapper &&)=delete
RGBDSensorWrapper(const RGBDSensorWrapper &)=delete
bool threadInit() override
Initialization method.
bool open(yarp::os::Searchable &params) override
Device driver interface.
void setId(const std::string &id)
void threadRelease() override
Release method.
bool detachAll() override
Detach the object (you must have first called attach).
bool attach(yarp::dev::PolyDriver *poly) override
Attach to another object.
Interface implemented by all device drivers.
Definition: DeviceDriver.h:38
A cheap and cheerful framework for human readable/writable forms of messages to devices.
Definition: DeviceDriver.h:134
An interface for retrieving intrinsic parameter from a depth camera.
Control interface for frame grabber devices.
Interface for an object that can wrap/attach to to another.
A generic interface for cameras that have both color camera as well as depth camera sensor,...
Definition: IRGBDSensor.h:56
An interface for retrieving intrinsic parameter from a rgb camera.
Definition: IVisualParams.h:73
Interface for an object that can wrap/or "attach" to another.
Definition: IWrapper.h:29
A container for a device driver.
Definition: PolyDriver.h:27
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
An interface for reading from a network connection.
The Node class.
Definition: Node.h:27
An abstraction for a periodic thread.
A mini-server for network communication.
Definition: Port.h:50
A class for storing options and configuration information.
Definition: Property.h:37
A base class for nested structures that can be searched.
Definition: Searchable.h:69
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:25
Image class with user control of representation details.
Definition: Image.h:403
const std::string depthInfoTopicName_param
const std::string depthTopicName_param
const std::string colorTopicName_param
const std::string nodeName_param
const std::string frameId_param
const std::string colorInfoTopicName_param
std::int32_t vocab32_t
Definition: numeric.h:52
constexpr yarp::conf::vocab32_t createVocab(char a, char b=0, char c=0, char d=0)
Definition: Vocab.h:22