YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
FakeLaserWithMotor_laser.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#define _USE_MATH_DEFINES
7
9
10#include <yarp/os/Time.h>
11#include <yarp/os/Log.h>
12#include <yarp/os/LogStream.h>
14#include <yarp/math/Vec2D.h>
15#include <iostream>
16#include <limits>
17#include <cstring>
18#include <cstdlib>
19#include <cmath>
20
21 //#define LASER_DEBUG
22#ifndef DEG2RAD
23#define DEG2RAD M_PI/180.0
24#endif
25
26YARP_LOG_COMPONENT(FAKE_LASER_LASER, "yarp.devices.fakeLaserWithMotor")
27
30using namespace yarp::dev::Nav2D;
31
32bool FakeLaserWithMotor::setDistanceRange(double min, double max)
33{
34 m_mutex.lock();
35 m_min_distance = min;
36 m_max_distance = max;
37 m_mutex.unlock();
38 return true;
39}
40
41bool FakeLaserWithMotor::setScanLimits(double min, double max)
42{
43 m_mutex.lock();
44 m_min_angle = min;
45 m_max_angle = max;
46 m_mutex.unlock();
47 return true;
48}
49
51{
52 m_mutex.lock();
54 m_mutex.unlock();
55 return true;
56}
57
59{
60 m_mutex.lock();
61 m_GENERAL_period = (1.0 / rate);
62 m_mutex.unlock();
63 return false;
64}
65
67{
69 double t = yarp::os::Time::now();
70 static double t_orig = yarp::os::Time::now();
71 double size = (t - (t_orig));
72
73 static int test_count = 0;
74 static int test = 0;
75
77 {
78 for (size_t i = 0; i < m_sensorsNum; i++)
79 {
80 double value = 0;
81 if (test == 0)
82 {
83 value = i / 100.0;
84 }
85 else if (test == 1)
86 {
87 value = size * 2;
88 }
89 else if (test == 2)
90 {
91 if (i <= 10) { value = 1.0 + i / 20.0; }
92 else if (i >= 90 && i <= 100) { value = 2.0 + (i - 90) / 20.0; }
93 else { value = m_min_distance; }
94 }
96 }
97
98 test_count++;
99 if (test_count == 60)
100 {
101 test_count = 0; test++; if (test > 2) { test = 0; }
103 }
104 }
105 else if (m_test_mode == NO_OBSTACLES)
106 {
107 for (size_t i = 0; i < m_sensorsNum; i++)
108 {
109 m_laser_data.push_back(std::numeric_limits<double>::infinity());
110 }
111 }
113 {
114 for (size_t i = 0; i < m_sensorsNum; i++)
115 {
117 }
118 }
119 else if (m_test_mode == USE_MAPFILE ||
121 {
122 m_robot_loc_x = _encoders[0];//loc.x;
123 m_robot_loc_y = _encoders[1];//loc.y;
124 m_robot_loc_t = _encoders[2];//loc.theta;
125
126 for (size_t i = 0; i < m_sensorsNum; i++)
127 {
128 //compute the ray in the robot reference frame
129 double ray_angle = i * m_resolution + m_min_angle;
132
133 //transforms the ray from the robot to the world reference frame
138
139 //beware! src is the robot position and it is always inside the image.
140 //dst is the end of the ray, and it can be out of the image!
141 //for this reason i am not going to call world2Cell() on dst, because that functions clips the point to the border without
142 //knowing the angular coefficient of the ray. I thus need the unclipped value, and run the LiangBarsky algorithm
143 //(which knows the angular coefficient of the ray) on it.
148 dst.x = int((+ray_world_rot.x - this->m_map.m_origin.get_x()) / this->m_map.m_resolution) + 0;
149 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;
150
153 double distance;
154 if (LiangBarsky_clip(0, (int)m_map.width(), 0, (int)m_map.height(),
155 XYCell_unbounded(src.x, src.y), dst, newsrc, newdst))
156 {
157 distance = checkStraightLine(src, newdst);
158 double simulated_noise = (*m_dis)(*m_gen);
160 }
161 else
162 {
163 //This should never happen, unless the robot is outside the map!
164 yDebug() << "Robot is outside the map?!";
165 m_laser_data.push_back(std::numeric_limits<double>::infinity());
166 }
167
168 }
169 }
170
171 //set the device status
173
174 return true;
175}
const yarp::os::LogComponent & FAKE_LASER_LASER()
float t
#define DEG2RAD
#define yDebug(...)
Definition Log.h:275
fakeLaserWithMotor : fake sensor device driver for testing purposes and reference for IRangefinder2D ...
bool setScanRate(double rate) override
set the scan rate (scans per seconds)
bool setHorizontalResolution(double step) override
get the angular step between two measurements (if available)
bool setScanLimits(double min, double max) override
set the scan angular 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::MapGrid2D m_map
yarp::dev::IRangefinder2D::Device_status m_device_status
XYCell world2Cell(XYWorld world) 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.
size_t width() const
Retrieves the map width, expressed in cells.
A mini-server for performing network communication in the background.
void step()
Call this to "step" the thread rather than starting it.
void push_back(const T &elem)
Push a new element in the vector: size is changed.
Definition Vector.h:249
#define YARP_LOG_COMPONENT(name,...)
yarp::math::Vec2D< double > XYWorld
Definition NavTypes.h:17
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition Time.cpp:121
The main, catch-all namespace for YARP.
Definition dirs.h:16