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 }
LogStream.h
yarp::os::Property::put
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition: Property.cpp:998
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
yCWarning
#define yCWarning(component,...)
Definition: LogComponent.h:146
yarp::os::Property::fromString
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
Definition: Property.cpp:1046
LaserFromDepth::threadInit
bool threadInit() override
Initialization method.
Definition: laserFromDepth.cpp:142
YARP_LOG_COMPONENT
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
LaserFromDepth::setHorizontalResolution
bool setHorizontalResolution(double step) override
get the angular step between two measurments (if available)
Definition: laserFromDepth.cpp:128
LaserFromDepth::setDistanceRange
bool setDistanceRange(double min, double max) override
set the device detection range.
Definition: laserFromDepth.cpp:113
DEG2RAD
#define DEG2RAD
Definition: laserFromDepth.cpp:38
yarp::os::Time::now
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:124
LaserFromDepth::setScanRate
bool setScanRate(double rate) override
set the scan rate (scans per seconds)
Definition: laserFromDepth.cpp:135
Log.h
LASER_FROM_DEPTH
const yarp::os::LogComponent & LASER_FROM_DEPTH()
Definition: laserFromDepth.cpp:41
LaserFromDepth::close
bool close() override
Close the DeviceDriver.
Definition: laserFromDepth.cpp:102
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
LaserFromDepth::setScanLimits
bool setScanLimits(double min, double max) override
set the scan angular range.
Definition: laserFromDepth.cpp:121
yCInfo
#define yCInfo(component,...)
Definition: LogComponent.h:135
yCDebug
#define yCDebug(component,...)
Definition: LogComponent.h:112
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
LaserFromDepth::threadRelease
void threadRelease() override
Release method.
Definition: laserFromDepth.cpp:192
LaserFromDepth
laserFromDepth: Documentation to be added
Definition: laserFromDepth.h:47
LaserFromDepth::run
void run() override
Loop function.
Definition: laserFromDepth.cpp:152
Time.h
laserFromDepth.h
ResourceFinder.h
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:37