YARP
Yet Another Robot Platform
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
28{
31}
32
34{
35 if (driver->isValid())
36 {
37 driver->view(iNav_target);
38 driver->view(iNav_ctrl);
39 driver->view(iNav_vel);
40 }
41
42 if (nullptr == iNav_target ||
43 nullptr == iNav_ctrl)
44 {
45 yCError(NAVIGATION2D_NWS_YARP, "Subdevice passed to attach method is invalid (it does not implement all the required interfaces)");
46 return false;
47 }
48
49 if (nullptr == iNav_vel)
50 {
51 yCWarning(NAVIGATION2D_NWS_YARP, "The attached subdevice does not implement INavigation2DVelocityActions interface");
52 }
53
55
56 PeriodicThread::setPeriod(m_period);
57 return PeriodicThread::start();
58}
59
61{
62 if (PeriodicThread::isRunning())
63 {
64 PeriodicThread::stop();
65 }
66 return true;
67}
68
70{
71 Property params;
72 params.fromString(config.toString().c_str());
73 yCDebug(NAVIGATION2D_NWS_YARP) << "Configuration: \n" << config.toString().c_str();
74
75 if (config.check("GENERAL") == false)
76 {
77 yCWarning(NAVIGATION2D_NWS_YARP) << "Missing GENERAL group, assuming default options";
78 }
79
80 Bottle& general_group = config.findGroup("GENERAL");
81 if (!general_group.check("period"))
82 {
83 yCInfo(NAVIGATION2D_NWS_YARP) << "Missing 'period' parameter. Using default value: 0.010";
84 m_period = 0.010;
85 }
86 else
87 {
88 m_period = config.find("period").asFloat64();
89 }
90
91 if (!general_group.check("name"))
92 {
93 yCInfo(NAVIGATION2D_NWS_YARP) << "Missing 'name' parameter. Using default value: " << m_local_name;
94 }
95 else
96 {
97 m_local_name = config.find("name").asString();
98 if (m_local_name.c_str()[0] != '/') { yCError(NAVIGATION2D_NWS_YARP) << "Missing '/' in name parameter"; return false; }
99 yCInfo(NAVIGATION2D_NWS_YARP) << "Using local name:" << m_local_name;
100 }
101 m_rpcPortName = m_local_name + "/rpc";
102
103 if (config.check("subdevice"))
104 {
105 Property p;
106 p.fromString(config.toString(), false);
107 p.put("device", config.find("subdevice").asString());
108
109 if (!pNav.open(p) || !pNav.isValid())
110 {
111 yCError(NAVIGATION2D_NWS_YARP) << "Failed to open subdevice.. check params";
112 return false;
113 }
114
115 if (!attach(&pNav))
116 {
117 yCError(NAVIGATION2D_NWS_YARP) << "Failed to attach subdevice.. check params";
118 return false;
119 }
120 }
121 else
122 {
123 yCInfo(NAVIGATION2D_NWS_YARP) << "Waiting for device to attach";
124 }
125
126 if (!initialize_YARP(config))
127 {
128 yCError(NAVIGATION2D_NWS_YARP) << "Error initializing YARP ports";
129 return false;
130 }
131
132 return true;
133}
134
136{
137 if (!m_rpcPort.open(m_rpcPortName.c_str()))
138 {
139 yCError(NAVIGATION2D_NWS_YARP, "Failed to open port %s", m_rpcPortName.c_str());
140 return false;
141 }
142 m_rpcPort.setReader(*this);
143 return true;
144}
145
147{
148 yCTrace(NAVIGATION2D_NWS_YARP, "Close");
149 if (PeriodicThread::isRunning())
150 {
151 PeriodicThread::stop();
152 }
153
154 detachAll();
155 return true;
156}
157
159{
160 bool b = m_RPC.read(connection);
161 if (b)
162 {
163 return true;
164 }
165 else
166 {
167 yCDebug(NAVIGATION2D_NWS_YARP) << "read() Command failed";
168 return false;
169 }
170}
171
173{
175
176 double m_stats_time_curr = yarp::os::Time::now();
177 if (m_stats_time_curr - m_stats_time_last > 5.0)
178 {
179 if (!ok)
180 {
181 yCError(NAVIGATION2D_NWS_YARP, "Unable to get Navigation Status!\n");
182 }
183 else
184 {
185 yCInfo(NAVIGATION2D_NWS_YARP) << "Running, ALL ok. Navigation status:" << getStatusAsString(m_navigation_status);
186 }
188 }
189}
190
191std::string navigation2D_nws_yarp::getStatusAsString(NavigationStatusEnum status)
192{
193 if (status == navigation_status_idle) {
194 return std::string("navigation_status_idle");
195 } else if (status == navigation_status_moving) {
196 return std::string("navigation_status_moving");
197 } else if (status == navigation_status_waiting_obstacle) {
198 return std::string("navigation_status_waiting_obstacle");
199 } else if (status == navigation_status_goal_reached) {
200 return std::string("navigation_status_goal_reached");
201 } else if (status == navigation_status_aborted) {
202 return std::string("navigation_status_aborted");
203 } else if (status == navigation_status_failing) {
204 return std::string("navigation_status_failing");
205 } else if (status == navigation_status_paused) {
206 return std::string("navigation_status_paused");
207 } else if (status == navigation_status_preparing_before_move) {
208 return std::string("navigation_status_preparing_before_move");
209 } else if (status == navigation_status_thinking) {
210 return std::string("navigation_status_thinking");
211 } else if (status == navigation_status_error) {
212 return std::string("navigation_status_error");
213 }
214 return std::string("navigation_status_error");
215}
define control board standard interfaces
contains the definition of a map type
constexpr double DEFAULT_THREAD_PERIOD
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
void setInterfaces(yarp::dev::Nav2D::INavigation2DTargetActions *iNav_target, yarp::dev::Nav2D::INavigation2DControlActions *iNav_ctrl, yarp::dev::Nav2D::INavigation2DVelocityActions *iNav_vel)
yarp::dev::Nav2D::INavigation2DControlActions * iNav_ctrl
virtual void run() override
Loop function.
virtual bool open(yarp::os::Searchable &prop) override
Open the DeviceDriver.
yarp::dev::Nav2D::NavigationStatusEnum m_navigation_status
virtual bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
virtual bool detach() override
Detach the object (you must have first called attach).
virtual bool attach(yarp::dev::PolyDriver *drv) override
Attach to another object.
navigation2D_nws_yarp()
Default module constructor.
yarp::dev::Nav2D::INavigation2DTargetActions * iNav_target
virtual bool close() override
Close the DeviceDriver.
yarp::dev::Nav2D::INavigation2DVelocityActions * iNav_vel
bool initialize_YARP(yarp::os::Searchable &config)
yarp::dev::PolyDriver pNav
bool view(T *&x)
Get an interface to the device driver.
Definition: DeviceDriver.h:88
virtual bool 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.
Definition: PolyDriver.cpp:196
bool open(const std::string &txt)
Construct and configure a device by its common name.
Definition: PolyDriver.cpp:140
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
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Definition: Bottle.cpp:277
An interface for reading from a network connection.
An abstraction for a periodic thread.
void setReader(PortReader &reader) override
Set an external reader for port data.
Definition: Port.cpp:511
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Port.cpp:79
A class for storing options and configuration information.
Definition: Property.h:33
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
Definition: Property.cpp:1063
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition: Property.cpp:1015
A base class for nested structures that can be searched.
Definition: Searchable.h:63
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
Definition: Value.cpp:222
virtual std::string asString() const
Get string value.
Definition: Value.cpp:234
#define yCInfo(component,...)
Definition: LogComponent.h:171
#define yCError(component,...)
Definition: LogComponent.h:213
#define yCTrace(component,...)
Definition: LogComponent.h:84
#define yCWarning(component,...)
Definition: LogComponent.h:192
#define yCDebug(component,...)
Definition: LogComponent.h:128
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:76
@ navigation_status_preparing_before_move
Definition: INavigation2D.h:31
@ navigation_status_goal_reached
Definition: INavigation2D.h:34
@ navigation_status_waiting_obstacle
Definition: INavigation2D.h:33
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.