YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
RGBDSensor_nws_yarp.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
7
9#include <yarp/os/LogStream.h>
10
11#include <cstdio>
12#include <cstring>
13#include <sstream>
14
15using namespace RGBDImpl;
16using namespace yarp::sig;
17using namespace yarp::dev;
18using namespace yarp::os;
19
20YARP_LOG_COMPONENT(RGBDSENSORNWSYARP, "yarp.devices.RgbdSensor_nws_yarp")
21
24 m_sensorStatus(IRGBDSensor::RGBD_SENSOR_NOT_READY),
25 verbose(4)
26{}
27
29{
30 close();
31 m_rgbdsensor = nullptr;
32 m_fgCtrl = nullptr;
33}
34
38{
39 if (!parseParams(config)) { return false; }
40
41 //port names
42 std::string rootName = m_name;
43 std::string rpcPort_Name = rootName + "/rpc:i";
44 colorFrame_StreamingPort_Name = rootName + "/rgbImage:o";
45 depthFrame_StreamingPort_Name = rootName + "/depthImage:o";
46
47 // Open ports
48 bool bRet;
49 bRet = true;
50 if (!rpcPort.open(rpcPort_Name))
51 {
52 yCError(RGBDSENSORNWSYARP) << "Unable to open rpc Port" << rpcPort_Name.c_str();
53 bRet = false;
54 }
55 rpcPort.setReader(*this);
56
57 if (!colorFrame_StreamingPort.open(colorFrame_StreamingPort_Name))
58 {
59 yCError(RGBDSENSORNWSYARP) << "Unable to open color streaming Port" << colorFrame_StreamingPort_Name.c_str();
60 bRet = false;
61 }
62 if (!depthFrame_StreamingPort.open(depthFrame_StreamingPort_Name))
63 {
64 yCError(RGBDSENSORNWSYARP) << "Unable to open depth streaming Port" << depthFrame_StreamingPort_Name.c_str();
65 bRet = false;
66 }
67
68 if (!bRet) {return false;}
69 return true;
70}
71
73{
74 yCTrace(RGBDSENSORNWSYARP, "Close");
75 detach();
76
77 // Closing port
78 rpcPort.interrupt();
79 colorFrame_StreamingPort.interrupt();
80 depthFrame_StreamingPort.interrupt();
81
82 rpcPort.close();
83 colorFrame_StreamingPort.close();
84 depthFrame_StreamingPort.close();
85
86 return true;
87}
88
94{
95 std::lock_guard lock (m_mutex);
96
99 }
100
101 m_rgbdsensor = nullptr;
102 m_fgCtrl = nullptr;
103 return true;
104}
105
107{
108 std::lock_guard lock (m_mutex);
109
110 if(poly)
111 {
112 poly->view(m_rgbdsensor);
113 poly->view(m_fgCtrl);
114 }
115
116 // m_rgbdsensor is mandatory
117 if(m_rgbdsensor == nullptr)
118 {
119 yCError(RGBDSENSORNWSYARP) << "Attached device has no valid IRGBDSensor interface.";
120 return false;
121 }
122
123 if(m_fgCtrl == nullptr)
124 {
125 yCWarning(RGBDSENSORNWSYARP) << "Attached device has no valid IFrameGrabberControls interface.";
126 }
127
128 // m_fgCtrl is optional and might be null
129 m_rgbd_RPC = std::make_unique<RGBDSensorMsgsImpl>(m_rgbdsensor, m_fgCtrl);
130
132 return PeriodicThread::start();
133}
134
135/* IRateThread interface */
136
138{
139 // Get interface from attached device if any.
140 return true;
141}
142
144{
145 // Detach() calls stop() which in turns calls this functions, therefore no calls to detach here!
146}
147
148bool RGBDSensor_nws_yarp::setCamInfo(const std::string& frame_id, const UInt& seq, const SensorType& sensorType)
149{
150 double phyF = 0.0;
151 double fx = 0.0;
152 double fy = 0.0;
153 double cx = 0.0;
154 double cy = 0.0;
155 double k1 = 0.0;
156 double k2 = 0.0;
157 double t1 = 0.0;
158 double t2 = 0.0;
159 double k3 = 0.0;
160 double stamp = 0.0;
161
162 std::string distModel, currentSensor;
163 UInt i;
165 std::vector<param<double> > parVector;
167 bool ok;
168
169 currentSensor = sensorType == COLOR_SENSOR ? "Rgb" : "Depth";
170 ok = sensorType == COLOR_SENSOR ? m_rgbdsensor->getRgbIntrinsicParam(camData) : m_rgbdsensor->getDepthIntrinsicParam(camData);
171
172 if (!ok)
173 {
174 yCError(RGBDSENSORNWSYARP) << "Unable to get intrinsic param from" << currentSensor << "sensor!";
175 return false;
176 }
177
178 if(!camData.check("distortionModel"))
179 {
180 yCWarning(RGBDSENSORNWSYARP) << "Missing distortion model";
181 return false;
182 }
183
184 distModel = camData.find("distortionModel").asString();
185 if (distModel != "plumb_bob")
186 {
187 yCError(RGBDSENSORNWSYARP) << "Distortion model not supported";
188 return false;
189 }
190
191 parVector.emplace_back(phyF,"physFocalLength");
192 parVector.emplace_back(fx,"focalLengthX");
193 parVector.emplace_back(fy,"focalLengthY");
194 parVector.emplace_back(cx,"principalPointX");
195 parVector.emplace_back(cy,"principalPointY");
196 parVector.emplace_back(k1,"k1");
197 parVector.emplace_back(k2,"k2");
198 parVector.emplace_back(t1,"t1");
199 parVector.emplace_back(t2,"t2");
200 parVector.emplace_back(k3,"k3");
201 parVector.emplace_back(stamp,"stamp");
202 for(i = 0; i < parVector.size(); i++)
203 {
204 par = &parVector[i];
205
206 if(!camData.check(par->parname))
207 {
208 yCWarning(RGBDSENSORNWSYARP) << "Driver has not the param:" << par->parname;
209 return false;
210 }
211 *par->var = camData.find(par->parname).asFloat64();
212 }
213
214 return true;
215}
216
217bool RGBDSensor_nws_yarp::writeData()
218{
219 if (!m_rgbdsensor->getImages(colorImage, depthImage, &colorStamp, &depthStamp))
220 {
221 return false;
222 }
223
224 static Stamp oldColorStamp = Stamp(0, 0);
225 static Stamp oldDepthStamp = Stamp(0, 0);
226 bool rgb_data_ok = true;
227 bool depth_data_ok = true;
228
229 if (((colorStamp.getTime() - oldColorStamp.getTime()) > 0) == false)
230 {
231 rgb_data_ok=false;
232 //return true;
233 }
234 else { oldColorStamp = colorStamp; }
235
236 if (((depthStamp.getTime() - oldDepthStamp.getTime()) > 0) == false)
237 {
238 depth_data_ok=false;
239 //return true;
240 }
241 else { oldDepthStamp = depthStamp; }
242
243 // TBD: We should check here somehow if the timestamp was correctly updated and, if not, update it ourselves.
244 // In the following piece of code we are uing a copy instead of setExternal() because it is safer in a multithreaded environment.
245 // Indeed colorImage and depthImage can be modified by the attached device running in a different thread.
246 if (rgb_data_ok && colorFrame_StreamingPort.getOutputCount() > 0)
247 {
248 FlexImage& yColorImage = colorFrame_StreamingPort.prepare();
249 yColorImage = colorImage;
250 colorFrame_StreamingPort.setEnvelope(colorStamp);
251 colorFrame_StreamingPort.write();
252 }
253 if (depth_data_ok && depthFrame_StreamingPort.getOutputCount() > 0)
254 {
255 ImageOf<PixelFloat>& yDepthImage = depthFrame_StreamingPort.prepare();
257 depthFrame_StreamingPort.setEnvelope(depthStamp);
258 depthFrame_StreamingPort.write();
259 }
260
261 return true;
262}
263
265{
266 std::lock_guard lock (m_mutex);
267
268 if (m_rgbdsensor!=nullptr)
269 {
270 static int i = 0;
271 ReturnValue v = m_rgbdsensor->getSensorStatus(m_sensorStatus);
272 switch (m_sensorStatus)
273 {
275 {
276 if (!writeData()) {
277 yCError(RGBDSENSORNWSYARP, "Image not captured.. check hardware configuration");
278 }
279 i = 0;
280 }
281 break;
283 {
284 if(i < 1000) {
285 if((i % 30) == 0) {
286 yCInfo(RGBDSENSORNWSYARP) << "Device not ready, waiting...";
287 }
288 } else {
289 yCWarning(RGBDSENSORNWSYARP) << "Device is taking too long to start..";
290 }
291 i++;
292 }
293 break;
294 default:
295 {
296 if (verbose >= 1) { // better not to print it every cycle anyway, too noisy
297 yCError(RGBDSENSORNWSYARP, "%s: Sensor returned error", m_name.c_str());
298 }
299 }
300 }
301 }
302 else
303 {
304 if(verbose >= 6) {
305 yCError(RGBDSENSORNWSYARP, "%s: Sensor interface is not valid", m_name.c_str());
306 }
307 }
308}
309
311{
312 if (!connection.isValid()) { return false;}
313
314 std::lock_guard<std::mutex> lock(m_mutex);
315
316 if (m_rgbd_RPC)
317 {
318 if (m_rgbd_RPC->read(connection))
319 {
320 return true;
321 }
322 }
323 else
324 {
325 yCError(RGBDSENSORNWSYARP) << "m_rgbd_RPC interface is not valid";
326 return false;
327 }
328
329 yCError(RGBDSENSORNWSYARP) << "read() Command failed";
330 return false;
331}
#define DEFAULT_THREAD_PERIOD
const yarp::os::LogComponent & RGBDSENSORNWSYARP()
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
RGBDSensor_nws_yarp: A Network grabber for kinect-like devices. This device will produce two streams ...
bool attach(yarp::dev::PolyDriver *poly) override
Attach to another object.
void run() override
Loop function.
bool close() override
Close the DeviceDriver.
void threadRelease() override
Release method.
bool threadInit() override
Initialization method.
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
bool detach() override
WrapperSingle interface.
bool open(yarp::os::Searchable &params) override
Device driver interface.
bool view(T *&x)
Get an interface to the device driver.
virtual yarp::dev::ReturnValue getDepthIntrinsicParam(yarp::os::Property &intrinsic)=0
Get the intrinsic parameters of the depth camera.
A generic interface for cameras that have both color camera as well as depth camera sensor,...
Definition IRGBDSensor.h:39
virtual yarp::dev::ReturnValue getImages(yarp::sig::FlexImage &colorFrame, yarp::sig::ImageOf< yarp::sig::PixelFloat > &depthFrame, yarp::os::Stamp *colorStamp=nullptr, yarp::os::Stamp *depthStamp=nullptr)=0
Get the both the color and depth frame in a single call.
virtual yarp::dev::ReturnValue getSensorStatus(RGBDSensor_status &status)=0
Get the current status of the sensor, using enum type.
virtual yarp::dev::ReturnValue getRgbIntrinsicParam(yarp::os::Property &intrinsic)=0
Get the intrinsic parameters of the rgb camera.
A container for a device driver.
Definition PolyDriver.h:23
A mini-server for performing network communication in the background.
void close() override
Stop port activity.
bool setEnvelope(PortWriter &envelope) override
Set an envelope (e.g., a timestamp) to the next message which will be sent.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
void interrupt() override
Interrupt any current reads or writes attached to the port.
int getOutputCount() override
Determine how many output connections this port has.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
An interface for reading from a network connection.
virtual bool isValid() const =0
An abstraction for a periodic thread.
bool setPeriod(double period)
Set the (new) period of the thread.
bool isRunning() const
Returns true when the thread is started, false otherwise.
bool start()
Call this to start the thread.
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
void setReader(PortReader &reader) override
Set an external reader for port data.
Definition Port.cpp:470
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition Port.cpp:347
void close() override
Stop port activity.
Definition Port.cpp:330
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition Port.cpp:79
A class for storing options and configuration information.
Definition Property.h:33
A base class for nested structures that can be searched.
Definition Searchable.h:31
An abstraction for a time stamp and/or sequence number.
Definition Stamp.h:21
double getTime() const
Get the time stamp.
Definition Stamp.cpp:34
Image class with user control of representation details.
Definition Image.h:361
#define yCInfo(component,...)
#define yCError(component,...)
#define yCTrace(component,...)
#define yCWarning(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.