YARP
Yet Another Robot Platform
fakeLaser.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #ifndef FAKE_LASER_H
11 #define FAKE_LASER_H
12 
13 
14 #include <yarp/os/PeriodicThread.h>
15 #include <yarp/os/BufferedPort.h>
20 #include <yarp/dev/MapGrid2D.h>
21 #include <yarp/dev/PolyDriver.h>
22 #include <yarp/sig/Vector.h>
23 
24 #include <mutex>
25 #include <random>
26 #include <string>
27 
65 {
66 protected:
69 
73 
74  double m_period;
80 
81  //this is the position of the localized robot in the map
82  double m_robot_loc_x;
83  double m_robot_loc_y;
84  double m_robot_loc_t;
85 
86  std::random_device* m_rd;
87  std::mt19937* m_gen;
88  std::uniform_real_distribution<>* m_dis;
89  double m_const_value=1;
90 
92 
93 public:
94  FakeLaser(double period = 0.02) : PeriodicThread(period),
97  m_loc_port(nullptr),
98  m_pLoc(nullptr),
99  m_iLoc(nullptr),
100  m_robot_loc_x(0.0),
101  m_robot_loc_y(0.0),
102  m_robot_loc_t(0.0)
103  {
104  //default parameters
105  m_min_distance = 0.1; //m
106  m_max_distance = 8.0; //m
107  m_min_angle = 0; //degrees
108  m_max_angle = 360; //degrees
109  m_resolution = 1.0; //degrees
110 
111  //noise generator
112  m_rd = new std::random_device;
113  m_gen = new std::mt19937((*m_rd)());
114  m_dis = new std::uniform_real_distribution<>(0, 0.01);
115  }
116 
118  {
119  delete m_rd;
120  delete m_gen;
121  delete m_dis;
122  m_rd = 0;
123  m_gen = 0;
124  m_dis = 0;
125  if (m_loc_port)
126  {
127  delete m_loc_port;
128  m_loc_port = 0;
129  }
130  if (m_pLoc)
131  {
132  delete m_pLoc;
133  m_pLoc = 0;
134  }
135  }
136 
137  bool open(yarp::os::Searchable& config) override;
138  bool close() override;
139  bool threadInit() override;
140  void threadRelease() override;
141  void run() override;
142 
143 private:
144  void drawStraightLine(yarp::dev::Nav2D::XYCell src, yarp::dev::Nav2D::XYCell dst);
145  void wall_the_robot(double siz = 1.0, double dist = 1.0);
146  void obst_the_robot(double siz = 1.0, double dist = 1.0);
147  void trap_the_robot(double siz = 1.0);
148  void free_the_robot();
149 
150  double checkStraightLine(yarp::dev::Nav2D::XYCell src, yarp::dev::Nav2D::XYCell dst);
151  bool LiangBarsky_clip(int edgeLeft, int edgeRight, int edgeTop, int edgeBottom,
153  yarp::dev::Nav2D::XYCell& src_clipped, yarp::dev::Nav2D::XYCell& dst_clipped);
154 
155 public:
156  //IRangefinder2D interface
157  bool setDistanceRange (double min, double max) override;
158  bool setScanLimits (double min, double max) override;
159  bool setHorizontalResolution (double step) override;
160  bool setScanRate (double rate) override;
161 
162 public:
163  bool read(yarp::os::ConnectionReader& connection) override;
164 };
165 
166 #endif
FakeLaser::m_const_value
double m_const_value
Definition: fakeLaser.h:89
MapGrid2D.h
contains the definition of a map type
FakeLaser::m_gen
std::mt19937 * m_gen
Definition: fakeLaser.h:87
FakeLaser::run
void run() override
Loop function.
Definition: fakeLaser.cpp:265
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
IRangefinder2D.h
FakeLaser::m_period
double m_period
Definition: fakeLaser.h:74
FakeLaser::close
bool close() override
Close the DeviceDriver.
Definition: fakeLaser.cpp:167
Vector.h
contains the definition of a Vector type
FakeLaser::USE_MAPFILE
@ USE_MAPFILE
Definition: fakeLaser.h:67
Lidar2DDeviceBase.h
yarp::dev::DeviceDriver
Interface implemented by all device drivers.
Definition: DeviceDriver.h:38
yarp::dev::Lidar2DDeviceBase::m_resolution
double m_resolution
Definition: Lidar2DDeviceBase.h:50
FakeLaser::LOC_FROM_CLIENT
@ LOC_FROM_CLIENT
Definition: fakeLaser.h:68
FakeLaser::m_loc_mode
localization_mode_t m_loc_mode
Definition: fakeLaser.h:72
yarp::dev::Nav2D::ILocalization2D
ILocalization2D interface.
Definition: ILocalization2D.h:40
FakeLaser::threadInit
bool threadInit() override
Initialization method.
Definition: fakeLaser.cpp:215
FakeLaser::setHorizontalResolution
bool setHorizontalResolution(double step) override
get the angular step between two measurments (if available)
Definition: fakeLaser.cpp:199
yarp::os::PeriodicThread::PeriodicThread
PeriodicThread(double period, ShouldUseSystemClock useSystemClock=ShouldUseSystemClock::No)
Constructor.
Definition: PeriodicThread.cpp:271
FakeLaser::FakeLaser
FakeLaser(double period=0.02)
Definition: fakeLaser.h:94
FakeLaser::read
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
Definition: fakeLaser.cpp:471
FakeLaser::m_test_mode
test_mode_t m_test_mode
Definition: fakeLaser.h:71
ControlBoardInterfaces.h
define control board standard interfaces
yarp::dev::Lidar2DDeviceBase::m_min_angle
double m_min_angle
Definition: Lidar2DDeviceBase.h:46
FakeLaser::setDistanceRange
bool setDistanceRange(double min, double max) override
set the device detection range.
Definition: fakeLaser.cpp:181
yarp::math::Vec2D
Definition: Vec2D.h:26
yarp::dev::Lidar2DDeviceBase
The DLidarDeviceTemplate class.
Definition: Lidar2DDeviceBase.h:33
yarp::os::Port
A mini-server for network communication.
Definition: Port.h:50
yarp::os::BufferedPort< yarp::os::Bottle >
FakeLaser::m_iLoc
yarp::dev::Nav2D::ILocalization2D * m_iLoc
Definition: fakeLaser.h:79
FakeLaser::threadRelease
void threadRelease() override
Release method.
Definition: fakeLaser.cpp:691
yarp::os::PortReader
Interface implemented by all objects that can read themselves from the network, such as Bottle object...
Definition: PortReader.h:28
yarp::dev::Nav2D::MapGrid2D
Definition: MapGrid2D.h:32
PolyDriver.h
FakeLaser::m_robot_loc_y
double m_robot_loc_y
Definition: fakeLaser.h:83
FakeLaser::driver
yarp::dev::PolyDriver driver
Definition: fakeLaser.h:70
yarp::dev::Lidar2DDeviceBase::m_max_distance
double m_max_distance
Definition: Lidar2DDeviceBase.h:49
FakeLaser::m_robot_loc_t
double m_robot_loc_t
Definition: fakeLaser.h:84
yarp::dev::PolyDriver
A container for a device driver.
Definition: PolyDriver.h:27
FakeLaser::setScanRate
bool setScanRate(double rate) override
set the scan rate (scans per seconds)
Definition: fakeLaser.cpp:207
FakeLaser::USE_PATTERN
@ USE_PATTERN
Definition: fakeLaser.h:67
FakeLaser::m_robot_loc_x
double m_robot_loc_x
Definition: fakeLaser.h:82
FakeLaser::~FakeLaser
~FakeLaser()
Definition: fakeLaser.h:117
FakeLaser::open
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: fakeLaser.cpp:37
BufferedPort.h
FakeLaser::NO_OBSTACLES
@ NO_OBSTACLES
Definition: fakeLaser.h:67
FakeLaser::m_rpcPort
yarp::os::Port m_rpcPort
Definition: fakeLaser.h:91
FakeLaser::m_map
yarp::dev::Nav2D::MapGrid2D m_map
Definition: fakeLaser.h:76
yarp::os::PeriodicThread
An abstraction for a periodic thread.
Definition: PeriodicThread.h:25
FakeLaser::setScanLimits
bool setScanLimits(double min, double max) override
set the scan angular range.
Definition: fakeLaser.cpp:190
FakeLaser::m_originally_loaded_map
yarp::dev::Nav2D::MapGrid2D m_originally_loaded_map
Definition: fakeLaser.h:75
PeriodicThread.h
ILocalization2D.h
FakeLaser::m_dis
std::uniform_real_distribution * m_dis
Definition: fakeLaser.h:88
FakeLaser::m_rd
std::random_device * m_rd
Definition: fakeLaser.h:86
yarp::os::ConnectionReader
An interface for reading from a network connection.
Definition: ConnectionReader.h:40
FakeLaser::localization_mode_t
localization_mode_t
Definition: fakeLaser.h:68
FakeLaser::test_mode_t
test_mode_t
Definition: fakeLaser.h:67
yarp::dev::Lidar2DDeviceBase::m_min_distance
double m_min_distance
Definition: Lidar2DDeviceBase.h:48
FakeLaser::LOC_FROM_PORT
@ LOC_FROM_PORT
Definition: fakeLaser.h:68
FakeLaser::LOC_NOT_SET
@ LOC_NOT_SET
Definition: fakeLaser.h:68
FakeLaser
fakeLaser : fake sensor device driver for testing purposes and reference for IRangefinder2D devices.
Definition: fakeLaser.h:65
FakeLaser::USE_CONSTANT_VALUE
@ USE_CONSTANT_VALUE
Definition: fakeLaser.h:67
yarp::os::PeriodicThread::step
void step()
Call this to "step" the thread rather than starting it.
Definition: PeriodicThread.cpp:306
FakeLaser::m_loc_port
yarp::os::BufferedPort< yarp::os::Bottle > * m_loc_port
Definition: fakeLaser.h:77
FakeLaser::m_pLoc
yarp::dev::PolyDriver * m_pLoc
Definition: fakeLaser.h:78
yarp::dev::Lidar2DDeviceBase::m_max_angle
double m_max_angle
Definition: Lidar2DDeviceBase.h:47