YARP
Yet Another Robot Platform
fakeLocalizerDev.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "fakeLocalizerDev.h"
20 
21 #include <yarp/os/Network.h>
22 #include <yarp/os/RFModule.h>
23 #include <yarp/os/Time.h>
24 #include <yarp/os/Port.h>
25 #include <yarp/os/LogComponent.h>
26 #include <yarp/os/LogStream.h>
27 #include <yarp/os/Node.h>
28 #include <yarp/dev/PolyDriver.h>
29 #include <yarp/os/Bottle.h>
30 #include <yarp/sig/Vector.h>
31 #include <yarp/dev/INavigation2D.h>
33 
34 #include <cmath>
35 #include <random>
36 #include <mutex>
37 #include <chrono>
38 
39 using namespace yarp::os;
40 using namespace yarp::dev;
41 using namespace yarp::dev::Nav2D;
42 
43 #ifndef M_PI
44 #define M_PI 3.14159265358979323846
45 #endif
46 
47 #define RAD2DEG 180/M_PI
48 #define DEG2RAD M_PI/180
49 
50 namespace {
51 YARP_LOG_COMPONENT(FAKELOCALIZER, "yarp.device.fakeLocalizer")
52 }
53 
55 {
57  return true;
58 }
59 
60 bool fakeLocalizer::getEstimatedPoses(std::vector<Map2DLocation>& poses)
61 {
62  poses.clear();
63  Map2DLocation loc;
64  locThread->getCurrentLoc(loc);
65  poses.push_back(loc);
66 #if 0
67  //The following block is used only for development and debug purposes.
68  //It should be never used in a real scenario
69  for (int i = 0; i < 10; i++)
70  {
71  yarp::dev::Map2DLocation newloc=loc;
72  unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
73  std::default_random_engine generator(seed);
74  std::uniform_real_distribution<double> dist(-1, 1);
75  std::uniform_real_distribution<double> dist_t(-180, 180);
76  double numberx = dist(generator);
77  double numbery = dist(generator);
78  double numbert = dist_t(generator);
79  newloc.x += numberx;
80  newloc.y += numbery;
81  newloc.theta += numbert;
82  poses.push_back(newloc);
83  }
84 #endif
85  return true;
86 }
87 
89 {
90  locThread->getCurrentLoc(loc);
91  return true;
92 }
93 
95 {
96  locThread->initializeLocalization(loc);
97  return true;
98 }
99 
101 {
102  locThread->getCurrentLoc(loc);
103  return true;
104 }
105 
107 {
108  locThread->initializeLocalization(loc);
109  return true;
110 }
111 
113 {
114  Map2DLocation loc;
115  locThread->getCurrentLoc(loc);
116  odom.odom_x = loc.x;
117  odom.odom_y = loc.y;
118  odom.odom_theta = loc.theta;
119  return true;
120 }
121 
123 
125 {
128 
133 }
134 
136 {
137  double current_time = yarp::os::Time::now();
138 
139  //print some stats every 10 seconds
140  if (current_time - m_last_statistics_printed > 10.0)
141  {
143  }
144 
145  std::lock_guard<std::mutex> lock(m_mutex);
146  yarp::sig::Vector loc(3);
147  loc[0] = 0.0;
148  loc[1] = 0.0;
149  loc[2] = 0.0;
150  if (1)
151  {
153  m_current_odom.x = loc.data()[0];
154  m_current_odom.y = loc.data()[1];
155  m_current_odom.theta = loc.data()[2];
156 
157  double c = cos((-m_initial_odom.theta + m_initial_loc.theta)*DEG2RAD);
158  double s = sin((-m_initial_odom.theta + m_initial_loc.theta)*DEG2RAD);
159  double df_x = (m_current_odom.x - m_initial_odom.x);
160  double df_y = (m_current_odom.y - m_initial_odom.y);
161  m_current_loc.x = df_x * c + df_y * -s + m_initial_loc.x;
162  m_current_loc.y = df_x * s + df_y * +c + m_initial_loc.y;
163 
165 
166  if (m_current_loc.theta >= +360) m_current_loc.theta -= 360;
167  else if (m_current_loc.theta <= -360) m_current_loc.theta += 360;
168  }
169  if (current_time - m_last_locdata_received > 0.1)
170  {
171  yCWarning(FAKELOCALIZER) << "No localization data received for more than 0.1s!";
172  }
173 }
174 
176 {
177  yCInfo(FAKELOCALIZER) << "Localization init request: (" << loc.map_id << ")";
178  std::lock_guard<std::mutex> lock(m_mutex);
180  m_initial_loc.x = loc.x;
181  m_initial_loc.y = loc.y;
182  m_initial_loc.theta = loc.theta;
186 
188  {
189  yCInfo(FAKELOCALIZER) << "Map changed from: " << m_current_loc.map_id << " to: " << m_initial_loc.map_id;
191  //@@@TO BE COMPLETED
195  }
196  return true;
197 }
198 
200 {
201  std::lock_guard<std::mutex> lock(m_mutex);
202  loc = m_current_loc;
203  return true;
204 }
205 
207 {
208  Map2DLocation loc;
209  loc.map_id="test";
210  loc.x = 0;
211  loc.y = 0;
212  loc.theta = 0;
213  this->initializeLocalization(loc);
214  return true;
215 }
216 
218 {
219 
220 }
221 
222 
224 {
226  locThread = new fakeLocalizerThread(0.010, p);
227 
228  if (!locThread->start())
229  {
230  delete locThread;
231  locThread = NULL;
232  return false;
233  }
234 
235  return true;
236 }
237 
239 {
240  locThread = NULL;
241 }
242 
244 {
245  if (locThread)
246  {
247  delete locThread;
248  locThread = NULL;
249  }
250 }
251 
253 {
254  return true;
255 }
256 
258 {
259  return true;
260 }
261 
263 {
264  return true;
265 }
LogStream.h
INavigation2D.h
Network.h
fakeLocalizerThread::m_current_odom
yarp::dev::Nav2D::Map2DLocation m_current_odom
Definition: fakeLocalizerDev.h:46
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
fakeLocalizerThread::m_last_locdata_received
double m_last_locdata_received
Definition: fakeLocalizerDev.h:42
Vector.h
contains the definition of a Vector type
yarp::dev::Map2DLocationData::y
double y
Definition: Map2DLocationData.h:32
yCWarning
#define yCWarning(component,...)
Definition: LogComponent.h:146
Port.h
fakeLocalizer::stopLocalizationService
virtual bool stopLocalizationService() override
Stops the localization service.
Definition: fakeLocalizerDev.cpp:257
fakeLocalizerThread::threadRelease
virtual void threadRelease() override
Release method.
Definition: fakeLocalizerDev.cpp:217
fakeLocalizer::getEstimatedOdometry
virtual bool getEstimatedOdometry(yarp::dev::OdometryData &odom) override
Gets the estimated odometry the robot, including its velocity expressed in the world and in the local...
Definition: fakeLocalizerDev.cpp:112
YARP_LOG_COMPONENT
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
fakeLocalizerThread::m_initial_loc
yarp::dev::Nav2D::Map2DLocation m_initial_loc
Definition: fakeLocalizerDev.h:43
yarp::dev::Nav2D::LocalizationStatusEnum
LocalizationStatusEnum
Definition: ILocalization2D.h:25
fakeLocalizer::close
virtual bool close() override
Close the DeviceDriver.
Definition: fakeLocalizerDev.cpp:262
fakeLocalizer::getEstimatedPoses
bool getEstimatedPoses(std::vector< yarp::dev::Nav2D::Map2DLocation > &poses) override
Gets a set of pose estimates computed by the localization algorithm.
Definition: fakeLocalizerDev.cpp:60
ControlBoardInterfaces.h
define control board standard interfaces
yarp::dev::Map2DLocationData::theta
double theta
Definition: Map2DLocationData.h:33
yarp::os::Time::now
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:124
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
yarp::dev::OdometryData
Definition: OdometryData.h:27
fakeLocalizer::fakeLocalizer
fakeLocalizer()
Definition: fakeLocalizerDev.cpp:238
fakeLocalizerThread::m_current_loc
yarp::dev::Nav2D::Map2DLocation m_current_loc
Definition: fakeLocalizerDev.h:45
fakeLocalizerThread
Definition: fakeLocalizerDev.h:38
yarp::sig::VectorOf< double >
fakeLocalizerDev.h
fakeLocalizer::startLocalizationService
virtual bool startLocalizationService() override
Starts the localization service.
Definition: fakeLocalizerDev.cpp:252
fakeLocalizer::open
virtual bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: fakeLocalizerDev.cpp:223
fakeLocalizerThread::m_last_statistics_printed
double m_last_statistics_printed
Definition: fakeLocalizerDev.h:41
yarp::dev::Nav2D::localization_status_localized_ok
@ localization_status_localized_ok
Definition: ILocalization2D.h:27
fakeLocalizerThread::m_initial_odom
yarp::dev::Nav2D::Map2DLocation m_initial_odom
Definition: fakeLocalizerDev.h:44
PolyDriver.h
yarp::dev::OdometryData::odom_theta
double odom_theta
orientation the robot [deg], expressed in the world reference frame
Definition: OdometryData.h:41
fakeLocalizerThread::initializeLocalization
bool initializeLocalization(const yarp::dev::Nav2D::Map2DLocation &loc)
Definition: fakeLocalizerDev.cpp:175
fakeLocalizerThread::threadInit
virtual bool threadInit() override
Initialization method.
Definition: fakeLocalizerDev.cpp:206
yarp::os::PeriodicThread::start
bool start()
Call this to start the thread.
Definition: PeriodicThread.cpp:311
Node.h
fakeLocalizer::locThread
fakeLocalizerThread * locThread
Definition: fakeLocalizerDev.h:72
fakeLocalizer::getLocalizationStatus
bool getLocalizationStatus(yarp::dev::Nav2D::LocalizationStatusEnum &status) override
Gets the current status of the localization task.
Definition: fakeLocalizerDev.cpp:54
DEG2RAD
#define DEG2RAD
Definition: fakeLocalizerDev.cpp:48
RFModule.h
yarp::dev::Nav2D
Definition: ILocalization2D.h:21
yarp::os::PeriodicThread
An abstraction for a periodic thread.
Definition: PeriodicThread.h:25
LogComponent.h
yarp::dev::Nav2D::Map2DLocation
Definition: Map2DLocation.h:30
yarp::sig::VectorOf::data
T * data()
Return a pointer to the first element of the vector.
Definition: Vector.h:239
fakeLocalizerThread::run
virtual void run() override
Loop function.
Definition: fakeLocalizerDev.cpp:135
yCInfo
#define yCInfo(component,...)
Definition: LogComponent.h:135
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
fakeLocalizer::getCurrentPosition
bool getCurrentPosition(yarp::dev::Nav2D::Map2DLocation &loc) override
Gets the current position of the robot w.r.t world reference frame.
Definition: fakeLocalizerDev.cpp:88
yarp::dev::OdometryData::odom_y
double odom_y
position of the robot [m], expressed in the world reference frame
Definition: OdometryData.h:37
yarp::dev::OdometryData::odom_x
double odom_x
position of the robot [m], expressed in the world reference frame
Definition: OdometryData.h:33
fakeLocalizer::~fakeLocalizer
virtual ~fakeLocalizer()
Definition: fakeLocalizerDev.cpp:243
Time.h
yarp::dev::Map2DLocationData::x
double x
Definition: Map2DLocationData.h:31
yarp::dev::Map2DLocationData::map_id
std::string map_id
Definition: Map2DLocationData.h:30
fakeLocalizerThread::m_mutex
std::mutex m_mutex
Definition: fakeLocalizerDev.h:47
fakeLocalizerThread::fakeLocalizerThread
fakeLocalizerThread(double _period, yarp::os::Searchable &_cfg)
Definition: fakeLocalizerDev.cpp:124
Bottle.h
fakeLocalizerThread::getCurrentLoc
bool getCurrentLoc(yarp::dev::Nav2D::Map2DLocation &loc)
Definition: fakeLocalizerDev.cpp:199
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:37
fakeLocalizer::setInitialPose
bool setInitialPose(const yarp::dev::Nav2D::Map2DLocation &loc) override
Sets the initial pose for the localization algorithm which estimates the current position of the robo...
Definition: fakeLocalizerDev.cpp:94
yarp::sig::Matrix
A class for a Matrix.
Definition: Matrix.h:46