6#define _USE_MATH_DEFINES
23#define DEG2RAD M_PI/180.0
34 if (!this->parseParams(config)) {
return false;}
36 m_info =
"Fake Laser device for test/debugging";
37 m_device_status = DEVICE_OK_STANDBY;
43 if (config.check(
"help"))
47 yCInfo(
FAKE_LASER,
"yarpdev --device rangefinder2D_nws_yarp --subdevice fakeLaser --period 0.01 --name /fakeLaser:o --test no_obstacles");
48 yCInfo(
FAKE_LASER,
"yarpdev --device rangefinder2D_nws_yarp --subdevice fakeLaser --period 0.01 --name /fakeLaser:o --test use_pattern");
49 yCInfo(
FAKE_LASER,
"yarpdev --device rangefinder2D_nws_yarp --subdevice fakeLaser --period 0.01 --name /fakeLaser:o --test use_constant --const_distance 0.5");
50 yCInfo(
FAKE_LASER,
"yarpdev --device rangefinder2D_nws_yarp --subdevice fakeLaser --period 0.01 --name /fakeLaser:o --test use_constant --const_distance 0.5 --SENSOR::resolution 0.5 --SKIP::min 0 50 --SKIP::max 10 60");
51 yCInfo(
FAKE_LASER,
"yarpdev --device rangefinder2D_nws_yarp --subdevice fakeLaser --period 0.01 --name /fakeLaser:o --test use_mapfile --map_file mymap.map");
52 yCInfo(
FAKE_LASER,
"yarpdev --device rangefinder2D_nws_yarp --subdevice fakeLaser --period 0.01 --name /fakeLaser:o --test use_mapfile --map_file mymap.map --localization_port /fakeLaser/location:i");
53 yCInfo(
FAKE_LASER,
"yarpdev --device rangefinder2D_nws_yarp --subdevice fakeLaser --period 0.01 --name /fakeLaser:o --test use_mapfile --map_file mymap.map --localization_server /localizationServer");
54 yCInfo(
FAKE_LASER,
"yarpdev --device rangefinder2D_nws_yarp --subdevice fakeLaser --period 0.01 --name /fakeLaser:o --test use_mapfile --map_file mymap.map --localization_client /fakeLaser/localizationClient --localization_server /localizationServer");
55 yCInfo(
FAKE_LASER,
"yarpdev --device rangefinder2D_nws_yarp --subdevice fakeLaser --period 0.01 --name /fakeLaser:o --test use_mapfile --map_context context --map_file mymap.map");
56 yCInfo(
FAKE_LASER,
"yarpdev --device rangefinder2D_nws_yarp --subdevice fakeLaser --period 0.01 --name /fakeLaser:o --test use_mapfile --map_file mymap.map --localization_client /fakeLaser/localizationClient --localization_server /localization2D_nws_yarp --localization_device localization2D_nwc_yarp");
64 else if (
string_test_mode ==
"use_constant") { m_test_mode = USE_CONSTANT_VALUE; }
65 else if (
string_test_mode ==
"use_square_trap") { m_test_mode = USE_SQUARE_TRAP; }
66 else {
yCError(
FAKE_LASER) <<
"invalid/unknown value for param 'test'";
return false; }
69 if (this->parseConfiguration(config) ==
false)
75 if (m_test_mode == USE_SQUARE_TRAP)
80 m_map.m_map_name =
"test_map_10x10m";
81 m_map.m_resolution = 0.01;
82 m_map.m_origin.setOrigin(-5,-5,0);
83 m_map.setSize_in_meters(10,10);
84 for (
size_t y = 0; y < m_map.m_height; y++)
86 for (
size_t x = 0; x < m_map.m_width; x++)
92 m_originally_loaded_map = m_map;
95 m_map.saveToFile(
"mio1");
97 else if (m_test_mode == USE_MAPFILE)
100 if (!m_MAP_MODE_map_context.empty() && !m_MAP_MODE_map_file.empty())
121 else if (!m_MAP_MODE_map_file.empty())
127 yCError(
FAKE_LASER) <<
"Missing `map_file` or `map_context`+`map_file` parameters";
136 bool ret = m_originally_loaded_map.loadFromFile(
map_file);
142 m_map = m_originally_loaded_map;
144 if (!m_localization_port.empty())
146 std::string
localization_port_name = config.check(
"localization_port",
Value(std::string(
"/fakeLaser/location:i")),
"name of localization input port").asString();
150 m_loc_mode = LOC_FROM_PORT;
152 else if (!m_localization_client.empty() ||
153 !m_localization_server.empty() ||
154 !m_localization_device.empty())
169 m_pLoc->view(m_iLoc);
170 if (m_iLoc ==
nullptr)
176 m_loc_mode = LOC_FROM_CLIENT;
180 yCInfo(
FAKE_LASER) <<
"No localization method selected. Robot location set to 0,0,0";
181 m_loc_mode = LOC_NOT_SET;
192 if (!m_rpcPort.open(
"/fakeLaser/rpc:i"))
197 m_rpcPort.setReader(*
this);
263 double t0 = 0.0;
double t1 = 1.0;
275 if (
p == 0 &&
q < 0) {
return false;}
279 if (r > t1) {
return false;}
280 else if (r >
t0) {
t0 = r;}
284 if (r <
t0) {
return false;}
285 else if (r < t1) {t1 = r;}
322 if (
i <= 10) { value = 1.0 +
i / 20.0; }
323 else if (
i >= 90 &&
i <= 100) { value = 2.0 + (
i - 90) / 20.0; }
332 test_count = 0; test++;
if (test > 2) { test = 0; }
377 yCError(
FAKE_LASER) <<
"Error occurred while getting current position from localization server";
382 yCDebug(
FAKE_LASER) <<
"No localization mode selected. This branch should be not reachable.";
423 yDebug() <<
"Robot is outside the map?!";
444void FakeLaser::wall_the_robot(
double siz,
double dist)
467void FakeLaser::obst_the_robot(
double siz,
double dist)
477void FakeLaser::trap_the_robot(
double siz)
505void FakeLaser::free_the_robot()
522 if (command.
size() == 1)
527 else if (command.
size() == 2)
539 if (command.
size() == 1)
541 wall_the_robot(1.0, 1.0);
542 wall_the_robot(1.0, 1.05);
545 else if (command.
size() == 2)
551 else if (command.
size() == 3)
570 reply.
addString(
"wall <size> <distance>: creates a wall in front of the robot");
571 reply.
addString(
"trap <size>: traps the robot in a box obstacle");
572 reply.
addString(
"free: removes all generated obstacles");
593 dx = (
long int)dst.
x - (
long int)src.
x;
594 dy = (
long int)dst.
y - (
long int)src.
y;
614 for (
i = 0; x <
xe;
i++)
623 if ((
dx < 0 &&
dy < 0) || (
dx > 0 &&
dy > 0))
651 for (
i = 0; y <
ye;
i++)
660 if ((
dx < 0 &&
dy < 0) || (
dx > 0 &&
dy > 0))
675double FakeLaser::checkStraightLine(
XYCell src,
XYCell dst)
683 int dx = abs(
int(dst.
x - src.
x));
684 int dy = abs(
int(dst.
y - src.
y));
689 if (src.
x < dst.
x) {
sx = 1; }
else {
sx = -1; }
690 if (src.
y < dst.
y) {
sy = 1; }
else {
sy = -1; }
727 return std::numeric_limits<double>::infinity();
const yarp::os::LogComponent & FAKE_LASER()
constexpr yarp::conf::vocab32_t VOCAB_OK
constexpr yarp::conf::vocab32_t VOCAB_ERR
double m_CONSTANT_MODE_const_distance
fakeLaser : fake sensor device driver for testing purposes and reference for IRangefinder2D devices.
void run() override
Loop function.
yarp::dev::Nav2D::MapGrid2D m_map
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
bool setDistanceRange(double min, double max) override
set the device detection range.
bool acquireDataFromHW() override final
This method should be implemented by the user, and contain the logic to grab data from the hardware.
yarp::dev::Nav2D::ILocalization2D * m_iLoc
localization_mode_t m_loc_mode
bool threadInit() override
Initialization method.
yarp::os::BufferedPort< yarp::os::Bottle > * m_loc_port
bool setScanRate(double rate) override
set the scan rate (scans per seconds)
bool setScanLimits(double min, double max) override
set the scan angular range.
bool close() override
Close the DeviceDriver.
void threadRelease() override
Release method.
yarp::dev::Nav2D::MapGrid2D m_originally_loaded_map
bool setHorizontalResolution(double step) override
get the angular step between two measurements (if available)
virtual bool updateLidarData()
This utility method calls in sequence: grabDataFromHW(), updateTimestamp and applyLimitsOnLaserData()...
yarp::dev::IRangefinder2D::Device_status m_device_status
yarp::sig::Vector m_laser_data
double theta
orientation [deg] in the map reference frame
double y
y position of the location [m], expressed in the map reference frame
double x
x position of the location [m], expressed in the map reference frame
virtual yarp::dev::ReturnValue getCurrentPosition(yarp::dev::Nav2D::Map2DLocation &loc)=0
Gets the current position of the robot w.r.t world reference frame.
XYCell world2Cell(XYWorld world) const
XYWorld cell2World(XYCell cell) const
MapGrid2DOrigin m_origin
pose of the map frame w.r.t. the bottom left corner of the map image
size_t height() const
Retrieves the map height, expressed in cells.
bool setMapFlag(XYCell cell, map_flags flag)
Set the flag of a specific cell of the map.
size_t width() const
Retrieves the map width, expressed in cells.
bool isWall(XYCell cell) const
Checks if a specific cell of the map contains a wall.
void getResolution(double &resolution) const
Retrieves the resolution of the map, i.e.
A container for a device driver.
A simple collection of objects that can be described and transmitted in a portable way.
void addVocab32(yarp::conf::vocab32_t x)
Places a vocabulary item in the bottle, at the end of the list.
size_type size() const
Gets the number of elements in the bottle.
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
void clear()
Empties the bottle of any objects it contains.
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
void addString(const char *str)
Places a string in the bottle, at the end of the list.
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.
void interrupt() override
Interrupt any current reads or writes attached to the port.
T * read(bool shouldWait=true) override
Read an available object from the port.
An interface for reading from a network connection.
An interface for writing to a network connection.
bool start()
Call this to start the thread.
void step()
Call this to "step" the thread rather than starting it.
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
A class for storing options and configuration information.
Helper class for finding config files and other external resources.
bool setDefaultContext(const std::string &contextName)
Sets the context for the current ResourceFinder object.
bool configure(int argc, char *argv[], bool skipFirstArgument=true)
Sets up the ResourceFinder.
std::string findFile(const std::string &name)
Find the full path to a file.
bool setDefaultConfigFile(const std::string &fname)
Provide a default value for the configuration file (can be overridden from command line with the –fro...
A base class for nested structures that can be searched.
A single value (typically within a Bottle).
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
virtual std::string asString() const
Get string value.
void push_back(const T &elem)
Push a new element in the vector: size is changed.
#define yCInfo(component,...)
#define yCError(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
yarp::math::Vec2D< size_t > XYCell
yarp::math::Vec2D< double > XYWorld
double now()
Return the current time in seconds, relative to an arbitrary starting point.
The main, catch-all namespace for YARP.