YARP
Yet Another Robot Platform
RGBDSensorClient_StreamingMsgParser.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
11 
12 #include <yarp/os/Time.h>
13 
14 using namespace yarp::dev;
15 
16 // Callback reader for rgb
18 {
19  std::lock_guard<std::mutex> lock(mutex);
20  local_arrival_time = yarp::os::Time::now();
21  std::swap(datum, last_rgb);
22  getEnvelope(stamp);
23 }
24 
25 std::tuple<bool, yarp::sig::FlexImage, yarp::os::Stamp> RgbImageBufferedPort::getImage() const
26 {
27  std::lock_guard<std::mutex> lock(mutex);
28  if (local_arrival_time <= 0.0) {
29  // No image received yet
30  // FIXME C++17:
31  // return {false, yarp::sig::FlexImage(), yarp::os::Stamp()};
32  // or perhaps just return {false, {}, {}}; ?
33  return std::make_tuple(false, yarp::sig::FlexImage(), yarp::os::Stamp());
34  }
35  // FIXME C++17:
36  // return {true, last_rgb, stamp};
37  return std::make_tuple(true, last_rgb, stamp);
38 }
39 
40 
41 // callback reader for depthImage
43 {
44  std::lock_guard<std::mutex> lock(mutex);
45  local_arrival_time = yarp::os::Time::now();
46  std::swap(datum, last_depth);
47  getEnvelope(stamp);
48 }
49 
50 std::tuple<bool, yarp::sig::ImageOf<yarp::sig::PixelFloat>, yarp::os::Stamp> FloatImageBufferedPort::getImage() const
51 {
52  std::lock_guard<std::mutex> lock(mutex);
53  if (local_arrival_time <= 0.0) {
54  // No image received yet
55  // FIXME C++17:
56  // return {false, yarp::sig::ImageOf<yarp::sig::PixelFloat>(), yarp::os::Stamp()};
57  // or perhaps just return {false, {}, {}}; ?
58  return std::make_tuple(false, yarp::sig::ImageOf<yarp::sig::PixelFloat>(), yarp::os::Stamp());
59 
60  }
61  // FIXME C++17:
62  // return {true, last_depth, stamp};
63  return std::make_tuple(true, last_depth, stamp);
64 }
65 
66 
67 // Streaming handler
69 {
70  auto result = port_rgb->getImage();
71 
72  if (!std::get<0>(result)) {
73  return false;
74  }
75 
76  data = std::get<1>(result);
77  if (timeStamp) {
78  *timeStamp = std::get<2>(result);
79  }
80 
81  return true;
82 }
83 
85 {
86  auto result = port_depth->getImage();
87 
88  if (!std::get<0>(result)) {
89  return false;
90  }
91 
92  data = std::get<1>(result);
93  if(timeStamp) {
94  *timeStamp = std::get<2>(result);
95  }
96 
97  return true;
98 }
99 
101 {
102  auto resultRgb = port_rgb->getImage();
103  auto resultDepth = port_depth->getImage();
104 
105  bool retRgb = std::get<0>(resultRgb);
106  bool retDepth = std::get<0>(resultDepth);
107 
108  if (!retRgb || !retDepth) {
109  return false;
110  }
111 
112  rgbImage = std::get<1>(resultRgb);
113  depthImage = std::get<1>(resultDepth);
114  if(rgbStamp) {
115  port_rgb->getEnvelope(*rgbStamp);
116  }
117 
118  if(depthStamp) {
119  port_depth->getEnvelope(*depthStamp);
120  }
121  return true;
122 }
123 
125  FloatImageBufferedPort* _port_depth)
126 {
127  port_rgb = _port_rgb;
128  port_depth = _port_depth;
129  port_rgb->useCallback();
130  port_depth->useCallback();
131 }
RGBDSensor_StreamingMsgParser::read
bool read(yarp::sig::FlexImage &rgbImage, yarp::sig::ImageOf< yarp::sig::PixelFloat > &depthImage, yarp::os::Stamp *rgbStamp=nullptr, yarp::os::Stamp *depthStamp=nullptr)
Definition: RGBDSensorClient_StreamingMsgParser.cpp:100
FloatImageBufferedPort
Definition: RGBDSensorClient_StreamingMsgParser.h:44
yarp::os::BufferedPort::useCallback
void useCallback(TypedReaderCallback< T > &callback) override
Set an object whose onRead method will be called when data is available.
Definition: BufferedPort-inl.h:211
yarp::os::Time::now
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:124
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
yarp::sig::ImageOf< yarp::sig::PixelFloat >
RGBDSensor_StreamingMsgParser::attach
void attach(RgbImageBufferedPort *_port_rgb, FloatImageBufferedPort *_port_depth)
Definition: RGBDSensorClient_StreamingMsgParser.cpp:124
RGBDSensorClient_StreamingMsgParser.h
RgbImageBufferedPort
Definition: RGBDSensorClient_StreamingMsgParser.h:25
yarp::sig::FlexImage
Image class with user control of representation details.
Definition: Image.h:403
RGBDSensor_StreamingMsgParser::readRgb
bool readRgb(yarp::sig::FlexImage &data, yarp::os::Stamp *timeStamp=nullptr)
Definition: RGBDSensorClient_StreamingMsgParser.cpp:68
FloatImageBufferedPort::getImage
std::tuple< bool, yarp::sig::ImageOf< yarp::sig::PixelFloat >, yarp::os::Stamp > getImage() const
Definition: RGBDSensorClient_StreamingMsgParser.cpp:50
yarp::os::Stamp
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:25
RgbImageBufferedPort::getImage
std::tuple< bool, yarp::sig::FlexImage, yarp::os::Stamp > getImage() const
Definition: RGBDSensorClient_StreamingMsgParser.cpp:25
RgbImageBufferedPort::onRead
void onRead(yarp::sig::FlexImage &datum) override
Definition: RGBDSensorClient_StreamingMsgParser.cpp:17
Time.h
FloatImageBufferedPort::onRead
void onRead(yarp::sig::ImageOf< yarp::sig::PixelFloat > &datum) override
Definition: RGBDSensorClient_StreamingMsgParser.cpp:42
RGBDSensor_StreamingMsgParser::readDepth
bool readDepth(yarp::sig::ImageOf< yarp::sig::PixelFloat > &data, yarp::os::Stamp *timeStamp=nullptr)
Definition: RGBDSensorClient_StreamingMsgParser.cpp:84