YARP
Yet Another Robot Platform
fakeLocalizerDev.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: LGPL-2.1-or-later
4  */
5 
6 #include "fakeLocalizerDev.h"
7 
8 #include <yarp/os/Network.h>
9 #include <yarp/os/RFModule.h>
10 #include <yarp/os/Time.h>
11 #include <yarp/os/Port.h>
12 #include <yarp/os/LogComponent.h>
13 #include <yarp/os/LogStream.h>
14 #include <yarp/os/Node.h>
15 #include <yarp/dev/PolyDriver.h>
16 #include <yarp/os/Bottle.h>
17 #include <yarp/sig/Vector.h>
18 #include <yarp/dev/INavigation2D.h>
20 
21 #include <cmath>
22 #include <random>
23 #include <mutex>
24 #include <chrono>
25 
26 using namespace yarp::os;
27 using namespace yarp::dev;
28 using namespace yarp::dev::Nav2D;
29 
30 #ifndef M_PI
31 #define M_PI 3.14159265358979323846
32 #endif
33 
34 #define RAD2DEG 180/M_PI
35 #define DEG2RAD M_PI/180
36 
37 namespace {
38 YARP_LOG_COMPONENT(FAKELOCALIZER, "yarp.device.fakeLocalizer")
39 }
40 
42 {
44  return true;
45 }
46 
47 bool fakeLocalizer::getEstimatedPoses(std::vector<Map2DLocation>& poses)
48 {
49  poses.clear();
50  Map2DLocation loc;
51  locThread->getCurrentLoc(loc);
52  poses.push_back(loc);
53 #if 0
54  //The following block is used only for development and debug purposes.
55  //It should be never used in a real scenario
56  for (int i = 0; i < 10; i++)
57  {
58  yarp::dev::Map2DLocation newloc=loc;
59  unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
60  std::default_random_engine generator(seed);
61  std::uniform_real_distribution<double> dist(-1, 1);
62  std::uniform_real_distribution<double> dist_t(-180, 180);
63  double numberx = dist(generator);
64  double numbery = dist(generator);
65  double numbert = dist_t(generator);
66  newloc.x += numberx;
67  newloc.y += numbery;
68  newloc.theta += numbert;
69  poses.push_back(newloc);
70  }
71 #endif
72  return true;
73 }
74 
76 {
77  locThread->getCurrentLoc(loc);
78  return true;
79 }
80 
82 {
83  locThread->initializeLocalization(loc);
84  return true;
85 }
86 
88 {
89  locThread->getCurrentLoc(loc);
90  return true;
91 }
92 
94 {
95  locThread->initializeLocalization(loc);
96  return true;
97 }
98 
100 {
101  Map2DLocation loc;
102  locThread->getCurrentLoc(loc);
103  odom.odom_x = loc.x;
104  odom.odom_y = loc.y;
105  odom.odom_theta = loc.theta;
106  return true;
107 }
108 
110 
112 {
115 
120 }
121 
123 {
124  double current_time = yarp::os::Time::now();
125 
126  //print some stats every 10 seconds
127  if (current_time - m_last_statistics_printed > 10.0)
128  {
130  }
131 
132  std::lock_guard<std::mutex> lock(m_mutex);
133  yarp::sig::Vector loc(3);
134  loc[0] = 0.0;
135  loc[1] = 0.0;
136  loc[2] = 0.0;
137  if (1)
138  {
140  m_current_odom.x = loc.data()[0];
141  m_current_odom.y = loc.data()[1];
142  m_current_odom.theta = loc.data()[2];
143 
144  double c = cos((-m_initial_odom.theta + m_initial_loc.theta)*DEG2RAD);
145  double s = sin((-m_initial_odom.theta + m_initial_loc.theta)*DEG2RAD);
146  double df_x = (m_current_odom.x - m_initial_odom.x);
147  double df_y = (m_current_odom.y - m_initial_odom.y);
148  m_current_loc.x = df_x * c + df_y * -s + m_initial_loc.x;
149  m_current_loc.y = df_x * s + df_y * +c + m_initial_loc.y;
150 
152 
153  if (m_current_loc.theta >= +360) {
154  m_current_loc.theta -= 360;
155  } else if (m_current_loc.theta <= -360) {
156  m_current_loc.theta += 360;
157  }
158  }
159  if (current_time - m_last_locdata_received > 0.1)
160  {
161  yCWarning(FAKELOCALIZER) << "No localization data received for more than 0.1s!";
162  }
163 }
164 
166 {
167  yCInfo(FAKELOCALIZER) << "Localization init request: (" << loc.map_id << ")";
168  std::lock_guard<std::mutex> lock(m_mutex);
170  m_initial_loc.x = loc.x;
171  m_initial_loc.y = loc.y;
172  m_initial_loc.theta = loc.theta;
176 
178  {
179  yCInfo(FAKELOCALIZER) << "Map changed from: " << m_current_loc.map_id << " to: " << m_initial_loc.map_id;
181  //@@@TO BE COMPLETED
185  }
186  return true;
187 }
188 
190 {
191  std::lock_guard<std::mutex> lock(m_mutex);
192  loc = m_current_loc;
193  return true;
194 }
195 
197 {
198  Map2DLocation loc;
199  loc.map_id="test";
200  loc.x = 0;
201  loc.y = 0;
202  loc.theta = 0;
203  this->initializeLocalization(loc);
204  return true;
205 }
206 
208 {
209 
210 }
211 
212 
214 {
216  locThread = new fakeLocalizerThread(0.010, p);
217 
218  if (!locThread->start())
219  {
220  delete locThread;
221  locThread = NULL;
222  return false;
223  }
224 
225  return true;
226 }
227 
229 {
230  locThread = NULL;
231 }
232 
234 {
235  if (locThread)
236  {
237  delete locThread;
238  locThread = NULL;
239  }
240 }
241 
243 {
244  return true;
245 }
246 
248 {
249  return true;
250 }
251 
253 {
254  return true;
255 }
define control board standard interfaces
contains the definition of a Vector type
virtual void run() override
Loop function.
yarp::dev::Nav2D::Map2DLocation m_current_loc
virtual bool threadInit() override
Initialization method.
yarp::dev::Nav2D::Map2DLocation m_current_odom
bool getCurrentLoc(yarp::dev::Nav2D::Map2DLocation &loc)
yarp::dev::Nav2D::Map2DLocation m_initial_loc
virtual void threadRelease() override
Release method.
fakeLocalizerThread(double _period, yarp::os::Searchable &_cfg)
bool initializeLocalization(const yarp::dev::Nav2D::Map2DLocation &loc)
yarp::dev::Nav2D::Map2DLocation m_initial_odom
virtual bool startLocalizationService() override
Starts the localization service.
virtual bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool getCurrentPosition(yarp::dev::Nav2D::Map2DLocation &loc) override
Gets the current position of the robot w.r.t world reference frame.
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...
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...
virtual ~fakeLocalizer()
virtual bool close() override
Close the DeviceDriver.
virtual bool stopLocalizationService() override
Stops the localization service.
bool getEstimatedPoses(std::vector< yarp::dev::Nav2D::Map2DLocation > &poses) override
Gets a set of pose estimates computed by the localization algorithm.
bool getLocalizationStatus(yarp::dev::Nav2D::LocalizationStatusEnum &status) override
Gets the current status of the localization task.
fakeLocalizerThread * locThread
double odom_x
position of the robot [m], expressed in the world reference frame
Definition: OdometryData.h:30
double odom_y
position of the robot [m], expressed in the world reference frame
Definition: OdometryData.h:34
double odom_theta
orientation the robot [deg], expressed in the world reference frame
Definition: OdometryData.h:38
An abstraction for a periodic thread.
bool start()
Call this to start the thread.
A class for storing options and configuration information.
Definition: Property.h:34
A base class for nested structures that can be searched.
Definition: Searchable.h:66
A class for a Matrix.
Definition: Matrix.h:43
T * data()
Return a pointer to the first element of the vector.
Definition: Vector.h:207
#define DEG2RAD
#define yCInfo(component,...)
Definition: LogComponent.h:132
#define yCWarning(component,...)
Definition: LogComponent.h:143
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:77
An interface for the device drivers.
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:121
An interface to the operating system, including Port based communication.