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 
11 #include <yarp/os/PeriodicThread.h>
12 #include <yarp/os/BufferedPort.h>
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 
62 {
63 protected:
66 
70 
71  double m_period;
77 
78  //this is the position of the localized robot in the map
79  double m_robot_loc_x;
80  double m_robot_loc_y;
81  double m_robot_loc_t;
82 
83  std::random_device* m_rd;
84  std::mt19937* m_gen;
85  std::uniform_real_distribution<>* m_dis;
86  double m_const_value=1;
87 
89 
90 public:
91  FakeLaser(double period = 0.02) : PeriodicThread(period),
94  m_loc_port(nullptr),
95  m_pLoc(nullptr),
96  m_iLoc(nullptr),
97  m_robot_loc_x(0.0),
98  m_robot_loc_y(0.0),
99  m_robot_loc_t(0.0)
100  {
101  //default parameters
102  m_min_distance = 0.1; //m
103  m_max_distance = 8.0; //m
104  m_min_angle = 0; //degrees
105  m_max_angle = 360; //degrees
106  m_resolution = 1.0; //degrees
107 
108  //noise generator
109  m_rd = new std::random_device;
110  m_gen = new std::mt19937((*m_rd)());
111  m_dis = new std::uniform_real_distribution<>(0, 0.01);
112  }
113 
115  {
116  delete m_rd;
117  delete m_gen;
118  delete m_dis;
119  m_rd = 0;
120  m_gen = 0;
121  m_dis = 0;
122  if (m_loc_port)
123  {
124  delete m_loc_port;
125  m_loc_port = 0;
126  }
127  if (m_pLoc)
128  {
129  delete m_pLoc;
130  m_pLoc = 0;
131  }
132  }
133 
134  bool open(yarp::os::Searchable& config) override;
135  bool close() override;
136  bool threadInit() override;
137  void threadRelease() override;
138  void run() override;
139 
140 private:
141  void drawStraightLine(yarp::dev::Nav2D::XYCell src, yarp::dev::Nav2D::XYCell dst);
142  void wall_the_robot(double siz = 1.0, double dist = 1.0);
143  void obst_the_robot(double siz = 1.0, double dist = 1.0);
144  void trap_the_robot(double siz = 1.0);
145  void free_the_robot();
146 
147  double checkStraightLine(yarp::dev::Nav2D::XYCell src, yarp::dev::Nav2D::XYCell dst);
148  bool LiangBarsky_clip(int edgeLeft, int edgeRight, int edgeTop, int edgeBottom,
150  yarp::dev::Nav2D::XYCell& src_clipped, yarp::dev::Nav2D::XYCell& dst_clipped);
151 
152 public:
153  //IRangefinder2D interface
154  bool setDistanceRange (double min, double max) override;
155  bool setScanLimits (double min, double max) override;
156  bool setHorizontalResolution (double step) override;
157  bool setScanRate (double rate) override;
158 
159 public:
160  //Lidar2DDeviceBase
161  bool acquireDataFromHW() override final;
162 
163 public:
164  bool read(yarp::os::ConnectionReader& connection) override;
165 };
166 
167 #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:62
double m_robot_loc_y
Definition: fakeLaser.h:80
FakeLaser(double period=0.02)
Definition: fakeLaser.h:91
yarp::dev::PolyDriver * m_pLoc
Definition: fakeLaser.h:75
std::mt19937 * m_gen
Definition: fakeLaser.h:84
double m_robot_loc_x
Definition: fakeLaser.h:79
double m_period
Definition: fakeLaser.h:71
void run() override
Loop function.
Definition: fakeLaser.cpp:428
yarp::dev::Nav2D::MapGrid2D m_map
Definition: fakeLaser.h:73
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
Definition: fakeLaser.cpp:502
bool setDistanceRange(double min, double max) override
set the device detection range.
Definition: fakeLaser.cpp:206
double m_robot_loc_t
Definition: fakeLaser.h:81
@ USE_CONSTANT_VALUE
Definition: fakeLaser.h:64
@ NO_OBSTACLES
Definition: fakeLaser.h:64
@ USE_MAPFILE
Definition: fakeLaser.h:64
@ USE_PATTERN
Definition: fakeLaser.h:64
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:289
yarp::dev::Nav2D::ILocalization2D * m_iLoc
Definition: fakeLaser.h:76
~FakeLaser()
Definition: fakeLaser.h:114
localization_mode_t m_loc_mode
Definition: fakeLaser.h:69
bool threadInit() override
Initialization method.
Definition: fakeLaser.cpp:240
yarp::os::BufferedPort< yarp::os::Bottle > * m_loc_port
Definition: fakeLaser.h:74
yarp::dev::PolyDriver driver
Definition: fakeLaser.h:67
std::random_device * m_rd
Definition: fakeLaser.h:83
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: fakeLaser.cpp:34
test_mode_t m_test_mode
Definition: fakeLaser.h:68
bool setScanRate(double rate) override
set the scan rate (scans per seconds)
Definition: fakeLaser.cpp:232
bool setScanLimits(double min, double max) override
set the scan angular range.
Definition: fakeLaser.cpp:215
std::uniform_real_distribution * m_dis
Definition: fakeLaser.h:85
bool close() override
Close the DeviceDriver.
Definition: fakeLaser.cpp:192
void threadRelease() override
Release method.
Definition: fakeLaser.cpp:722
yarp::dev::Nav2D::MapGrid2D m_originally_loaded_map
Definition: fakeLaser.h:72
localization_mode_t
Definition: fakeLaser.h:65
@ LOC_NOT_SET
Definition: fakeLaser.h:65
@ LOC_FROM_CLIENT
Definition: fakeLaser.h:65
@ LOC_FROM_PORT
Definition: fakeLaser.h:65
yarp::os::Port m_rpcPort
Definition: fakeLaser.h:88
bool setHorizontalResolution(double step) override
get the angular step between two measurments (if available)
Definition: fakeLaser.cpp:224
double m_const_value
Definition: fakeLaser.h:86
Interface implemented by all device drivers.
Definition: DeviceDriver.h:35
The Lidar2DDeviceBase class.
ILocalization2D interface.
A container for a device driver.
Definition: PolyDriver.h:24
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:25
A mini-server for network communication.
Definition: Port.h:47
A base class for nested structures that can be searched.
Definition: Searchable.h:66
The main, catch-all namespace for YARP.
Definition: dirs.h:16