YARP
Yet Another Robot Platform
fakeLaser.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef FAKE_LASER_H
8#define FAKE_LASER_H
9
10
17#include <yarp/dev/MapGrid2D.h>
18#include <yarp/dev/PolyDriver.h>
19#include <yarp/sig/Vector.h>
20
21#include <mutex>
22#include <random>
23#include <string>
24
64{
65protected:
68
72
73 double m_period;
79
80 //this is the position of the localized robot in the map
84
85 std::random_device* m_rd;
86 std::mt19937* m_gen;
87 std::uniform_real_distribution<>* m_dis;
88 double m_const_value=1;
89
91
92public:
93 FakeLaser(double period = 0.02) : PeriodicThread(period),
96 m_loc_port(nullptr),
97 m_pLoc(nullptr),
98 m_iLoc(nullptr),
99 m_robot_loc_x(0.0),
100 m_robot_loc_y(0.0),
101 m_robot_loc_t(0.0)
102 {
103 //default parameters
104 m_min_distance = 0.1; //m
105 m_max_distance = 8.0; //m
106 m_min_angle = 0; //degrees
107 m_max_angle = 360; //degrees
108 m_resolution = 1.0; //degrees
109
110 //noise generator
111 m_rd = new std::random_device;
112 m_gen = new std::mt19937((*m_rd)());
113 m_dis = new std::uniform_real_distribution<>(0, 0.01);
114 }
115
117 {
118 delete m_rd;
119 delete m_gen;
120 delete m_dis;
121 m_rd = 0;
122 m_gen = 0;
123 m_dis = 0;
124 if (m_loc_port)
125 {
126 delete m_loc_port;
127 m_loc_port = 0;
128 }
129 if (m_pLoc)
130 {
131 delete m_pLoc;
132 m_pLoc = 0;
133 }
134 }
135
136 bool open(yarp::os::Searchable& config) override;
137 bool close() override;
138 bool threadInit() override;
139 void threadRelease() override;
140 void run() override;
141
142private:
143 void drawStraightLine(yarp::dev::Nav2D::XYCell src, yarp::dev::Nav2D::XYCell dst);
144 void wall_the_robot(double siz = 1.0, double dist = 1.0);
145 void obst_the_robot(double siz = 1.0, double dist = 1.0);
146 void trap_the_robot(double siz = 1.0);
147 void free_the_robot();
148
149 double checkStraightLine(yarp::dev::Nav2D::XYCell src, yarp::dev::Nav2D::XYCell dst);
150 bool LiangBarsky_clip(int edgeLeft, int edgeRight, int edgeTop, int edgeBottom,
152 yarp::dev::Nav2D::XYCell& src_clipped, yarp::dev::Nav2D::XYCell& dst_clipped);
153
154public:
155 //IRangefinder2D interface
156 bool setDistanceRange (double min, double max) override;
157 bool setScanLimits (double min, double max) override;
158 bool setHorizontalResolution (double step) override;
159 bool setScanRate (double rate) override;
160
161public:
162 //Lidar2DDeviceBase
163 bool acquireDataFromHW() override final;
164
165public:
166 bool read(yarp::os::ConnectionReader& connection) override;
167};
168
169#endif
define control board standard interfaces
contains the definition of a map type
contains the definition of a Vector type
fakeLaser : fake sensor device driver for testing purposes and reference for IRangefinder2D devices.
Definition: fakeLaser.h:64
double m_robot_loc_y
Definition: fakeLaser.h:82
FakeLaser(double period=0.02)
Definition: fakeLaser.h:93
yarp::dev::PolyDriver * m_pLoc
Definition: fakeLaser.h:77
std::mt19937 * m_gen
Definition: fakeLaser.h:86
double m_robot_loc_x
Definition: fakeLaser.h:81
double m_period
Definition: fakeLaser.h:73
void run() override
Loop function.
Definition: fakeLaser.cpp:431
yarp::dev::Nav2D::MapGrid2D m_map
Definition: fakeLaser.h:75
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
Definition: fakeLaser.cpp:505
bool setDistanceRange(double min, double max) override
set the device detection range.
Definition: fakeLaser.cpp:209
double m_robot_loc_t
Definition: fakeLaser.h:83
@ USE_CONSTANT_VALUE
Definition: fakeLaser.h:66
@ NO_OBSTACLES
Definition: fakeLaser.h:66
@ USE_MAPFILE
Definition: fakeLaser.h:66
@ USE_PATTERN
Definition: fakeLaser.h:66
bool acquireDataFromHW() override final
This method should be implemented by the user, and contain the logic to grab data from the hardware.
Definition: fakeLaser.cpp:292
yarp::dev::Nav2D::ILocalization2D * m_iLoc
Definition: fakeLaser.h:78
~FakeLaser()
Definition: fakeLaser.h:116
localization_mode_t m_loc_mode
Definition: fakeLaser.h:71
bool threadInit() override
Initialization method.
Definition: fakeLaser.cpp:243
yarp::os::BufferedPort< yarp::os::Bottle > * m_loc_port
Definition: fakeLaser.h:76
yarp::dev::PolyDriver driver
Definition: fakeLaser.h:69
std::random_device * m_rd
Definition: fakeLaser.h:85
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: fakeLaser.cpp:34
test_mode_t m_test_mode
Definition: fakeLaser.h:70
bool setScanRate(double rate) override
set the scan rate (scans per seconds)
Definition: fakeLaser.cpp:235
bool setScanLimits(double min, double max) override
set the scan angular range.
Definition: fakeLaser.cpp:218
std::uniform_real_distribution * m_dis
Definition: fakeLaser.h:87
bool close() override
Close the DeviceDriver.
Definition: fakeLaser.cpp:195
void threadRelease() override
Release method.
Definition: fakeLaser.cpp:725
yarp::dev::Nav2D::MapGrid2D m_originally_loaded_map
Definition: fakeLaser.h:74
localization_mode_t
Definition: fakeLaser.h:67
@ LOC_NOT_SET
Definition: fakeLaser.h:67
@ LOC_FROM_CLIENT
Definition: fakeLaser.h:67
@ LOC_FROM_PORT
Definition: fakeLaser.h:67
yarp::os::Port m_rpcPort
Definition: fakeLaser.h:90
bool setHorizontalResolution(double step) override
get the angular step between two measurements (if available)
Definition: fakeLaser.cpp:227
double m_const_value
Definition: fakeLaser.h:88
Interface implemented by all device drivers.
Definition: DeviceDriver.h:30
The Lidar2DDeviceBase class.
ILocalization2D interface.
A container for a device driver.
Definition: PolyDriver.h:23
An abstraction for a periodic thread.
PeriodicThread(double period, ShouldUseSystemClock useSystemClock=ShouldUseSystemClock::No, PeriodicThreadClock clockAccuracy=PeriodicThreadClock::Relative)
Constructor.
void step()
Call this to "step" the thread rather than starting it.
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:24
A mini-server for network communication.
Definition: Port.h:46
A base class for nested structures that can be searched.
Definition: Searchable.h:63
The main, catch-all namespace for YARP.
Definition: dirs.h:16