YARP
Yet Another Robot Platform
fakeLaser.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #define _USE_MATH_DEFINES
10 
11 #include "fakeLaser.h"
12 
13 #include <yarp/os/Time.h>
14 #include <yarp/os/Log.h>
15 #include <yarp/os/LogStream.h>
16 #include <yarp/math/Vec2D.h>
17 #include <iostream>
18 #include <limits>
19 #include <cstring>
20 #include <cstdlib>
21 #include <cmath>
22 
23 //#define LASER_DEBUG
24 #ifndef DEG2RAD
25 #define DEG2RAD M_PI/180.0
26 #endif
27 
28 YARP_LOG_COMPONENT(FAKE_LASER, "yarp.devices.fakeLaser")
29 
30 using namespace std;
31 using namespace yarp::os;
32 using namespace yarp::dev;
33 using namespace yarp::dev::Nav2D;
34 
35 // see FakeLaser.h for device documentation
36 
37 bool FakeLaser::open(yarp::os::Searchable& config)
38 {
39  m_info = "Fake Laser device for test/debugging";
40  m_device_status = DEVICE_OK_STANBY;
41 
42 #ifdef LASER_DEBUG
43  yCDebug(FAKE_LASER) << "%s\n", config.toString().c_str());
44 #endif
45 
46  if (config.check("help"))
47  {
48  yCInfo(FAKE_LASER,"Some examples:");
49  yCInfo(FAKE_LASER,"yarpdev --device fakeLaser --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");
58  return false;
59  }
60 
61  bool br = config.check("GENERAL");
62  if (br != false)
63  {
64  yarp::os::Searchable& general_config = config.findGroup("GENERAL");
65  m_period = general_config.check("Period", Value(50), "Period of the sampling thread").asInt32() / 1000.0;
66  }
67 
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; }
74 
75  //parse all the parameters related to the linear/angular range of the sensor
76  if (this->parseConfiguration(config) == false)
77  {
78  yCError(FAKE_LASER) << ": error parsing parameters";
79  return false;
80  }
81 
82  //the different fake laser modalities
83  else if (m_test_mode == USE_CONSTANT_VALUE)
84  {
85  if (config.check("const_distance"))
86  {
87  m_const_value = config.check("const_distance", Value(1.0), "default constant distance").asFloat64();
88  }
89  }
90  else if (m_test_mode == USE_MAPFILE)
91  {
92  string map_file;
93  if (config.check("map_file"))
94  {
95  map_file = config.check("map_file",Value(string("map.yaml")),"map filename").asString();
96  }
97  else
98  {
99  yCError(FAKE_LASER) << "Missing map_file";
100  return false;
101  }
102  bool ret = m_originally_loaded_map.loadFromFile(map_file);
103  if (ret == false)
104  {
105  yCError(FAKE_LASER) << "A problem occurred while opening:" << map_file;
106  return false;
107  }
108  m_map = m_originally_loaded_map;
109 
110  if (config.check("localization_port"))
111  {
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;
117  }
118  else if (config.check("localization_client") ||
119  config.check("localization_server" ))
120  {
121  Property loc_options;
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);
128  m_pLoc = new PolyDriver;
129  if (m_pLoc->open(loc_options) == false)
130  {
131  yCError(FAKE_LASER) << "Unable to open localization driver";
132  return false;
133  }
134  m_pLoc->view(m_iLoc);
135  if (m_iLoc == nullptr)
136  {
137  yCError(FAKE_LASER) << "Unable to open localization interface";
138  return false;
139  }
140  yCInfo(FAKE_LASER) << "Robot localization will be obtained from localization server: " << localization_server_name;
141  m_loc_mode = LOC_FROM_CLIENT;
142  }
143  else
144  {
145  yCInfo(FAKE_LASER) << "No localization method selected. Robot location set to 0,0,0";
146  m_loc_mode = LOC_NOT_SET;
147  }
148 
149  m_robot_loc_x=0;
150  m_robot_loc_y=0;
151  m_robot_loc_t=0;
152  }
153 
154  yCInfo(FAKE_LASER) << "Starting debug mode";
155  yCInfo(FAKE_LASER) << "test mode:"<< m_test_mode;
156 
157  if (!m_rpcPort.open("/fakeLaser/rpc:i"))
158  {
159  yCError(FAKE_LASER, "Failed to open port %s", "/fakeLaser/rpc:i");
160  return false;
161  }
162  m_rpcPort.setReader(*this);
163 
164  return PeriodicThread::start();
165 }
166 
168 {
169  PeriodicThread::stop();
170 
171  driver.close();
172 
173  if (m_loc_port)
174  {
176  m_loc_port->close();
177  }
178  return true;
179 }
180 
181 bool FakeLaser::setDistanceRange(double min, double max)
182 {
183  m_mutex.lock();
184  m_min_distance = min;
185  m_max_distance = max;
186  m_mutex.unlock();
187  return true;
188 }
189 
190 bool FakeLaser::setScanLimits(double min, double max)
191 {
192  m_mutex.lock();
193  m_min_angle = min;
194  m_max_angle = max;
195  m_mutex.unlock();
196  return true;
197 }
198 
200 {
201  m_mutex.lock();
202  m_resolution = step;
203  m_mutex.unlock();
204  return true;
205 }
206 
207 bool FakeLaser::setScanRate(double rate)
208 {
209  m_mutex.lock();
210  m_period = (1.0 / rate);
211  m_mutex.unlock();
212  return false;
213 }
214 
216 {
217 #ifdef LASER_DEBUG
218  yCDebug(FAKE_LASER)<<"thread initialising...\n");
219  yCDebug(FAKE_LASER)<<"... done!\n");
220 #endif
221 
222  return true;
223 }
224 
225 
226 bool FakeLaser::LiangBarsky_clip(int edgeLeft, int edgeRight, int edgeTop, int edgeBottom,
228  XYCell& src_clipped, XYCell& dst_clipped)
229 {
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);
233  double p, q, r;
234 
235  for (int edge = 0; edge < 4; edge++)
236  {
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); }
241  r = q / p;
242  if (p == 0 && q < 0) {return false;} //line is outside (parallel)
243 
244  if (p < 0)
245  {
246  if (r > t1) {return false;} //line is outside.
247  else if (r > t0) {t0 = r;} //line is clipped
248  }
249  else if (p > 0)
250  {
251  if (r < t0) {return false;} //line is outside.
252  else if (r < t1) {t1 = r;} //line is clipped
253  }
254  }
255 
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);
260 
261  return true; //line is clipped
262 }
263 
264 
266 {
267  m_mutex.lock();
269  double t = yarp::os::Time::now();
270  static double t_orig = yarp::os::Time::now();
271  double size = (t - (t_orig));
272 
273  static int test_count = 0;
274  static int test = 0;
275 
276  if (m_test_mode == USE_PATTERN)
277  {
278  for (size_t i = 0; i < m_sensorsNum; i++)
279  {
280  double value = 0;
281  if (test == 0)
282  { value = i / 100.0; }
283  else if (test == 1)
284  { value = size * 2; }
285  else if (test == 2)
286  {
287  if (i <= 10) { value = 1.0 + i / 20.0; }
288  else if (i >= 90 && i <= 100) { value = 2.0 + (i - 90) / 20.0; }
289  else { value = m_min_distance; }
290  }
291  m_laser_data.push_back(value);
292  }
293 
294  test_count++;
295  if (test_count == 60)
296  {
297  test_count = 0; test++; if (test > 2) { test = 0; }
298  t_orig = yarp::os::Time::now();
299  }
300  }
301  else if (m_test_mode == NO_OBSTACLES)
302  {
303  for (size_t i = 0; i < m_sensorsNum; i++)
304  {
305  m_laser_data.push_back(std::numeric_limits<double>::infinity());
306  }
307  }
308  else if (m_test_mode == USE_CONSTANT_VALUE)
309  {
310  for (size_t i = 0; i < m_sensorsNum; i++)
311  {
313  }
314  }
315  else if (m_test_mode == USE_MAPFILE)
316  {
317  if (m_loc_mode == LOC_NOT_SET)
318  {
319  //do nothing
320  }
321  else if (m_loc_mode == LOC_FROM_PORT)
322  {
323  Bottle* b = m_loc_port->read(false);
324  if (b)
325  {
326  m_robot_loc_x = b->get(0).asFloat64();
327  m_robot_loc_y = b->get(1).asFloat64();
328  m_robot_loc_t = b->get(2).asFloat64();
329  }
330  }
331  else if (m_loc_mode == LOC_FROM_CLIENT)
332  {
333  Map2DLocation loc;
334  if (m_iLoc->getCurrentPosition(loc))
335  {
336  m_robot_loc_x = loc.x;
337  m_robot_loc_y = loc.y;
338  m_robot_loc_t = loc.theta;
339  }
340  else
341  {
342  yCError(FAKE_LASER) << "Error occurred while getting current position from localization server";
343  }
344  }
345  else
346  {
347  yCDebug(FAKE_LASER) << "No localization mode selected. This branch should be not reachable.";
348  }
349 
350  for (size_t i = 0; i < m_sensorsNum; i++)
351  {
352  //compute the ray in the robot reference frame
353  double ray_angle = i* m_resolution + m_min_angle;
354  double ray_target_x = m_max_distance * cos(ray_angle * DEG2RAD);
355  double ray_target_y = m_max_distance * sin(ray_angle * DEG2RAD);
356 
357  //transforms the ray from the robot to the world reference frame
358  XYWorld ray_world;
359  ray_world.x = ray_target_x *cos(m_robot_loc_t*DEG2RAD) - ray_target_y *sin(m_robot_loc_t*DEG2RAD) + m_robot_loc_x;
360  ray_world.y = ray_target_x *sin(m_robot_loc_t*DEG2RAD) + ray_target_y *cos(m_robot_loc_t*DEG2RAD) + m_robot_loc_y;
362 
363  //beware! src is the robot position and it is always inside the image.
364  //dst is the end of the ray, and it can be out of the image!
365  //for this reason i am not going to call world2Cell() on dst, because that functions clips the point to the border without
366  //knowing the angular coefficient of the ray. I thus need the unclipped value, and run the LiangBarsky algorithm
367  //(which knows the angular coefficient of the ray) on it.
368  XYWorld ray_world_rot;
369  XYCell_unbounded dst;
370  ray_world_rot.x = ray_world.x * m_map.m_origin.get_ca() - ray_world.y * m_map.m_origin.get_sa();
371  ray_world_rot.y = ray_world.x * m_map.m_origin.get_sa() + ray_world.y * m_map.m_origin.get_ca();
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;
374 
375  XYCell newsrc;
376  XYCell newdst;
377  double distance;
378  if (LiangBarsky_clip(0,(int)m_map.width(),0, (int)m_map.height(),
379  XYCell_unbounded(src.x,src.y), dst, newsrc, newdst))
380  {
381  distance = checkStraightLine(src, newdst);
382  double simulated_noise = (*m_dis)(*m_gen);
383  m_laser_data.push_back(distance + simulated_noise);
384  }
385  else
386  {
387  //This should never happen, unless the robot is outside the map!
388  yDebug() << "Robot is outside the map?!";
389  m_laser_data.push_back(std::numeric_limits<double>::infinity());
390  }
391 
392  }
393  }
394 
395  //for all the different types of tests, apply the limits
397 
398  //set the device status
400 
401  m_mutex.unlock();
402  return;
403 }
404 
405 void FakeLaser::wall_the_robot(double siz, double dist)
406 {
407  //double res;
408  //m_map.getResolution(res);
409  //size_t siz_cell = siz / res;
410  //size_t dist_cell = dist / res;
412 
413  XYWorld ray_start;
414  XYWorld start (0+dist, 0 - siz);
415  ray_start.x = start.x * cos(m_robot_loc_t * DEG2RAD) - start.y * sin(m_robot_loc_t * DEG2RAD) + m_robot_loc_x;
416  ray_start.y = start.x * sin(m_robot_loc_t * DEG2RAD) + start.y * cos(m_robot_loc_t * DEG2RAD) + m_robot_loc_y;
417  XYCell start_cell = m_map.world2Cell(ray_start);
418 
419  XYWorld ray_end;
420  XYWorld end(0 + dist, 0 + siz);
421  ray_end.x = end.x * cos(m_robot_loc_t * DEG2RAD) - end.y * sin(m_robot_loc_t * DEG2RAD) + m_robot_loc_x;
422  ray_end.y = end.x * sin(m_robot_loc_t * DEG2RAD) + end.y * cos(m_robot_loc_t * DEG2RAD) + m_robot_loc_y;
423  XYCell end_cell = m_map.world2Cell(ray_end);
424 
425  drawStraightLine(start_cell,end_cell);
426 }
427 
428 void FakeLaser::obst_the_robot(double siz, double dist)
429 {
430  //NOT YET IMPLEMENTED
431  /*double res;
432  m_map.getResolution(res);
433  size_t siz_cell = size_t(siz / res);
434  size_t dist_cell = size_t(dist / res);
435  XYCell robot = m_map.world2Cell(XYWorld(m_robot_loc_x, m_robot_loc_y));*/
436 }
437 
438 void FakeLaser::trap_the_robot(double siz)
439 {
440  double res;
441  m_map.getResolution(res);
442  size_t siz_cell = size_t(siz / res);
444  for (size_t x= robot.x- siz_cell; x< robot.x + siz_cell; x++)
445  {
446  size_t y=robot.y- siz_cell;
447  m_map.setMapFlag(XYCell(x, y), yarp::dev::Nav2D::MapGrid2D::map_flags::MAP_CELL_WALL);
448  }
449  for (size_t x = robot.x - siz_cell; x < robot.x + siz_cell; x++)
450  {
451  size_t y = robot.y + siz_cell;
452  m_map.setMapFlag(XYCell(x, y), yarp::dev::Nav2D::MapGrid2D::map_flags::MAP_CELL_WALL);
453  }
454  for (size_t y = robot.y - siz_cell; y < robot.y + siz_cell; y++)
455  {
456  size_t x = robot.x - siz_cell;
457  m_map.setMapFlag(XYCell(x, y), yarp::dev::Nav2D::MapGrid2D::map_flags::MAP_CELL_WALL);
458  }
459  for (size_t y = robot.y - siz_cell; y < robot.y + siz_cell; y++)
460  {
461  size_t x = robot.x + siz_cell;
462  m_map.setMapFlag(XYCell(x, y), yarp::dev::Nav2D::MapGrid2D::map_flags::MAP_CELL_WALL);
463  }
464 }
465 
466 void FakeLaser::free_the_robot()
467 {
469 }
470 
472 {
473  yarp::os::Bottle command;
474  yarp::os::Bottle reply;
475  bool ok = command.read(connection);
476  if (!ok) {
477  return false;
478  }
479  reply.clear();
480 
481  if (command.get(0).asString() == "trap")
482  {
483  if (command.size() == 1)
484  {
485  trap_the_robot();
486  reply.addVocab(VOCAB_OK);
487  }
488  else if (command.size() == 2)
489  {
490  trap_the_robot(command.get(1).asFloat64());
491  reply.addVocab(VOCAB_OK);
492  }
493  else
494  {
495  reply.addVocab(VOCAB_ERR);
496  }
497  }
498  else if (command.get(0).asString() == "wall")
499  {
500  if (command.size() == 1)
501  {
502  wall_the_robot(1.0, 1.0);
503  wall_the_robot(1.0, 1.05);
504  reply.addVocab(VOCAB_OK);
505  }
506  else if (command.size() == 2)
507  {
508  wall_the_robot(command.get(1).asFloat64(), 1.0);
509  wall_the_robot(command.get(1).asFloat64(), 1.05);
510  reply.addVocab(VOCAB_OK);
511  }
512  else if (command.size() == 3)
513  {
514  wall_the_robot(command.get(1).asFloat64(), command.get(2).asFloat64());
515  wall_the_robot(command.get(1).asFloat64(), command.get(2).asFloat64()+0.05);
516  reply.addVocab(VOCAB_OK);
517  }
518  else
519  {
520  reply.addVocab(VOCAB_ERR);
521  }
522  }
523  else if (command.get(0).asString() == "free")
524  {
525  free_the_robot();
526  reply.addVocab(VOCAB_OK);
527  }
528  else if (command.get(0).asString() == "help")
529  {
530  reply.addVocab(yarp::os::Vocab::encode("many"));
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");
534  }
535  else
536  {
537  yCError(FAKE_LASER) << "Invalid command";
538  reply.addVocab(VOCAB_ERR);
539  }
540 
541  yarp::os::ConnectionWriter* returnToSender = connection.getWriter();
542  if (returnToSender != nullptr)
543  {
544  reply.write(*returnToSender);
545  }
546  return true;
547 }
548 
549 
550 void FakeLaser::drawStraightLine(XYCell src, XYCell dst)
551 {
552  long int x, y;
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;
556  dx1 = abs(dx);
557  dy1 = abs(dy);
558  px = 2 * dy1 - dx1;
559  py = 2 * dx1 - dy1;
560  if (dy1 <= dx1)
561  {
562  if (dx >= 0)
563  {
564  x = (long int)src.x;
565  y = (long int)src.y;
566  xe = (long int)dst.x;
567  }
568  else
569  {
570  x = (long int)dst.x;
571  y = (long int)dst.y;
572  xe = (long int)src.x;
573  }
574  m_map.setMapFlag(XYCell(x, y), yarp::dev::Nav2D::MapGrid2D::map_flags::MAP_CELL_WALL);
575  for (i = 0; x < xe; i++)
576  {
577  x = x + 1;
578  if (px < 0)
579  {
580  px = px + 2 * dy1;
581  }
582  else
583  {
584  if ((dx < 0 && dy < 0) || (dx > 0 && dy > 0))
585  {
586  y = y + 1;
587  }
588  else
589  {
590  y = y - 1;
591  }
592  px = px + 2 * (dy1 - dx1);
593  }
594  m_map.setMapFlag(XYCell(x, y), yarp::dev::Nav2D::MapGrid2D::map_flags::MAP_CELL_WALL);
595  }
596  }
597  else
598  {
599  if (dy >= 0)
600  {
601  x = (long int)src.x;
602  y = (long int)src.y;
603  ye = (long int)dst.y;
604  }
605  else
606  {
607  x = (long int)dst.x;
608  y = (long int)dst.y;
609  ye = (long int)src.y;
610  }
611  m_map.setMapFlag(XYCell(x, y), yarp::dev::Nav2D::MapGrid2D::map_flags::MAP_CELL_WALL);
612  for (i = 0; y < ye; i++)
613  {
614  y = y + 1;
615  if (py <= 0)
616  {
617  py = py + 2 * dx1;
618  }
619  else
620  {
621  if ((dx < 0 && dy < 0) || (dx > 0 && dy > 0))
622  {
623  x = x + 1;
624  }
625  else
626  {
627  x = x - 1;
628  }
629  py = py + 2 * (dx1 - dy1);
630  }
631  m_map.setMapFlag(XYCell(x, y), yarp::dev::Nav2D::MapGrid2D::map_flags::MAP_CELL_WALL);
632  }
633  }
634 }
635 
636 double FakeLaser::checkStraightLine(XYCell src, XYCell dst)
637 {
638  //BEWARE: src and dest must be already clipped and >0 in this function
639  XYCell test_point;
640  test_point.x = src.x;
641  test_point.y = src.y;
642 
643  //here using the fast Bresenham algorithm
644  int dx = abs(int(dst.x - src.x));
645  int dy = abs(int(dst.y - src.y));
646  int err = dx - dy;
647 
648  int sx;
649  int sy;
650  if (src.x < dst.x) { sx = 1; } else { sx = -1; }
651  if (src.y < dst.y) { sy = 1; } else { sy = -1; }
652 
653  while (true)
654  {
655  //the test point is going to move from src until it reaches dst OR
656  //if it reaches the boundaries of the image
657  if (test_point.x==0 || test_point.y ==0 || test_point.x>=m_map.width() || test_point.y>=m_map.height())
658  {
659  break;
660  }
661 
662  //if (m_map.isFree(src) == false)
663  if (m_map.isWall(XYCell(test_point.x,test_point.y)))
664  {
665  XYWorld world_start = m_map.cell2World(src);
666  XYWorld world_end = m_map.cell2World(XYCell(test_point.x, test_point.y));
667  double dist = sqrt(pow(world_start.x - world_end.x, 2) + pow(world_start.y - world_end.y, 2));
668  return dist;
669  }
670 
671  if (test_point.x == dst.x && test_point.y == dst.y)
672  {
673  break;
674  }
675 
676  int e2 = err * 2;
677  if (e2 > -dy)
678  {
679  err = err - dy;
680  test_point.x += sx;
681  }
682  if (e2 < dx)
683  {
684  err = err + dx;
685  test_point.y += sy;
686  }
687  }
688  return std::numeric_limits<double>::infinity();
689 }
690 
692 {
693 #ifdef LASER_DEBUG
694  yCDebug(FAKE_LASER) << "FakeLaser Thread releasing...");
695  yCDebug(FAKE_LASER) << "... done.");
696 #endif
697 }
LogStream.h
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
FakeLaser::m_const_value
double m_const_value
Definition: fakeLaser.h:89
yarp::dev::Lidar2DDeviceBase::m_laser_data
yarp::sig::Vector m_laser_data
Definition: Lidar2DDeviceBase.h:37
yarp::os::Property::put
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition: Property.cpp:998
yarp::os::Bottle::clear
void clear()
Empties the bottle of any objects it contains.
Definition: Bottle.cpp:124
FakeLaser::run
void run() override
Loop function.
Definition: fakeLaser.cpp:265
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
FakeLaser::m_period
double m_period
Definition: fakeLaser.h:74
FakeLaser::close
bool close() override
Close the DeviceDriver.
Definition: fakeLaser.cpp:167
yarp::os::Bottle::size
size_type size() const
Gets the number of elements in the bottle.
Definition: Bottle.cpp:254
yarp::dev::Map2DLocationData::y
double y
Definition: Map2DLocationData.h:32
t
float t
Definition: FfmpegWriter.cpp:74
FakeLaser::USE_MAPFILE
@ USE_MAPFILE
Definition: fakeLaser.h:67
yarp::os::BufferedPort::read
T * read(bool shouldWait=true) override
Read an available object from the port.
Definition: BufferedPort-inl.h:154
yarp::math::Vec2D::x
T x
Definition: Vec2D.h:27
yarp::os::Searchable::findGroup
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
yarp::dev::Lidar2DDeviceBase::m_device_status
yarp::dev::IRangefinder2D::Device_status m_device_status
Definition: Lidar2DDeviceBase.h:38
yarp::dev::Lidar2DDeviceBase::applyLimitsOnLaserData
void applyLimitsOnLaserData()
Definition: Lidar2DDeviceBase.cpp:226
yarp::dev::Nav2D::XYWorld
yarp::math::Vec2D< double > XYWorld
Definition: NavTypes.h:25
YARP_LOG_COMPONENT
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
yarp::dev::Lidar2DDeviceBase::m_resolution
double m_resolution
Definition: Lidar2DDeviceBase.h:50
FakeLaser::LOC_FROM_CLIENT
@ LOC_FROM_CLIENT
Definition: fakeLaser.h:68
FakeLaser::m_loc_mode
localization_mode_t m_loc_mode
Definition: fakeLaser.h:72
FakeLaser::threadInit
bool threadInit() override
Initialization method.
Definition: fakeLaser.cpp:215
FakeLaser::setHorizontalResolution
bool setHorizontalResolution(double step) override
get the angular step between two measurments (if available)
Definition: fakeLaser.cpp:199
yarp::os::BufferedPort::interrupt
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition: BufferedPort-inl.h:82
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
yarp::sig::VectorOf::clear
void clear()
Definition: Vector.h:521
FakeLaser::read
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
Definition: fakeLaser.cpp:471
FakeLaser::m_test_mode
test_mode_t m_test_mode
Definition: fakeLaser.h:71
yarp::dev::Map2DLocationData::theta
double theta
Definition: Map2DLocationData.h:33
yarp::dev::Lidar2DDeviceBase::m_min_angle
double m_min_angle
Definition: Lidar2DDeviceBase.h:46
FakeLaser::setDistanceRange
bool setDistanceRange(double min, double max) override
set the device detection range.
Definition: fakeLaser.cpp:181
yarp::math::Vec2D
Definition: Vec2D.h:26
yarp::os::Time::now
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:124
yarp::dev::Nav2D::MapGrid2DInfo::cell2World
XYWorld cell2World(XYCell cell) const
Definition: MapGrid2DInfo.cpp:65
FAKE_LASER
const yarp::os::LogComponent & FAKE_LASER()
Definition: fakeLaser.cpp:28
yarp::os::BufferedPort< yarp::os::Bottle >
Log.h
FakeLaser::m_iLoc
yarp::dev::Nav2D::ILocalization2D * m_iLoc
Definition: fakeLaser.h:79
FakeLaser::threadRelease
void threadRelease() override
Release method.
Definition: fakeLaser.cpp:691
yarp::dev::Lidar2DDeviceBase::m_sensorsNum
size_t m_sensorsNum
Definition: Lidar2DDeviceBase.h:45
yarp::dev::Nav2D::MapGrid2D::width
size_t width() const
Retrieves the map width, expressed in cells.
Definition: MapGrid2D.cpp:142
yarp::os::Bottle::get
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:249
yarp::os::Bottle::write
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
Definition: Bottle.cpp:233
yarp::os::ConnectionWriter
An interface for writing to a network connection.
Definition: ConnectionWriter.h:40
FakeLaser::m_robot_loc_y
double m_robot_loc_y
Definition: fakeLaser.h:83
FakeLaser::driver
yarp::dev::PolyDriver driver
Definition: fakeLaser.h:70
yarp::dev::Lidar2DDeviceBase::m_max_distance
double m_max_distance
Definition: Lidar2DDeviceBase.h:49
FakeLaser::m_robot_loc_t
double m_robot_loc_t
Definition: fakeLaser.h:84
yarp::os::Value::asString
virtual std::string asString() const
Get string value.
Definition: Value.cpp:237
yarp::os::PeriodicThread::start
bool start()
Call this to start the thread.
Definition: PeriodicThread.cpp:311
yarp::dev::Nav2D::MapGrid2DOrigin::get_ca
double get_ca() const
Definition: MapGrid2DInfo.h:49
yarp::dev::Nav2D::XYCell
yarp::math::Vec2D< size_t > XYCell
Definition: NavTypes.h:24
yarp::dev::PolyDriver
A container for a device driver.
Definition: PolyDriver.h:27
yarp::dev::PolyDriver::close
bool close() override
Close the DeviceDriver.
Definition: PolyDriver.cpp:176
yarp::os::BufferedPort::open
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: BufferedPort-inl.h:41
yarp::os::Searchable::check
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
FakeLaser::setScanRate
bool setScanRate(double rate) override
set the scan rate (scans per seconds)
Definition: fakeLaser.cpp:207
FakeLaser::USE_PATTERN
@ USE_PATTERN
Definition: fakeLaser.h:67
yarp::os::Vocab::encode
NetInt32 encode(const std::string &str)
Convert a string into a vocabulary identifier.
Definition: Vocab.cpp:14
FakeLaser::m_robot_loc_x
double m_robot_loc_x
Definition: fakeLaser.h:82
yarp::os::Property::check
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Definition: Property.cpp:1024
yarp::os::ConnectionReader::getWriter
virtual ConnectionWriter * getWriter()=0
Gets a way to reply to the message, if possible.
FakeLaser::NO_OBSTACLES
@ NO_OBSTACLES
Definition: fakeLaser.h:67
yarp::os::Bottle::addString
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition: Bottle.cpp:173
DEG2RAD
#define DEG2RAD
Definition: fakeLaser.cpp:25
FakeLaser::m_map
yarp::dev::Nav2D::MapGrid2D m_map
Definition: fakeLaser.h:76
yarp::os::Bottle::addVocab
void addVocab(int x)
Places a vocabulary item in the bottle, at the end of the list.
Definition: Bottle.cpp:167
FakeLaser::setScanLimits
bool setScanLimits(double min, double max) override
set the scan angular range.
Definition: fakeLaser.cpp:190
FakeLaser::m_originally_loaded_map
yarp::dev::Nav2D::MapGrid2D m_originally_loaded_map
Definition: fakeLaser.h:75
yarp::dev::Nav2D::MapGrid2DInfo::m_height
size_t m_height
cells
Definition: MapGrid2DInfo.h:62
VOCAB_ERR
constexpr yarp::conf::vocab32_t VOCAB_ERR
Definition: GenericVocabs.h:20
yarp::dev::Nav2D::Map2DLocation
Definition: Map2DLocation.h:30
yarp::os::ConnectionReader
An interface for reading from a network connection.
Definition: ConnectionReader.h:40
fakeLaser.h
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
yCInfo
#define yCInfo(component,...)
Definition: LogComponent.h:135
yCDebug
#define yCDebug(component,...)
Definition: LogComponent.h:112
yarp::dev::Nav2D::MapGrid2DOrigin::get_sa
double get_sa() const
Definition: MapGrid2DInfo.h:50
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::dev::IRangefinder2D::DEVICE_OK_IN_USE
@ DEVICE_OK_IN_USE
Definition: IRangefinder2D.h:43
Vec2D.h
yarp::dev::Lidar2DDeviceBase::m_min_distance
double m_min_distance
Definition: Lidar2DDeviceBase.h:48
FakeLaser::LOC_FROM_PORT
@ LOC_FROM_PORT
Definition: fakeLaser.h:68
yarp::os::BufferedPort::close
void close() override
Stop port activity.
Definition: BufferedPort-inl.h:73
yarp::os::Bottle::read
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Definition: Bottle.cpp:243
FakeLaser::LOC_NOT_SET
@ LOC_NOT_SET
Definition: fakeLaser.h:68
yDebug
#define yDebug(...)
Definition: Log.h:237
FakeLaser
fakeLaser : fake sensor device driver for testing purposes and reference for IRangefinder2D devices.
Definition: fakeLaser.h:65
yarp::dev::Nav2D::MapGrid2DInfo::world2Cell
XYCell world2Cell(XYWorld world) const
Definition: MapGrid2DInfo.cpp:83
FakeLaser::USE_CONSTANT_VALUE
@ USE_CONSTANT_VALUE
Definition: fakeLaser.h:67
Time.h
yarp::dev::Map2DLocationData::x
double x
Definition: Map2DLocationData.h:31
yarp::dev::Nav2D::MapGrid2DInfo::m_origin
MapGrid2DOrigin m_origin
pose of the map frame w.r.t. the bottom left corner of the map image
Definition: MapGrid2DInfo.h:60
yarp::os::PeriodicThread::step
void step()
Call this to "step" the thread rather than starting it.
Definition: PeriodicThread.cpp:306
yarp::dev::Nav2D::MapGrid2D::setMapFlag
bool setMapFlag(XYCell cell, map_flags flag)
Set the flag of a specific cell of the map.
Definition: MapGrid2D.cpp:962
yarp::dev::Nav2D::ILocalization2D::getCurrentPosition
virtual bool getCurrentPosition(yarp::dev::Nav2D::Map2DLocation &loc)=0
Gets the current position of the robot w.r.t world reference frame.
yarp::os::Value
A single value (typically within a Bottle).
Definition: Value.h:47
yarp::dev::Nav2D::MapGrid2D::height
size_t height() const
Retrieves the map height, expressed in cells.
Definition: MapGrid2D.cpp:137
yarp::dev::Nav2D::MapGrid2D::isWall
bool isWall(XYCell cell) const
Checks if a specific cell of the map contains a wall.
Definition: MapGrid2D.cpp:125
yarp::os::Value::asFloat64
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
Definition: Value.cpp:225
FakeLaser::m_loc_port
yarp::os::BufferedPort< yarp::os::Bottle > * m_loc_port
Definition: fakeLaser.h:77
yarp::sig::VectorOf::push_back
void push_back(const T &elem)
Push a new element in the vector: size is changed.
Definition: Vector.h:282
yarp::dev::Nav2D::MapGrid2D::getResolution
void getResolution(double &resolution) const
Retrieves the resolution of the map, i.e.
Definition: MapGrid2D.cpp:895
VOCAB_OK
constexpr yarp::conf::vocab32_t VOCAB_OK
Definition: GenericVocabs.h:18
yarp::math::Vec2D::y
T y
Definition: Vec2D.h:31
yarp::dev::Lidar2DDeviceBase::m_max_angle
double m_max_angle
Definition: Lidar2DDeviceBase.h:47
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:37
yarp::dev::Lidar2DDeviceBase::m_mutex
std::mutex m_mutex
Definition: Lidar2DDeviceBase.h:39