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_map.loadFromFile(map_file);
109 if (config.check(
"localization_port"))
111 string localization_port_name = config.check(
"localization_port",
Value(
string(
"/fakeLaser/location:i")),
"name of localization input port").asString();
113 m_loc_port->
open(localization_port_name);
114 yCInfo(
FAKE_LASER) <<
"Robot localization will be obtained from port" << localization_port_name;
115 m_loc_mode = LOC_FROM_PORT;
117 else if (config.check(
"localization_client") ||
118 config.check(
"localization_server" ))
121 string localization_client_name = config.
check(
"localization_client",
Value(
string(
"/fakeLaser/localizationClient")),
"local name of localization client device").asString();
122 string localization_server_name = config.check(
"localization_server",
Value(
string(
"/localizationServer")),
"the name of the remote localization server device").asString();
123 loc_options.
put(
"device",
"localization2DClient");
124 loc_options.
put(
"local", localization_client_name);
125 loc_options.
put(
"remote", localization_server_name);
126 loc_options.
put(
"period", 10);
128 if (m_pLoc->open(loc_options) ==
false)
133 m_pLoc->view(m_iLoc);
134 if (m_iLoc ==
nullptr)
139 yCInfo(
FAKE_LASER) <<
"Robot localization will be obtained from localization server: " << localization_server_name;
140 m_loc_mode = LOC_FROM_CLIENT;
144 yCInfo(
FAKE_LASER) <<
"No localization method selected. Robot location set to 0,0,0";
145 m_loc_mode = LOC_NOT_SET;
155 return PeriodicThread::start();
160 PeriodicThread::stop();
222 double size = (
t - (t_orig));
224 static int test_count = 0;
233 { value = i / 100.0; }
235 { value = size * 2; }
238 if (i <= 10) { value = 1.0 + i / 20.0; }
239 else if (i >= 90 && i <= 100) { value = 2.0 + (i - 90) / 20.0; }
246 if (test_count == 60)
248 test_count = 0; test++;
if (test > 2) { test = 0; }
293 yCError(
FAKE_LASER) <<
"Error occurred while getting current position from localization server";
298 yCDebug(
FAKE_LASER) <<
"No localization mode selected. This branch should be not reachable.";
316 double distance = checkStraightLine(src, dst);
317 double simulated_noise = (*m_dis)(*m_gen);
336 double FakeLaser::checkStraightLine(
XYCell src,
XYCell dst)
341 int dx = abs(
int(dst.
x - src.
x));
342 int dy = abs(
int(dst.
y - src.
y));
347 if (src.
x < dst.
x) { sx = 1; }
else { sx = -1; }
348 if (src.
y < dst.
y) { sy = 1; }
else { sy = -1; }
357 return sqrt(pow(world_start.
x - world_end.
x, 2) + pow(world_start.
y - world_end.
y, 2));
359 if (src.
x == dst.
x && src.
y == dst.
y)
break;
372 return std::numeric_limits<double>::infinity();
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 setDistanceRange(double min, double max) override
set the device detection range.
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.
bool setHorizontalResolution(double step) override
get the angular step between two measurments (if available)
yarp::dev::IRangefinder2D::Device_status m_device_status
void applyLimitsOnLaserData()
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
bool isInsideMap(XYCell cell) const
Checks if a cell is inside the map.
XYWorld cell2World(XYCell cell) const
bool isWall(XYCell cell) const
Checks if a specific cell of the map contains a wall.
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.
Value & get(size_type index) const
Reads a Value v from a certain part 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.
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.
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.
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 yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
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.