YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Navigation2D_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#include <yarp/os/Network.h>
7#include <yarp/os/RFModule.h>
8#include <yarp/os/Time.h>
9#include <yarp/os/Port.h>
11#include <yarp/os/LogStream.h>
14#include <yarp/dev/MapGrid2D.h>
15#include <math.h>
16#include <cmath>
18
19using namespace yarp::os;
20using namespace yarp::dev;
21using namespace yarp::dev::Nav2D;
22
23namespace {
24YARP_LOG_COMPONENT(NAVIGATION2D_NWS_YARP, "yarp.device.navigation2D_nws_yarp")
25}
26
33
35{
36 std::lock_guard lock (m_mutex);
37
38 if (driver->isValid())
39 {
40 driver->view(iNav_target);
41 driver->view(iNav_ctrl);
42 driver->view(iNav_vel);
43 }
44
45 if (nullptr == iNav_target ||
46 nullptr == iNav_ctrl)
47 {
48 yCError(NAVIGATION2D_NWS_YARP, "Subdevice passed to attach method is invalid (it does not implement all the required interfaces)");
49 return false;
50 }
51
52 if (nullptr == iNav_vel)
53 {
54 yCWarning(NAVIGATION2D_NWS_YARP, "The attached subdevice does not implement INavigation2DVelocityActions interface");
55 }
56
58
60 return PeriodicThread::start();
61}
62
64{
65 std::lock_guard lock (m_mutex);
66
68 {
70 }
71
72 if (m_RPC)
73 {
74 delete m_RPC;
75 m_RPC = nullptr;
76 }
77
80
81 return true;
82}
83
85{
86 if (!parseParams(config)) { return false; }
87
89 m_statusPortName = m_GENERAL_name + "/status:o";
90
91 if (!initialize_YARP(config))
92 {
93 yCError(NAVIGATION2D_NWS_YARP) << "Error initializing YARP ports";
94 return false;
95 }
96
97 yCInfo(NAVIGATION2D_NWS_YARP) << "Waiting for device to attach";
98
99 return true;
100}
101
103{
104 if (!m_rpcPort.open(m_rpcPortName.c_str()))
105 {
106 yCError(NAVIGATION2D_NWS_YARP, "Failed to open port %s", m_rpcPortName.c_str());
107 return false;
108 }
109
111 {
112 yCError(NAVIGATION2D_NWS_YARP, "Failed to open port %s", m_statusPortName.c_str());
113 return false;
114 }
115
116 m_rpcPort.setReader(*this);
117 return true;
118}
119
121{
124 {
126 }
127
128 detachAll();
129 return true;
130}
131
133{
134 if (!connection.isValid()) { return false;}
135 if (!m_RPC) { return false;}
136
137 std::lock_guard<std::mutex> lock(m_mutex);
138
139 bool b = m_RPC->read(connection);
140 if (b)
141 {
142 return true;
143 }
144 else
145 {
146 yCDebug(NAVIGATION2D_NWS_YARP) << "read() Command failed";
147 return false;
148 }
149}
150
152{
153 std::lock_guard lock(m_mutex);
154
156
158
159 std::string a_navigation_status = getStatusAsString(m_navigation_status);
160
163 {
164 if (!ok)
165 {
166 yCError(NAVIGATION2D_NWS_YARP, "Unable to get Navigation Status!\n");
167 }
168 else
169 {
170 yCInfo(NAVIGATION2D_NWS_YARP) << "Running, ALL ok. Navigation status:" << a_navigation_status;
171 updateStatusPort(a_navigation_status);
173 }
175 }
176}
177
178std::string Navigation2D_nws_yarp::getStatusAsString(NavigationStatusEnum status)
179{
180 if (status == navigation_status_idle) {
181 return std::string("navigation_status_idle");
182 } else if (status == navigation_status_moving) {
183 return std::string("navigation_status_moving");
184 } else if (status == navigation_status_waiting_obstacle) {
185 return std::string("navigation_status_waiting_obstacle");
186 } else if (status == navigation_status_goal_reached) {
187 return std::string("navigation_status_goal_reached");
188 } else if (status == navigation_status_aborted) {
189 return std::string("navigation_status_aborted");
190 } else if (status == navigation_status_failing) {
191 return std::string("navigation_status_failing");
192 } else if (status == navigation_status_paused) {
193 return std::string("navigation_status_paused");
194 } else if (status == navigation_status_preparing_before_move) {
195 return std::string("navigation_status_preparing_before_move");
196 } else if (status == navigation_status_thinking) {
197 return std::string("navigation_status_thinking");
198 } else if (status == navigation_status_error) {
199 return std::string("navigation_status_error");
200 }
201 return std::string("navigation_status_error");
202}
203
204void Navigation2D_nws_yarp::updateStatusPort(std::string& status)
205{
207 output_state.addString(status);
209}
define control board standard interfaces
#define DEFAULT_THREAD_PERIOD
contains the definition of a map type
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
virtual void run() override
Loop function.
virtual bool detach() override
Detach the object (you must have first called attach).
virtual bool open(yarp::os::Searchable &prop) override
Open the DeviceDriver.
INavigation2DRPCd * m_RPC
yarp::dev::Nav2D::INavigation2DControlActions * iNav_ctrl
bool initialize_YARP(yarp::os::Searchable &config)
virtual bool close() override
Close the DeviceDriver.
yarp::dev::Nav2D::NavigationStatusEnum m_navigation_status
virtual bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
Navigation2D_nws_yarp()
Default module constructor.
virtual bool attach(yarp::dev::PolyDriver *drv) override
Attach to another object.
yarp::dev::Nav2D::INavigation2DVelocityActions * iNav_vel
yarp::dev::Nav2D::INavigation2DTargetActions * iNav_target
bool view(T *&x)
Get an interface to the device driver.
virtual yarp::dev::ReturnValue getNavigationStatus(NavigationStatusEnum &status)=0
Gets the current status of the navigation task.
A container for a device driver.
Definition PolyDriver.h:23
bool isValid() const
Check if device is valid.
bool detachAll() final
Detach the object (you must have first called attach).
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
A mini-server for performing network communication in the background.
An interface for reading from a network connection.
virtual bool isValid() const =0
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...
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
Definition Port.cpp:436
void setReader(PortReader &reader) override
Set an external reader for port data.
Definition Port.cpp:511
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
#define yCInfo(component,...)
#define yCError(component,...)
#define yCTrace(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
@ navigation_status_preparing_before_move
@ navigation_status_waiting_obstacle
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.