|
YARP
Yet Another Robot Platform
|
|
Go to the documentation of this file.
9 #define _USE_MATH_DEFINES
25 #define DEG2RAD M_PI/180.0
31 using namespace
yarp::os;
32 using namespace
yarp::dev;
33 using namespace
yarp::dev::Nav2D;
39 m_info =
"Fake Laser device for test/debugging";
40 m_device_status = DEVICE_OK_STANBY;
46 if (config.check(
"help"))
50 yCInfo(
FAKE_LASER,
"yarpdev --device Rangefinder2DWrapper --subdevice fakeLaser --period 10 --name /fakeLaser:o --test no_obstacles");
51 yCInfo(
FAKE_LASER,
"yarpdev --device Rangefinder2DWrapper --subdevice fakeLaser --period 10 --name /fakeLaser:o --test use_pattern");
52 yCInfo(
FAKE_LASER,
"yarpdev --device Rangefinder2DWrapper --subdevice fakeLaser --period 10 --name /fakeLaser:o --test use_constant --const_distance 0.5");
53 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");
54 yCInfo(
FAKE_LASER,
"yarpdev --device Rangefinder2DWrapper --subdevice fakeLaser --period 10 --name /fakeLaser:o --test use_mapfile --map_file mymap.map");
55 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");
56 yCInfo(
FAKE_LASER,
"yarpdev --device Rangefinder2DWrapper --subdevice fakeLaser --period 10 --name /fakeLaser:o --test use_mapfile --map_file mymap.map --localization_server /localizationServer");
57 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");
61 bool br = config.check(
"GENERAL");
65 m_period = general_config.
check(
"Period",
Value(50),
"Period of the sampling thread").asInt32() / 1000.0;
68 string string_test_mode = config.check(
"test",
Value(
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_file"))
95 map_file = config.check(
"map_file",
Value(
string(
"map.yaml")),
"map filename").asString();
102 bool ret = m_originally_loaded_map.loadFromFile(map_file);
108 m_map = m_originally_loaded_map;
110 if (config.check(
"localization_port"))
112 string localization_port_name = config.check(
"localization_port",
Value(
string(
"/fakeLaser/location:i")),
"name of localization input port").asString();
114 m_loc_port->
open(localization_port_name);
115 yCInfo(
FAKE_LASER) <<
"Robot localization will be obtained from port" << localization_port_name;
116 m_loc_mode = LOC_FROM_PORT;
118 else if (config.check(
"localization_client") ||
119 config.check(
"localization_server" ))
122 string localization_client_name = config.
check(
"localization_client",
Value(
string(
"/fakeLaser/localizationClient")),
"local name of localization client device").asString();
123 string localization_server_name = config.check(
"localization_server",
Value(
string(
"/localizationServer")),
"the name of the remote localization server device").asString();
124 loc_options.
put(
"device",
"localization2DClient");
125 loc_options.
put(
"local", localization_client_name);
126 loc_options.
put(
"remote", localization_server_name);
127 loc_options.
put(
"period", 10);
129 if (m_pLoc->open(loc_options) ==
false)
134 m_pLoc->view(m_iLoc);
135 if (m_iLoc ==
nullptr)
140 yCInfo(
FAKE_LASER) <<
"Robot localization will be obtained from localization server: " << localization_server_name;
141 m_loc_mode = LOC_FROM_CLIENT;
145 yCInfo(
FAKE_LASER) <<
"No localization method selected. Robot location set to 0,0,0";
146 m_loc_mode = LOC_NOT_SET;
157 if (!m_rpcPort.open(
"/fakeLaser/rpc:i"))
162 m_rpcPort.setReader(*
this);
164 return PeriodicThread::start();
169 PeriodicThread::stop();
226 bool FakeLaser::LiangBarsky_clip(
int edgeLeft,
int edgeRight,
int edgeTop,
int edgeBottom,
230 double t0 = 0.0;
double t1 = 1.0;
231 double xdelta = double(dst.
x - src.
x);
232 double ydelta = double(dst.
y - src.
y);
235 for (
int edge = 0; edge < 4; edge++)
237 if (edge == 0) { p = -xdelta; q = -(edgeLeft - src.
x); }
238 if (edge == 1) { p = xdelta; q = (edgeRight - src.
x); }
239 if (edge == 2) { p = -ydelta; q = -(edgeTop - src.
y); }
240 if (edge == 3) { p = ydelta; q = (edgeBottom - src.
y); }
242 if (p == 0 && q < 0) {
return false;}
246 if (r > t1) {
return false;}
247 else if (r > t0) {t0 = r;}
251 if (r < t0) {
return false;}
252 else if (r < t1) {t1 = r;}
256 src_clipped.
x = size_t(
double(src.
x) + t0 * xdelta);
257 src_clipped.
y = size_t(
double(src.
y) + t0 * ydelta);
258 dst_clipped.
x = size_t(
double(src.
x) + t1 * xdelta);
259 dst_clipped.
y = size_t(
double(src.
y) + t1 * ydelta);
271 double size = (
t - (t_orig));
273 static int test_count = 0;
282 { value = i / 100.0; }
284 { value = size * 2; }
287 if (i <= 10) { value = 1.0 + i / 20.0; }
288 else if (i >= 90 && i <= 100) { value = 2.0 + (i - 90) / 20.0; }
295 if (test_count == 60)
297 test_count = 0; test++;
if (test > 2) { test = 0; }
342 yCError(
FAKE_LASER) <<
"Error occurred while getting current position from localization server";
347 yCDebug(
FAKE_LASER) <<
"No localization mode selected. This branch should be not reachable.";
372 dst.
x = int((+ray_world_rot.
x - this->m_map.m_origin.get_x()) / this->m_map.m_resolution) + 0;
373 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;
381 distance = checkStraightLine(src, newdst);
382 double simulated_noise = (*m_dis)(*m_gen);
388 yDebug() <<
"Robot is outside the map?!";
405 void FakeLaser::wall_the_robot(
double siz,
double dist)
420 XYWorld end(0 + dist, 0 + siz);
425 drawStraightLine(start_cell,end_cell);
428 void FakeLaser::obst_the_robot(
double siz,
double dist)
438 void FakeLaser::trap_the_robot(
double siz)
442 size_t siz_cell = size_t(siz / res);
444 for (
size_t x= robot.
x- siz_cell; x< robot.
x + siz_cell; x++)
446 size_t y=robot.
y- siz_cell;
449 for (
size_t x = robot.
x - siz_cell; x < robot.
x + siz_cell; x++)
451 size_t y = robot.
y + siz_cell;
454 for (
size_t y = robot.
y - siz_cell; y < robot.
y + siz_cell; y++)
456 size_t x = robot.
x - siz_cell;
459 for (
size_t y = robot.
y - siz_cell; y < robot.
y + siz_cell; y++)
461 size_t x = robot.
x + siz_cell;
466 void FakeLaser::free_the_robot()
475 bool ok = command.
read(connection);
483 if (command.
size() == 1)
488 else if (command.
size() == 2)
500 if (command.
size() == 1)
502 wall_the_robot(1.0, 1.0);
503 wall_the_robot(1.0, 1.05);
506 else if (command.
size() == 2)
512 else if (command.
size() == 3)
531 reply.
addString(
"wall <size> <distance>: creates a wall in front of the robot");
532 reply.
addString(
"trap <size>: traps the robot in a box obstacle");
533 reply.
addString(
"free: removes all generated obstacles");
542 if (returnToSender !=
nullptr)
544 reply.
write(*returnToSender);
550 void FakeLaser::drawStraightLine(
XYCell src,
XYCell dst)
553 long int dx, dy, dx1, dy1, px, py, xe, ye, i;
554 dx = (
long int)dst.
x - (
long int)src.
x;
555 dy = (
long int)dst.
y - (
long int)src.
y;
566 xe = (
long int)dst.
x;
572 xe = (
long int)src.
x;
575 for (i = 0; x < xe; i++)
584 if ((dx < 0 && dy < 0) || (dx > 0 && dy > 0))
592 px = px + 2 * (dy1 - dx1);
603 ye = (
long int)dst.
y;
609 ye = (
long int)src.
y;
612 for (i = 0; y < ye; i++)
621 if ((dx < 0 && dy < 0) || (dx > 0 && dy > 0))
629 py = py + 2 * (dx1 - dy1);
636 double FakeLaser::checkStraightLine(
XYCell src,
XYCell dst)
640 test_point.
x = src.
x;
641 test_point.
y = src.
y;
644 int dx = abs(
int(dst.
x - src.
x));
645 int dy = abs(
int(dst.
y - src.
y));
650 if (src.
x < dst.
x) { sx = 1; }
else { sx = -1; }
651 if (src.
y < dst.
y) { sy = 1; }
else { sy = -1; }
667 double dist = sqrt(pow(world_start.
x - world_end.
x, 2) + pow(world_start.
y - world_end.
y, 2));
671 if (test_point.
x == dst.
x && test_point.
y == dst.
y)
688 return std::numeric_limits<double>::infinity();
A simple collection of objects that can be described and transmitted in a portable way.
yarp::sig::Vector m_laser_data
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
void clear()
Empties the bottle of any objects it contains.
void run() override
Loop function.
A base class for nested structures that can be searched.
bool close() override
Close the DeviceDriver.
size_type size() const
Gets the number of elements in the bottle.
T * read(bool shouldWait=true) override
Read an available object from the port.
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
yarp::dev::IRangefinder2D::Device_status m_device_status
void applyLimitsOnLaserData()
yarp::math::Vec2D< double > XYWorld
#define YARP_LOG_COMPONENT(name,...)
localization_mode_t m_loc_mode
bool threadInit() override
Initialization method.
bool setHorizontalResolution(double step) override
get the angular step between two measurments (if available)
void interrupt() override
Interrupt any current reads or writes attached to the port.
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.
double now()
Return the current time in seconds, relative to an arbitrary starting point.
XYWorld cell2World(XYCell cell) const
const yarp::os::LogComponent & FAKE_LASER()
yarp::dev::Nav2D::ILocalization2D * m_iLoc
void threadRelease() override
Release method.
size_t width() const
Retrieves the map width, expressed in cells.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
An interface for writing to a network connection.
yarp::dev::PolyDriver driver
virtual std::string asString() const
Get string value.
bool start()
Call this to start the thread.
yarp::math::Vec2D< size_t > XYCell
A container for a device driver.
bool close() override
Close the DeviceDriver.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
bool setScanRate(double rate) override
set the scan rate (scans per seconds)
NetInt32 encode(const std::string &str)
Convert a string into a vocabulary identifier.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
virtual ConnectionWriter * getWriter()=0
Gets a way to reply to the message, if possible.
void addString(const char *str)
Places a string in the bottle, at the end of the list.
yarp::dev::Nav2D::MapGrid2D m_map
void addVocab(int x)
Places a vocabulary item in the bottle, at the end of the list.
bool setScanLimits(double min, double max) override
set the scan angular range.
yarp::dev::Nav2D::MapGrid2D m_originally_loaded_map
constexpr yarp::conf::vocab32_t VOCAB_ERR
An interface for reading from a network connection.
#define yCError(component,...)
#define yCInfo(component,...)
#define yCDebug(component,...)
The main, catch-all namespace for YARP.
void close() override
Stop port activity.
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
fakeLaser : fake sensor device driver for testing purposes and reference for IRangefinder2D devices.
XYCell world2Cell(XYWorld world) const
MapGrid2DOrigin m_origin
pose of the map frame w.r.t. the bottom left corner of the map image
void step()
Call this to "step" the thread rather than starting it.
bool setMapFlag(XYCell cell, map_flags flag)
Set the flag of a specific cell of the map.
virtual bool getCurrentPosition(yarp::dev::Nav2D::Map2DLocation &loc)=0
Gets the current position of the robot w.r.t world reference frame.
A single value (typically within a Bottle).
size_t height() const
Retrieves the map height, expressed in cells.
bool isWall(XYCell cell) const
Checks if a specific cell of the map contains a wall.
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
yarp::os::BufferedPort< yarp::os::Bottle > * m_loc_port
void push_back(const T &elem)
Push a new element in the vector: size is changed.
void getResolution(double &resolution) const
Retrieves the resolution of the map, i.e.
constexpr yarp::conf::vocab32_t VOCAB_OK
A class for storing options and configuration information.