YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Rangefinder2D_nwc_yarp.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>
12#include <yarp/math/Math.h>
13
14#include <limits>
15#include <cmath>
16
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
27#define DEFAULT_THREAD_PERIOD 20 //ms
28const int LASER_TIMEOUT=100; //ms
29
30namespace {
31YARP_LOG_COMPONENT(RANGEFINDER2DCLIENT, "yarp.device.Rangefinder2D_nwc_yarp")
32}
33
35{
36 mutex.lock();
37 count=0;
38 deltaT=0;
39 deltaTMax=0;
40 deltaTMin=1e22;
42 prev=now;
43 mutex.unlock();
44}
45
51
53{
55 mutex.lock();
56
57 if (count>0)
58 {
59 double tmpDT=now-prev;
60 deltaT+=tmpDT;
61 if (tmpDT > deltaTMax) {
62 deltaTMax = tmpDT;
63 }
64 if (tmpDT < deltaTMin) {
65 deltaTMin = tmpDT;
66 }
67
68 //compare network time
69 if (tmpDT*1000<LASER_TIMEOUT)
70 {
71 state = b.status;
72 }
73 else
74 {
76 }
77 }
78
79 prev=now;
80 count++;
81
82 lastScan=b;
85
86 //initializations (first received data)
87 if (lastStamp.isValid()==false)
88 {
89 lastStamp = newStamp;
90 }
91
92 //now compare timestamps
93 if ((1000*(newStamp.getTime()-lastStamp.getTime()))<LASER_TIMEOUT)
94 {
95 state = b.status;
96 }
97 else
98 {
100 }
101 lastStamp = newStamp;
102
103 mutex.unlock();
104}
105
107{
108 mutex.lock();
109 int ret=state;
111 {
112 data=lastScan;
113 stmp = lastStamp;
114 }
115 mutex.unlock();
116
117 return ret;
118}
119
121{
122 mutex.lock();
123 auto status = (yarp::dev::IRangefinder2D::Device_status) lastScan.status;
124 mutex.unlock();
125 return status;
126}
127
129{
130 mutex.lock();
131 int ret=count;
132 mutex.unlock();
133 return ret;
134}
135
136// time is in ms
137void Rangefinder2DInputPortProcessor::getEstFrequency(int &ite, double &av, double &min, double &max)
138{
139 mutex.lock();
140 ite=count;
141 min=deltaTMin*1000;
142 max=deltaTMax*1000;
143 if (count<1)
144 {
145 av=0;
146 }
147 else
148 {
149 av=deltaT/count;
150 }
151 av=av*1000;
152 mutex.unlock();
153}
154
156{
157 if (!parseParams(config)) { return false; }
158
159 std::string local_rpc_portname = m_local+ "/rpc:o";
160 std::string remote_rpc_portname = m_remote + "/rpc:i";
161
163 {
164 yCError(RANGEFINDER2DCLIENT, "open() error could not open port %s, check network\n", m_local.c_str());
165 return false;
166 }
168
170 {
171 yCError(RANGEFINDER2DCLIENT, "open() error could not open rpc port %s, check network\n", local_rpc_portname.c_str());
172 return false;
173 }
174
175 bool ok=Network::connect(m_remote.c_str(), m_local.c_str(), m_carrier);
176 if (!ok)
177 {
178 yCError(RANGEFINDER2DCLIENT, "open() error could not connect to %s\n", m_remote.c_str());
179 return false;
180 }
181
182 ok=Network::connect(local_rpc_portname, remote_rpc_portname);
183 if (!ok)
184 {
185 yCError(RANGEFINDER2DCLIENT, "open() error could not connect to %s\n", remote_rpc_portname.c_str());
186 return false;
187 }
188
190 {
191 yCError(RANGEFINDER2DCLIENT, "Error! Cannot attach the port as a client");
192 return false;
193 }
194
195 //getScanLimits is used here to update the cached values of scan_angle_min, scan_angle_max
197 {
198 yCError(RANGEFINDER2DCLIENT) << "getScanLimits failed";
199 return false;
200 }
201
202 return true;
203}
204
206{
210 return true;
211}
212
214{
215 std::lock_guard<std::mutex> lg(m_mutex);
218
219 data = scan.scans;
220
221 if (timestamp != nullptr)
222 {
223 *timestamp = m_lastTs.getTime();
224 }
225 return true;
226}
227
228bool Rangefinder2D_nwc_yarp::getLaserMeasurement(std::vector<LaserMeasurementData> &data, double* timestamp)
229{
230 std::lock_guard<std::mutex> lg(m_mutex);
233
234 size_t size = scan.scans.size();
235 data.resize(size);
236 if (m_scan_angle_max < m_scan_angle_min) { yCError(RANGEFINDER2DCLIENT) << "getLaserMeasurement failed"; return false; }
238 for (size_t i = 0; i < size; i++)
239 {
241 double value = scan.scans[i];
242 data[i].set_polar(value,angle);
243 }
244 if (timestamp!=nullptr)
245 {
246 *timestamp = m_lastTs.getTime();
247 }
248 return true;
249}
250
251bool Rangefinder2D_nwc_yarp::getDistanceRange(double& min, double& max)
252{
253 std::lock_guard<std::mutex> lg(m_mutex);
255 if (!ret.retval) {
256 yCError(RANGEFINDER2DCLIENT, "Unable to getDistanceRange");
257 return false;
258 }
259 min = ret.min;
260 max = ret.max;
261 return true;
262}
263
265{
266 std::lock_guard<std::mutex> lg(m_mutex);
267 auto ret = m_RPC.setDistanceRange_RPC(min, max);
268 if (!ret) {
269 yCError(RANGEFINDER2DCLIENT, "Unable to setDistanceRange");
270 return false;
271 }
272 return true;
273}
274
275bool Rangefinder2D_nwc_yarp::getScanLimits(double& min, double& max)
276{
277 std::lock_guard<std::mutex> lg(m_mutex);
278 auto ret = m_RPC.getScanLimits_RPC();
279 if (!ret.retval) {
280 yCError(RANGEFINDER2DCLIENT, "Unable to getScanLimits");
281 return false;
282 }
283 min = ret.min;
284 max = ret.max;
285 return true;
286}
287
288bool Rangefinder2D_nwc_yarp::setScanLimits(double min, double max)
289{
290 std::lock_guard<std::mutex> lg(m_mutex);
291 auto ret = m_RPC.setScanLimits_RPC(min,max);
292 if (!ret) {
293 yCError(RANGEFINDER2DCLIENT, "Unable to setScanLimits");
294 return false;
295 }
296 return true;
297}
298
300{
301 std::lock_guard<std::mutex> lg(m_mutex);
303 if (!ret.retval) {
304 yCError(RANGEFINDER2DCLIENT, "Unable to getHorizontalResolution");
305 return false;
306 }
307 step = ret.step;
308 return true;
309}
310
312{
313 std::lock_guard<std::mutex> lg(m_mutex);
314 auto ret = m_RPC.setScanRate_RPC(step);
315 if (!ret) {
316 yCError(RANGEFINDER2DCLIENT, "Unable to setHorizontalResolution");
317 return false;
318 }
319 return true;
320}
321
323{
324 std::lock_guard<std::mutex> lg(m_mutex);
325 auto ret = m_RPC.getScanRate_RPC();
326 if (!ret.retval) {
327 yCError(RANGEFINDER2DCLIENT, "Unable to getScanRate");
328 return false;
329 }
330 rate = ret.rate;
331 return true;
332}
333
335{
336 std::lock_guard<std::mutex> lg(m_mutex);
337 auto ret = m_RPC.setScanRate_RPC(rate);
338 if (!ret) {
339 yCError(RANGEFINDER2DCLIENT, "Unable to setScanRate");
340 return false;
341 }
342 return true;
343}
344
346{
347 std::lock_guard <std::mutex> lg(m_mutex);
348 status = m_inputPort.getStatus();
349 return true;
350}
351
352bool Rangefinder2D_nwc_yarp::getDeviceInfo(std::string &device_info)
353{
354 std::lock_guard<std::mutex> lg(m_mutex);
355 auto ret = m_RPC.getDeviceInfo_RPC();
356 if (!ret.retval) {
357 yCError(RANGEFINDER2DCLIENT, "Unable to getDeviceInfo");
358 return false;
359 }
360 device_info = ret.device_info;
361 return true;
362}
bool ret
#define DEG2RAD
const int LASER_TIMEOUT
virtual bool setScanRate_RPC(const double rate)
virtual return_getScanRate getScanRate_RPC()
virtual bool setDistanceRange_RPC(const double min, const double max)
virtual bool setScanLimits_RPC(const double min, const double max)
virtual return_getHorizontalResolution getHorizontalResolution_RPC()
virtual return_getScanLimits getScanLimits_RPC()
virtual return_getDeviceInfo getDeviceInfo_RPC()
virtual return_getDistanceRange getDistanceRange_RPC()
void onRead(yarp::sig::LaserScan2D &v) override
int getLast(yarp::sig::LaserScan2D &data, yarp::os::Stamp &stmp)
void getEstFrequency(int &ite, double &av, double &min, double &max)
yarp::dev::IRangefinder2D::Device_status getStatus()
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
bool getDeviceStatus(Device_status &status) override
get the device status
bool close() override
Close the DeviceDriver.
bool getDistanceRange(double &min, double &max) override
get the device detection range
bool getDeviceInfo(std::string &device_info) override
get the device hardware characteristics
bool setScanRate(double rate) override
set the scan rate (scans per seconds)
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool setDistanceRange(double min, double max) override
set the device detection range.
bool getRawData(yarp::sig::Vector &data, double *timestamp=nullptr) override
Get the device measurements.
bool getScanLimits(double &min, double &max) override
get the scan angular range.
bool setScanLimits(double min, double max) override
set the scan angular range.
bool setHorizontalResolution(double step) override
get the angular step between two measurements (if available)
bool getScanRate(double &rate) override
get the scan rate (scans per seconds)
Rangefinder2DInputPortProcessor m_inputPort
bool getHorizontalResolution(double &step) override
get the angular step between two measurements.
bool getLaserMeasurement(std::vector< yarp::sig::LaserMeasurementData > &data, double *timestamp=nullptr) override
Get the device measurements.
A mini-server for performing network communication in the background.
bool getEnvelope(PortReader &envelope) override
void close() override
Stop port activity.
void disableCallback() override
Remove a callback set up with useCallback()
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
void useCallback(TypedReaderCallback< T > &callback) override
Set an object whose onRead method will be called when data is available.
void close() override
Stop port activity.
Definition Port.cpp:363
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition Port.cpp:79
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
bool isValid() const
Check if this Stamp is valid.
Definition Stamp.cpp:39
static double nowSystem()
yarp::os::WireLink & yarp()
Get YARP state associated with this object.
Definition Wire.h:28
yarp::sig::Vector scans
the scan data, measured in [m].
Definition LaserScan2D.h:46
std::int32_t status
the status of the device.
Definition LaserScan2D.h:50
size_t size() const
Definition Vector.h:322
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
Definition jointData.cpp:13
An interface to the operating system, including Port based communication.