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 if (driver->isValid())
37 {
38 driver->view(iNav_target);
39 driver->view(iNav_ctrl);
40 driver->view(iNav_vel);
41 }
42
43 if (nullptr == iNav_target ||
44 nullptr == iNav_ctrl)
45 {
46 yCError(NAVIGATION2D_NWS_YARP, "Subdevice passed to attach method is invalid (it does not implement all the required interfaces)");
47 return false;
48 }
49
50 if (nullptr == iNav_vel)
51 {
52 yCWarning(NAVIGATION2D_NWS_YARP, "The attached subdevice does not implement INavigation2DVelocityActions interface");
53 }
54
56
58 return PeriodicThread::start();
59}
60
62{
64 {
66 }
67
70
71 return true;
72}
73
75{
76 if (!parseParams(config)) { return false; }
77
79 m_statusPortName = m_GENERAL_name + "/status:o";
80
81 if (!initialize_YARP(config))
82 {
83 yCError(NAVIGATION2D_NWS_YARP) << "Error initializing YARP ports";
84 return false;
85 }
86
87 yCInfo(NAVIGATION2D_NWS_YARP) << "Waiting for device to attach";
88
89 return true;
90}
91
93{
94 if (!m_rpcPort.open(m_rpcPortName.c_str()))
95 {
96 yCError(NAVIGATION2D_NWS_YARP, "Failed to open port %s", m_rpcPortName.c_str());
97 return false;
98 }
99
101 {
102 yCError(NAVIGATION2D_NWS_YARP, "Failed to open port %s", m_statusPortName.c_str());
103 return false;
104 }
105
106 m_rpcPort.setReader(*this);
107 return true;
108}
109
111{
114 {
116 }
117
118 detachAll();
119 return true;
120}
121
123{
124 bool b = m_RPC.read(connection);
125 if (b)
126 {
127 return true;
128 }
129 else
130 {
131 yCDebug(NAVIGATION2D_NWS_YARP) << "read() Command failed";
132 return false;
133 }
134}
135
137{
139
141
142 std::string a_navigation_status = getStatusAsString(m_navigation_status);
143
146 {
147 if (!ok)
148 {
149 yCError(NAVIGATION2D_NWS_YARP, "Unable to get Navigation Status!\n");
150 }
151 else
152 {
153 yCInfo(NAVIGATION2D_NWS_YARP) << "Running, ALL ok. Navigation status:" << a_navigation_status;
154 updateStatusPort(a_navigation_status);
156 }
158 }
159}
160
161std::string Navigation2D_nws_yarp::getStatusAsString(NavigationStatusEnum status)
162{
163 if (status == navigation_status_idle) {
164 return std::string("navigation_status_idle");
165 } else if (status == navigation_status_moving) {
166 return std::string("navigation_status_moving");
167 } else if (status == navigation_status_waiting_obstacle) {
168 return std::string("navigation_status_waiting_obstacle");
169 } else if (status == navigation_status_goal_reached) {
170 return std::string("navigation_status_goal_reached");
171 } else if (status == navigation_status_aborted) {
172 return std::string("navigation_status_aborted");
173 } else if (status == navigation_status_failing) {
174 return std::string("navigation_status_failing");
175 } else if (status == navigation_status_paused) {
176 return std::string("navigation_status_paused");
177 } else if (status == navigation_status_preparing_before_move) {
178 return std::string("navigation_status_preparing_before_move");
179 } else if (status == navigation_status_thinking) {
180 return std::string("navigation_status_thinking");
181 } else if (status == navigation_status_error) {
182 return std::string("navigation_status_error");
183 }
184 return std::string("navigation_status_error");
185}
186
187void Navigation2D_nws_yarp::updateStatusPort(std::string& status)
188{
190 output_state.addString(status);
192}
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.
void setInterfaces(yarp::dev::Nav2D::INavigation2DTargetActions *iNav_target, yarp::dev::Nav2D::INavigation2DControlActions *iNav_ctrl, yarp::dev::Nav2D::INavigation2DVelocityActions *iNav_vel)
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.
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 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.
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.
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.
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.