YARP
Yet Another Robot Platform
INavigation2DServerImpl.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
7#include <yarp/os/LogStream.h>
9
12using namespace yarp::os;
13using namespace yarp::dev;
14using namespace yarp::dev::Nav2D;
15using namespace std;
16
17namespace {
18YARP_LOG_COMPONENT(NAVIGATION2DSERVER, "yarp.device.navigation2DServer")
19}
20
21#define CHECK_POINTER(xxx) {if (xxx==nullptr) {yCError(NAVIGATION2DSERVER, "Invalid interface"); return false;}}
22
24{
25 m_iNav_target = iNav_target;
26 m_iNav_ctrl = iNav_ctrl;
27 m_iNav_vel = iNav_vel;
28}
29
30// ------------ INavigation2DControlActions
32{
33 std::lock_guard <std::mutex> lg(m_mutex);
34 {if (m_iNav_ctrl == nullptr) { yCError(NAVIGATION2DSERVER, "Invalid interface"); return false; }}
35
36 if (!m_iNav_ctrl->stopNavigation())
37 {
38 yCError(NAVIGATION2DSERVER, "Unable to stopNavigation");
39 return false;
40 }
41 m_current_goal_name.clear();
42 return true;
43}
44
46{
47 std::lock_guard <std::mutex> lg(m_mutex);
48 {if (m_iNav_ctrl == nullptr) { yCError(NAVIGATION2DSERVER, "Invalid interface"); return false; }}
49
50 if (!m_iNav_ctrl->resumeNavigation())
51 {
52 yCError(NAVIGATION2DSERVER, "Unable to resumeNavigation");
53 return false;
54 }
55 return true;
56}
57
59{
60 std::lock_guard <std::mutex> lg(m_mutex);
61 {if (m_iNav_ctrl == nullptr) { yCError(NAVIGATION2DSERVER, "Invalid interface"); return false; }}
62
63 if (!m_iNav_ctrl->suspendNavigation(time_s))
64 {
65 yCError(NAVIGATION2DSERVER, "Unable to suspendNavigation");
66 return false;
67 }
68 return true;
69}
70
72{
73 std::lock_guard <std::mutex> lg(m_mutex);
74 {if (m_iNav_ctrl == nullptr) { yCError(NAVIGATION2DSERVER, "Invalid interface"); return false; }}
75
76 if (!m_iNav_ctrl->recomputeCurrentNavigationPath())
77 {
78 yCError(NAVIGATION2DSERVER, "Unable to recomputeCurrentNavigationPath");
79 return false;
80 }
81 return true;
82}
83
85{
86 std::lock_guard <std::mutex> lg(m_mutex);
87
89
90 {if (m_iNav_ctrl == nullptr) { yCError(NAVIGATION2DSERVER, "Invalid interface"); return ret; }}
91
93 if (!m_iNav_ctrl->getNavigationStatus(status))
94 {
95 yCError(NAVIGATION2DSERVER, "Unable to getNavigationStatus");
96 ret.ret = false;
97 }
98 else
99 {
100 ret.ret = true;
101 ret.status = status;
102 }
103 return ret;
104}
105
107{
108 std::lock_guard <std::mutex> lg(m_mutex);
109
111
112 {if (m_iNav_ctrl == nullptr) { yCError(NAVIGATION2DSERVER, "Invalid interface"); return ret; }}
113
114 Map2DLocation loc;
115 if (!m_iNav_ctrl->getCurrentNavigationWaypoint(loc))
116 {
117 yCError(NAVIGATION2DSERVER, "Unable to getCurrentNavigationWaypoint");
118 ret.ret = false;
119 }
120 else
121 {
122 ret.ret = true;
123 ret.waypoint = loc;
124 }
125 return ret;
126}
127
129{
130 std::lock_guard <std::mutex> lg(m_mutex);
131
133
134 {if (m_iNav_ctrl == nullptr) { yCError(NAVIGATION2DSERVER, "Invalid interface"); return ret; }}
135
136 Map2DPath path;
137 if (!m_iNav_ctrl->getAllNavigationWaypoints(trajectory_type, path))
138 {
139 yCError(NAVIGATION2DSERVER, "Unable to getAllNavigationWaypoints");
140 ret.ret = false;
141 }
142 else
143 {
144 ret.ret = true;
145 ret.waypoints = path;
146 }
147 return ret;
148}
149
151{
152 std::lock_guard <std::mutex> lg(m_mutex);
153
155
156 {if (m_iNav_ctrl == nullptr) { yCError(NAVIGATION2DSERVER, "Invalid interface"); return ret; }}
157
158 MapGrid2D themap;
159 if (!m_iNav_ctrl->getCurrentNavigationMap(map_type,themap))
160 {
161 yCError(NAVIGATION2DSERVER, "Unable to getRelativeLocationOfCurrentTarget");
162 ret.ret = false;
163 }
164 else
165 {
166 ret.ret = true;
167 ret.mapgrid = themap;
168 }
169 return ret;
170}
171
172
173
174// ------------ INavigation2DTargetActions
176{
177 std::lock_guard <std::mutex> lg(m_mutex);
178 {if (m_iNav_target == nullptr) { yCError(NAVIGATION2DSERVER, "Invalid interface"); return false; }}
179
180 if (!m_iNav_target->gotoTargetByAbsoluteLocation(loc))
181 {
182 yCError(NAVIGATION2DSERVER, "Unable to gotoTargetByAbsoluteLocation");
183 return false;
184 }
185 m_current_goal_name.clear();
186 return true;
187}
188
190{
191 std::lock_guard <std::mutex> lg(m_mutex);
192 {if (m_iNav_target == nullptr) { yCError(NAVIGATION2DSERVER, "Invalid interface"); return false; }}
193
194 if (!m_iNav_target->gotoTargetByRelativeLocation(x,y))
195 {
196 yCError(NAVIGATION2DSERVER, "Unable to gotoTargetByRelativeLocation");
197 return false;
198 }
199 m_current_goal_name.clear();
200 return true;
201}
202
204{
205 std::lock_guard <std::mutex> lg(m_mutex);
206 {if (m_iNav_target == nullptr) { yCError(NAVIGATION2DSERVER, "Invalid interface"); return false; }}
207
208 if (!m_iNav_target->gotoTargetByRelativeLocation(x,y,theta))
209 {
210 yCError(NAVIGATION2DSERVER, "Unable to gotoTargetByRelativeLocation");
211 return false;
212 }
213 m_current_goal_name.clear();
214 return true;
215}
216
218{
219 std::lock_guard <std::mutex> lg(m_mutex);
220 {if (m_iNav_target == nullptr) { yCError(NAVIGATION2DSERVER, "Invalid interface"); return false; }}
221
222 if (!m_iNav_target->gotoTargetByAbsoluteLocation(loc))
223 {
224 yCError(NAVIGATION2DSERVER, "Unable to gotoTargetByAbsoluteLocation");
225 return false;
226 }
227
228 m_current_goal_name.set_current_goal_name(name);
229 return true;
230}
231
233{
234 std::lock_guard <std::mutex> lg(m_mutex);
235
237
238 {if (m_iNav_target == nullptr) { yCError(NAVIGATION2DSERVER, "Invalid interface"); return ret; }}
239
241 if (!m_iNav_target->getAbsoluteLocationOfCurrentTarget(loc))
242 {
243 yCError(NAVIGATION2DSERVER, "Unable to getAbsoluteLocationOfCurrentTarget");
244 ret.ret = false;
245 }
246 else
247 {
248 ret.ret = true;
249 ret.loc = loc;
250 }
251 return ret;
252}
253
255{
256 std::lock_guard <std::mutex> lg(m_mutex);
257
259
260 {if (m_iNav_target == nullptr) { yCError(NAVIGATION2DSERVER, "Invalid interface"); return ret; }}
261
262 double x,y,t;
263 if (!m_iNav_target->getRelativeLocationOfCurrentTarget(x,y,t))
264 {
265 yCError(NAVIGATION2DSERVER, "Unable to getRelativeLocationOfCurrentTarget");
266 ret.ret = false;
267 }
268 else
269 {
270 ret.ret = true;
271 ret.x = x;
272 ret.y = y;
273 ret.theta = t;
274 }
275 return ret;
276}
277
278// ------------ INavigation2DVelocityActions
279bool INavigation2DRPCd::apply_velocity_command_RPC(double x_vel, double y_vel, double theta_vel, double timeout)
280{
281 std::lock_guard <std::mutex> lg(m_mutex);
282 {if (m_iNav_vel == nullptr) { yCError(NAVIGATION2DSERVER, "Invalid interface"); return false; }}
283
284 if (!m_iNav_vel->applyVelocityCommand(x_vel, y_vel, theta_vel))
285 {
286 yCError(NAVIGATION2DSERVER, "Unable to applyVelocityCommand");
287 return false;
288 }
289 return true;
290}
291
293{
294 std::lock_guard <std::mutex> lg(m_mutex);
295
297
298 {if (m_iNav_vel == nullptr) { yCError(NAVIGATION2DSERVER, "Invalid interface"); return ret; }}
299
300 double x, y, t;
301 if (!m_iNav_vel->getLastVelocityCommand(x, y, t))
302 {
303 yCError(NAVIGATION2DSERVER, "Unable to getLastVelocityCommand");
304 ret.ret = false;
305 }
306 else
307 {
308 ret.ret = true;
309 ret.x_vel = x;
310 ret.y_vel = y;
311 ret.theta_vel = t;
312 }
313 return ret;
314}
315
316// ------------ extra
318{
319 std::lock_guard <std::mutex> lg(m_mutex);
320
322
323 if (m_current_goal_name.get_current_goal_name(ret.name))
324 {
325 ret.ret = true;
326 }
327 else
328 {
329 yCError(NAVIGATION2DSERVER, "Unable to getNameOfCurrentTarget");
330 ret.ret = false;
331 }
332
333 std::string name;
334 return ret;
335}
336
337// ------------ internal stuff
338bool LastGoalStorage::set_current_goal_name(const std::string& name)
339{
340 m_current_goal_name = name;
341 return true;
342}
343
345{
346 if (m_current_goal_name == "")
347 {
348 return true;
349 }
350 name = m_current_goal_name;
351 return true;
352}
353
355{
356 m_current_goal_name = "";
357 return true;
358}
float t
bool ret
return_get_current_nav_waypoint get_current_nav_waypoint_RPC() override
return_get_all_nav_waypoints get_all_navigation_waypoints_RPC(yarp::dev::Nav2D::TrajectoryTypeEnum trajectory_type) override
bool goto_target_by_relative_location2_RPC(double x, double y, double theta) override
return_get_abs_loc_of_curr_target get_absolute_location_of_current_target_RPC() override
bool suspend_navigation_RPC(double time_s) override
return_get_name_of_current_target get_name_of_current_target_RPC() override
void setInterfaces(yarp::dev::Nav2D::INavigation2DTargetActions *iNav_target, yarp::dev::Nav2D::INavigation2DControlActions *iNav_ctrl, yarp::dev::Nav2D::INavigation2DVelocityActions *iNav_vel)
return_get_current_nav_map get_current_navigation_map_RPC(yarp::dev::Nav2D::NavigationMapTypeEnum map_type) override
return_get_rel_loc_of_curr_target get_relative_location_of_current_target_RPC() override
bool apply_velocity_command_RPC(double x_vel, double y_vel, double theta_vel, double timeout) override
return_get_last_velocity_command get_last_velocity_command_RPC() override
bool goto_target_by_absolute_location_RPC(const yarp::dev::Nav2D::Map2DLocation &loc) override
bool goto_target_by_absolute_location_and_set_name_RPC(const yarp::dev::Nav2D::Map2DLocation &loc, const std::string &name) override
return_get_navigation_status get_navigation_status_RPC() override
bool recompute_current_navigation_path_RPC() override
bool resume_navigation_RPC() override
bool goto_target_by_relative_location1_RPC(double x, double y) override
bool stop_navigation_RPC() override
bool set_current_goal_name(const std::string &name)
bool get_current_goal_name(std::string &name)
virtual bool recomputeCurrentNavigationPath()=0
Forces the navigation system to recompute the path from the current robot position to the current goa...
virtual bool getAllNavigationWaypoints(yarp::dev::Nav2D::TrajectoryTypeEnum trajectory_type, yarp::dev::Nav2D::Map2DPath &waypoints)=0
Returns the list of waypoints generated by the navigation algorithm.
virtual bool getCurrentNavigationWaypoint(yarp::dev::Nav2D::Map2DLocation &curr_waypoint)=0
Returns the current waypoint pursued by the navigation algorithm.
virtual bool resumeNavigation()=0
Resume a previously suspended navigation task.
virtual bool getNavigationStatus(NavigationStatusEnum &status)=0
Gets the current status of the navigation task.
virtual bool stopNavigation()=0
Terminates the current navigation task.
virtual bool suspendNavigation(const double time_s=std::numeric_limits< double >::infinity())=0
Ask to the robot to suspend the current navigation task for a defined amount of time.
virtual bool getCurrentNavigationMap(yarp::dev::Nav2D::NavigationMapTypeEnum map_type, yarp::dev::Nav2D::MapGrid2D &map)=0
Returns the current navigation map processed by the navigation algorithm.
virtual bool gotoTargetByRelativeLocation(double x, double y)=0
Ask the robot to reach a position defined in the robot reference frame.
virtual bool gotoTargetByAbsoluteLocation(yarp::dev::Nav2D::Map2DLocation loc)=0
Ask the robot to reach a position defined in the world reference frame.
virtual bool getAbsoluteLocationOfCurrentTarget(yarp::dev::Nav2D::Map2DLocation &loc)=0
Gets the last navigation target in the world reference frame.
virtual bool getRelativeLocationOfCurrentTarget(double &x, double &y, double &theta)=0
Gets the last navigation target in the robot reference frame.
virtual bool applyVelocityCommand(double x_vel, double y_vel, double theta_vel, double timeout=0.1)=0
Apply a velocity command.
virtual bool getLastVelocityCommand(double &x_vel, double &y_vel, double &theta_vel)=0
Returns the last applied velocity command.
#define yCError(component,...)
Definition: LogComponent.h:213
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:76
STL namespace.
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.