YARP
Yet Another Robot Platform
RGBDSensorClient.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "RGBDSensorClient.h"
8 #include <yarp/os/Portable.h>
9 #include <yarp/os/LogComponent.h>
10 #include <yarp/os/LogStream.h>
12 
13 using namespace yarp::os;
14 using namespace yarp::sig;
15 using namespace yarp::dev;
16 
17 #define RGBD_INTERFACE_PROTOCOL_VERSION_MAJOR 1
18 #define RGBD_INTERFACE_PROTOCOL_VERSION_MINOR 0
19 
20 YARP_LOG_COMPONENT(RGBDSENSORCLIENT, "yarp.devices.RGBDSensorClient")
21 
22 
24  yarp::proto::framegrabber::FrameGrabberControls_Forwarder(rpcPort),
25  RgbMsgSender(new yarp::proto::framegrabber::RgbVisualParams_Forwarder(rpcPort)),
26  DepthMsgSender(new yarp::proto::framegrabber::DepthVisualParams_Forwarder(rpcPort)),
27  streamingReader(new RGBDSensor_StreamingMsgParser)
28 {
29 }
30 
32 {
33  close();
34  delete RgbMsgSender;
35  delete DepthMsgSender;
36  delete streamingReader;
37 }
38 
40 {
41  if(verbose >= 5) {
42  yCTrace(RGBDSENSORCLIENT) << "\n Paramerters are: \n" << config.toString();
43  }
44 
45  if(!fromConfig(config))
46  {
47  yCError(RGBDSENSORCLIENT) << "Failed to open, check previous log for error messages.";
48  return false;
49  }
50 
51  sensorId= "RGBDSensorClient for " + local_depthFrame_StreamingPort_name;
52 
53  if(!initialize_YARP(config) )
54  {
55  yCError(RGBDSENSORCLIENT) << sensorId << "\n\t* Error initializing YARP ports *";
56  return false;
57  }
58 
59  return true;
60 }
61 
62 
64 {
65  // Parse LOCAL port names
66  // TBD: check if user types '...' as port name, how to create RPC port names
67  if (!config.check("localImagePort", "full name of the port for streaming color image"))
68  {
69  yCError(RGBDSENSORCLIENT) << "Missing 'localImagePort' parameter. Check you configuration file; it must be like:";
70  yCError(RGBDSENSORCLIENT) << " localImagePort: Full name of the local port to open, e.g. /myApp/image_camera";
71  return false;
72  }
73 
74  local_colorFrame_StreamingPort_name = config.find("localImagePort").asString();
75 
76  if (!config.check("localDepthPort", "full name of the port for streaming depth image"))
77  {
78  yCError(RGBDSENSORCLIENT) << "Missing 'localDepthPort' parameter. Check you configuration file; it must be like:";
79  yCError(RGBDSENSORCLIENT) << " localDepthPort: Full name of the local port to open, e.g. /myApp/depth_camera";
80  return false;
81  }
82 
83  local_depthFrame_StreamingPort_name = config.find("localDepthPort").asString();
84 
85  // Parse REMOTE port names
86  if (!config.check("remoteImagePort", "full name of the port for streaming color image"))
87  {
88  yCError(RGBDSENSORCLIENT) << "Missing 'remoteImagePort' parameter. Check you configuration file; it must be like:";
89  yCError(RGBDSENSORCLIENT) << " remoteImagePort: Full name of the port to read color images from, e.g. /robotName/image_camera";
90  return false;
91  }
92 
93  remote_colorFrame_StreamingPort_name = config.find("remoteImagePort").asString();
94 
95  if (!config.check("remoteDepthPort", "full name of the port for streaming depth image"))
96  {
97  yCError(RGBDSENSORCLIENT) << "Missing 'remoteDepthPort' parameter. Check you configuration file; it must be like:";
98  yCError(RGBDSENSORCLIENT) << " remoteDepthPort: Full name of the port to read depth images from, e.g. /robotName/depth_camera ";
99  return false;
100  }
101 
102  remote_depthFrame_StreamingPort_name = config.find("remoteDepthPort").asString();
103 
104  // Single RPC port
105  if (!config.check("localRpcPort", "full name of the port for streaming depth image"))
106  {
107  yCError(RGBDSENSORCLIENT) << "Missing 'localRpcPort' parameter. Check you configuration file; it must be like:";
108  yCError(RGBDSENSORCLIENT) << " localRpcPort: Full name of the local RPC port to open, e.g. /myApp/RGBD/rpc";
109  return false;
110  }
111 
112  local_rpcPort_name = config.find("localRpcPort").asString();
113 
114  if (!config.check("remoteRpcPort", "full name of the port for streaming depth image"))
115  {
116  yCError(RGBDSENSORCLIENT) << "Missing 'remoteRpcPort' parameter. Check you configuration file; it must be like:";
117  yCError(RGBDSENSORCLIENT) << " remoteRpcPort: Full name of the remote RPC port, e.g. /robotName/RGBD/rpc";
118  return false;
119  }
120 
121  remote_rpcPort_name = config.find("remoteRpcPort").asString();
122 
123  image_carrier_type = "udp";
124  depth_carrier_type = "udp";
125 
126  if (config.check("ImageCarrier", "carrier for the image stream"))
127  {
128  image_carrier_type = config.find("ImageCarrier").asString();
129  }
130 
131  if (config.check("DepthCarrier", "carrier for the depth tream"))
132  {
133  depth_carrier_type = config.find("DepthCarrier").asString();
134  }
135 
136  /*
137  * When using multiple RPC ports
138  *
139  local_colorFrame_rpcPort_Name = local_colorFrame_StreamingPort_Name + "/rpc:i";
140  remote_colorFrame_rpcPort_Name = remote_colorFrame_StreamingPort_Name + "/rpc:i";
141  local_depthFrame_rpcPort_Name = local_depthFrame_StreamingPort_Name + "/rpc:i";
142  remote_depthFrame_rpcPort_Name = remote_depthFrame_StreamingPort_Name + "/rpc:i";
143 
144  */
145 
146  return true;
147 }
148 
150 {
151  bool ret;
152 
153  // Opening Streaming ports
154  ret = colorFrame_StreamingPort.open(local_colorFrame_StreamingPort_name);
155  ret &= depthFrame_StreamingPort.open(local_depthFrame_StreamingPort_name);
156 
157  if(!ret)
158  {
159  yCError(RGBDSENSORCLIENT) << sensorId << " cannot open local streaming ports.";
160  colorFrame_StreamingPort.close();
161  depthFrame_StreamingPort.close();
162  }
163 
164  if(! yarp::os::Network::connect(remote_colorFrame_StreamingPort_name, colorFrame_StreamingPort.getName(), image_carrier_type) )
165  {
166  yCError(RGBDSENSORCLIENT) << colorFrame_StreamingPort.getName() << " cannot connect to remote port " << remote_colorFrame_StreamingPort_name << "with carrier " << image_carrier_type;
167  return false;
168  }
169 
170  if(! yarp::os::Network::connect(remote_depthFrame_StreamingPort_name, depthFrame_StreamingPort.getName(), depth_carrier_type) )
171  {
172  yCError(RGBDSENSORCLIENT) << depthFrame_StreamingPort.getName() << " cannot connect to remote port " << remote_depthFrame_StreamingPort_name << "with carrier " << depth_carrier_type;
173  return false;
174  }
175 
176 
177  // Single RPC port
178  ret = rpcPort.open(local_rpcPort_name);
179 
180  if(!ret)
181  {
182  yCError(RGBDSENSORCLIENT) << sensorId << " cannot open local RPC port " << local_rpcPort_name;
183  colorFrame_StreamingPort.close();
184  depthFrame_StreamingPort.close();
185  rpcPort.close();
186  }
187 
188  if(! rpcPort.addOutput(remote_rpcPort_name) )
189  {
190  yCError(RGBDSENSORCLIENT) << sensorId << " cannot connect to port " << remote_rpcPort_name;
191  colorFrame_StreamingPort.close();
192  depthFrame_StreamingPort.close();
193  rpcPort.close();
194  return false;
195  }
196 
197  // Check protocol version
198  yarp::os::Bottle cmd;
199  yarp::os::Bottle response;
201  cmd.addVocab32(VOCAB_GET);
203  rpcPort.write(cmd, response);
204  int major = response.get(3).asInt32();
205  int minor = response.get(4).asInt32();
206 
208  {
209  yCError(RGBDSENSORCLIENT) << "Major protocol number does not match, please verify client and server are updated. \
210  Expected: " << RGBD_INTERFACE_PROTOCOL_VERSION_MAJOR << " received: " << major;
211  return false;
212  }
213 
214 
216  {
217  yCWarning(RGBDSENSORCLIENT) << "Minor protocol number does not match, please verify client and server are updated.\
218  Expected: " << RGBD_INTERFACE_PROTOCOL_VERSION_MINOR << " received: " << minor;
219  }
220 
221  /*
222  * Multiple RPC ports
223  *
224  ret &= colorFrame_rpcPort.open(local_colorFrame_rpcPort_Name.c_str() );
225  ret &= depthFrame_rpcPort.open(local_depthFrame_rpcPort_Name.c_str() );
226 
227  if(!ret)
228  yCError(RGBDSENSORCLIENT) << "sensorId cannot open ports";
229 
230  // doing connections: How to correctly handle YARP_PORT_PREFIX for remote port names??
231  if(! colorFrame_rpcPort.addOutput(remote_colorFrame_rpcPort_Name.c_str()) ) // This will handle local port names only
232  {
233  yCError(RGBDSENSORCLIENT) << sensorId << " cannot add output " << remote_colorFrame_rpcPort_Name;
234  return false;
235  }
236 
237  if(! depthFrame_rpcPort.addOutput(remote_depthFrame_rpcPort_Name.c_str()) ) // This will handle local port names only
238  {
239  yCError(RGBDSENSORCLIENT) << sensorId << " cannot add output " << remote_depthFrame_rpcPort_Name;
240  return false;
241  }
242  */
243 
244 
245  streamingReader->attach(&colorFrame_StreamingPort, &depthFrame_StreamingPort);
246 
247  return true;
248 }
249 
251 {
252  colorFrame_StreamingPort.close();
253  depthFrame_StreamingPort.close();
254  rpcPort.close();
255  return true;
256 }
257 
258 /*
259  * IDepthVisualParams interface. Look at IVisualParams.h for documentation
260  *
261  * Implemented by DepthVisualParams_Forwarder
262  */
263 
264 /*
265  * IDepthVisualParams interface. Look at IVisualParams.h for documentation
266  *
267  * Implemented by DepthVisualParams_Forwarder
268  */
269 
270 
271 /*
272  * IRGBDSensor specific interface methods
273  */
274 
276 {
277  yarp::os::Bottle cmd;
278  yarp::os::Bottle response;
280  cmd.addVocab32(VOCAB_GET);
282  rpcPort.write(cmd, response);
283 
284  // Minimal check on response, we suppose the response is always correctly formatted
285  if((response.get(0).asVocab32()) == VOCAB_FAILED)
286  {
287  extrinsic.zero();
288  return false;
289  }
290 
291  return Property::copyPortable(response.get(3), extrinsic); // will it really work??
292 }
293 
294 
296 {
297  yarp::os::Bottle cmd;
298  yarp::os::Bottle response;
300  cmd.addVocab32(VOCAB_GET);
302  rpcPort.write(cmd, response);
303  return static_cast<IRGBDSensor::RGBDSensor_status>(response.get(3).asInt32());
304 }
305 
306 
308 {
309  yarp::os::Bottle cmd;
310  yarp::os::Bottle response;
312  cmd.addVocab32(VOCAB_GET);
314  rpcPort.write(cmd, response);
315  return response.get(3).asString();
316 }
317 
319 {
320  return streamingReader->readRgb(rgbImage, timeStamp);
321 }
322 
324 {
325  return streamingReader->readDepth(depthImage, timeStamp);
326 }
327 
329 {
330  return streamingReader->read(rgbImage,
331  depthImage,
332  rgbStamp,
333  depthStamp);
334 }
335 
336 //
337 // IFrame Grabber Control 2 interface is implemented by FrameGrabberControls2_Forwarder
338 //
339 
340 //
341 // Rgb
342 //
344 {
345  return RgbMsgSender->getRgbHeight();
346 }
347 
349 {
350  return RgbMsgSender->getRgbWidth();
351 }
352 
354 {
355  return RgbMsgSender->getRgbSupportedConfigurations(configurations);
356 }
357 
358 bool RGBDSensorClient::getRgbResolution(int &width, int &height)
359 {
360  return RgbMsgSender->getRgbResolution(width, height);
361 }
362 
363 bool RGBDSensorClient::setRgbResolution(int width, int height)
364 {
365  return RgbMsgSender->setRgbResolution(width, height);
366 }
367 
368 bool RGBDSensorClient::getRgbFOV(double &horizontalFov, double &verticalFov)
369 {
370  return RgbMsgSender->getRgbFOV(horizontalFov, verticalFov);
371 }
372 
373 bool RGBDSensorClient::setRgbFOV(double horizontalFov, double verticalFov)
374 {
375  return RgbMsgSender->getRgbFOV(horizontalFov, verticalFov);
376 }
377 
379 {
380  return RgbMsgSender->getRgbIntrinsicParam(intrinsic);
381 }
382 
384 {
385  return RgbMsgSender->getRgbMirroring(mirror);
386 }
387 
389 {
390  return RgbMsgSender->setRgbMirroring(mirror);
391 }
392 
393 //
394 // Depth
395 //
397 {
398  return DepthMsgSender->getDepthHeight();
399 }
400 
402 {
403  return DepthMsgSender->getDepthWidth();
404 }
405 
406 bool RGBDSensorClient::setDepthResolution(int width, int height)
407 {
408  return DepthMsgSender->setDepthResolution(width, height);
409 }
410 
411 bool RGBDSensorClient::getDepthFOV(double &horizontalFov, double &verticalFov)
412 {
413  return DepthMsgSender->getDepthFOV(horizontalFov, verticalFov);
414 }
415 
416 bool RGBDSensorClient::setDepthFOV(double horizontalFov, double verticalFov)
417 {
418  return DepthMsgSender->setDepthFOV(horizontalFov, verticalFov);
419 }
420 
422 {
423  return DepthMsgSender->getDepthAccuracy();
424 }
425 
427 {
428  return DepthMsgSender->setDepthAccuracy(accuracy);
429 }
430 
431 bool RGBDSensorClient::getDepthClipPlanes(double &nearPlane, double &farPlane)
432 {
433  return DepthMsgSender->getDepthClipPlanes(nearPlane, farPlane);
434 }
435 
436 bool RGBDSensorClient::setDepthClipPlanes(double nearPlane, double farPlane)
437 {
438  return DepthMsgSender->setDepthClipPlanes(nearPlane, farPlane);
439 }
440 
442 {
443  return DepthMsgSender->getDepthIntrinsicParam(intrinsic);
444 }
445 
447 {
448  return DepthMsgSender->getDepthMirroring(mirror);
449 }
450 
452 {
453  return DepthMsgSender->setDepthMirroring(mirror);
454 }
constexpr yarp::conf::vocab32_t VOCAB_RGBD_SENSOR
Definition: CameraVocabs.h:20
constexpr yarp::conf::vocab32_t VOCAB_EXTRINSIC_PARAM
Definition: CameraVocabs.h:114
constexpr yarp::conf::vocab32_t VOCAB_RGBD_PROTOCOL_VERSION
Definition: CameraVocabs.h:21
constexpr yarp::conf::vocab32_t VOCAB_STATUS
Definition: CameraVocabs.h:119
constexpr yarp::conf::vocab32_t VOCAB_ERROR_MSG
Definition: CameraVocabs.h:115
constexpr yarp::conf::vocab32_t VOCAB_GET
Definition: GenericVocabs.h:13
constexpr yarp::conf::vocab32_t VOCAB_FAILED
Definition: GenericVocabs.h:16
bool ret
const yarp::os::LogComponent & RGBDSENSORCLIENT()
#define RGBD_INTERFACE_PROTOCOL_VERSION_MINOR
#define RGBD_INTERFACE_PROTOCOL_VERSION_MAJOR
RGBDSensorClient: A Network client to receive data from kinect-like devices. This device will read fr...
bool getRgbResolution(int &width, int &height) override
Get the resolution of the rgb image from the camera.
bool setRgbFOV(double horizontalFov, double verticalFov) override
Set the field of view (FOV) of the rgb camera.
int getDepthWidth() override
Return the height of each frame.
bool getRgbMirroring(bool &mirror) override
Get the mirroring setting of the sensor.
bool setDepthMirroring(bool mirror) override
Set the mirroring setting of the sensor.
std::string getLastErrorMsg(yarp::os::Stamp *timeStamp=nullptr) override
Return an error message in case of error.
bool getDepthClipPlanes(double &near, double &far) override
Get the clipping planes of the sensor.
bool setRgbResolution(int width, int height) override
Set the resolution of the rgb image from the camera.
bool setDepthAccuracy(double accuracy) override
Set the minimum detectable variation in distance [meter] when possible.
bool getRgbSupportedConfigurations(yarp::sig::VectorOf< yarp::dev::CameraConfig > &configurations) override
Get the possible configurations of the camera.
int getRgbWidth() override
Return the width of each frame.
bool setDepthResolution(int width, int height) override
Set the resolution of the depth image from the camera.
bool getDepthIntrinsicParam(yarp::os::Property &intrinsic) override
Get the intrinsic parameters of the depth camera.
bool getDepthFOV(double &horizontalFov, double &verticalFov) override
Get the field of view (FOV) of the depth camera.
bool initialize_YARP(yarp::os::Searchable &config)
bool getImages(yarp::sig::FlexImage &colorFrame, yarp::sig::ImageOf< yarp::sig::PixelFloat > &depthFrame, yarp::os::Stamp *colorStamp=nullptr, yarp::os::Stamp *depthStamp=nullptr) override
Get the both the color and depth frame in a single call.
bool setDepthClipPlanes(double near, double far) override
Set the clipping planes of the sensor.
bool getDepthMirroring(bool &mirror) override
Get the mirroring setting of the sensor.
bool getDepthImage(yarp::sig::ImageOf< yarp::sig::PixelFloat > &depthImage, yarp::os::Stamp *timeStamp=nullptr) override
Get the depth frame from the device.
bool getRgbFOV(double &horizontalFov, double &verticalFov) override
Get the field of view (FOV) of the rgb camera.
~RGBDSensorClient() override
bool close() override
Close the DeviceDriver.
double getDepthAccuracy() override
Get the minimum detectable variation in distance [meter].
bool getRgbImage(yarp::sig::FlexImage &rgbImage, yarp::os::Stamp *timeStamp=nullptr) override
Get the rgb frame from the device.
bool fromConfig(yarp::os::Searchable &config)
bool setDepthFOV(double horizontalFov, double verticalFov) override
Set the field of view (FOV) of the depth camera.
bool getExtrinsicParam(yarp::sig::Matrix &extrinsic) override
Get the extrinsic parameters from the device.
int getDepthHeight() override
Return the height of each frame.
IRGBDSensor::RGBDSensor_status getSensorStatus() override
Get the surrent status of the sensor, using enum type.
bool getRgbIntrinsicParam(yarp::os::Property &intrinsic) override
Get the intrinsic parameters of the rgb camera.
bool open(yarp::os::Searchable &config) override
Create and configure a device, by name.
int getRgbHeight() override
Return the height of each frame.
bool setRgbMirroring(bool mirror) override
Set the mirroring setting of the sensor.
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
void addVocab32(yarp::conf::vocab32_t x)
Places a vocabulary item in the bottle, at the end of the list.
Definition: Bottle.cpp:164
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:246
static bool connect(const std::string &src, const std::string &dest, const std::string &carrier="", bool quiet=true)
Request that an output port connect to an input port.
Definition: Network.cpp:682
static bool copyPortable(const PortWriter &writer, PortReader &reader)
Copy one portable to another, via writing and reading.
Definition: Portable.cpp:16
A class for storing options and configuration information.
Definition: Property.h:34
A base class for nested structures that can be searched.
Definition: Searchable.h:66
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:22
virtual yarp::conf::vocab32_t asVocab32() const
Get vocabulary identifier as an integer.
Definition: Value.cpp:228
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition: Value.cpp:204
virtual std::string asString() const
Get string value.
Definition: Value.cpp:234
Image class with user control of representation details.
Definition: Image.h:414
A class for a Matrix.
Definition: Matrix.h:43
void zero()
Zero the matrix.
Definition: Matrix.cpp:323
Provides:
Definition: Vector.h:119
#define yCError(component,...)
Definition: LogComponent.h:154
#define yCTrace(component,...)
Definition: LogComponent.h:85
#define yCWarning(component,...)
Definition: LogComponent.h:143
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:77
An interface for the device drivers.
An interface to the operating system, including Port based communication.
Signal processing.
Definition: Image.h:22
The main, catch-all namespace for YARP.
Definition: dirs.h:16