6#define _USE_MATH_DEFINES
23#define DEG2RAD M_PI/180.0
28using namespace
yarp::os;
29using namespace
yarp::dev;
30using namespace
yarp::dev::Nav2D;
36 m_info =
"Fake Laser device for test/debugging";
37 m_device_status = DEVICE_OK_STANBY;
43 if (config.check(
"help"))
47 yCInfo(
FAKE_LASER,
"yarpdev --device Rangefinder2DWrapper --subdevice fakeLaser --period 10 --name /fakeLaser:o --test no_obstacles");
48 yCInfo(
FAKE_LASER,
"yarpdev --device Rangefinder2DWrapper --subdevice fakeLaser --period 10 --name /fakeLaser:o --test use_pattern");
49 yCInfo(
FAKE_LASER,
"yarpdev --device Rangefinder2DWrapper --subdevice fakeLaser --period 10 --name /fakeLaser:o --test use_constant --const_distance 0.5");
50 yCInfo(
FAKE_LASER,
"yarpdev --device Rangefinder2DWrapper --subdevice fakeLaser --period 10 --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 Rangefinder2DWrapper --subdevice fakeLaser --period 10 --name /fakeLaser:o --test use_mapfile --map_file mymap.map");
52 yCInfo(
FAKE_LASER,
"yarpdev --device Rangefinder2DWrapper --subdevice fakeLaser --period 10 --name /fakeLaser:o --test use_mapfile --map_file mymap.map --localization_port /fakeLaser/location:i");
53 yCInfo(
FAKE_LASER,
"yarpdev --device Rangefinder2DWrapper --subdevice fakeLaser --period 10 --name /fakeLaser:o --test use_mapfile --map_file mymap.map --localization_server /localizationServer");
54 yCInfo(
FAKE_LASER,
"yarpdev --device Rangefinder2DWrapper --subdevice fakeLaser --period 10 --name /fakeLaser:o --test use_mapfile --map_file mymap.map --localization_client /fakeLaser/localizationClient --localization_server /localizationServer");
55 yCInfo(
FAKE_LASER,
"yarpdev --device Rangefinder2DWrapper --subdevice fakeLaser --period 10 --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");
60 bool br = config.check(
"GENERAL");
64 m_period = general_config.
check(
"period",
Value(0.02),
"Period of the sampling thread in s").asFloat64();
65 this->setPeriod(m_period);
68 std::string string_test_mode = config.check(
"test",
Value(std::string(
"use_pattern")),
"string to select test mode").asString();
69 if (string_test_mode ==
"no_obstacles") { m_test_mode = NO_OBSTACLES; }
70 else if (string_test_mode ==
"use_pattern") { m_test_mode = USE_PATTERN; }
71 else if (string_test_mode ==
"use_mapfile") { m_test_mode = USE_MAPFILE; }
72 else if (string_test_mode ==
"use_constant") { m_test_mode = USE_CONSTANT_VALUE; }
73 else {
yCError(
FAKE_LASER) <<
"invalid/unknown value for param 'test'";
return false; }
76 if (this->parseConfiguration(config) ==
false)
83 else if (m_test_mode == USE_CONSTANT_VALUE)
85 if (config.check(
"const_distance"))
87 m_const_value = config.check(
"const_distance",
Value(1.0),
"default constant distance").asFloat64();
90 else if (m_test_mode == USE_MAPFILE)
93 if (config.check(
"map_context") && config.check(
"map_file"))
96 std::string tmp_filename = config.
find(
"map_file").
asString();
97 std::string tmp_contextname = config.find(
"map_context").asString();
103 map_file = rf.
findFile(tmp_filename);
106 yCWarning(
FAKE_LASER,
"Unable to find file: %s from context: %s\n", tmp_filename.c_str(), tmp_contextname.c_str());
111 yCWarning(
FAKE_LASER,
"Unable to find file: %s from context: %s\n", tmp_filename.c_str(), tmp_contextname.c_str());
114 else if (config.check(
"map_file"))
116 map_file = config.check(
"map_file",
Value(std::string(
"map.yaml")),
"map filename").asString();
120 yCError(
FAKE_LASER) <<
"Missing `map_file` or `map_context`+`map_file` parameters";
129 bool ret = m_originally_loaded_map.loadFromFile(map_file);
135 m_map = m_originally_loaded_map;
137 if (config.check(
"localization_port"))
139 std::string localization_port_name = config.check(
"localization_port",
Value(std::string(
"/fakeLaser/location:i")),
"name of localization input port").asString();
141 m_loc_port->
open(localization_port_name);
142 yCInfo(
FAKE_LASER) <<
"Robot localization will be obtained from port" << localization_port_name;
143 m_loc_mode = LOC_FROM_PORT;
145 else if (config.check(
"localization_client") ||
146 config.check(
"localization_server") ||
147 config.check(
"localization_device"))
150 std::string localization_client_name = config.
check(
"localization_client",
Value(std::string(
"/fakeLaser/localizationClient")),
"local name of localization client device").asString();
151 std::string localization_server_name = config.check(
"localization_server",
Value(std::string(
"/localizationServer")),
"the name of the remote localization server device").asString();
152 std::string localization_device_name = config.check(
"localization_device",
Value(std::string(
"localization2DClient")),
"the type of localization device").asString();
153 loc_options.
put(
"device", localization_device_name);
154 loc_options.
put(
"local", localization_client_name);
155 loc_options.
put(
"remote", localization_server_name);
157 if (m_pLoc->open(loc_options) ==
false)
162 m_pLoc->view(m_iLoc);
163 if (m_iLoc ==
nullptr)
168 yCInfo(
FAKE_LASER) <<
"Robot localization will be obtained from localization server: " << localization_server_name;
169 m_loc_mode = LOC_FROM_CLIENT;
173 yCInfo(
FAKE_LASER) <<
"No localization method selected. Robot location set to 0,0,0";
174 m_loc_mode = LOC_NOT_SET;
183 yCInfo(
FAKE_LASER) <<
"test mode:"<< m_test_mode <<
" i.e. " << string_test_mode;
185 if (!m_rpcPort.open(
"/fakeLaser/rpc:i"))
190 m_rpcPort.setReader(*
this);
192 return PeriodicThread::start();
197 PeriodicThread::stop();
254bool FakeLaser::LiangBarsky_clip(
int edgeLeft,
int edgeRight,
int edgeTop,
int edgeBottom,
258 double t0 = 0.0;
double t1 = 1.0;
259 double xdelta = double(dst.
x - src.
x);
260 double ydelta = double(dst.
y - src.
y);
263 for (
int edge = 0; edge < 4; edge++)
265 if (edge == 0) { p = -xdelta; q = -(edgeLeft - src.
x); }
266 if (edge == 1) { p = xdelta; q = (edgeRight - src.
x); }
267 if (edge == 2) { p = -ydelta; q = -(edgeTop - src.
y); }
268 if (edge == 3) { p = ydelta; q = (edgeBottom - src.
y); }
270 if (p == 0 && q < 0) {
return false;}
274 if (r > t1) {
return false;}
275 else if (r > t0) {t0 = r;}
279 if (r < t0) {
return false;}
280 else if (r < t1) {t1 = r;}
284 src_clipped.
x =
size_t(
double(src.
x) + t0 * xdelta);
285 src_clipped.
y =
size_t(
double(src.
y) + t0 * ydelta);
286 dst_clipped.
x =
size_t(
double(src.
x) + t1 * xdelta);
287 dst_clipped.
y =
size_t(
double(src.
y) + t1 * ydelta);
297 double size = (
t - (t_orig));
299 static int test_count = 0;
317 if (i <= 10) { value = 1.0 + i / 20.0; }
318 else if (i >= 90 && i <= 100) { value = 2.0 + (i - 90) / 20.0; }
325 if (test_count == 60)
327 test_count = 0; test++;
if (test > 2) { test = 0; }
372 yCError(
FAKE_LASER) <<
"Error occurred while getting current position from localization server";
377 yCDebug(
FAKE_LASER) <<
"No localization mode selected. This branch should be not reachable.";
402 dst.
x = int((+ray_world_rot.
x - this->m_map.m_origin.get_x()) / this->m_map.m_resolution) + 0;
403 dst.
y = int((-ray_world_rot.
y + this->m_map.m_origin.get_y()) / this->m_map.m_resolution) + (int)
m_map.
m_height - 1;
411 distance = checkStraightLine(src, newdst);
412 double simulated_noise = (*m_dis)(*m_gen);
418 yDebug() <<
"Robot is outside the map?!";
439void FakeLaser::wall_the_robot(
double siz,
double dist)
454 XYWorld end(0 + dist, 0 + siz);
459 drawStraightLine(start_cell,end_cell);
462void FakeLaser::obst_the_robot(
double siz,
double dist)
472void FakeLaser::trap_the_robot(
double siz)
476 size_t siz_cell =
size_t(siz / res);
478 for (
size_t x= robot.
x- siz_cell; x< robot.
x + siz_cell; x++)
480 size_t y=robot.
y- siz_cell;
483 for (
size_t x = robot.
x - siz_cell; x < robot.
x + siz_cell; x++)
485 size_t y = robot.
y + siz_cell;
488 for (
size_t y = robot.
y - siz_cell; y < robot.
y + siz_cell; y++)
490 size_t x = robot.
x - siz_cell;
493 for (
size_t y = robot.
y - siz_cell; y < robot.
y + siz_cell; y++)
495 size_t x = robot.
x + siz_cell;
500void FakeLaser::free_the_robot()
509 bool ok = command.
read(connection);
517 if (command.
size() == 1)
522 else if (command.
size() == 2)
534 if (command.
size() == 1)
536 wall_the_robot(1.0, 1.0);
537 wall_the_robot(1.0, 1.05);
540 else if (command.
size() == 2)
546 else if (command.
size() == 3)
565 reply.
addString(
"wall <size> <distance>: creates a wall in front of the robot");
566 reply.
addString(
"trap <size>: traps the robot in a box obstacle");
567 reply.
addString(
"free: removes all generated obstacles");
576 if (returnToSender !=
nullptr)
578 reply.
write(*returnToSender);
587 long int dx, dy, dx1, dy1, px, py, xe, ye, i;
588 dx = (
long int)dst.
x - (
long int)src.
x;
589 dy = (
long int)dst.
y - (
long int)src.
y;
600 xe = (
long int)dst.
x;
606 xe = (
long int)src.
x;
609 for (i = 0; x < xe; i++)
618 if ((dx < 0 && dy < 0) || (dx > 0 && dy > 0))
626 px = px + 2 * (dy1 - dx1);
637 ye = (
long int)dst.
y;
643 ye = (
long int)src.
y;
646 for (i = 0; y < ye; i++)
655 if ((dx < 0 && dy < 0) || (dx > 0 && dy > 0))
663 py = py + 2 * (dx1 - dy1);
670double FakeLaser::checkStraightLine(
XYCell src,
XYCell dst)
674 test_point.
x = src.
x;
675 test_point.
y = src.
y;
678 int dx = abs(
int(dst.
x - src.
x));
679 int dy = abs(
int(dst.
y - src.
y));
684 if (src.
x < dst.
x) { sx = 1; }
else { sx = -1; }
685 if (src.
y < dst.
y) { sy = 1; }
else { sy = -1; }
701 double dist = sqrt(pow(world_start.
x - world_end.
x, 2) + pow(world_start.
y - world_end.
y, 2));
705 if (test_point.
x == dst.
x && test_point.
y == dst.
y)
722 return std::numeric_limits<double>::infinity();
constexpr yarp::conf::vocab32_t VOCAB_OK
constexpr yarp::conf::vocab32_t VOCAB_ERR
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
yarp::dev::PolyDriver driver
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 bool 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.
bool close() override
Close the DeviceDriver.
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.
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.
virtual ConnectionWriter * getWriter()=0
Gets a way to reply to the message, if possible.
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.
A class for storing options and configuration information.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
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.
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
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.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
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.
const yarp::os::LogComponent & FAKE_LASER()
#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.