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 
129  public yarp::dev::IWrapper,
132 {
133 private:
139  typedef unsigned int UInt;
140 
141  enum SensorType{COLOR_SENSOR, DEPTH_SENSOR};
142 
143  template <class T>
144  struct param
145  {
146  param(T& inVar, std::string inName)
147  {
148  var = &inVar;
149  parname = inName;
150  }
151  T* var;
152  std::string parname;
153  };
154 
155  std::string colorFrame_StreamingPort_Name;
156  std::string depthFrame_StreamingPort_Name;
157  ImagePortType colorFrame_StreamingPort;
158  DepthPortType depthFrame_StreamingPort;
159 
160  // One RPC port should be enough for the wrapper in all cases
161  yarp::os::Port rpcPort;
162  std::string rpcPort_Name;
163  ImageTopicType rosPublisherPort_color;
164  ImageTopicType rosPublisherPort_depth;
165  DepthTopicType rosPublisherPort_colorCaminfo;
166  DepthTopicType rosPublisherPort_depthCaminfo;
167  yarp::os::Node* rosNode;
168  std::string nodeName;
169  std::string depthTopicName;
170  std::string colorTopicName;
171  std::string dInfoTopicName;
172  std::string cInfoTopicName;
173  std::string rosFrameId;
174  yarp::sig::FlexImage colorImage;
176  UInt nodeSeq;
177 
178  // It should be possible to attach this guy to more than one port, try to see what
179  // will happen when receiving 2 calls at the same time (receive one calls while serving
180  // another one, it will result in concurrent thread most probably) and buffering issues.
181 // sensor::depth::RGBDSensor_RPCMgsParser RPC_parser;
182 
183  //Helper class for RPCs
184  RGBDImpl::RGBDSensorParser rgbdParser;
185 
186  // Image data specs
187  // int hDim, vDim;
188  double period;
189  std::string sensorId;
190  yarp::dev::IRGBDSensor* sensor_p;
193  int verbose;
194  bool use_YARP;
195  bool use_ROS;
196  bool forceInfoSync;
197  bool initialize_YARP(yarp::os::Searchable& config);
198  bool initialize_ROS(yarp::os::Searchable& config);
199  bool read(yarp::os::ConnectionReader& connection);
200 
201  // Open the wrapper only, the attach method needs to be called before using it
202  // Typical usage: yarprobotinterface
203  bool openDeferredAttach(yarp::os::Searchable& prop);
204 
205  // If a subdevice parameter is given, the wrapper will open it and attach to immediately.
206  // Typical usage: simulator or command line
207  bool isSubdeviceOwned;
208  yarp::dev::PolyDriver* subDeviceOwned;
209  bool openAndAttachSubDevice(yarp::os::Searchable& prop);
210 
211  // Synch
212  yarp::os::Stamp colorStamp;
213  yarp::os::Stamp depthStamp;
214  yarp::os::Property m_conf;
215 
216  bool writeData();
217  bool setCamInfo(yarp::rosmsg::sensor_msgs::CameraInfo& cameraInfo,
218  const std::string& frame_id,
219  const UInt& seq,
220  const SensorType& sensorType);
221 
222  static std::string yarp2RosPixelCode(int code);
223 
224 public:
230  ~RGBDSensorWrapper() override;
231 
232  bool open(yarp::os::Searchable &params) override;
233  bool fromConfig(yarp::os::Searchable &params);
234  bool close() override;
235 
236  void setId(const std::string &id);
237  std::string getId();
238 
242  bool attachAll(const yarp::dev::PolyDriverList &p) override;
243  bool detachAll() override;
244 
245  bool attach(yarp::dev::PolyDriver *poly) override;
247  bool detach() override;
248 
249  bool threadInit() override;
250  void threadRelease() override;
251  void run() override;
252 };
253 
254 #endif // YARP_DEV_RGBDSENSORWRAPPER_RGBDSENSORWRAPPER_H
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
TickTime.h
RGBDSensorWrapper::setId
void setId(const std::string &id)
Definition: RGBDSensorWrapper.cpp:493
yarp::os::createVocab
constexpr yarp::conf::vocab32_t createVocab(char a, char b=0, char c=0, char d=0)
Definition: Vocab.h:22
Network.h
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
Vector.h
contains the definition of a Vector type
Subscriber.h
RGBDImpl::depthInfoTopicName_param
const std::string depthInfoTopicName_param
Definition: RGBDSensorWrapper.h:51
yarp::rosmsg::sensor_msgs::CameraInfo
Definition: CameraInfo.h:162
RGBDSensorWrapper::operator=
RGBDSensorWrapper & operator=(RGBDSensorWrapper &&)=delete
RGBDSensorWrapper::threadInit
bool threadInit() override
Initialization method.
Definition: RGBDSensorWrapper.cpp:619
RGBDImpl::colorInfoTopicName_param
const std::string colorInfoTopicName_param
Definition: RGBDSensorWrapper.h:52
IWrapper.h
RGBDImpl::frameId_param
const std::string frameId_param
Definition: RGBDSensorWrapper.h:47
RGBDSensorWrapper::getId
std::string getId()
Definition: RGBDSensorWrapper.cpp:498
Port.h
yarp::dev::DeviceDriver
Interface implemented by all device drivers.
Definition: DeviceDriver.h:38
RGBDSensorWrapper::close
bool close() override
Close the DeviceDriver.
Definition: RGBDSensorWrapper.cpp:391
RGBDImpl::depthTopicName_param
const std::string depthTopicName_param
Definition: RGBDSensorWrapper.h:50
RGBDSensorWrapper::attachAll
bool attachAll(const yarp::dev::PolyDriverList &p) override
Specify which sensor this thread has to read from.
Definition: RGBDSensorWrapper.cpp:506
RGBDSensorWrapper::~RGBDSensorWrapper
~RGBDSensorWrapper() override
Definition: RGBDSensorWrapper.cpp:190
yarp::dev::IRGBDSensor::RGBDSensor_status
RGBDSensor_status
Definition: IRGBDSensor.h:63
yarp::os::Publisher< yarp::rosmsg::sensor_msgs::Image >
yarp::dev::PolyDriverList
Definition: PolyDriverList.h:22
RGBDImpl::RGBDSensorParser::respond
bool respond(const yarp::os::Bottle &cmd, yarp::os::Bottle &response) override
Respond to a message.
Definition: RGBDSensorWrapper.cpp:59
RGBDSensorWrapper::operator=
RGBDSensorWrapper & operator=(const RGBDSensorWrapper &)=delete
FrameGrabberControlImpl.h
RGBDImpl::RGBDSensorParser
Definition: RGBDSensorWrapper.h:68
RGBDImpl::RGBDSensorParser::configure
bool configure(yarp::dev::IRGBDSensor *interface)
Definition: RGBDSensorWrapper.cpp:38
RGBDSensorWrapper::attach
bool attach(yarp::dev::PolyDriver *poly) override
Attach to another object.
Definition: RGBDSensorWrapper.cpp:582
yarp::sig::ImageOf< yarp::sig::PixelFloat >
yarp::os::Port
A mini-server for network communication.
Definition: Port.h:50
yarp::os::BufferedPort< DepthImage >
VOCAB_PROTOCOL_VERSION
constexpr yarp::conf::vocab32_t VOCAB_PROTOCOL_VERSION
Definition: RGBDSensorWrapper.h:60
IVisualParamsImpl.h
RGBDImpl::nodeName_param
const std::string nodeName_param
Definition: RGBDSensorWrapper.h:48
Property.h
RGBDSensorWrapper::RGBDSensorWrapper
RGBDSensorWrapper(const RGBDSensorWrapper &)=delete
PolyDriver.h
yarp::sig::FlexImage
Image class with user control of representation details.
Definition: Image.h:403
Stamp.h
RGBDSensorWrapper::detachAll
bool detachAll() override
Detach the object (you must have first called attach).
Definition: RGBDSensorWrapper.cpp:543
RGBDSensorWrapper
RGBDSensorWrapper: A Network grabber for kinect-like devices.
Definition: RGBDSensorWrapper.h:132
yarp::dev::PolyDriver
A container for a device driver.
Definition: PolyDriver.h:27
Node.h
yarp::dev::IWrapper
Interface for an object that can wrap/or "attach" to another.
Definition: IWrapper.h:29
RGBDImpl
Definition: RGBDSensorWrapper.h:46
CameraInfo.h
RGBDSensorWrapper::open
bool open(yarp::os::Searchable &params) override
Device driver interface.
Definition: RGBDSensorWrapper.cpp:199
RGBDSensorWrapper::threadRelease
void threadRelease() override
Release method.
Definition: RGBDSensorWrapper.cpp:625
yarp::os::Node
The Node class.
Definition: Node.h:27
yarp::dev::IRGBDSensor
A generic interface for cameras that have both color camera as well as depth camera sensor,...
Definition: IRGBDSensor.h:56
yarp::dev::IFrameGrabberControls
Control interface for frame grabber devices.
Definition: FrameGrabberInterfaces.h:365
BufferedPort.h
IMultipleWrapper.h
yarp::os::PeriodicThread
An abstraction for a periodic thread.
Definition: PeriodicThread.h:25
yarp::dev::Implement_RgbVisualParams_Parser
Definition: IVisualParamsImpl.h:54
PeriodicThread.h
yarp::os::ConnectionReader
An interface for reading from a network connection.
Definition: ConnectionReader.h:40
yarp::os::Stamp
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:25
IRGBDSensor.h
yarp::dev::DeviceResponder
A cheap and cheerful framework for human readable/writable forms of messages to devices.
Definition: DeviceDriver.h:134
yarp::dev::IRgbVisualParams
An interface for retrieving intrinsic parameter from a rgb camera.
Definition: IVisualParams.h:73
RGBDSensorWrapper::detach
bool detach() override
Detach the object (you must have first called attach).
Definition: RGBDSensorWrapper.cpp:611
RGBDSensorWrapper::RGBDSensorWrapper
RGBDSensorWrapper()
Definition: RGBDSensorWrapper.cpp:174
RGBDSensorWrapper::run
void run() override
Loop function.
Definition: RGBDSensorWrapper.cpp:840
RGBDImpl::RGBDSensorParser::~RGBDSensorParser
~RGBDSensorParser() override=default
Publisher.h
yarp::dev::IMultipleWrapper
Interface for an object that can wrap/attach to to another.
Definition: IMultipleWrapper.h:30
yarp::conf::vocab32_t
std::int32_t vocab32_t
Definition: numeric.h:52
RGBDImpl::colorTopicName_param
const std::string colorTopicName_param
Definition: RGBDSensorWrapper.h:49
Time.h
RGBDImpl::RGBDSensorParser::RGBDSensorParser
RGBDSensorParser()
Definition: RGBDSensorWrapper.cpp:33
yarp::dev::IDepthVisualParams
An interface for retrieving intrinsic parameter from a depth camera.
Definition: IVisualParams.h:183
RGBDSensorWrapper::fromConfig
bool fromConfig(yarp::os::Searchable &params)
Definition: RGBDSensorWrapper.cpp:248
RGBDSensorWrapper::RGBDSensorWrapper
RGBDSensorWrapper(RGBDSensorWrapper &&)=delete
Image.h
yarp::dev::FrameGrabberControls_Parser
Definition: FrameGrabberControlImpl.h:59
yarp::dev::Implement_DepthVisualParams_Parser
Definition: IVisualParamsImpl.h:95
Bottle.h
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:37