6 #define _USE_MATH_DEFINES
23 #define DEG2RAD M_PI/180.0
28 using namespace
yarp::os;
29 using namespace
yarp::dev;
30 using 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");
59 bool br = config.check(
"GENERAL");
63 m_period = general_config.
check(
"Period",
Value(50),
"Period of the sampling thread").asInt32() / 1000.0;
66 std::string string_test_mode = config.check(
"test",
Value(std::string(
"use_pattern")),
"string to select test mode").asString();
67 if (string_test_mode ==
"no_obstacles") { m_test_mode = NO_OBSTACLES; }
68 else if (string_test_mode ==
"use_pattern") { m_test_mode = USE_PATTERN; }
69 else if (string_test_mode ==
"use_mapfile") { m_test_mode = USE_MAPFILE; }
70 else if (string_test_mode ==
"use_constant") { m_test_mode = USE_CONSTANT_VALUE; }
71 else {
yCError(
FAKE_LASER) <<
"invalid/unknown value for param 'test'";
return false; }
74 if (this->parseConfiguration(config) ==
false)
81 else if (m_test_mode == USE_CONSTANT_VALUE)
83 if (config.check(
"const_distance"))
85 m_const_value = config.check(
"const_distance",
Value(1.0),
"default constant distance").asFloat64();
88 else if (m_test_mode == USE_MAPFILE)
91 if (config.check(
"map_context") && config.check(
"map_file"))
94 std::string tmp_filename = config.
find(
"map_file").
asString();
95 std::string tmp_contextname = config.find(
"map_context").asString();
101 map_file = rf.
findFile(tmp_filename);
104 yCWarning(
FAKE_LASER,
"Unable to find file: %s from context: %s\n", tmp_filename.c_str(), tmp_contextname.c_str());
109 yCWarning(
FAKE_LASER,
"Unable to find file: %s from context: %s\n", tmp_filename.c_str(), tmp_contextname.c_str());
112 else if (config.check(
"map_file"))
114 map_file = config.check(
"map_file",
Value(std::string(
"map.yaml")),
"map filename").asString();
118 yCError(
FAKE_LASER) <<
"Missing `map_file` or `map_context`+`map_file` parameters";
127 bool ret = m_originally_loaded_map.loadFromFile(map_file);
133 m_map = m_originally_loaded_map;
135 if (config.check(
"localization_port"))
137 std::string localization_port_name = config.check(
"localization_port",
Value(std::string(
"/fakeLaser/location:i")),
"name of localization input port").asString();
139 m_loc_port->
open(localization_port_name);
140 yCInfo(
FAKE_LASER) <<
"Robot localization will be obtained from port" << localization_port_name;
141 m_loc_mode = LOC_FROM_PORT;
143 else if (config.check(
"localization_client") ||
144 config.check(
"localization_server" ))
147 std::string localization_client_name = config.
check(
"localization_client",
Value(std::string(
"/fakeLaser/localizationClient")),
"local name of localization client device").asString();
148 std::string localization_server_name = config.check(
"localization_server",
Value(std::string(
"/localizationServer")),
"the name of the remote localization server device").asString();
149 loc_options.
put(
"device",
"localization2DClient");
150 loc_options.
put(
"local", localization_client_name);
151 loc_options.
put(
"remote", localization_server_name);
152 loc_options.
put(
"period", 10);
154 if (m_pLoc->open(loc_options) ==
false)
159 m_pLoc->view(m_iLoc);
160 if (m_iLoc ==
nullptr)
165 yCInfo(
FAKE_LASER) <<
"Robot localization will be obtained from localization server: " << localization_server_name;
166 m_loc_mode = LOC_FROM_CLIENT;
170 yCInfo(
FAKE_LASER) <<
"No localization method selected. Robot location set to 0,0,0";
171 m_loc_mode = LOC_NOT_SET;
182 if (!m_rpcPort.open(
"/fakeLaser/rpc:i"))
187 m_rpcPort.setReader(*
this);
189 return PeriodicThread::start();
194 PeriodicThread::stop();
251 bool FakeLaser::LiangBarsky_clip(
int edgeLeft,
int edgeRight,
int edgeTop,
int edgeBottom,
255 double t0 = 0.0;
double t1 = 1.0;
256 double xdelta = double(dst.
x - src.
x);
257 double ydelta = double(dst.
y - src.
y);
260 for (
int edge = 0; edge < 4; edge++)
262 if (edge == 0) { p = -xdelta; q = -(edgeLeft - src.
x); }
263 if (edge == 1) { p = xdelta; q = (edgeRight - src.
x); }
264 if (edge == 2) { p = -ydelta; q = -(edgeTop - src.
y); }
265 if (edge == 3) { p = ydelta; q = (edgeBottom - src.
y); }
267 if (p == 0 && q < 0) {
return false;}
271 if (r > t1) {
return false;}
272 else if (r > t0) {t0 = r;}
276 if (r < t0) {
return false;}
277 else if (r < t1) {t1 = r;}
281 src_clipped.
x =
size_t(
double(src.
x) + t0 * xdelta);
282 src_clipped.
y =
size_t(
double(src.
y) + t0 * ydelta);
283 dst_clipped.
x =
size_t(
double(src.
x) + t1 * xdelta);
284 dst_clipped.
y =
size_t(
double(src.
y) + t1 * ydelta);
294 double size = (
t - (t_orig));
296 static int test_count = 0;
314 if (i <= 10) { value = 1.0 + i / 20.0; }
315 else if (i >= 90 && i <= 100) { value = 2.0 + (i - 90) / 20.0; }
322 if (test_count == 60)
324 test_count = 0; test++;
if (test > 2) { test = 0; }
369 yCError(
FAKE_LASER) <<
"Error occurred while getting current position from localization server";
374 yCDebug(
FAKE_LASER) <<
"No localization mode selected. This branch should be not reachable.";
399 dst.
x = int((+ray_world_rot.
x - this->m_map.m_origin.get_x()) / this->m_map.m_resolution) + 0;
400 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;
408 distance = checkStraightLine(src, newdst);
409 double simulated_noise = (*m_dis)(*m_gen);
415 yDebug() <<
"Robot is outside the map?!";
436 void FakeLaser::wall_the_robot(
double siz,
double dist)
451 XYWorld end(0 + dist, 0 + siz);
456 drawStraightLine(start_cell,end_cell);
459 void FakeLaser::obst_the_robot(
double siz,
double dist)
469 void FakeLaser::trap_the_robot(
double siz)
473 size_t siz_cell =
size_t(siz / res);
475 for (
size_t x= robot.
x- siz_cell; x< robot.
x + siz_cell; x++)
477 size_t y=robot.
y- siz_cell;
480 for (
size_t x = robot.
x - siz_cell; x < robot.
x + siz_cell; x++)
482 size_t y = robot.
y + siz_cell;
485 for (
size_t y = robot.
y - siz_cell; y < robot.
y + siz_cell; y++)
487 size_t x = robot.
x - siz_cell;
490 for (
size_t y = robot.
y - siz_cell; y < robot.
y + siz_cell; y++)
492 size_t x = robot.
x + siz_cell;
497 void FakeLaser::free_the_robot()
506 bool ok = command.
read(connection);
514 if (command.
size() == 1)
519 else if (command.
size() == 2)
531 if (command.
size() == 1)
533 wall_the_robot(1.0, 1.0);
534 wall_the_robot(1.0, 1.05);
537 else if (command.
size() == 2)
543 else if (command.
size() == 3)
562 reply.
addString(
"wall <size> <distance>: creates a wall in front of the robot");
563 reply.
addString(
"trap <size>: traps the robot in a box obstacle");
564 reply.
addString(
"free: removes all generated obstacles");
573 if (returnToSender !=
nullptr)
575 reply.
write(*returnToSender);
581 void FakeLaser::drawStraightLine(
XYCell src,
XYCell dst)
584 long int dx, dy, dx1, dy1, px, py, xe, ye, i;
585 dx = (
long int)dst.
x - (
long int)src.
x;
586 dy = (
long int)dst.
y - (
long int)src.
y;
597 xe = (
long int)dst.
x;
603 xe = (
long int)src.
x;
606 for (i = 0; x < xe; i++)
615 if ((dx < 0 && dy < 0) || (dx > 0 && dy > 0))
623 px = px + 2 * (dy1 - dx1);
634 ye = (
long int)dst.
y;
640 ye = (
long int)src.
y;
643 for (i = 0; y < ye; i++)
652 if ((dx < 0 && dy < 0) || (dx > 0 && dy > 0))
660 py = py + 2 * (dx1 - dy1);
667 double FakeLaser::checkStraightLine(
XYCell src,
XYCell dst)
671 test_point.
x = src.
x;
672 test_point.
y = src.
y;
675 int dx = abs(
int(dst.
x - src.
x));
676 int dy = abs(
int(dst.
y - src.
y));
681 if (src.
x < dst.
x) { sx = 1; }
else { sx = -1; }
682 if (src.
y < dst.
y) { sy = 1; }
else { sy = -1; }
698 double dist = sqrt(pow(world_start.
x - world_end.
x, 2) + pow(world_start.
y - world_end.
y, 2));
702 if (test_point.
x == dst.
x && test_point.
y == dst.
y)
719 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 measurments (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
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.