YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Rangefinder2DTransformer.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
9
10#include <yarp/os/Log.h>
11#include <yarp/os/LogStream.h>
13#include <yarp/math/Math.h>
15
16#include <limits>
17#include <cmath>
18
19using namespace yarp::dev;
20using namespace yarp::os;
21using namespace yarp::sig;
22
23#ifndef DEG2RAD
24#define DEG2RAD M_PI/180.0
25#endif
26
27namespace {
28YARP_LOG_COMPONENT(RANGEFINDER2DTRANSFORMER, "yarp.device.Rangefinder2DTransformer")
29}
30
32{
33 if (!parseParams(config))
34 {
35 return false;
36 }
37
38 if (!m_laser_frame_name.empty() && !m_robot_frame_name.empty())
39 {
40 // get the position of the device, if it is available
41 auto* drv = new yarp::dev::PolyDriver;
43 TransformClientOptions.put("device", "frameTransformClient");
44 TransformClientOptions.put("local", "/rangefinder2DTransformClient");
45 TransformClientOptions.put("remote", "/transformServer");
46 TransformClientOptions.put("period", 0.010);
49 {
51 drv->view(iTrf);
52 if (!iTrf)
53 {
54 yCError(RANGEFINDER2DTRANSFORMER) << "A Problem occurred while trying to view the IFrameTransform interface";
55 drv->close();
56 delete drv;
57 return false;
58 }
59
66 if (fabs(v[0]) < 1e-6 && fabs(v[1]) < 1e-6)
67 {
68 yCError(RANGEFINDER2DTRANSFORMER) << "Laser device is not planar";
69 return false;
70 }
71 yCInfo(RANGEFINDER2DTRANSFORMER) << "Position information obtained from frametransform server (x,y,t):" << m_device_position_x << " " << m_device_position_y << " " << m_device_position_theta;
72 drv->close();
73 delete drv;
74 }
75 }
76 else
77 {
78 yCInfo(RANGEFINDER2DTRANSFORMER) << "Position information obtained from configuration parameters (x,y,t):" << m_device_position_x << " " << m_device_position_y << " " << m_device_position_theta;
79 }
80
81 return true;
82}
83
85{
86 return true;
87}
88
90{
91 std::vector<LaserMeasurementData> scans;
92 double lastTs;
93
94 auto ret = sens_p->getLaserMeasurement(scans, &lastTs);
95 if (!ret) {
96 return ret;
97 }
98
99 if (timestamp != nullptr)
100 {
101 *timestamp = lastTs;
102 }
103 return ret;
104}
105
106ReturnValue Rangefinder2DTransformer::getLaserMeasurement(std::vector<LaserMeasurementData>& data, double* timestamp)
107{
108 std::vector<LaserMeasurementData> scans;
109 double lastTs;
110
111 auto ret = sens_p->getLaserMeasurement(scans,&lastTs);
112 if (!ret)
113 {
114 return ret;
115 }
116
117 size_t size = scans.size();
118 data.resize(size);
120 {
121 yCError(RANGEFINDER2DTRANSFORMER) << "getLaserMeasurement failed";
122 return ReturnValue::return_code::return_value_error_method_failed;
123 }
125 for (size_t i = 0; i < size; i++)
126 {
128 double rho;
129 double theta;
130 scans[i].get_polar(rho, theta);
131#if 1 //cartesian version is preferable, even if more computationally expensive, since it takes in account device_position
132 data[i].set_cartesian(rho * cos(angle) + m_device_position_x, rho * sin(angle) + m_device_position_y);
133#else
134 data[i].set_polar(rho, theta);
135#endif
136 }
137 if (timestamp!=nullptr)
138 {
139 *timestamp = lastTs;
140 }
141 return ReturnValue_ok;
142}
143
145{
146 return sens_p->getDistanceRange(min, max);
147}
148
150{
151 return sens_p->setDistanceRange(min, max);
152}
153
155{
156 return sens_p->getScanLimits(min, max);
157}
158
160{
161 return sens_p->setScanLimits(min, max);
162}
163
168
173
175{
176 return sens_p->getScanRate(rate);
177}
178
180{
181 return sens_p->setScanRate(rate);
182}
183
188
190{
191 return sens_p->getDeviceInfo(device_info);
192}
193
195{
196 if (driver->isValid())
197 {
198 driver->view(sens_p);
200 {
201 yCError(RANGEFINDER2DTRANSFORMER) << "getScanLimits failed";
202 return false;
203 }
204 }
205
206 if (nullptr == sens_p)
207 {
208 yCError(RANGEFINDER2DTRANSFORMER, "subdevice passed to attach method is invalid");
209 return false;
210 }
211 return true;
212}
213
215{
216 sens_p = nullptr;
217 return true;
218}
bool ret
#define DEG2RAD
#define ReturnValue_ok
Definition ReturnValue.h:77
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
bool attach(yarp::dev::PolyDriver *driver) override
Attach to another object.
yarp::dev::ReturnValue setDistanceRange(double min, double max) override
set the device detection range.
bool close() override
Close the DeviceDriver.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
yarp::dev::IRangefinder2D * sens_p
yarp::dev::ReturnValue getDistanceRange(double &min, double &max) override
get the device detection range
bool detach() override
Detach the object (you must have first called attach).
yarp::dev::ReturnValue getLaserMeasurement(std::vector< yarp::sig::LaserMeasurementData > &data, double *timestamp=nullptr) override
Get the device measurements.
yarp::dev::ReturnValue getHorizontalResolution(double &step) override
get the angular step between two measurements.
yarp::dev::ReturnValue setScanRate(double rate) override
set the scan rate (scans per seconds)
yarp::dev::ReturnValue getRawData(yarp::sig::Vector &data, double *timestamp=nullptr) override
Get the device measurements.
yarp::dev::ReturnValue setScanLimits(double min, double max) override
set the scan angular range.
yarp::dev::ReturnValue getScanRate(double &rate) override
get the scan rate (scans per seconds)
yarp::dev::ReturnValue getDeviceStatus(Device_status &status) override
get the device status
yarp::dev::ReturnValue getScanLimits(double &min, double &max) override
get the scan angular range.
yarp::dev::ReturnValue getDeviceInfo(std::string &device_info) override
get the device hardware characteristics
yarp::dev::ReturnValue setHorizontalResolution(double step) override
get the angular step between two measurements (if available)
bool view(T *&x)
Get an interface to the device driver.
Transform Interface.
virtual ReturnValue getDistanceRange(double &min, double &max)=0
get the device detection range
virtual ReturnValue setDistanceRange(double min, double max)=0
set the device detection range.
virtual ReturnValue getDeviceInfo(std::string &device_info)=0
get the device hardware characteristics
virtual ReturnValue getScanRate(double &rate)=0
get the scan rate (scans per seconds)
virtual ReturnValue setScanLimits(double min, double max)=0
set the scan angular range.
virtual ReturnValue getScanLimits(double &min, double &max)=0
get the scan angular range.
virtual ReturnValue getHorizontalResolution(double &step)=0
get the angular step between two measurements.
virtual ReturnValue getDeviceStatus(Device_status &status)=0
get the device status
virtual ReturnValue getLaserMeasurement(std::vector< yarp::sig::LaserMeasurementData > &data, double *timestamp=nullptr)=0
Get the device measurements.
virtual ReturnValue setHorizontalResolution(double step)=0
get the angular step between two measurements (if available)
virtual ReturnValue setScanRate(double rate)=0
set the scan rate (scans per seconds)
A container for a device driver.
Definition PolyDriver.h:23
bool isValid() const
Check if device is valid.
A mini-server for performing network communication in the background.
void close() override
Stop port activity.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
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
A class for a Matrix.
Definition Matrix.h:39
#define yCInfo(component,...)
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
yarp::sig::Vector dcm2rpy(const yarp::sig::Matrix &R)
Converts a dcm (direction cosine matrix) rotation matrix to roll-pitch-yaw angles (defined in Math....
Definition math.cpp:808
An interface to the operating system, including Port based communication.