YARP
Yet Another Robot Platform
Navigation2DClient.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "Navigation2DClient.h"
20 #include <yarp/dev/INavigation2D.h>
21 #include <yarp/os/Log.h>
22 #include <yarp/os/LogComponent.h>
23 #include <yarp/os/LogStream.h>
24 #include <mutex>
25 #include <cmath>
26 
29 using namespace yarp::dev;
30 using namespace yarp::dev::Nav2D;
31 using namespace yarp::os;
32 using namespace yarp::sig;
33 
34 namespace {
35 YARP_LOG_COMPONENT(NAVIGATION2DCLIENT, "yarp.device.navigation2DClient")
36 }
37 
38 bool Navigation2DClient::set_current_goal_name(const std::string& name)
39 {
40  m_current_goal_name = name;
41  return true;
42 }
43 
44 bool Navigation2DClient::get_current_goal_name(std::string& name)
45 {
46  if (m_current_goal_name == "")
47  {
48  return false;
49  }
50  name = m_current_goal_name;
51  return true;
52 }
53 
54 bool Navigation2DClient::reset_current_goal_name()
55 {
56  m_current_goal_name = "";
57  return true;
58 }
59 
60 //------------------------------------------------------------------------------------------------------------------------------
61 
63 {
64  m_local_name.clear();
65  m_navigation_server_name.clear();
66  m_map_locations_server_name.clear();
67  m_localization_server_name.clear();
68 
69  m_local_name = config.find("local").asString();
70  m_navigation_server_name = config.find("navigation_server").asString();
71  m_map_locations_server_name = config.find("map_locations_server").asString();
72  m_localization_server_name = config.find("localization_server").asString();
73 
74  if (m_local_name == "")
75  {
76  yCError(NAVIGATION2DCLIENT, "open() error you have to provide a valid 'local' param");
77  return false;
78  }
79 
80  if (m_navigation_server_name == "")
81  {
82  yCError(NAVIGATION2DCLIENT, "open() error you have to provide a valid 'navigation_server' param");
83  return false;
84  }
85 
86  if (m_map_locations_server_name == "")
87  {
88  yCError(NAVIGATION2DCLIENT, "open() error you have to provide valid 'map_locations_server' param");
89  return false;
90  }
91 
92  if (m_localization_server_name == "")
93  {
94  yCError(NAVIGATION2DCLIENT, "open() error you have to provide valid 'localization_server' param");
95  return false;
96  }
97 
98  if (config.check("period"))
99  {
100  m_period = config.find("period").asInt32();
101  }
102  else
103  {
104  m_period = 10;
105  yCWarning(NAVIGATION2DCLIENT, "Using default period of %d ms" , m_period);
106  }
107 
108  std::string
109  local_rpc_1,
110  local_rpc_2,
111  local_rpc_3,
112  local_rpc_4,
113  remote_rpc_1,
114  remote_rpc_2,
115  remote_rpc_3,
116  remote_streaming_name,
117  local_streaming_name;
118 
119  local_rpc_1 = m_local_name + "/navigation/rpc";
120  local_rpc_2 = m_local_name + "/locations/rpc";
121  local_rpc_3 = m_local_name + "/localization/rpc";
122  local_rpc_4 = m_local_name + "/user_commands/rpc";
123  remote_rpc_1 = m_navigation_server_name + "/rpc";
124  remote_rpc_2 = m_map_locations_server_name + "/rpc";
125  remote_rpc_3 = m_localization_server_name + "/rpc";
126  remote_streaming_name = m_localization_server_name + "/stream:o";
127  local_streaming_name = m_local_name + "/stream:i";
128 
129  if (!m_rpc_port_navigation_server.open(local_rpc_1))
130  {
131  yCError(NAVIGATION2DCLIENT, "open() error could not open rpc port %s, check network", local_rpc_1.c_str());
132  return false;
133  }
134 
135  if (!m_rpc_port_map_locations_server.open(local_rpc_2))
136  {
137  yCError(NAVIGATION2DCLIENT, "open() error could not open rpc port %s, check network", local_rpc_2.c_str());
138  return false;
139  }
140 
141  if (!m_rpc_port_localization_server.open(local_rpc_3))
142  {
143  yCError(NAVIGATION2DCLIENT, "open() error could not open rpc port %s, check network", local_rpc_3.c_str());
144  return false;
145  }
146 
147  bool ok = true;
148 
149  ok = Network::connect(local_rpc_1, remote_rpc_1);
150  if (!ok)
151  {
152  yCError(NAVIGATION2DCLIENT, "open() error could not connect to %s", remote_rpc_1.c_str());
153  return false;
154  }
155 
156  ok = Network::connect(local_rpc_2, remote_rpc_2);
157  if (!ok)
158  {
159  yCError(NAVIGATION2DCLIENT, "open() error could not connect to %s", remote_rpc_2.c_str());
160  return false;
161  }
162 
163  ok = Network::connect(local_rpc_3, remote_rpc_3);
164  if (!ok)
165  {
166  yCError(NAVIGATION2DCLIENT, "open() error could not connect to %s", remote_rpc_3.c_str());
167  return false;
168  }
169 
170  if (!m_rpc_port_user_commands.open(local_rpc_4.c_str()))
171  {
172  yCError(NAVIGATION2DCLIENT, "Failed to open port %s", local_rpc_4.c_str());
173  return false;
174  }
175  m_rpc_port_user_commands.setReader(*this);
176 
177  return true;
178 }
179 
181 {
182  m_rpc_port_navigation_server.close();
183  m_rpc_port_map_locations_server.close();
184  m_rpc_port_localization_server.close();
185  m_rpc_port_user_commands.close();
186  return true;
187 }
188 
190 {
191  if (command.get(0).isString() == false)
192  {
193  yCError(NAVIGATION2DCLIENT) << "General error in parse_respond_string";
194  return false;
195  }
196 
197  if (command.get(0).asString() == "help")
198  {
199  reply.addVocab(Vocab::encode("many"));
200  reply.addString("Available commands are:");
201  reply.addString("goto <locationName>");
202  //reply.addString("gotoAbs <x> <y> <angle in degrees>");
203  //reply.addString("gotoRel <x> <y> <angle in degrees>");
204  reply.addString("store_location <location_name> <map_id> <x> <y> <y>");
205  reply.addString("store_current_location <location_name>");
206  reply.addString("delete_location <location_name>");
207  reply.addString("clear_all_locations");
208  reply.addString("get_last_target");
209  reply.addString("get_location_list");
210  reply.addString("get_navigation_status");
211  reply.addString("stop_loc");
212  reply.addString("start_loc");
213  reply.addString("stop_nav");
214  reply.addString("pause_nav");
215  reply.addString("resume_nav");
216  reply.addString("get_current_loc");
217  reply.addString("initLoc <map_name> <x> <y> <angle in degrees>");
218  }
219  else if (command.get(0).asString() == "store_current_location")
220  {
221  bool ret = this->storeCurrentPosition(command.get(1).asString());
222  if (ret)
223  {
224  reply.addString("store_current_location done");
225  }
226  else
227  {
228  reply.addString("store_current_location failed");
229  }
230  }
231  else if (command.get(0).asString() == "gotoAbs")
232  {
233  Map2DLocation loc;
234  loc.map_id = command.get(1).asString();
235  loc.x = command.get(2).asFloat64();
236  loc.y = command.get(3).asFloat64();
237  if (command.size() == 5)
238  {
239  loc.theta = command.get(4).asFloat64();
240  }
241  else
242  {
243  loc.theta = nan("");
244  }
245 
246  bool ret = this->gotoTargetByAbsoluteLocation(loc);
247  if (ret)
248  {
249  reply.addString("gotoTargetByAbsoluteLocation() executed successfully");
250  }
251  else
252  {
253  reply.addString("gotoTargetByAbsoluteLocation() returned an error");
254  }
255  }
256 
257  else if (command.get(0).asString() == "gotoRel")
258  {
260  double x = command.get(1).asFloat64();
261  double y = command.get(2).asFloat64();
262  bool ret;
263  if (command.size() == 4)
264  {
265  double t = command.get(3).asFloat64();
266  ret = this->gotoTargetByRelativeLocation(x, y, t);
267  }
268  else
269  {
270  ret = this->gotoTargetByRelativeLocation(x, y);
271  }
272 
273  if (ret)
274  {
275  reply.addString("gotoTargetByRelativeLocation() executed successfully");
276  }
277  else
278  {
279  reply.addString("gotoTargetByRelativeLocation() returned an error");
280  }
281  }
282  else if (command.get(0).asString() == "get_location_list")
283  {
284  std::vector<std::string> locations;
285  bool ret = getLocationsList(locations);
286  if (ret)
287  {
288  for (size_t i=0; i < locations.size(); i++)
289  {
290  reply.addString(locations[i]);
291  }
292  }
293  else
294  {
295  reply.addString("get_location_list failed");
296  }
297  }
298  else if (command.get(0).asString() == "get_navigation_status")
299  {
301  bool ret = this->getNavigationStatus(ss);
302  if (ret)
303  {
305  reply.addString(s.c_str());
306  }
307  else
308  {
309  reply.addString("getNavigationStatus() failed");
310  }
311  }
312  else if (command.get(0).isString() && command.get(0).asString() == "get_current_loc")
313  {
314  {
315  Map2DLocation curr_loc;
316  bool ret1 = this->getCurrentPosition(curr_loc);
317  if (ret1)
318  {
319  std::string s = std::string("Current Location is: ") + curr_loc.toString();
320  reply.addString(s);
321  }
322  else
323  {
324  reply.addString("getCurrentPosition(curr_loc) failed");
325  }
326  }
327  {
328  Map2DLocation curr_loc;
329  Matrix cov;
330  bool ret2 = this->getCurrentPosition(curr_loc, cov);
331  if (ret2)
332  {
333  std::string s = std::string("Current Location with covariance is: ") + curr_loc.toString() + "\n" + cov.toString();
334  reply.addString(s);
335  }
336  else
337  {
338  reply.addString("getCurrentPosition(curr_loc, covariance) failed");
339  }
340  }
341  }
342  else if (command.get(0).isString() && command.get(0).asString() == "initLoc")
343  {
344  Map2DLocation init_loc;
345  bool ret = false;
346  if (command.size() == 5)
347  {
348  init_loc.map_id = command.get(1).asString();
349  init_loc.x = command.get(2).asFloat64();
350  init_loc.y = command.get(3).asFloat64();
351  init_loc.theta = command.get(4).asFloat64();
352  ret = this->setInitialPose(init_loc);
353  }
354  else if (command.size() == 6)
355  {
356  init_loc.map_id = command.get(1).asString();
357  init_loc.x = command.get(2).asFloat64();
358  init_loc.y = command.get(3).asFloat64();
359  init_loc.theta = command.get(4).asFloat64();
360  Bottle* b= command.get(5).asList();
361  if (b && b->size()==9)
362  {
363  yarp::sig::Matrix cov(3,3);
364  for (size_t i = 0; i < 3; i++) { for (size_t j = 0; j < 3; j++) { cov[i][j] = b->get(i * 3 + j).asFloat64(); } }
365  ret = this->setInitialPose(init_loc, cov);
366  }
367  else ret = false;
368  }
369 
370  if (ret)
371  {
372  std::string s = std::string("Localization initialized to: ") + init_loc.toString();
373  reply.addString(s);
374  }
375  else
376  {
377  reply.addString("setInitialPose() failed");
378  }
379  }
380  else if (command.get(0).asString() == "store_location")
381  {
382  if (command.size() != 6)
383  {
384  reply.addString("store_location failed (invalid params)");
385  }
386  else
387  {
388  Map2DLocation loc;
389  loc.map_id = command.get(2).asString();
390  loc.x = command.get(3).asFloat64();
391  loc.y = command.get(4).asFloat64();
392  loc.theta = command.get(5).asFloat64();
393  bool ret = this->storeLocation(command.get(1).asString(), loc);
394  if (ret)
395  {
396  reply.addString("store_location done");
397  }
398  else
399  {
400  reply.addString("store_location failed");
401  }
402  }
403  }
404  else if (command.get(0).asString() == "delete_location")
405  {
406  bool ret = this->deleteLocation(command.get(1).asString());
407  if (ret)
408  {
409  reply.addString("delete_location done");
410  }
411  else
412  {
413  reply.addString("delete_location failed");
414  }
415  }
416  else if (command.get(0).asString() == "clear_all_locations")
417  {
418  std::vector<std::string> locations;
419  bool ret = getLocationsList(locations);
420  if (ret)
421  {
422  for (size_t i = 0; i < locations.size(); i++)
423  {
424  bool ret = this->deleteLocation(locations[i]);
425  if (ret == false)
426  {
427  reply.addString("clear_all_locations failed");
428  }
429  }
430  reply.addString("clear_all_locations done");
431  }
432  else
433  {
434  reply.addString("clear_all_locations failed");
435  }
436  }
437  else if (command.get(0).asString() == "goto")
438  {
439  bool ret = this->gotoTargetByLocationName(command.get(1).asString());
440  if (ret)
441  {
442  reply.addString("goto done");
443  }
444  else
445  {
446  reply.addString("goto failed");
447  }
448 
449  }
450  else if (command.get(0).asString() == "get_last_target")
451  {
452  std::string last_target;
453  bool b = this->getNameOfCurrentTarget(last_target);
454  if (b)
455  {
456  reply.addString(last_target);
457  }
458  else
459  {
460  yCError(NAVIGATION2DCLIENT) << "get_last_target failed: goto <location_name> target not found.";
461  reply.addString("not found");
462  }
463  }
464  else if (command.get(0).asString() == "stop_nav")
465  {
466  this->stopNavigation();
467  reply.addString("Stopping movement.");
468  }
469  else if (command.get(0).asString() == "stop_loc")
470  {
471  this->stopLocalizationService();
472  reply.addString("Stopping localization service.");
473  }
474  else if (command.get(0).asString() == "pause_nav")
475  {
476  double time = -1;
477  if (command.size() > 1)
478  time = command.get(1).asFloat64();
479  this->suspendNavigation(time);
480  reply.addString("Pausing.");
481  }
482  else if (command.get(0).asString() == "resume_nav")
483  {
484  this->resumeNavigation();
485  reply.addString("Resuming.");
486  }
487  else if (command.get(0).asString() == "start_loc")
488  {
489  this->startLocalizationService();
490  reply.addString("Starting localization service.");
491  }
492  else
493  {
494  yCError(NAVIGATION2DCLIENT) << "Unknown command";
495  reply.addVocab(VOCAB_ERR);
496  }
497  return true;
498 }
499 
501 {
502  yarp::os::Bottle command;
503  yarp::os::Bottle reply;
504  bool ok = command.read(connection);
505  if (!ok) return false;
506  reply.clear();
507 
508  if (command.get(0).isString())
509  {
510  parse_respond_string(command, reply);
511  }
512  else
513  {
514  yCError(NAVIGATION2DCLIENT) << "Invalid command type";
515  reply.addVocab(VOCAB_ERR);
516  }
517 
518  yarp::os::ConnectionWriter *returnToSender = connection.getWriter();
519  if (returnToSender != nullptr)
520  {
521  reply.write(*returnToSender);
522  }
523  return true;
524 }
525 
527 {
529  yarp::os::Bottle resp;
530 
533  bool ret = m_rpc_port_navigation_server.write(b, resp);
534  if (ret)
535  {
536  if (resp.get(0).asVocab() != VOCAB_OK)
537  {
538  yCError(NAVIGATION2DCLIENT) << "getNavigationStatus() received error from navigation server";
539  return false;
540  }
541  else
542  {
543  status = (NavigationStatusEnum) resp.get(1).asInt32();
544  return true;
545  }
546  }
547  else
548  {
549  yCError(NAVIGATION2DCLIENT) << "getNavigationStatus() error on writing on rpc port";
550  return false;
551  }
552  return true;
553 }
554 
555 
557 {
559  yarp::os::Bottle resp;
560 
563  b.addString(loc.map_id);
564  b.addFloat64(loc.x);
565  b.addFloat64(loc.y);
566  b.addFloat64(loc.theta);
567  bool ret = m_rpc_port_navigation_server.write(b, resp);
568  if (ret)
569  {
570  if (resp.get(0).asVocab() != VOCAB_OK)
571  {
572  yCError(NAVIGATION2DCLIENT) << "gotoTargetByAbsoluteLocation() received error from navigation server";
573  return false;
574  }
575  }
576  else
577  {
578  yCError(NAVIGATION2DCLIENT) << "gotoTargetByAbsoluteLocation() error on writing on rpc port";
579  return false;
580  }
581 
582  reset_current_goal_name();
583  return true;
584 }
585 
586 bool Navigation2DClient::locations_are_similar(Map2DLocation loc1, Map2DLocation loc2, double linear_tolerance, double angular_tolerance)
587 {
588  if (linear_tolerance < 0) return false;
589  if (angular_tolerance < 0) return false;
590  yCAssert(NAVIGATION2DCLIENT, linear_tolerance >= 0);
591  yCAssert(NAVIGATION2DCLIENT, angular_tolerance >= 0);
592 
593  if (loc1.map_id != loc2.map_id)
594  {
595  return false;
596  }
597  if (sqrt(pow((loc1.x - loc2.x),2) + pow((loc1.y - loc2.y),2)) > linear_tolerance)
598  {
599  return false;
600  }
601 
602  if (angular_tolerance != std::numeric_limits<double>::infinity())
603  {
604  //In the following blocks, I'm giving two possibile solution to the problem of
605  //determining if the difference of two angles is below a certain threshold.
606  //The problem is tricky, because it must take in account the critical points 0,180,360,-180, -360 etc.
607  //Both the formulas lead to the same result, however I'm not sure I they have the same performances.
608  //Please do not remove the unused block, since it may be a useful reference for the future.
609 #if 1
610  //check in the range 0-360
611  double diff = loc2.theta - loc1.theta + 180.0;
612  diff = fmod(diff, 360.0) - 180.0;
613  diff = (diff < -180.0) ? (diff + 360.0) : (diff);
614  if (fabs(diff) > angular_tolerance)
615 #else
616  //check in the range 0-180
617  double angle1 = normalize_angle(loc1.theta);
618  double angle2 = normalize_angle(loc2.theta);
619  double diff = angle1 - angle2;
620  diff += (diff > 180) ? -360 : (diff < -180) ? 360 : 0;
621  if (fabs(diff) > angular_tolerance)
622 #endif
623  {
624  return false;
625  }
626  }
627  return true;
628 }
629 
630 bool Navigation2DClient::checkNearToLocation(Map2DLocation loc, double linear_tolerance, double angular_tolerance)
631 {
632  Map2DLocation curr_loc;
633  if (getCurrentPosition(curr_loc) == false)
634  {
635  yCError(NAVIGATION2DCLIENT) << "checkInsideArea() unable to get robot position";
636  return false;
637  }
638 
639  return locations_are_similar(loc, curr_loc, linear_tolerance, angular_tolerance);
640 
641  return true;
642 }
643 
644 bool Navigation2DClient::checkNearToLocation(std::string location_name, double linear_tolerance, double angular_tolerance)
645 {
646  Map2DLocation loc;
647  Map2DLocation curr_loc;
648  if (this->getLocation(location_name, loc) == false)
649  {
650  yCError(NAVIGATION2DCLIENT) << "Location" << location_name << "not found";
651  return false;
652  }
653 
654  if (getCurrentPosition(curr_loc) == false)
655  {
656  yCError(NAVIGATION2DCLIENT) << "checkInsideArea() unable to get robot position";
657  return false;
658  }
659 
660  return locations_are_similar(loc, curr_loc, linear_tolerance, angular_tolerance);
661 }
662 
664 {
665  Map2DLocation curr_loc;
666  if (getCurrentPosition(curr_loc) == false)
667  {
668  yCError(NAVIGATION2DCLIENT) << "checkInsideArea() unable to get robot position";
669  return false;
670  }
671 
672  if (area.checkLocationInsideArea(curr_loc) == false)
673  {
674  //yCDebug(NAVIGATION2DCLIENT) << "Not inside Area";
675  return false;
676  }
677 
678  return true;
679 }
680 
681 bool Navigation2DClient::checkInsideArea(std::string area_name)
682 {
683  Map2DLocation curr_loc;
684  Map2DArea area;
685  if (this->getArea(area_name, area) == false)
686  {
687  yCError(NAVIGATION2DCLIENT) << "Area" << area_name << "not found";
688  return false;
689  }
690 
691  if (getCurrentPosition(curr_loc) == false)
692  {
693  yCError(NAVIGATION2DCLIENT) << "checkInsideArea() unable to get robot position";
694  return false;
695  }
696 
697  if (area.checkLocationInsideArea(curr_loc) == false)
698  {
699  //yCDebug(NAVIGATION2DCLIENT) << "Not inside Area";
700  return false;
701  }
702 
703  return true;
704 }
705 
706 bool Navigation2DClient::gotoTargetByLocationName(std::string location_name)
707 {
708  Map2DLocation loc;
709  Map2DArea area;
710 
711  //first of all, ask to the location server if location_name exists as a location_name...
712  bool found = this->getLocation(location_name, loc);
713 
714  //...if found, ok...otherwise check if location_name is an area name instead...
715  if (found == false)
716  {
717  found = this->getArea(location_name, area);
718  if (found)
719  {
720  area.getRandomLocation(loc);
721  }
722  }
723 
724  //...if it is neither a location, nor an area then quit...
725  if (found == false)
726  {
727  yCError(NAVIGATION2DCLIENT) << "Location not found";
728  return false;
729  }
730 
731  //...otherwise we can go to the found/computed location!
732  this->gotoTargetByAbsoluteLocation(loc);
733  set_current_goal_name(location_name);
734 
735  return true;
736 }
737 
739 {
741  yarp::os::Bottle resp;
742 
745  b.addFloat64(x);
746  b.addFloat64(y);
747 
748  bool ret = m_rpc_port_navigation_server.write(b, resp);
749  if (ret)
750  {
751  if (resp.get(0).asVocab() != VOCAB_OK)
752  {
753  yCError(NAVIGATION2DCLIENT) << "gotoTargetByRelativeLocation() received error from navigation server";
754  return false;
755  }
756  }
757  else
758  {
759  yCError(NAVIGATION2DCLIENT) << "gotoTargetByRelativeLocation() error on writing on rpc port";
760  return false;
761  }
762 
763  reset_current_goal_name();
764  return true;
765 }
766 
767 bool Navigation2DClient::gotoTargetByRelativeLocation(double x, double y, double theta)
768 {
770  yarp::os::Bottle resp;
771 
774  b.addFloat64(x);
775  b.addFloat64(y);
776  b.addFloat64(theta);
777 
778  bool ret = m_rpc_port_navigation_server.write(b, resp);
779  if (ret)
780  {
781  if (resp.get(0).asVocab() != VOCAB_OK)
782  {
783  yCError(NAVIGATION2DCLIENT) << "gotoTargetByRelativeLocation() received error from navigation server";
784  return false;
785  }
786  }
787  else
788  {
789  yCError(NAVIGATION2DCLIENT) << "gotoTargetByRelativeLocation() error on writing on rpc port";
790  return false;
791  }
792 
793  reset_current_goal_name();
794  return true;
795 }
796 
798 {
800  yarp::os::Bottle resp;
801 
804 
805  bool ret = m_rpc_port_navigation_server.write(b, resp);
806  if (ret)
807  {
808  if (resp.get(0).asVocab() != VOCAB_OK)
809  {
810  yCError(NAVIGATION2DCLIENT) << "recomputeCurrentNavigationPath() received error from navigation server";
811  return false;
812  }
813  }
814  else
815  {
816  yCError(NAVIGATION2DCLIENT) << "recomputeCurrentNavigationPath() error on writing on rpc port";
817  return false;
818  }
819  return true;
820 }
821 
823 {
825  yarp::os::Bottle resp;
826 
829  b.addString(loc.map_id);
830  b.addFloat64(loc.x);
831  b.addFloat64(loc.y);
832  b.addFloat64(loc.theta);
833 
834  bool ret = m_rpc_port_localization_server.write(b, resp);
835  if (ret)
836  {
837  if (resp.get(0).asVocab() != VOCAB_OK)
838  {
839  yCError(NAVIGATION2DCLIENT) << "setInitialPose() received error from localization server";
840  return false;
841  }
842  }
843  else
844  {
845  yCError(NAVIGATION2DCLIENT) << "setInitialPose() error on writing on rpc port";
846  return false;
847  }
848  return true;
849 }
850 
852 {
853  if (cov.rows() != 3 || cov.cols() != 3)
854  {
855  yCError(NAVIGATION2DCLIENT) << "Covariance matrix is expected to have size (3,3)";
856  return false;
857  }
859  yarp::os::Bottle resp;
860 
863  b.addString(loc.map_id);
864  b.addFloat64(loc.x);
865  b.addFloat64(loc.y);
866  b.addFloat64(loc.theta);
867  yarp::os::Bottle& mc = b.addList();
868  for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { mc.addFloat64(cov[i][j]); } }
869 
870  bool ret = m_rpc_port_localization_server.write(b, resp);
871  if (ret)
872  {
873  if (resp.get(0).asVocab() != VOCAB_OK)
874  {
875  yCError(NAVIGATION2DCLIENT) << "setInitialPose() received error from localization server";
876  return false;
877  }
878  }
879  else
880  {
881  yCError(NAVIGATION2DCLIENT) << "setInitialPose() error on writing on rpc port";
882  return false;
883  }
884  return true;
885 }
886 
888 {
890  yarp::os::Bottle resp;
891 
894 
895  bool ret = m_rpc_port_localization_server.write(b, resp);
896  if (ret)
897  {
898  if (resp.get(0).asVocab() != VOCAB_OK || resp.size() != 6)
899  {
900  yCError(NAVIGATION2DCLIENT) << "getCurrentPosition() received error from localization server";
901  return false;
902  }
903  else
904  {
905  if (cov.rows() != 3 || cov.cols() != 3)
906  {
907  yCDebug(NAVIGATION2DCLIENT) << "Performance warning: covariance matrix is not (3,3), resizing...";
908  cov.resize(3, 3);
909  }
910  loc.map_id = resp.get(1).asString();
911  loc.x = resp.get(2).asFloat64();
912  loc.y = resp.get(3).asFloat64();
913  loc.theta = resp.get(4).asFloat64();
914  Bottle* mc = resp.get(5).asList();
915  if (mc == nullptr) return false;
916  for (size_t i = 0; i < 3; i++) { for (size_t j = 0; j < 3; j++) { cov[i][j] = mc->get(i * 3 + j).asFloat64(); } }
917  return true;
918  }
919  }
920  else
921  {
922  yCError(NAVIGATION2DCLIENT) << "getCurrentPosition() error on writing on rpc port";
923  return false;
924  }
925  return true;
926 }
927 
929 {
931  yarp::os::Bottle resp;
932 
935 
936  bool ret = m_rpc_port_localization_server.write(b, resp);
937  if (ret)
938  {
939  if (resp.get(0).asVocab() != VOCAB_OK || resp.size() != 10)
940  {
941  yCError(NAVIGATION2DCLIENT) << "getEstimatedOdometry() received error from localization server";
942  return false;
943  }
944  else
945  {
946  odom.odom_x = resp.get(1).asFloat64();
947  odom.odom_y = resp.get(2).asFloat64();
948  odom.odom_theta = resp.get(3).asFloat64();
949  odom.base_vel_x = resp.get(4).asFloat64();
950  odom.base_vel_y = resp.get(5).asFloat64();
951  odom.base_vel_theta = resp.get(6).asFloat64();
952  odom.odom_vel_x = resp.get(7).asFloat64();
953  odom.odom_vel_y = resp.get(8).asFloat64();
954  odom.odom_vel_theta = resp.get(9).asFloat64();
955  return true;
956  }
957  }
958  else
959  {
960  yCError(NAVIGATION2DCLIENT) << "getEstimatedOdometry() error on writing on rpc port";
961  return false;
962  }
963  return true;
964 }
965 
967 {
969  yarp::os::Bottle resp;
970 
973 
974  bool ret = m_rpc_port_localization_server.write(b, resp);
975  if (ret)
976  {
977  if (resp.get(0).asVocab() != VOCAB_OK || resp.size() != 5)
978  {
979  yCError(NAVIGATION2DCLIENT) << "getCurrentPosition() received error from localization server";
980  return false;
981  }
982  else
983  {
984  loc.map_id = resp.get(1).asString();
985  loc.x = resp.get(2).asFloat64();
986  loc.y = resp.get(3).asFloat64();
987  loc.theta = resp.get(4).asFloat64();
988  return true;
989  }
990  }
991  else
992  {
993  yCError(NAVIGATION2DCLIENT) << "getCurrentPosition() error on writing on rpc port";
994  return false;
995  }
996  return true;
997 }
998 
999 bool Navigation2DClient::suspendNavigation(const double time_s)
1000 {
1001  yarp::os::Bottle b;
1002  yarp::os::Bottle resp;
1003 
1006  b.addFloat64(time_s);
1007 
1008  bool ret = m_rpc_port_navigation_server.write(b, resp);
1009  if (ret)
1010  {
1011  if (resp.get(0).asVocab() != VOCAB_OK)
1012  {
1013  yCError(NAVIGATION2DCLIENT) << "suspendNavigation() received error from navigation server";
1014  return false;
1015  }
1016  }
1017  else
1018  {
1019  yCError(NAVIGATION2DCLIENT) << "suspendNavigation() error on writing on rpc port";
1020  return false;
1021  }
1022  return true;
1023 }
1024 
1026 {
1027  yarp::os::Bottle b;
1028  yarp::os::Bottle resp;
1029 
1032 
1033  bool ret = m_rpc_port_navigation_server.write(b, resp);
1034  if (ret)
1035  {
1036  if (resp.get(0).asVocab() != VOCAB_OK || resp.size() != 5)
1037  {
1038  yCError(NAVIGATION2DCLIENT) << "getAbsoluteLocationOfCurrentTarget() received error from navigation server";
1039  return false;
1040  }
1041  else
1042  {
1043  loc.map_id = resp.get(1).asString();
1044  loc.x = resp.get(2).asFloat64();
1045  loc.y = resp.get(3).asFloat64();
1046  loc.theta = resp.get(4).asFloat64();
1047  return true;
1048  }
1049  }
1050  else
1051  {
1052  yCError(NAVIGATION2DCLIENT) << "getAbsoluteLocationOfCurrentTarget() error on writing on rpc port";
1053  return false;
1054  }
1055  return true;
1056 }
1057 
1058 bool Navigation2DClient::getNameOfCurrentTarget(std::string& location_name)
1059 {
1060  std::string s;
1061  if (get_current_goal_name(s))
1062  {
1063  location_name = s;
1064  return true;
1065  }
1066 
1067  location_name = "";
1068  yCError(NAVIGATION2DCLIENT) << "No name for the current target, or no target set";
1069  return true;
1070 }
1071 
1072 bool Navigation2DClient::getRelativeLocationOfCurrentTarget(double& x, double& y, double& theta)
1073 {
1074  yarp::os::Bottle b;
1075  yarp::os::Bottle resp;
1076 
1079 
1080  bool ret = m_rpc_port_navigation_server.write(b, resp);
1081  if (ret)
1082  {
1083  if (resp.get(0).asVocab() != VOCAB_OK || resp.size()!=4)
1084  {
1085  yCError(NAVIGATION2DCLIENT) << "getRelativeLocationOfCurrentTarget() received error from navigation server";
1086  return false;
1087  }
1088  else
1089  {
1090  x = resp.get(1).asFloat64();
1091  y = resp.get(2).asFloat64();
1092  theta = resp.get(3).asFloat64();
1093  return true;
1094  }
1095  }
1096  else
1097  {
1098  yCError(NAVIGATION2DCLIENT) << "getRelativeLocationOfCurrentTarget() error on writing on rpc port";
1099  return false;
1100  }
1101  return true;
1102 }
1103 
1104 bool Navigation2DClient::storeCurrentPosition(std::string location_name)
1105 {
1106  yarp::os::Bottle b_nav;
1107  yarp::os::Bottle resp_nav;
1108  yarp::os::Bottle b_loc;
1109  yarp::os::Bottle resp_loc;
1110  Map2DLocation loc;
1111 
1112  b_nav.addVocab(VOCAB_INAVIGATION);
1114  bool ret_nav = m_rpc_port_localization_server.write(b_nav, resp_nav);
1115  if (ret_nav)
1116  {
1117  if (resp_nav.get(0).asVocab() != VOCAB_OK || resp_nav.size()!=5)
1118  {
1119  yCError(NAVIGATION2DCLIENT) << "storeCurrentPosition() received error from localization server";
1120  return false;
1121  }
1122  else
1123  {
1124  loc.map_id = resp_nav.get(1).asString();
1125  loc.x = resp_nav.get(2).asFloat64();
1126  loc.y = resp_nav.get(3).asFloat64();
1127  loc.theta = resp_nav.get(4).asFloat64();
1128  }
1129  }
1130  else
1131  {
1132  yCError(NAVIGATION2DCLIENT) << "storeCurrentPosition() error on writing on rpc port";
1133  return false;
1134  }
1135 
1136  b_loc.addVocab(VOCAB_INAVIGATION);
1137  b_loc.addVocab(VOCAB_NAV_STORE_X);
1139  b_loc.addString(location_name);
1140  b_loc.addString(loc.map_id);
1141  b_loc.addFloat64(loc.x);
1142  b_loc.addFloat64(loc.y);
1143  b_loc.addFloat64(loc.theta);
1144 
1145  bool ret_loc = m_rpc_port_map_locations_server.write(b_loc, resp_loc);
1146  if (ret_loc)
1147  {
1148  if (resp_loc.get(0).asVocab() != VOCAB_OK)
1149  {
1150  yCError(NAVIGATION2DCLIENT) << "storeCurrentPosition() received error from locations server";
1151  return false;
1152  }
1153  }
1154  else
1155  {
1156  yCError(NAVIGATION2DCLIENT) << "storeCurrentPosition() error on writing on rpc port";
1157  return false;
1158  }
1159  return true;
1160 }
1161 
1162 bool Navigation2DClient::storeLocation(std::string location_name, Map2DLocation loc)
1163 {
1164  yarp::os::Bottle b;
1165  yarp::os::Bottle resp;
1166 
1170  b.addString(location_name);
1171  b.addString(loc.map_id);
1172  b.addFloat64(loc.x);
1173  b.addFloat64(loc.y);
1174  b.addFloat64(loc.theta);
1175 
1176  bool ret = m_rpc_port_map_locations_server.write(b, resp);
1177  if (ret)
1178  {
1179  if (resp.get(0).asVocab() != VOCAB_OK)
1180  {
1181  yCError(NAVIGATION2DCLIENT) << "storeLocation() received error from locations server";
1182  return false;
1183  }
1184  }
1185  else
1186  {
1187  yCError(NAVIGATION2DCLIENT) << "storeLocation() error on writing on rpc port";
1188  return false;
1189  }
1190  return true;
1191 }
1192 
1193 bool Navigation2DClient::getLocationsList(std::vector<std::string>& locations)
1194 {
1195  yarp::os::Bottle b;
1196  yarp::os::Bottle resp;
1197 
1201 
1202  bool ret = m_rpc_port_map_locations_server.write(b, resp);
1203  if (ret)
1204  {
1205  if (resp.get(0).asVocab() != VOCAB_OK)
1206  {
1207  yCError(NAVIGATION2DCLIENT) << "getLocationsList() received error from locations server";
1208  return false;
1209  }
1210  else
1211  {
1212  Bottle* list = resp.get(1).asList();
1213  if (list)
1214  {
1215  locations.clear();
1216  for (size_t i = 0; i < list->size(); i++)
1217  {
1218  locations.push_back(list->get(i).asString());
1219  }
1220  return true;
1221  }
1222  else
1223  {
1224  yCError(NAVIGATION2DCLIENT) << "getLocationsList() error while reading from locations server";
1225  return false;
1226  }
1227  }
1228  }
1229  else
1230  {
1231  yCError(NAVIGATION2DCLIENT) << "getLocationsList() error on writing on rpc port";
1232  return false;
1233  }
1234  return true;
1235 }
1236 
1237 bool Navigation2DClient::getLocation(std::string location_name, Map2DLocation& loc)
1238 {
1239  yarp::os::Bottle b;
1240  yarp::os::Bottle resp;
1241 
1245  b.addString(location_name);
1246 
1247  bool ret = m_rpc_port_map_locations_server.write(b, resp);
1248  if (ret)
1249  {
1250  if (resp.get(0).asVocab() != VOCAB_OK)
1251  {
1252  yCError(NAVIGATION2DCLIENT) << "getLocation() received error from locations server";
1253  return false;
1254  }
1255  else
1256  {
1257  loc.map_id = resp.get(1).asString();
1258  loc.x = resp.get(2).asFloat64();
1259  loc.y = resp.get(3).asFloat64();
1260  loc.theta = resp.get(4).asFloat64();
1261  }
1262  }
1263  else
1264  {
1265  yCError(NAVIGATION2DCLIENT) << "getLocation() error on writing on rpc port";
1266  return false;
1267  }
1268  return true;
1269 }
1270 
1271 bool Navigation2DClient::getArea(std::string area_name, Map2DArea& area)
1272 {
1273  yarp::os::Bottle b_loc;
1274  yarp::os::Bottle resp_loc;
1275 
1276  {
1277  b_loc.clear();
1278  b_loc.addVocab(VOCAB_INAVIGATION);
1279  b_loc.addVocab(VOCAB_NAV_GET_X);
1280  b_loc.addVocab(VOCAB_NAV_AREA);
1281  b_loc.addString(area_name);
1282 
1283  bool ret = m_rpc_port_map_locations_server.write(b_loc, resp_loc);
1284  if (ret)
1285  {
1286  if (resp_loc.get(0).asVocab() != VOCAB_OK)
1287  {
1288  yCError(NAVIGATION2DCLIENT) << "getArea() received error from locations server";
1289  return false;
1290  }
1291  else
1292  {
1293  Value& b = resp_loc.get(1);
1294  if (Property::copyPortable(b, area) == false)
1295  {
1296  yCError(NAVIGATION2DCLIENT) << "getArea() received error from locations server";
1297  return false;
1298  }
1299  }
1300  }
1301  else
1302  {
1303  yCError(NAVIGATION2DCLIENT) << "getArea() error on writing on rpc port";
1304  return false;
1305  }
1306  }
1307  return true;
1308 }
1309 
1310 bool Navigation2DClient::deleteLocation(std::string location_name)
1311 {
1312  yarp::os::Bottle b;
1313  yarp::os::Bottle resp;
1314 
1318  b.addString(location_name);
1319 
1320  bool ret = m_rpc_port_map_locations_server.write(b, resp);
1321  if (ret)
1322  {
1323  if (resp.get(0).asVocab() != VOCAB_OK)
1324  {
1325  yCError(NAVIGATION2DCLIENT) << "deleteLocation() received error from locations server";
1326  return false;
1327  }
1328  }
1329  else
1330  {
1331  yCError(NAVIGATION2DCLIENT) << "deleteLocation() error on writing on rpc port";
1332  return false;
1333  }
1334  return true;
1335 }
1336 
1338 {
1339  yarp::os::Bottle b;
1340  yarp::os::Bottle resp;
1341 
1345 
1346  bool ret = m_rpc_port_map_locations_server.write(b, resp);
1347  if (ret)
1348  {
1349  if (resp.get(0).asVocab() != VOCAB_OK)
1350  {
1351  yCError(NAVIGATION2DCLIENT) << "clearAllLocations() received error from locations server";
1352  return false;
1353  }
1354  }
1355  else
1356  {
1357  yCError(NAVIGATION2DCLIENT) << "clearAllLocations() error on writing on rpc port";
1358  return false;
1359  }
1360  return true;
1361 }
1362 
1364 {
1365  yarp::os::Bottle b;
1366  yarp::os::Bottle resp;
1367 
1370 
1371  bool ret = m_rpc_port_navigation_server.write(b, resp);
1372  if (ret)
1373  {
1374  if (resp.get(0).asVocab() != VOCAB_OK)
1375  {
1376  yCError(NAVIGATION2DCLIENT) << "stopNavigation() received error from navigation server";
1377  return false;
1378  }
1379  }
1380  else
1381  {
1382  yCError(NAVIGATION2DCLIENT) << "stopNavigation() error on writing on rpc port";
1383  return false;
1384  }
1385  return true;
1386 }
1387 
1389 {
1390  yarp::os::Bottle b;
1391  yarp::os::Bottle resp;
1392 
1395 
1396  bool ret = m_rpc_port_navigation_server.write(b, resp);
1397  if (ret)
1398  {
1399  if (resp.get(0).asVocab() != VOCAB_OK)
1400  {
1401  yCError(NAVIGATION2DCLIENT) << "resumeNavigation() received error from navigation server";
1402  return false;
1403  }
1404  }
1405  else
1406  {
1407  yCError(NAVIGATION2DCLIENT) << "resumeNavigation() error on writing on rpc port";
1408  return false;
1409  }
1410  return true;
1411 }
1412 
1414 {
1415  yarp::os::Bottle b;
1416  yarp::os::Bottle resp;
1417 
1420  b.addVocab(trajectory_type);
1421 
1422  bool ret = m_rpc_port_navigation_server.write(b, resp);
1423  if (ret)
1424  {
1425  if (resp.get(0).asVocab() != VOCAB_OK)
1426  {
1427  yCError(NAVIGATION2DCLIENT) << "getNavigationWaypoints() received error from navigation server";
1428  return false;
1429  }
1430  else if (resp.get(1).isList() && resp.get(1).asList()->size()>0)
1431  {
1432  waypoints.clear();
1433  Bottle* waypoints_bottle = resp.get(1).asList();
1434  if (waypoints_bottle == 0) { yCError(NAVIGATION2DCLIENT) << "getNavigationWaypoints parsing error"; return false; }
1435  for (size_t i = 0; i < waypoints_bottle->size(); i++)
1436  {
1437  Bottle* the_waypoint = waypoints_bottle->get(i).asList();
1438  if (the_waypoint == 0) { yCError(NAVIGATION2DCLIENT) << "getNavigationWaypoints parsing error"; return false; }
1439  Map2DLocation loc;
1440  loc.map_id = the_waypoint->get(0).asString();
1441  loc.x = the_waypoint->get(1).asFloat64();
1442  loc.y = the_waypoint->get(2).asFloat64();
1443  loc.theta = the_waypoint->get(3).asFloat64();
1444  waypoints.push_back(loc);
1445  }
1446  return true;
1447  }
1448  else
1449  {
1450  //not available
1451  waypoints.clear();
1452  return false;
1453  }
1454  }
1455  else
1456  {
1457  yCError(NAVIGATION2DCLIENT) << "getCurrentPosition() error on writing on rpc port";
1458  return false;
1459  }
1460  return true;
1461 }
1462 
1464 {
1465  yarp::os::Bottle b;
1466  yarp::os::Bottle resp;
1467 
1470 
1471  bool ret = m_rpc_port_navigation_server.write(b, resp);
1472  if (ret)
1473  {
1474  if (resp.get(0).asVocab() != VOCAB_OK)
1475  {
1476  yCError(NAVIGATION2DCLIENT) << "getCurrentNavigationWaypoint() received error from navigation server";
1477  return false;
1478  }
1479  else if (resp.size() == 5)
1480  {
1481  curr_waypoint.map_id = resp.get(1).asString();
1482  curr_waypoint.x = resp.get(2).asFloat64();
1483  curr_waypoint.y = resp.get(3).asFloat64();
1484  curr_waypoint.theta = resp.get(4).asFloat64();
1485  return true;
1486  }
1487  else
1488  {
1489  //not available
1490  curr_waypoint.map_id = "invalid";
1491  curr_waypoint.x = std::nan("");
1492  curr_waypoint.y = std::nan("");
1493  curr_waypoint.theta = std::nan("");
1494  return false;
1495  }
1496  }
1497  else
1498  {
1499  yCError(NAVIGATION2DCLIENT) << "getCurrentPosition() error on writing on rpc port";
1500  return false;
1501  }
1502  return true;
1503 }
1504 
1506 {
1507  yarp::os::Bottle b;
1508  yarp::os::Bottle resp;
1509 
1512  b.addVocab(map_type);
1513 
1514  bool ret = m_rpc_port_navigation_server.write(b, resp);
1515  if (ret)
1516  {
1517  if (resp.get(0).asVocab() != VOCAB_OK)
1518  {
1519  yCError(NAVIGATION2DCLIENT) << "getCurrentNavigationMap() received error from server";
1520  return false;
1521  }
1522  else
1523  {
1524  Value& b = resp.get(1);
1525  if (Property::copyPortable(b, map))
1526  {
1527  return true;
1528  }
1529  else
1530  {
1531  yCError(NAVIGATION2DCLIENT) << "getCurrentNavigationMap() failed copyPortable()";
1532  return false;
1533  }
1534  }
1535  }
1536  else
1537  {
1538  yCError(NAVIGATION2DCLIENT) << "getCurrentNavigationMap() error on writing on rpc port";
1539  return false;
1540  }
1541  return true;
1542 }
1543 
1545 {
1546  yarp::os::Bottle b;
1547  yarp::os::Bottle resp;
1548 
1551 
1552  bool ret = m_rpc_port_localization_server.write(b, resp);
1553  if (ret)
1554  {
1555  if (resp.get(0).asVocab() != VOCAB_OK || resp.size() != 2)
1556  {
1557  yCError(NAVIGATION2DCLIENT) << "getLocalizationStatus() received error from localization server";
1558  return false;
1559  }
1560  else
1561  {
1563  return true;
1564  }
1565  }
1566  else
1567  {
1568  yCError(NAVIGATION2DCLIENT) << "getLocalizationStatus() error on writing on rpc port";
1569  return false;
1570  }
1571  return true;
1572 }
1573 
1574 bool Navigation2DClient::applyVelocityCommand(double x_vel, double y_vel, double theta_vel, double timeout)
1575 {
1576  yarp::os::Bottle b;
1577  yarp::os::Bottle resp;
1578 
1581  b.addFloat64(x_vel);
1582  b.addFloat64(y_vel);
1583  b.addFloat64(theta_vel);
1584  b.addFloat64(timeout);
1585 
1586  bool ret = m_rpc_port_navigation_server.write(b, resp);
1587  if (ret)
1588  {
1589  if (resp.get(0).asVocab() != VOCAB_OK)
1590  {
1591  yCError(NAVIGATION2DCLIENT) << "applyVelocityCommand() received error from navigation server";
1592  return false;
1593  }
1594  }
1595  else
1596  {
1597  yCError(NAVIGATION2DCLIENT) << "applyVelocityCommand() error on writing on rpc port";
1598  return false;
1599  }
1600 
1601  reset_current_goal_name();
1602  return true;
1603 }
1604 
1605 bool Navigation2DClient::getEstimatedPoses(std::vector<Map2DLocation>& poses)
1606 {
1607  yarp::os::Bottle b;
1608  yarp::os::Bottle resp;
1609 
1612 
1613  bool ret = m_rpc_port_localization_server.write(b, resp);
1614  if (ret)
1615  {
1616  if (resp.get(0).asVocab() != VOCAB_OK)
1617  {
1618  yCError(NAVIGATION2DCLIENT) << "getEstimatedPoses() received error from localization server";
1619  return false;
1620  }
1621  else
1622  {
1623  int nposes = resp.get(1).asInt32();
1624  poses.clear();
1625  for (int i = 0; i < nposes; i++)
1626  {
1627  Map2DLocation loc;
1628  Bottle* b = resp.get(2 + i).asList();
1629  if (b)
1630  {
1631  loc.map_id = b->get(0).asString();
1632  loc.x = b->get(1).asFloat64();
1633  loc.y = b->get(2).asFloat64();
1634  loc.theta = b->get(3).asFloat64();
1635  }
1636  else
1637  {
1638  poses.clear();
1639  yCError(NAVIGATION2DCLIENT) << "getEstimatedPoses() parsing error";
1640  return false;
1641  }
1642  poses.push_back(loc);
1643  }
1644  return true;
1645  }
1646  }
1647  else
1648  {
1649  yCError(NAVIGATION2DCLIENT) << "getEstimatedPoses() error on writing on rpc port";
1650  return false;
1651  }
1652  return true;
1653 }
1654 
1655 //this function receives an angle from (-inf,+inf) and returns an angle in (0,180) or (-180,0)
1656 double Navigation2DClient::normalize_angle(double angle)
1657 {
1658  angle = std::remainder(angle, 360);
1659 
1660  if (angle > 180 && angle < 360)
1661  {
1662  angle = angle - 360;
1663  }
1664 
1665  if (angle<-180 && angle>-360)
1666  {
1667  angle = angle + 360;
1668  }
1669  return angle;
1670 }
1671 
1673 {
1674  yarp::os::Bottle b;
1675  yarp::os::Bottle resp;
1676 
1679 
1680  bool ret = m_rpc_port_localization_server.write(b, resp);
1681  if (ret)
1682  {
1683  if (resp.get(0).asVocab() != VOCAB_OK)
1684  {
1685  yCError(NAVIGATION2DCLIENT) << "startLocalizationService() received error from navigation server";
1686  return false;
1687  }
1688  }
1689  else
1690  {
1691  yCError(NAVIGATION2DCLIENT) << "startLocalizationService() error on writing on rpc port";
1692  return false;
1693  }
1694  return true;
1695 }
1696 
1698 {
1699  yarp::os::Bottle b;
1700  yarp::os::Bottle resp;
1701 
1704 
1705  bool ret = m_rpc_port_localization_server.write(b, resp);
1706  if (ret)
1707  {
1708  if (resp.get(0).asVocab() != VOCAB_OK)
1709  {
1710  yCError(NAVIGATION2DCLIENT) << "stopLocalizationService() received error from navigation server";
1711  return false;
1712  }
1713  }
1714  else
1715  {
1716  yCError(NAVIGATION2DCLIENT) << "stopLocalizationService() error on writing on rpc port";
1717  return false;
1718  }
1719  return true;
1720 }
float t
constexpr yarp::conf::vocab32_t VOCAB_OK
Definition: GenericVocabs.h:18
constexpr yarp::conf::vocab32_t VOCAB_ERR
Definition: GenericVocabs.h:20
constexpr yarp::conf::vocab32_t VOCAB_NAV_DELETE_X
constexpr yarp::conf::vocab32_t VOCAB_NAV_GET_LOCALIZER_POSES
constexpr yarp::conf::vocab32_t VOCAB_NAV_VELOCITY_CMD
constexpr yarp::conf::vocab32_t VOCAB_INAVIGATION
constexpr yarp::conf::vocab32_t VOCAB_NAV_GET_LIST_X
constexpr yarp::conf::vocab32_t VOCAB_NAV_AREA
constexpr yarp::conf::vocab32_t VOCAB_NAV_GET_ABS_TARGET
constexpr yarp::conf::vocab32_t VOCAB_NAV_RECOMPUTE_PATH
constexpr yarp::conf::vocab32_t VOCAB_NAV_STORE_X
constexpr yarp::conf::vocab32_t VOCAB_NAV_GET_REL_TARGET
constexpr yarp::conf::vocab32_t VOCAB_NAV_LOCALIZATION_STOP
constexpr yarp::conf::vocab32_t VOCAB_NAV_GET_X
constexpr yarp::conf::vocab32_t VOCAB_NAV_SET_INITIAL_POSCOV
constexpr yarp::conf::vocab32_t VOCAB_NAV_CLEAR_X
constexpr yarp::conf::vocab32_t VOCAB_NAV_LOCATION
constexpr yarp::conf::vocab32_t VOCAB_NAV_GOTOREL
constexpr yarp::conf::vocab32_t VOCAB_NAV_GET_CURRENT_POS
constexpr yarp::conf::vocab32_t VOCAB_NAV_GET_LOCALIZER_STATUS
constexpr yarp::conf::vocab32_t VOCAB_NAV_GET_ESTIMATED_ODOM
constexpr yarp::conf::vocab32_t VOCAB_NAV_SET_INITIAL_POS
constexpr yarp::conf::vocab32_t VOCAB_NAV_GET_CURRENT_POSCOV
constexpr yarp::conf::vocab32_t VOCAB_NAV_GET_NAVIGATION_STATUS
constexpr yarp::conf::vocab32_t VOCAB_NAV_GOTOABS
constexpr yarp::conf::vocab32_t VOCAB_NAV_LOCALIZATION_START
constexpr yarp::conf::vocab32_t VOCAB_NAV_RESUME
constexpr yarp::conf::vocab32_t VOCAB_NAV_GET_CURRENT_WAYPOINT
constexpr yarp::conf::vocab32_t VOCAB_NAV_GET_NAVIGATION_WAYPOINTS
constexpr yarp::conf::vocab32_t VOCAB_NAV_GET_NAV_MAP
constexpr yarp::conf::vocab32_t VOCAB_NAV_STOP
constexpr yarp::conf::vocab32_t VOCAB_NAV_SUSPEND
bool ret
bool getLocation(std::string location_name, yarp::dev::Nav2D::Map2DLocation &loc) override
Retrieves a location previously stored by the user.
bool storeCurrentPosition(std::string location_name) override
Store the current location of the robot.
bool getCurrentPosition(yarp::dev::Nav2D::Map2DLocation &loc) override
Gets the current position of the robot w.r.t world reference frame.
bool gotoTargetByRelativeLocation(double x, double y, double theta) override
Ask the robot to reach a position defined in the robot reference frame.
bool getNavigationStatus(yarp::dev::Nav2D::NavigationStatusEnum &status) override
Gets the current status of the navigation task.
bool parse_respond_string(const yarp::os::Bottle &command, yarp::os::Bottle &reply)
bool getEstimatedOdometry(yarp::dev::OdometryData &odom) override
Gets the estimated odometry the robot, including its velocity expressed in the world and in the local...
bool getLocalizationStatus(yarp::dev::Nav2D::LocalizationStatusEnum &status) override
Gets the current status of the localization task.
bool getEstimatedPoses(std::vector< yarp::dev::Nav2D::Map2DLocation > &poses) override
Gets a set of pose estimates computed by the localization algorithm.
bool storeLocation(std::string location_name, yarp::dev::Nav2D::Map2DLocation loc) override
Store a location specified by the user in the world reference frame.
bool getAllNavigationWaypoints(yarp::dev::Nav2D::TrajectoryTypeEnum trajectory_type, yarp::dev::Nav2D::Map2DPath &waypoints) override
Returns the list of waypoints generated by the navigation algorithm.
bool getAbsoluteLocationOfCurrentTarget(yarp::dev::Nav2D::Map2DLocation &loc) override
Gets the last navigation target in the world reference frame.
virtual bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
virtual bool checkInsideArea(yarp::dev::Nav2D::Map2DArea area) override
Check if the robot is currently inside the specified area.
bool applyVelocityCommand(double x_vel, double y_vel, double theta_vel, double timeout=0.1) override
Apply a velocity command.
bool getNameOfCurrentTarget(std::string &location_name) override
Gets the name of the current target, if available (set by gotoTargetByLocationName)
bool deleteLocation(std::string location_name) override
Delete a location.
bool getRelativeLocationOfCurrentTarget(double &x, double &y, double &theta) override
Gets the last navigation target in the robot reference frame.
virtual bool checkNearToLocation(yarp::dev::Nav2D::Map2DLocation loc, double linear_tolerance, double angular_tolerance=std::numeric_limits< double >::infinity()) override
Check if the robot is currently near to the specified area.
bool recomputeCurrentNavigationPath() override
Forces the navigation system to recompute the path from the current robot position to the current goa...
bool getArea(std::string area_name, yarp::dev::Nav2D::Map2DArea &area) override
Retrieves an area previously stored by the user.
bool stopNavigation() override
Terminates the current navigation task.
bool suspendNavigation(const double time_s) override
Ask to the robot to suspend the current navigation task for a defined amount of time.
bool stopLocalizationService() override
Stops the localization service.
bool setInitialPose(const yarp::dev::Nav2D::Map2DLocation &loc) override
Sets the initial pose for the localization algorithm which estimates the current position of the robo...
bool clearAllLocations() override
Delete all stored locations.
bool gotoTargetByAbsoluteLocation(yarp::dev::Nav2D::Map2DLocation loc) override
Ask the robot to reach a position defined in the world reference frame.
bool close() override
Close the DeviceDriver.
bool gotoTargetByLocationName(std::string location_name) override
Ask the robot to reach a previously stored location/area.
bool startLocalizationService() override
Starts the localization service.
bool getCurrentNavigationMap(yarp::dev::Nav2D::NavigationMapTypeEnum map_type, yarp::dev::Nav2D::MapGrid2D &map) override
Returns the current navigation map processed by the navigation algorithm.
bool getCurrentNavigationWaypoint(yarp::dev::Nav2D::Map2DLocation &curr_waypoint) override
Returns the current waypoint pursued by the navigation algorithm.
bool getLocationsList(std::vector< std::string > &locations) override
Get a list of all stored locations.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool resumeNavigation() override
Resume a previously suspended navigation task.
bool getRandomLocation(yarp::dev::Nav2D::Map2DLocation &loc)
get a random Map2DLocation inside the Map2DArea @loc the computed Map2DLocation
Definition: Map2DArea.cpp:205
bool checkLocationInsideArea(yarp::dev::Nav2D::Map2DLocation loc)
Check if a Map2DLocation is inside a Map2DArea.
Definition: Map2DArea.cpp:150
void push_back(yarp::dev::Nav2D::Map2DLocation loc)
Inserts a new location into the path @loc the location to be inserted.
Definition: Map2DPath.cpp:127
void clear()
Remove all elements from the path.
Definition: Map2DPath.cpp:58
double base_vel_x
velocity of the robot [m/s] expressed in the robot reference frame
Definition: OdometryData.h:45
double odom_vel_y
velocity of the robot [m/s] expressed in the world reference frame
Definition: OdometryData.h:61
double base_vel_theta
angular velocity of the robot [deg/s] expressed in the robot reference frame
Definition: OdometryData.h:53
double odom_vel_theta
angular velocity of the robot [deg/s] expressed in the world reference frame
Definition: OdometryData.h:65
double base_vel_y
velocity of the robot [m/s] expressed in the robot reference frame
Definition: OdometryData.h:49
double odom_x
position of the robot [m], expressed in the world reference frame
Definition: OdometryData.h:33
double odom_y
position of the robot [m], expressed in the world reference frame
Definition: OdometryData.h:37
double odom_theta
orientation the robot [deg], expressed in the world reference frame
Definition: OdometryData.h:41
double odom_vel_x
velocity of the robot [m/s] expressed in the world reference frame
Definition: OdometryData.h:57
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
Bottle & addList()
Places an empty nested list in the bottle, at the end of the list.
Definition: Bottle.cpp:185
size_type size() const
Gets the number of elements in the bottle.
Definition: Bottle.cpp:254
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Definition: Bottle.cpp:243
void addFloat64(yarp::conf::float64_t x)
Places a 64-bit floating point number in the bottle, at the end of the list.
Definition: Bottle.cpp:161
void addVocab(int x)
Places a vocabulary item in the bottle, at the end of the list.
Definition: Bottle.cpp:167
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:249
void clear()
Empties the bottle of any objects it contains.
Definition: Bottle.cpp:124
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
Definition: Bottle.cpp:233
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition: Bottle.cpp:173
An interface for reading from a network connection.
virtual ConnectionWriter * getWriter()=0
Gets a way to reply to the message, if possible.
An interface for writing to a network connection.
A base class for nested structures that can be searched.
Definition: Searchable.h:69
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
A single value (typically within a Bottle).
Definition: Value.h:47
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
Definition: Value.cpp:225
virtual bool isString() const
Checks if value is a string.
Definition: Value.cpp:159
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition: Value.cpp:207
virtual bool isList() const
Checks if value is a list.
Definition: Value.cpp:165
virtual Bottle * asList() const
Get list value.
Definition: Value.cpp:243
virtual std::int32_t asVocab() const
Get vocabulary identifier as an integer.
Definition: Value.cpp:231
virtual std::string asString() const
Get string value.
Definition: Value.cpp:237
A class for a Matrix.
Definition: Matrix.h:46
void resize(size_t r, size_t c)
Resize the matrix, if matrix is not empty preserve old content.
Definition: Matrix.cpp:251
size_t cols() const
Return number of columns.
Definition: Matrix.h:101
size_t rows() const
Return number of rows.
Definition: Matrix.h:95
std::string toString(int precision=-1, int width=-1, const char *endRowStr="\n") const
Print matrix to a string.
Definition: Matrix.cpp:167
#define yCError(component,...)
Definition: LogComponent.h:157
#define yCAssert(component, x)
Definition: LogComponent.h:172
#define yCWarning(component,...)
Definition: LogComponent.h:146
#define yCDebug(component,...)
Definition: LogComponent.h:112
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
std::string statusToString(NavigationStatusEnum status)
An interface for the device drivers.
NetInt32 encode(const std::string &str)
Convert a string into a vocabulary identifier.
Definition: Vocab.cpp:14
An interface to the operating system, including Port based communication.
Signal processing.
Definition: Image.h:25
std::string toString() const
Returns text representation of the location.
Definition: Map2DLocation.h:77