YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
FakeLocalizer.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 "FakeLocalizer.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>
13#include <yarp/os/LogStream.h>
14#include <yarp/dev/PolyDriver.h>
15#include <yarp/os/Bottle.h>
16#include <yarp/sig/Vector.h>
19
20#include <cmath>
21#include <random>
22#include <mutex>
23#include <chrono>
24
25using namespace yarp::os;
26using namespace yarp::dev;
27using namespace yarp::dev::Nav2D;
28
29#ifndef M_PI
30#define M_PI 3.14159265358979323846
31#endif
32
33#define RAD2DEG 180/M_PI
34#define DEG2RAD M_PI/180
35
36namespace {
37YARP_LOG_COMPONENT(FAKELOCALIZER, "yarp.device.fakeLocalizer")
38}
39
48
50{
51 std::lock_guard<std::mutex> lock(m_mutex);
53
54 poses.clear();
55 Map2DLocation loc;
57 poses.push_back(loc);
58#if 0
59 //The following block is used only for development and debug purposes.
60 //It should be never used in a real scenario
61 for (int i = 0; i < 10; i++)
62 {
63 yarp::dev::Map2DLocation newloc=loc;
64 unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
65 std::default_random_engine generator(seed);
66 std::uniform_real_distribution<double> dist(-1, 1);
67 std::uniform_real_distribution<double> dist_t(-180, 180);
68 double numberx = dist(generator);
69 double numbery = dist(generator);
70 double numbert = dist_t(generator);
71 newloc.x += numberx;
72 newloc.y += numbery;
73 newloc.theta += numbert;
74 poses.push_back(newloc);
75 }
76#endif
77 return yarp::dev::ReturnValue_ok;
78}
79
81{
82 std::lock_guard<std::mutex> lock(m_mutex);
84
86 return yarp::dev::ReturnValue_ok;
87}
88
90{
91 std::lock_guard<std::mutex> lock(m_mutex);
93
95 return yarp::dev::ReturnValue_ok;
96}
97
99{
100 std::lock_guard<std::mutex> lock(m_mutex);
102
103 locThread->getCurrentLoc(loc,cov);
104 return yarp::dev::ReturnValue_ok;
105}
106
108{
109 std::lock_guard<std::mutex> lock(m_mutex);
111
113 return yarp::dev::ReturnValue_ok;
114}
115
117{
118 std::lock_guard<std::mutex> lock(m_mutex);
120
121 Map2DLocation loc;
123 odom.odom_x = loc.x;
124 odom.odom_y = loc.y;
125 odom.odom_theta = loc.theta;
126 return yarp::dev::ReturnValue_ok;
127}
128
130{
131 if (!this->parseParams(config)) {return false;}
132
135
136 if (!locThread->start())
137 {
138 delete locThread;
139 locThread = NULL;
140 return false;
141 }
142
143 return true;
144}
145
149
153
155{
156 std::lock_guard<std::mutex> lock(m_mutex);
158
159 return yarp::dev::ReturnValue_ok;
160}
161
163{
164 std::lock_guard<std::mutex> lock(m_mutex);
166
167 return yarp::dev::ReturnValue_ok;
168}
169
171{
172 std::lock_guard<std::mutex> lock(m_mutex);
173 if (locThread)
174 {
175 locThread->stop();
176 delete locThread;
177 locThread = NULL;
178 }
179 return true;
180}
181
183
194
196{
198
199 //print some stats every 10 seconds
201 {
203 }
204
205 std::lock_guard<std::mutex> lock(m_mutex_thread);
206 yarp::sig::Vector loc(3);
207 loc[0] = 0.0;
208 loc[1] = 0.0;
209 loc[2] = 0.0;
210 if (1)
211 {
213 m_current_odom.x = loc.data()[0];
214 m_current_odom.y = loc.data()[1];
215 m_current_odom.theta = loc.data()[2];
216
219 double df_x = (m_current_odom.x - m_initial_odom.x);
220 double df_y = (m_current_odom.y - m_initial_odom.y);
221 m_current_loc.x = df_x * c + df_y * -s + m_initial_loc.x;
222 m_current_loc.y = df_x * s + df_y * +c + m_initial_loc.y;
223
225
226 if (m_current_loc.theta >= +360) {
227 m_current_loc.theta -= 360;
228 } else if (m_current_loc.theta <= -360) {
229 m_current_loc.theta += 360;
230 }
231 }
233 {
234 yCWarning(FAKELOCALIZER) << "No localization data received for more than 0.1s!";
235 }
236}
237
239{
240 yCInfo(FAKELOCALIZER) << "Localization init request: (" << loc.map_id << ")";
241 std::lock_guard<std::mutex> lock(m_mutex_thread);
243 m_initial_loc.x = loc.x;
244 m_initial_loc.y = loc.y;
249
251 {
252 yCInfo(FAKELOCALIZER) << "Map changed from: " << m_current_loc.map_id << " to: " << m_initial_loc.map_id;
254 //@@@TO BE COMPLETED
258 }
259 return true;
260}
261
263{
264 std::lock_guard<std::mutex> lock(m_mutex_thread);
265 loc = m_current_loc;
266 return true;
267}
268
270{
271 std::lock_guard<std::mutex> lock(m_mutex_thread);
272 loc = m_current_loc;
273 cov.resize(3,3);
274 cov.eye();
275 return true;
276}
277
279{
280 Map2DLocation loc;
281 loc.map_id="test";
282 loc.x = 0;
283 loc.y = 0;
284 loc.theta = 0;
285 this->initializeLocalization(loc);
286 return true;
287}
288
define control board standard interfaces
#define DEG2RAD
contains the definition of a Vector type
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
virtual bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
std::mutex m_mutex
fakeLocalizerThread * locThread
virtual bool close() override
Close the DeviceDriver.
yarp::dev::ReturnValue getLocalizationStatus(yarp::dev::Nav2D::LocalizationStatusEnum &status) override
Gets the current status of the localization task.
yarp::dev::ReturnValue startLocalizationService() override
Starts the localization service.
yarp::dev::ReturnValue stopLocalizationService() override
Stops the localization service.
virtual ~FakeLocalizer()
yarp::dev::ReturnValue setInitialPose(const yarp::dev::Nav2D::Map2DLocation &loc) override
Sets the initial pose for the localization algorithm which estimates the current position of the robo...
yarp::dev::ReturnValue getEstimatedPoses(std::vector< yarp::dev::Nav2D::Map2DLocation > &poses) override
Gets a set of pose estimates computed by the localization algorithm.
yarp::dev::ReturnValue getEstimatedOdometry(yarp::dev::OdometryData &odom) override
Gets the estimated odometry the robot, including its velocity expressed in the world and in the local...
yarp::dev::ReturnValue getCurrentPosition(yarp::dev::Nav2D::Map2DLocation &loc) override
Gets the current position of the robot w.r.t world reference frame.
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)
double m_last_statistics_printed
bool initializeLocalization(const yarp::dev::Nav2D::Map2DLocation &loc)
yarp::dev::Nav2D::Map2DLocation m_initial_odom
std::mutex m_mutex_thread
std::string map_id
name of the map
double theta
orientation [deg] in the map reference frame
double y
y position of the location [m], expressed in the map reference frame
double x
x position of the location [m], expressed in the map reference frame
double odom_x
position of the robot [m], expressed in the world reference frame
double odom_y
position of the robot [m], expressed in the world reference frame
double odom_theta
orientation the robot [deg], expressed in the world reference frame
@ return_value_error_method_failed
Method is deprecated.
A mini-server for performing network communication in the background.
An abstraction for a periodic thread.
bool start()
Call this to start the thread.
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
A class for storing options and configuration information.
Definition Property.h:33
A base class for nested structures that can be searched.
Definition Searchable.h:31
A class for a Matrix.
Definition Matrix.h:39
void resize(size_t r, size_t c)
Resize the matrix, if matrix is not empty preserve old content.
Definition Matrix.cpp:265
const Matrix & eye()
Build an identity matrix, don't resize.
Definition Matrix.cpp:456
T * data()
Return a pointer to the first element of the vector.
Definition Vector.h:206
#define yCInfo(component,...)
#define yCError(component,...)
#define yCWarning(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
Definition jointData.cpp:13
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.