YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
laserFromDepth.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6#define _USE_MATH_DEFINES
7
8#include "laserFromDepth.h"
9
10#include <yarp/os/Time.h>
11#include <yarp/os/Log.h>
12#include <yarp/os/LogStream.h>
14
15#include <cmath>
16#include <cstring>
17#include <cstdlib>
18#include <iostream>
19#include <limits>
20#include <mutex>
21
22
23#ifndef DEG2RAD
24#define DEG2RAD M_PI/180.0
25#endif
26
27YARP_LOG_COMPONENT(LASER_FROM_DEPTH, "yarp.devices.laserFromDepth")
28
29//-------------------------------------------------------------------------------------
30
32{
34 m_info = "LaserFromDepth device";
35 m_device_status = DEVICE_OK_STANDBY;
36
37#ifdef LASER_DEBUG
38 yCDebug(LASER_FROM_DEPTH) << "%s\n", config.toString().c_str();
39#endif
40
41 m_min_distance = 0.1; //m
42 m_max_distance = 2.5; //m
43 m_min_angle = 0;
44 m_max_angle = 60;
45 m_resolution = 1;
46
47 if (this->parseConfiguration(config) == false)
48 {
49 yCError(LASER_FROM_DEPTH) << "error parsing parameters";
50 return false;
51 }
52
53 Property prop;
54 if(!config.check("RGBD_SENSOR_CLIENT"))
55 {
56 yCError(LASER_FROM_DEPTH) << "missing RGBD_SENSOR_CLIENT section in configuration file!";
57 return false;
58 }
59 prop.fromString(config.findGroup("RGBD_SENSOR_CLIENT").toString());
60 prop.put("device", "RGBDSensorClient");
61
62 driver.open(prop);
63 if (!driver.isValid())
64 {
65 yCError(LASER_FROM_DEPTH) << "Error opening PolyDriver check parameters";
66 return false;
67 }
68 driver.view(iRGBD);
69 if (!iRGBD)
70 {
71 yCError(LASER_FROM_DEPTH) << "Error opening iRGBD interface. Device not available";
72 return false;
73 }
74
75 m_depth_width = iRGBD->getDepthWidth();
76 m_depth_height = iRGBD->getDepthHeight();
77 double hfov = 0;
78 double vfov = 0;
79 iRGBD->getDepthFOV(hfov, vfov);
80 m_sensorsNum = m_depth_width;
81 m_resolution = hfov / m_depth_width;
82 m_laser_data.resize(m_sensorsNum, 0.0);
83 m_max_angle = +hfov / 2;
84 m_min_angle = -hfov / 2;
86
87 yCInfo(LASER_FROM_DEPTH) << "Sensor ready";
88 return true;
89}
90
92{
94
95 if (driver.isValid()) {
96 driver.close();
97 }
98
99 yCInfo(LASER_FROM_DEPTH) << "closed";
100 return true;
101}
102
104{
105 std::lock_guard<std::mutex> guard(m_mutex);
106 m_min_distance = min;
107 m_max_distance = max;
108 return ReturnValue_ok;
109}
110
112{
113 std::lock_guard<std::mutex> guard(m_mutex);
114 yCWarning(LASER_FROM_DEPTH) << "setScanLimits not yet implemented";
115 return ReturnValue_ok;
116}
117
119{
120 std::lock_guard<std::mutex> guard(m_mutex);
121 yCWarning(LASER_FROM_DEPTH) << "setHorizontalResolution not yet implemented";
122 return ReturnValue_ok;
123}
124
126{
127 std::lock_guard<std::mutex> guard(m_mutex);
128 yCWarning(LASER_FROM_DEPTH) << "setScanRate not yet implemented";
129 return ReturnValue::return_code::return_value_error_not_implemented_by_device;
130}
131
133{
134#ifdef LASER_DEBUG
135 yCDebug(LASER_FROM_DEPTH) << "thread initialising...\n";
136 yCDebug(LASER_FROM_DEPTH) << "... done!\n";
137#endif
138
139 return true;
140}
141
143{
144#ifdef DEBUG_TIMING
145 double t1 = yarp::os::Time::now();
146#endif
147
149 if (m_depth_image.getRawImage() == nullptr)
150 {
151 yCError(LASER_FROM_DEPTH) << "invalid image received";
152 return false;
153 }
154
157 {
158 yCError(LASER_FROM_DEPTH) << "invalid image size";
159 return false;
160 }
161
162
163 auto* pointer = (float*)m_depth_image.getPixelAddress(0, m_depth_height / 2);
164 double angle, distance, angleShift;
165
167
168 for (size_t elem = 0; elem < m_sensorsNum; elem++)
169 {
170 angle = elem * m_resolution; //deg
171 //the 1 / cos(blabla) distortion simulate the way RGBD devices calculate the distance..
172 distance = *(pointer + elem);
173 distance /= cos((angle - angleShift) * DEG2RAD); //m
175 }
176
177 return true;
178}
179
181{
182 m_mutex.lock();
184 m_mutex.unlock();
185 return;
186}
187
189{
190#ifdef LASER_DEBUG
191 yCDebug(LASER_FROM_DEPTH)<<"LaserFromDepth Thread releasing...";
192 yCDebug(LASER_FROM_DEPTH) <<"... done.";
193#endif
194
195 return;
196}
#define DEG2RAD
#define ReturnValue_ok
Definition ReturnValue.h:77
laserFromDepth: This devices connects to an RGBDSensor_nws_yarp to receive depth data.
PolyDriver driver
yarp::dev::ReturnValue setScanRate(double rate) override
set the scan rate (scans per seconds)
void run() override
Loop function.
IRGBDSensor * iRGBD
bool acquireDataFromHW() override final
This method should be implemented by the user, and contain the logic to grab data from the hardware.
yarp::dev::ReturnValue setHorizontalResolution(double step) override
get the angular step between two measurements (if available)
bool close() override
Close the DeviceDriver.
yarp::dev::ReturnValue setScanLimits(double min, double max) override
set the scan angular range.
void threadRelease() override
Release method.
yarp::dev::ReturnValue setDistanceRange(double min, double max) override
set the device detection range.
bool threadInit() override
Initialization method.
yarp::sig::ImageOf< float > m_depth_image
virtual bool getDepthImage(yarp::sig::ImageOf< yarp::sig::PixelFloat > &depthImage, yarp::os::Stamp *timeStamp=nullptr)=0
Get the depth frame from the device.
virtual bool updateLidarData()
This utility method calls in sequence: grabDataFromHW(), updateTimestamp and applyLimitsOnLaserData()...
bool close() override
Close the DeviceDriver.
bool isValid() const
Check if device is valid.
A mini-server for performing network communication in the background.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
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...
A class for storing options and configuration information.
Definition Property.h:33
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition Property.cpp:987
A base class for nested structures that can be searched.
Definition Searchable.h:31
size_t width() const
Gets width of image in pixels.
Definition Image.h:171
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition Image.cpp:479
size_t height() const
Gets height of image in pixels.
Definition Image.h:177
unsigned char * getPixelAddress(size_t x, size_t y) const
Get address of a pixel in memory.
Definition Image.h:245
const yarp::os::LogComponent & LASER_FROM_DEPTH()
#define yCInfo(component,...)
#define yCError(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition Time.cpp:121
The main, catch-all namespace for YARP.
Definition dirs.h:16