YARP
Yet Another Robot Platform
laserFromDepth.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #define _USE_MATH_DEFINES
20 
21 #include "laserFromDepth.h"
22 
23 #include <yarp/os/Time.h>
24 #include <yarp/os/Log.h>
25 #include <yarp/os/LogStream.h>
26 #include <yarp/os/ResourceFinder.h>
27 
28 #include <cmath>
29 #include <cstring>
30 #include <cstdlib>
31 #include <iostream>
32 #include <limits>
33 #include <mutex>
34 
35 using namespace std;
36 
37 #ifndef DEG2RAD
38 #define DEG2RAD M_PI/180.0
39 #endif
40 
41 YARP_LOG_COMPONENT(LASER_FROM_DEPTH, "yarp.devices.laserFromDepth")
42 
43 //-------------------------------------------------------------------------------------
44 
45 bool LaserFromDepth::open(yarp::os::Searchable& config)
46 {
47  Property subConfig;
48  m_info = "LaserFromDepth device";
49  m_device_status = DEVICE_OK_STANBY;
50 
51 #ifdef LASER_DEBUG
52  yCDebug(LASER_FROM_DEPTH) << "%s\n", config.toString().c_str();
53 #endif
54 
55  m_min_distance = 0.1; //m
56  m_max_distance = 2.5; //m
57 
58  if (this->parseConfiguration(config) == false)
59  {
60  yCError(LASER_FROM_DEPTH) << "error parsing parameters";
61  return false;
62  }
63 
64  Property prop;
65  if(!config.check("RGBD_SENSOR_CLIENT"))
66  {
67  yCError(LASER_FROM_DEPTH) << "missing RGBD_SENSOR_CLIENT section in configuration file!";
68  return false;
69  }
70  prop.fromString(config.findGroup("RGBD_SENSOR_CLIENT").toString());
71  prop.put("device", "RGBDSensorClient");
72 
73  driver.open(prop);
74  if (!driver.isValid())
75  {
76  yCError(LASER_FROM_DEPTH) << "Error opening PolyDriver check parameters";
77  return false;
78  }
79  driver.view(iRGBD);
80  if (!iRGBD)
81  {
82  yCError(LASER_FROM_DEPTH) << "Error opening iRGBD interface. Device not available";
83  return false;
84  }
85 
86  m_depth_width = iRGBD->getDepthWidth();
87  m_depth_height = iRGBD->getDepthHeight();
88  double hfov = 0;
89  double vfov = 0;
90  iRGBD->getDepthFOV(hfov, vfov);
91  m_sensorsNum = m_depth_width;
92  m_resolution = hfov / m_depth_width;
93  m_laser_data.resize(m_sensorsNum, 0.0);
94  m_max_angle = +hfov / 2;
95  m_min_angle = -hfov / 2;
96  PeriodicThread::start();
97 
98  yCInfo(LASER_FROM_DEPTH) << "Sensor ready";
99  return true;
100 }
101 
103 {
104  PeriodicThread::stop();
105 
106  if(driver.isValid())
107  driver.close();
108 
109  yCInfo(LASER_FROM_DEPTH) << "closed";
110  return true;
111 }
112 
113 bool LaserFromDepth::setDistanceRange(double min, double max)
114 {
115  std::lock_guard<std::mutex> guard(m_mutex);
116  m_min_distance = min;
117  m_max_distance = max;
118  return true;
119 }
120 
121 bool LaserFromDepth::setScanLimits(double min, double max)
122 {
123  std::lock_guard<std::mutex> guard(m_mutex);
124  yCWarning(LASER_FROM_DEPTH) << "setScanLimits not yet implemented";
125  return true;
126 }
127 
129 {
130  std::lock_guard<std::mutex> guard(m_mutex);
131  yCWarning(LASER_FROM_DEPTH) << "setHorizontalResolution not yet implemented";
132  return true;
133 }
134 
136 {
137  std::lock_guard<std::mutex> guard(m_mutex);
138  yCWarning(LASER_FROM_DEPTH) << "setScanRate not yet implemented";
139  return false;
140 }
141 
143 {
144 #ifdef LASER_DEBUG
145  yCDebug(LASER_FROM_DEPTH) << "thread initialising...\n";
146  yCDebug(LASER_FROM_DEPTH) << "... done!\n";
147 #endif
148 
149  return true;
150 }
151 
153 {
154 #ifdef DEBUG_TIMING
155  double t1 = yarp::os::Time::now();
156 #endif
157  std::lock_guard<std::mutex> guard(m_mutex);
158 
159  iRGBD->getDepthImage(m_depth_image);
160  if (m_depth_image.getRawImage()==nullptr)
161  {
162  yCDebug(LASER_FROM_DEPTH)<<"invalid image received";
163  return;
164  }
165 
166  if (m_depth_image.width()!=m_depth_width ||
167  m_depth_image.height()!=m_depth_height)
168  {
169  yCDebug(LASER_FROM_DEPTH)<<"invalid image size";
170  return;
171  }
172 
173 
174  auto* pointer = (float*)m_depth_image.getPixelAddress(0, m_depth_height / 2);
175  double angle, distance, angleShift;
176 
177  angleShift = m_sensorsNum * m_resolution / 2;
178 
179  for (size_t elem = 0; elem < m_sensorsNum; elem++)
180  {
181  angle = elem * m_resolution; //deg
182  //the 1 / cos(blabla) distortion simulate the way RGBD devices calculate the distance..
183  distance = *(pointer + elem);
184  distance /= cos((angle - angleShift) * DEG2RAD); //m
185  m_laser_data[m_sensorsNum - 1 - elem] = distance;
186  }
187  applyLimitsOnLaserData();
188 
189  return;
190 }
191 
193 {
194 #ifdef LASER_DEBUG
195  yCDebug(LASER_FROM_DEPTH)<<"LaserFromDepth Thread releasing...";
196  yCDebug(LASER_FROM_DEPTH) <<"... done.";
197 #endif
198 
199  return;
200 }
void run() override
Loop function.
bool close() override
Close the DeviceDriver.
bool setScanLimits(double min, double max) override
set the scan angular range.
bool setScanRate(double rate) override
set the scan rate (scans per seconds)
void threadRelease() override
Release method.
bool threadInit() override
Initialization method.
bool setDistanceRange(double min, double max) override
set the device detection range.
bool setHorizontalResolution(double step) override
get the angular step between two measurments (if available)
A class for storing options and configuration information.
Definition: Property.h:37
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
Definition: Property.cpp:1046
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition: Property.cpp:998
A base class for nested structures that can be searched.
Definition: Searchable.h:69
const yarp::os::LogComponent & LASER_FROM_DEPTH()
#define DEG2RAD
#define yCInfo(component,...)
Definition: LogComponent.h:135
#define yCError(component,...)
Definition: LogComponent.h:157
#define yCWarning(component,...)
Definition: LogComponent.h:146
#define yCDebug(component,...)
Definition: LogComponent.h:112
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:124
The main, catch-all namespace for YARP.
Definition: environment.h:18