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>
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
41{
43 return true;
44}
45
46bool fakeLocalizer::getEstimatedPoses(std::vector<Map2DLocation>& poses)
47{
48 poses.clear();
49 Map2DLocation loc;
51 poses.push_back(loc);
52#if 0
53 //The following block is used only for development and debug purposes.
54 //It should be never used in a real scenario
55 for (int i = 0; i < 10; i++)
56 {
57 yarp::dev::Map2DLocation newloc=loc;
58 unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
59 std::default_random_engine generator(seed);
60 std::uniform_real_distribution<double> dist(-1, 1);
61 std::uniform_real_distribution<double> dist_t(-180, 180);
62 double numberx = dist(generator);
63 double numbery = dist(generator);
64 double numbert = dist_t(generator);
65 newloc.x += numberx;
66 newloc.y += numbery;
67 newloc.theta += numbert;
68 poses.push_back(newloc);
69 }
70#endif
71 return true;
72}
73
75{
77 return true;
78}
79
81{
83 return true;
84}
85
87{
88 locThread->getCurrentLoc(loc,cov);
89 return true;
90}
91
93{
95 return true;
96}
97
99{
100 Map2DLocation loc;
102 odom.odom_x = loc.x;
103 odom.odom_y = loc.y;
104 odom.odom_theta = loc.theta;
105 return true;
106}
107
109
111{
114
119}
120
122{
123 double current_time = yarp::os::Time::now();
124
125 //print some stats every 10 seconds
126 if (current_time - m_last_statistics_printed > 10.0)
127 {
129 }
130
131 std::lock_guard<std::mutex> lock(m_mutex);
132 yarp::sig::Vector loc(3);
133 loc[0] = 0.0;
134 loc[1] = 0.0;
135 loc[2] = 0.0;
136 if (1)
137 {
139 m_current_odom.x = loc.data()[0];
140 m_current_odom.y = loc.data()[1];
141 m_current_odom.theta = loc.data()[2];
142
143 double c = cos((-m_initial_odom.theta + m_initial_loc.theta)*DEG2RAD);
144 double s = sin((-m_initial_odom.theta + m_initial_loc.theta)*DEG2RAD);
145 double df_x = (m_current_odom.x - m_initial_odom.x);
146 double df_y = (m_current_odom.y - m_initial_odom.y);
147 m_current_loc.x = df_x * c + df_y * -s + m_initial_loc.x;
148 m_current_loc.y = df_x * s + df_y * +c + m_initial_loc.y;
149
151
152 if (m_current_loc.theta >= +360) {
153 m_current_loc.theta -= 360;
154 } else if (m_current_loc.theta <= -360) {
155 m_current_loc.theta += 360;
156 }
157 }
158 if (current_time - m_last_locdata_received > 0.1)
159 {
160 yCWarning(FAKELOCALIZER) << "No localization data received for more than 0.1s!";
161 }
162}
163
165{
166 yCInfo(FAKELOCALIZER) << "Localization init request: (" << loc.map_id << ")";
167 std::lock_guard<std::mutex> lock(m_mutex);
169 m_initial_loc.x = loc.x;
170 m_initial_loc.y = loc.y;
175
177 {
178 yCInfo(FAKELOCALIZER) << "Map changed from: " << m_current_loc.map_id << " to: " << m_initial_loc.map_id;
180 //@@@TO BE COMPLETED
184 }
185 return true;
186}
187
189{
190 std::lock_guard<std::mutex> lock(m_mutex);
191 loc = m_current_loc;
192 return true;
193}
194
196{
197 std::lock_guard<std::mutex> lock(m_mutex);
198 loc = m_current_loc;
199 cov.resize(3,3);
200 cov.eye();
201 return true;
202}
203
205{
206 Map2DLocation loc;
207 loc.map_id="test";
208 loc.x = 0;
209 loc.y = 0;
210 loc.theta = 0;
211 this->initializeLocalization(loc);
212 return true;
213}
214
216{
217
218}
219
220
222{
224 locThread = new fakeLocalizerThread(0.010, p);
225
226 if (!locThread->start())
227 {
228 delete locThread;
229 locThread = NULL;
230 return false;
231 }
232
233 return true;
234}
235
237{
238 locThread = NULL;
239}
240
242{
243 if (locThread)
244 {
245 delete locThread;
246 locThread = NULL;
247 }
248}
249
251{
252 return true;
253}
254
256{
257 return true;
258}
259
261{
262 return true;
263}
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
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
Definition: OdometryData.h:29
double odom_y
position of the robot [m], expressed in the world reference frame
Definition: OdometryData.h:33
double odom_theta
orientation the robot [deg], expressed in the world reference frame
Definition: OdometryData.h:37
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:33
A base class for nested structures that can be searched.
Definition: Searchable.h:63
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:205
#define DEG2RAD
#define yCInfo(component,...)
Definition: LogComponent.h:171
#define yCWarning(component,...)
Definition: LogComponent.h:192
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:76
For streams capable of holding different kinds of content, check what they actually have.
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.