YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Localization2D_nws_yarp.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#define _USE_MATH_DEFINES
7
9
10#include <yarp/os/Bottle.h>
12#include <yarp/os/LogStream.h>
13#include <yarp/os/Network.h>
14#include <yarp/os/Port.h>
15#include <yarp/os/RFModule.h>
16#include <yarp/os/Time.h>
17
18#include <yarp/sig/Vector.h>
19
22#include <yarp/dev/IMap2D.h>
23#include <yarp/dev/PolyDriver.h>
24
25#include <yarp/math/Math.h>
26
27#include <cmath>
28
31using namespace yarp::os;
32using namespace yarp::dev;
33using namespace yarp::dev::Nav2D;
34
35#define DEFAULT_THREAD_PERIOD 0.01
36
37namespace
38{
39 YARP_LOG_COMPONENT(LOCALIZATION2D_NWS_YARP, "yarp.device.localization2D_nws_yarp")
40}
41
42//------------------------------------------------------------------------------------------------------------------------------
43
48
50{
51 std::lock_guard<std::mutex> lock(m_mutex);
52
53 if (driver->isValid())
54 {
55 driver->view(iLoc);
58 }
59
60 if (nullptr == iLoc)
61 {
62 yCError(LOCALIZATION2D_NWS_YARP, "Subdevice passed to attach method is invalid");
63 return false;
64 }
65
66 //initialize m_current_position and m_current_status, if available
67 bool ret = true;
69 Map2DLocation loc;
70 ret &= iLoc->getLocalizationStatus(status);
72 if (ret)
73 {
74 m_RPC->m_current_status = status;
76 }
77 else
78 {
79 yCWarning(LOCALIZATION2D_NWS_YARP) << "Localization data not yet available during server initialization";
80 }
81
82 if (!m_rpcPort.open(m_rpcPortName.c_str()))
83 {
84 yCError(LOCALIZATION2D_NWS_YARP, "Failed to open port %s", m_rpcPortName.c_str());
85 return false;
86 }
87
89 return this->start();
90}
91
93{
94 std::lock_guard<std::mutex> lock(m_mutex);
95
96 if (this->isRunning())
97 {
98 this->stop();
99 }
102 if (m_RPC)
103 {
104 delete m_RPC;
105 m_RPC = nullptr;
106 }
107
108 iLoc = nullptr;
109 return true;
110}
111
113{
114 if (!parseParams(config)) { return false; }
115
116 //check some parameters consistency
118 {
120
122 {yCWarning(LOCALIZATION2D_NWS_YARP) << "retrieve_position_periodically is true, but data is not published because publish_odometry is false. This configuration is strange";}
124 {yCWarning(LOCALIZATION2D_NWS_YARP) << "retrieve_position_periodically is true, but data is not published because publish_location is false. This configuration is strange";}
125 }
126 else
127 {
129 }
130
131 //Some debug prints
133 {
134 yCInfo(LOCALIZATION2D_NWS_YARP) << "retrieve_position_periodically requested, Period:" << m_GENERAL_period;
135 }
136 else
137 {
138 yCInfo(LOCALIZATION2D_NWS_YARP) << "retrieve_position_periodically NOT requested. Localization data obtained asynchronously.";
139 }
140
141
143 m_rpcPortName = m_local_name + "/rpc";
144 m_2DLocationPortName = m_local_name + "/streaming:o";
145 m_odometryPortName = m_local_name + "/odometry:o";
146
148
149 m_rpcPort.setReader(*this);
150
151 yCInfo(LOCALIZATION2D_NWS_YARP) << "Waiting for device to attach";
152
153 return true;
154}
155
157{
159 {
160 yCError(LOCALIZATION2D_NWS_YARP, "Failed to open port %s", m_2DLocationPortName.c_str());
161 return false;
162 }
163
165 {
166 yCError(LOCALIZATION2D_NWS_YARP, "Failed to open port %s", m_odometryPortName.c_str());
167 return false;
168 }
169 return true;
170}
171
173{
175// m_2DLocationPort.interrupt();
177// m_odometryPort.interrupt();
179}
180
182{
184
185 detach();
186
187 yCDebug(LOCALIZATION2D_NWS_YARP) << "Execution terminated";
188 return true;
189}
190
192{
193 if (!connection.isValid()) { return false;}
194 if (!m_RPC) { return false;}
195
196 std::lock_guard<std::mutex> lock(m_mutex);
197
198 bool b = m_RPC->read(connection);
199 if (b)
200 {
201 return true;
202 }
203 else
204 {
205 yCDebug(LOCALIZATION2D_NWS_YARP) << "read() Command failed";
206 return false;
207 }
208}
209
211{
212 std::lock_guard<std::mutex> lock(m_mutex);
213
216 {
217 yCInfo(LOCALIZATION2D_NWS_YARP) << "Running";
219 }
220
221 //enter the critical section
222 if (!m_RPC) { return;}
223 m_RPC->getMutex()->lock();
224 {
226 {
228 if (ret == false)
229 {
230 yCError(LOCALIZATION2D_NWS_YARP) << "getLocalizationStatus() failed";
231 }
232
233 if (m_RPC->m_current_status == LocalizationStatusEnum::localization_status_localized_ok)
234 {
236 if (ret2 == false)
237 {
238 yCError(LOCALIZATION2D_NWS_YARP) << "getCurrentPosition() failed";
239 }
240 else
241 {
243 }
245 if (ret3 == false)
246 {
247 //yCError(LOCALIZATION2D_NWS_YARP) << "getEstimatedOdometry() failed";
248 }
249 else
250 {
252 }
253 }
254 else
255 {
256 yCWarning(LOCALIZATION2D_NWS_YARP, "The system is not properly localized!");
257 }
258 }
259 }
260 m_RPC->getMutex()->unlock();
261
263 publish_odometry_on_yarp_port();
264 }
266 publish_2DLocation_on_yarp_port();
267 }
268}
269
270void Localization2D_nws_yarp::publish_odometry_on_yarp_port()
271{
272 if (!m_RPC) return;
273
275 {
278
279 //send data to port
282 }
283}
284
285void Localization2D_nws_yarp::publish_2DLocation_on_yarp_port()
286{
287 if (!m_RPC) return;
288
290 {
292 if (m_RPC->m_current_status == LocalizationStatusEnum::localization_status_localized_ok)
293 {
295 }
296 else
297 {
299 temp_loc.x = std::nan("");
300 temp_loc.y = std::nan("");
301 temp_loc.theta = std::nan("");
302 loc = temp_loc;
303 }
304
305 //send data to port
308 }
309}
define control board standard interfaces
bool ret
#define DEFAULT_THREAD_PERIOD
contains the definition of a Vector type
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
yarp::dev::OdometryData m_current_odometry
yarp::dev::Nav2D::LocalizationStatusEnum m_current_status
yarp::dev::Nav2D::Map2DLocation m_current_position
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
void run() override
Loop function.
yarp::os::BufferedPort< yarp::dev::OdometryData > m_odometryPort
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
yarp::dev::Nav2D::ILocalization2D * iLoc
bool open(yarp::os::Searchable &prop) override
Open the DeviceDriver.
bool attach(yarp::dev::PolyDriver *driver) override
Attach to another object.
bool close() override
Close the DeviceDriver.
bool threadInit() override
Initialization method.
void threadRelease() override
Release method.
bool detach() override
Detach the object (you must have first called attach).
yarp::os::BufferedPort< yarp::dev::Nav2D::Map2DLocation > m_2DLocationPort
bool view(T *&x)
Get an interface to the device driver.
virtual yarp::dev::ReturnValue getLocalizationStatus(LocalizationStatusEnum &status)=0
Gets the current status of the localization task.
virtual yarp::dev::ReturnValue getCurrentPosition(yarp::dev::Nav2D::Map2DLocation &loc)=0
Gets the current position of the robot w.r.t world reference frame.
virtual yarp::dev::ReturnValue getEstimatedOdometry(yarp::dev::OdometryData &odom)=0
Gets the estimated odometry the robot, including its velocity expressed in the world and in the local...
A container for a device driver.
Definition PolyDriver.h:23
bool isValid() const
Check if device is valid.
A mini-server for performing network communication in the background.
void close() override
Stop port activity.
bool setEnvelope(PortWriter &envelope) override
Set an envelope (e.g., a timestamp) to the next message which will be sent.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
int getOutputCount() override
Determine how many output connections this port has.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
An interface for reading from a network connection.
An abstraction for a periodic thread.
bool setPeriod(double period)
Set the (new) period of the thread.
bool isRunning() const
Returns true when the thread is started, false otherwise.
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...
void setReader(PortReader &reader) override
Set an external reader for port data.
Definition Port.cpp:511
void interrupt() override
Interrupt any current reads or writes attached to the port.
Definition Port.cpp:383
void close() override
Stop port activity.
Definition Port.cpp:363
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition Port.cpp:79
A base class for nested structures that can be searched.
Definition Searchable.h:31
void update()
Set the timestamp to the current time, and increment the sequence number (wrapping to 0 if the sequen...
Definition Stamp.cpp:124
#define yCInfo(component,...)
#define yCError(component,...)
#define yCTrace(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
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
void delay(double seconds)
Wait for a certain number of seconds.
Definition Time.cpp:111
An interface to the operating system, including Port based communication.