YARP
Yet Another Robot Platform
Map2D_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 "Map2D_nws_yarp.h"
7 
8 #include <yarp/os/Log.h>
9 #include <yarp/os/LogComponent.h>
10 #include <yarp/os/LogStream.h>
11 #include <yarp/os/Node.h>
12 #include <yarp/os/Publisher.h>
13 #include <yarp/os/Subscriber.h>
14 
15 #include <yarp/dev/GenericVocabs.h>
16 #include <yarp/dev/IMap2D.h>
17 #include <yarp/dev/INavigation2D.h>
18 
19 #include <yarp/math/Math.h>
20 
22 #include <yarp/rosmsg/TickTime.h>
23 
24 #include <cstdlib>
25 #include <fstream>
26 #include <limits>
27 #include <mutex>
28 #include <sstream>
29 
30 using namespace yarp::sig;
31 using namespace yarp::dev;
32 using namespace yarp::dev::Nav2D;
33 using namespace yarp::os;
34 
35 namespace {
36 YARP_LOG_COMPONENT(MAP2D_NWS_YARP, "yarp.device.map2D_nws_yarp")
37 }
38 
44 {
45 }
46 
48 
49 void Map2D_nws_yarp::parse_vocab_command(yarp::os::Bottle& in, yarp::os::Bottle& out)
50 {
51  int code = in.get(0).asVocab32();
52  if (code == VOCAB_IMAP)
53  {
54  int cmd = in.get(1).asVocab32();
55  if (cmd == VOCAB_IMAP_SET_MAP)
56  {
57  MapGrid2D the_map;
58  Value& b = in.get(2);
59  if (Property::copyPortable(b, the_map))
60  {
61  m_iMap2D->store_map(the_map);
62  out.clear();
64  }
65  else
66  {
67  out.clear();
69  yCError(MAP2D_NWS_YARP) << "Error in copyPortable";
70  }
71  }
72  else if (cmd == VOCAB_IMAP_GET_MAP)
73  {
74  std::string name = in.get(2).asString();
75  MapGrid2D map;
76  if (m_iMap2D->get_map(name, map))
77  {
78  map.enable_map_compression_over_network(m_send_maps_compressed);
79  out.clear();
81  yarp::os::Bottle& mapbot = out.addList();
82  Property::copyPortable(map, mapbot);
83  }
84 
85  else
86  {
87  out.clear();
89  yCError(MAP2D_NWS_YARP) << "Map" << name << "not found";
90  }
91  }
92  else if (cmd == VOCAB_IMAP_GET_NAMES)
93  {
94  out.clear();
96  std::vector<std::string> map_names;
97  m_iMap2D->get_map_names(map_names);
98  for (auto& it : map_names)
99  {
100  out.addString(it);
101  }
102  }
103  else if (cmd == VOCAB_IMAP_REMOVE)
104  {
105  std::string name = in.get(2).asString();
106  if (m_iMap2D->remove_map(name))
107  {
108  out.clear();
110  }
111  else
112  {
113  yCError(MAP2D_NWS_YARP) << "Map not found";
114  out.clear();
116  }
117  }
118  else if (cmd == VOCAB_IMAP_CLEAR_ALL_MAPS)
119  {
120  m_iMap2D->clearAllMaps();
121  out.clear();
123  }
124  else if (cmd == VOCAB_IMAP_SAVE_X)
125  {
127  {
128  std::string mapfile = in.get(3).asString();
129  if (m_iMap2D->saveMapsCollection(mapfile))
130  {
131  out.clear();
133  }
134  else
135  {
136  yCError(MAP2D_NWS_YARP, "Unable to save collection");
137  out.clear();
139  }
140  }
141  else if (in.get(2).asVocab32() == VOCAB_IMAP_LOCATIONS_COLLECTION)
142  {
143  std::string locfile = in.get(3).asString();
144  if (m_iMap2D->saveLocationsAndExtras(locfile))
145  {
146  out.clear();
148  }
149  else
150  {
151  yCError(MAP2D_NWS_YARP, "Unable to save collection");
152  out.clear();
154  }
155  }
156  else
157  {
158  yCError(MAP2D_NWS_YARP, "Parser error");
159  out.clear();
161  }
162  }
163  else if (cmd == VOCAB_IMAP_LOAD_X)
164  {
166  {
167  std::string mapfile = in.get(3).asString();
168  if (m_iMap2D->loadMapsCollection(mapfile))
169  {
170  out.clear();
172  }
173  else
174  {
175  yCError(MAP2D_NWS_YARP, "Unable to load collection");
176  out.clear();
178  }
179  }
181  {
182  std::string locfile = in.get(3).asString();
183  if (m_iMap2D->loadLocationsAndExtras(locfile))
184  {
185  out.clear();
187  }
188  else
189  {
190  yCError(MAP2D_NWS_YARP, "Unable to load collection");
191  out.clear();
193  }
194  }
195  else
196  {
197  yCError(MAP2D_NWS_YARP, "Parser error");
198  out.clear();
200  }
201  }
202  else
203  {
204  yCError(MAP2D_NWS_YARP, "Invalid vocab received in Map2D_nws_yarp");
205  out.clear();
207  }
208  }
209  else if (code == VOCAB_INAVIGATION)
210  {
211  int cmd = in.get(1).asVocab32();
212  if (cmd == VOCAB_NAV_GET_LIST_X && in.get(2).asVocab32() == VOCAB_NAV_LOCATION)
213  {
214  std::string info;
215 
216  out.addVocab32(VOCAB_OK);
217  Bottle& l = out.addList();
218 
219  std::vector<std::string> loc_names;
220  m_iMap2D->getLocationsList(loc_names);
221  for (auto& it : loc_names)
222  {
223  l.addString(it);
224  }
225  yCInfo(MAP2D_NWS_YARP) << "The following locations are currently stored in the server:" << l.toString();
226  }
227  else if (cmd == VOCAB_NAV_GET_LIST_X && in.get(2).asVocab32() == VOCAB_NAV_AREA)
228  {
229  std::string info;
230 
231  out.addVocab32(VOCAB_OK);
232  Bottle& l = out.addList();
233 
234  std::vector<std::string> area_names;
235  m_iMap2D->getAreasList(area_names);
236  for (auto& it : area_names)
237  {
238  l.addString(it);
239  }
240  yCInfo(MAP2D_NWS_YARP) << "The following areas are currently stored in the server:" << l.toString();
241  }
242  else if (cmd == VOCAB_NAV_GET_LIST_X && in.get(2).asVocab32() == VOCAB_NAV_PATH)
243  {
244  std::string info;
245 
246  out.addVocab32(VOCAB_OK);
247  Bottle& l = out.addList();
248 
249  std::vector<std::string> path_names;
250  m_iMap2D->getPathsList(path_names);
251  for (auto& it : path_names)
252  {
253  l.addString(it);
254  }
255  yCInfo(MAP2D_NWS_YARP) << "The following paths are currently stored in the server: " << l.toString();
256  }
257  else if (cmd == VOCAB_NAV_CLEARALL_X && in.get(2).asVocab32() == VOCAB_NAV_LOCATION)
258  {
259  m_iMap2D->clearAllLocations();
260 
261  yCInfo(MAP2D_NWS_YARP) << "All locations deleted";
262  out.addVocab32(VOCAB_OK);
263  }
264  else if (cmd == VOCAB_NAV_CLEARALL_X && in.get(2).asVocab32() == VOCAB_NAV_AREA)
265  {
266  m_iMap2D->clearAllAreas();
267 
268  yCInfo(MAP2D_NWS_YARP) << "All areas deleted";
269  out.addVocab32(VOCAB_OK);
270  }
271  else if (cmd == VOCAB_NAV_CLEARALL_X && in.get(2).asVocab32() == VOCAB_NAV_PATH)
272  {
273  m_iMap2D->clearAllPaths();
274  yCInfo(MAP2D_NWS_YARP) << "All paths deleted";
275  out.addVocab32(VOCAB_OK);
276  }
277  else if (cmd == VOCAB_NAV_CLEARALL_X && in.get(2).asVocab32() == VOCAB_NAV_TEMPORARY_FLAGS)
278  {
279  m_iMap2D->clearAllMapsTemporaryFlags();
280  yCInfo(MAP2D_NWS_YARP) << "Temporary flags deleted from all maps";
281  out.addVocab32(VOCAB_OK);
282  }
283  else if (cmd == VOCAB_NAV_DELETE_X && in.get(2).asVocab32() == VOCAB_NAV_TEMPORARY_FLAGS)
284  {
285  std::string map_name = in.get(3).asString();
286  if (m_iMap2D->clearMapTemporaryFlags(map_name))
287  {
288  yCInfo(MAP2D_NWS_YARP) << "Temporary flags cleaned" << map_name;
289  out.addVocab32(VOCAB_OK);
290  }
291  else
292  {
293  yCError(MAP2D_NWS_YARP, "User requested an invalid map name");
294  out.addVocab32(VOCAB_ERR);
295  }
296  }
297  else if (cmd == VOCAB_NAV_DELETE_X && in.get(2).asVocab32() == VOCAB_NAV_LOCATION)
298  {
299  std::string location_name = in.get(3).asString();
300  if (m_iMap2D->deletePath(location_name))
301  {
302  out.addVocab32(VOCAB_OK);
303  yCInfo(MAP2D_NWS_YARP) << "Deleted location" << location_name;
304  }
305  else
306  {
307  yCError(MAP2D_NWS_YARP, "User requested an invalid location name");
308  out.addVocab32(VOCAB_ERR);
309  }
310  }
311  else if (cmd == VOCAB_NAV_DELETE_X && in.get(2).asVocab32() == VOCAB_NAV_AREA)
312  {
313  std::string area_name = in.get(3).asString();
314  if (m_iMap2D->deleteArea(area_name))
315  {
316  yCInfo(MAP2D_NWS_YARP) << "Deleted area" << area_name;
317  out.addVocab32(VOCAB_OK);
318  }
319  else
320  {
321  yCError(MAP2D_NWS_YARP, "User requested an invalid area name");
322  out.addVocab32(VOCAB_ERR);
323  }
324  }
325  else if (cmd == VOCAB_NAV_DELETE_X && in.get(2).asVocab32() == VOCAB_NAV_PATH)
326  {
327  std::string path_name = in.get(3).asString();
328  if (m_iMap2D->deletePath(path_name))
329  {
330  yCInfo(MAP2D_NWS_YARP) << "Deleted path" << path_name;
331  out.addVocab32(VOCAB_OK);
332  }
333  else
334  {
335  yCError(MAP2D_NWS_YARP, "User requested an invalid location name");
336  out.addVocab32(VOCAB_ERR);
337  }
338  }
339  else if (cmd == VOCAB_NAV_RENAME_X && in.get(2).asVocab32() == VOCAB_NAV_LOCATION)
340  {
341  std::string orig_name = in.get(3).asString();
342  std::string new_name = in.get(4).asString();
343 
344  if (m_iMap2D->renameLocation(orig_name, new_name))
345  {
346  yCInfo(MAP2D_NWS_YARP) << "Location:" << orig_name << "renamed to:" << new_name;
347  out.addVocab32(VOCAB_OK);
348  }
349  else
350  {
351  yCError(MAP2D_NWS_YARP, "User requested an invalid rename operation");
352  out.addVocab32(VOCAB_ERR);
353  }
354  }
355  else if (cmd == VOCAB_NAV_RENAME_X && in.get(2).asVocab32() == VOCAB_NAV_AREA)
356  {
357  std::string orig_name = in.get(3).asString();
358  std::string new_name = in.get(4).asString();
359 
360  if (m_iMap2D->renameArea(orig_name, new_name))
361  {
362  yCInfo(MAP2D_NWS_YARP) << "Area:" << orig_name << "renamed to:" << new_name;
363  out.addVocab32(VOCAB_OK);
364  }
365  else
366  {
367  yCError(MAP2D_NWS_YARP, "User requested an invalid rename operation");
368  out.addVocab32(VOCAB_ERR);
369  }
370  }
371  else if (cmd == VOCAB_NAV_RENAME_X && in.get(2).asVocab32() == VOCAB_NAV_PATH)
372  {
373  std::string orig_name = in.get(3).asString();
374  std::string new_name = in.get(4).asString();
375 
376  if (m_iMap2D->renamePath(orig_name, new_name))
377  {
378  yCInfo(MAP2D_NWS_YARP) << "Path:" << orig_name << "renamed to:" << new_name;
379  out.addVocab32(VOCAB_OK);
380  }
381  else
382  {
383  yCError(MAP2D_NWS_YARP, "User requested an invalid rename operation");
384  out.addVocab32(VOCAB_ERR);
385  }
386  }
387  else if (cmd == VOCAB_NAV_GET_X && in.get(2).asVocab32() == VOCAB_NAV_LOCATION)
388  {
389  std::string loc_name = in.get(3).asString();
390  Map2DLocation loc;
391  if (m_iMap2D->getLocation(loc_name, loc))
392  {
393  out.addVocab32(VOCAB_OK);
394  yCInfo(MAP2D_NWS_YARP) << "Retrieved location" << loc_name << "at" << loc.toString();
395  out.addString(loc.map_id);
396  out.addFloat64(loc.x);
397  out.addFloat64(loc.y);
398  out.addFloat64(loc.theta);
399  }
400  else
401  {
402  out.addVocab32(VOCAB_ERR);
403  yCError(MAP2D_NWS_YARP, "User requested an invalid location name");
404  }
405  }
406  else if (cmd == VOCAB_NAV_GET_X && in.get(2).asVocab32() == VOCAB_NAV_AREA)
407  {
408  std::string area_name = in.get(3).asString();
409  Map2DArea area;
410  if (m_iMap2D->getArea(area_name, area))
411  {
412  yarp::os::Bottle areabot;
413  Map2DArea areatemp = area;
414  if (Property::copyPortable(areatemp, areabot) == false)
415  {
416  yCError(MAP2D_NWS_YARP) << "VOCAB_NAV_GET_X VOCAB_NAV_AREA failed copyPortable()";
417  out.addVocab32(VOCAB_ERR);
418  }
419  else
420  {
421  yCInfo(MAP2D_NWS_YARP) << "Retrieved area" << area_name << "at" << area.toString();
422  out.addVocab32(VOCAB_OK);
423 
424  yarp::os::Bottle& areabot = out.addList();
425  Property::copyPortable(areatemp, areabot);
426  }
427  }
428  else
429  {
430  out.addVocab32(VOCAB_ERR);
431  yCError(MAP2D_NWS_YARP, "User requested an invalid area name");
432  }
433  }
434  else if (cmd == VOCAB_NAV_GET_X && in.get(2).asVocab32() == VOCAB_NAV_PATH)
435  {
436  std::string path_name = in.get(3).asString();
437  Map2DPath path;
438  if (m_iMap2D->getPath(path_name, path))
439  {
440  yarp::os::Bottle pathbot;
441  if (Property::copyPortable(path, pathbot) == false)
442  {
443  yCError(MAP2D_NWS_YARP) << "VOCAB_NAV_GET_X VOCAB_NAV_PATH failed copyPortable()";
444  out.addVocab32(VOCAB_ERR);
445  }
446  else
447  {
448  yCInfo(MAP2D_NWS_YARP) << "Retrieved path" << path_name << "at" << path.toString();
449  out.addVocab32(VOCAB_OK);
450 
451  yarp::os::Bottle& pathbot = out.addList();
452  Property::copyPortable(path, pathbot);
453  }
454  }
455  else
456  {
457  out.addVocab32(VOCAB_ERR);
458  yCError(MAP2D_NWS_YARP, "User requested an invalid path name");
459  }
460  }
461  else if (cmd == VOCAB_NAV_STORE_X && in.get(2).asVocab32() == VOCAB_NAV_LOCATION)
462  {
463  Map2DLocation location;
464  std::string name = in.get(3).asString();
465 
466  location.map_id = in.get(4).asString();
467  location.x = in.get(5).asFloat64();
468  location.y = in.get(6).asFloat64();
469  location.theta = in.get(7).asFloat64();
470 
471  m_iMap2D->storeLocation(name, location);
472  yCInfo(MAP2D_NWS_YARP) << "Added location" << name << "at" << location.toString();
473  out.addVocab32(VOCAB_OK);
474  }
475  else if (cmd == VOCAB_NAV_STORE_X && in.get(2).asVocab32() == VOCAB_NAV_AREA)
476  {
477  Map2DArea area;
478  std::string area_name = in.get(3).asString();
479 
480  Value& b = in.get(4);
481  if (Property::copyPortable(b, area))
482  {
483  m_iMap2D->storeArea(area_name, area);
484  yCInfo(MAP2D_NWS_YARP) << "Added area" << area_name << "at" << area.toString();
485  out.addVocab32(VOCAB_OK);
486  }
487  else
488  {
489  yCError(MAP2D_NWS_YARP) << "VOCAB_NAV_STORE_X VOCAB_NAV_AREA failed copyPortable()";
490  out.addVocab32(VOCAB_ERR);
491  }
492  }
493  else if (cmd == VOCAB_NAV_STORE_X && in.get(2).asVocab32() == VOCAB_NAV_PATH)
494  {
495  Map2DPath path;
496  std::string path_name = in.get(3).asString();
497 
498  Value& b = in.get(4);
499  if (Property::copyPortable(b, path))
500  {
501  m_iMap2D->storePath(path_name, path);
502  yCInfo(MAP2D_NWS_YARP) << "Added path" << path_name << "at" << path.toString();
503  out.addVocab32(VOCAB_OK);
504  }
505  else
506  {
507  yCError(MAP2D_NWS_YARP) << "VOCAB_NAV_STORE_X VOCAB_NAV_PATH failed copyPortable()";
508  out.addVocab32(VOCAB_ERR);
509  }
510  }
511  else
512  {
513  yCError(MAP2D_NWS_YARP) << "Invalid vocab received:" << in.toString();
514  out.clear();
515  out.addVocab32(VOCAB_ERR);
516  }
517  }
518  else
519  {
520  yCError(MAP2D_NWS_YARP) << "Invalid vocab received:" << in.toString();
521  out.clear();
523  }
524 }
525 
526 void Map2D_nws_yarp::parse_string_command(yarp::os::Bottle& in, yarp::os::Bottle& out)
527 {
528  if (in.get(0).asString() == "save_locations&areas" && in.get(1).isString())
529  {
530  if(m_iMap2D->saveLocationsAndExtras(in.get(1).asString()))
531  {
532  out.addString(in.get(1).asString() + " successfully saved");
533  }
534  else
535  {
536  out.addString("save_locations&areas failed");
537  }
538  }
539  else if (in.get(0).asString() == "load_locations&areas" && in.get(1).isString())
540  {
541  if(m_iMap2D->loadLocationsAndExtras(in.get(1).asString()))
542  {
543  out.addString(in.get(1).asString() + " successfully loaded");
544  }
545  else
546  {
547  out.addString("load_locations&areas failed");
548  }
549  }
550  else if(in.get(0).asString() == "list_locations")
551  {
552  std::vector<std::string> vec;
553  m_iMap2D->getLocationsList(vec);
554  for (auto it = vec.begin(); it != vec.end(); ++it)
555  {
556  out.addString(*it);
557  }
558  }
559  else if (in.get(0).asString() == "list_areas")
560  {
561  std::vector<std::string> vec;
562  m_iMap2D->getAreasList(vec);
563  for (auto it = vec.begin(); it != vec.end(); ++it)
564  {
565  out.addString(*it);
566  }
567  }
568  else if (in.get(0).asString() == "list_paths")
569  {
570  std::vector<std::string> vec;
571  m_iMap2D->getPathsList(vec);
572  for (auto it = vec.begin(); it != vec.end(); ++it)
573  {
574  out.addString(*it);
575  }
576  }
577  else if (in.get(0).asString() == "save_maps" && in.get(1).isString())
578  {
579  if(m_iMap2D->saveMapsCollection(in.get(1).asString()))
580  {
581  out.addString(in.get(1).asString() + " successfully saved");
582  }
583  else
584  {
585  out.addString("save_maps failed");
586  }
587  }
588  else if (in.get(0).asString() == "load_maps" && in.get(1).isString())
589  {
590  if(m_iMap2D->loadMapsCollection(in.get(1).asString()))
591  {
592  out.addString(in.get(1).asString() + " successfully loaded");
593  }
594  else
595  {
596  out.addString("load_maps failed");
597  }
598  }
599  else if (in.get(0).asString() == "save_map" && in.get(1).isString() && in.get(2).isString())
600  {
601  std::string map_name = in.get(1).asString();
602  std::string map_file = in.get(2).asString() + ".map";
603 
604  MapGrid2D map;
605  if (m_iMap2D->get_map(map_name, map)==false)
606  {
607  out.addString("save_map failed: map " + map_name + " not found");
608  }
609  else
610  {
611  bool b = map.saveToFile(map_file);
612  if (b)
613  {
614  out.addString(map_file + " successfully saved");
615  }
616  else
617  {
618  out.addString("save_map failed: unable to save " + map_name + " to "+ map_file);
619  }
620  }
621  }
622  else if (in.get(0).asString() == "load_map" && in.get(1).isString())
623  {
624  MapGrid2D map;
625  bool r = map.loadFromFile(in.get(1).asString());
626  if(r)
627  {
628  if (m_iMap2D->store_map(map))
629  {
630  out.addString(in.get(1).asString() + " successfully loaded.");
631  }
632  else
633  {
634  out.addString(in.get(1).asString() + " already exists, skipping.");
635  }
636  }
637  else
638  {
639  out.addString("load_map failed. Unable to load " + in.get(1).asString());
640  }
641  }
642  else if(in.get(0).asString() == "list_maps")
643  {
644  std::vector<std::string> vec;
645  m_iMap2D->get_map_names(vec);
646  for (auto it = vec.begin(); it != vec.end(); ++it)
647  {
648  out.addString(*it);
649  }
650  }
651  else if(in.get(0).asString() == "clear_all_locations")
652  {
653  m_iMap2D->clearAllLocations();
654  out.addString("all locations cleared");
655  }
656  else if (in.get(0).asString() == "clear_all_areas")
657  {
658  m_iMap2D->clearAllAreas();
659  out.addString("all areas cleared");
660  }
661  else if (in.get(0).asString() == "clear_all_paths")
662  {
663  m_iMap2D->clearAllPaths();
664  out.addString("all paths cleared");
665  }
666  else if(in.get(0).asString() == "clear_all_maps")
667  {
668  m_iMap2D->clearAllMaps();
669  out.addString("all maps cleared");
670  }
671  else if (in.get(0).asString() == "send_maps_compressed")
672  {
673  m_send_maps_compressed = in.get(1).asBool();
674  out.addString("compression mode of all maps set to:"+ in.get(1).asString());
675  }
676  else if(in.get(0).asString() == "help")
677  {
678  out.addVocab32("many");
679  out.addString("'save_locations&areas <full path filename>' to save locations and areas on a file");
680  out.addString("'load_locations&areas <full path filename>' to load locations and areas from a file");
681  out.addString("'list_locations' to view a list of all stored locations");
682  out.addString("'list_areas' to view a list of all stored areas");
683  out.addString("'list_paths' to view a list of all stored paths");
684  out.addString("'clear_all_locations' to clear all stored locations");
685  out.addString("'clear_all_areas' to clear all stored areas");
686  out.addString("'clear_all_paths' to clear all stored paths");
687  out.addString("'save_maps <full path>' to save a map collection to a folder");
688  out.addString("'load_maps <full path>' to load a map collection from a folder");
689  out.addString("'save_map <map_name> <full path>' to save a single map");
690  out.addString("'load_map <full path>' to load a single map");
691  out.addString("'list_maps' to view a list of all stored maps");
692  out.addString("'clear_all_maps' to clear all stored maps");
693  out.addString("'send_maps_compressed <0/1>' to set the map transmission mode");
694  }
695  else
696  {
697  out.addString("request not understood, call 'help' to see a list of available commands");
698  }
699 }
700 
701 
702 bool Map2D_nws_yarp::read(yarp::os::ConnectionReader& connection)
703 {
704  std::lock_guard<std::mutex> lock(m_mutex);
705  yarp::os::Bottle in;
706  yarp::os::Bottle out;
707  bool ok = in.read(connection);
708  if (!ok) {
709  return false;
710  }
711 
712  //parse string command
713  if(in.get(0).isString())
714  {
715  parse_string_command(in, out);
716  }
717  // parse vocab command
718  else if(in.get(0).isVocab32())
719  {
720  parse_vocab_command(in, out);
721  }
722 
723  yarp::os::ConnectionWriter *returnToSender = connection.getWriter();
724  if (returnToSender != nullptr)
725  {
726  out.write(*returnToSender);
727  }
728  else
729  {
730  yCError(MAP2D_NWS_YARP) << "Invalid return to sender";
731  }
732  return true;
733 }
734 
736 {
737  Property params;
738  params.fromString(config.toString());
739 
740  if (!config.check("name"))
741  {
742  m_rpcPortName = "/map2D_nws_yarp/rpc";
743  }
744  else
745  {
746  m_rpcPortName = config.find("name").asString();
747  }
748 
749  //open rpc port
750  if (!m_rpcPort.open(m_rpcPortName))
751  {
752  yCError(MAP2D_NWS_YARP, "Failed to open port %s", m_rpcPortName.c_str());
753  return false;
754  }
755  m_rpcPort.setReader(*this);
756 
757  //subdevice handling
758  if (config.check("subdevice"))
759  {
760  Property p;
761  p.fromString(config.toString(), false);
762  p.put("device", config.find("subdevice").asString());
763 
764  if (!m_drv.open(p) || !m_drv.isValid())
765  {
766  yCError(MAP2D_NWS_YARP) << "Failed to open subdevice.. check params";
767  return false;
768  }
769 
770  if (!attach(&m_drv))
771  {
772  yCError(MAP2D_NWS_YARP) << "Failed to open subdevice.. check params";
773  return false;
774  }
775  }
776  else
777  {
778  yCInfo(MAP2D_NWS_YARP) << "Waiting for device to attach";
779  }
780 
781  return true;
782 }
783 
785 {
786  yCTrace(MAP2D_NWS_YARP, "Close");
787  return true;
788 }
789 
791 {
792  m_iMap2D = nullptr;
793  return true;
794 }
795 
797 {
798  if (driver->isValid())
799  {
800  driver->view(m_iMap2D);
801  }
802 
803  if (nullptr == m_iMap2D)
804  {
805  yCError(MAP2D_NWS_YARP, "Subdevice passed to attach method is invalid");
806  return false;
807  }
808 
809  return true;
810 }
constexpr yarp::conf::vocab32_t VOCAB_OK
Definition: GenericVocabs.h:15
constexpr yarp::conf::vocab32_t VOCAB_ERR
Definition: GenericVocabs.h:17
constexpr yarp::conf::vocab32_t VOCAB_NAV_DELETE_X
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_STORE_X
constexpr yarp::conf::vocab32_t VOCAB_NAV_GET_X
constexpr yarp::conf::vocab32_t VOCAB_NAV_RENAME_X
constexpr yarp::conf::vocab32_t VOCAB_NAV_LOCATION
constexpr yarp::conf::vocab32_t VOCAB_NAV_TEMPORARY_FLAGS
constexpr yarp::conf::vocab32_t VOCAB_NAV_CLEARALL_X
constexpr yarp::conf::vocab32_t VOCAB_NAV_PATH
constexpr yarp::conf::vocab32_t VOCAB_IMAP_LOAD_X
Definition: IMap2D.h:248
constexpr yarp::conf::vocab32_t VOCAB_IMAP_OK
Definition: IMap2D.h:252
constexpr yarp::conf::vocab32_t VOCAB_IMAP_SAVE_X
Definition: IMap2D.h:249
constexpr yarp::conf::vocab32_t VOCAB_IMAP
Definition: IMap2D.h:242
constexpr yarp::conf::vocab32_t VOCAB_IMAP_GET_MAP
Definition: IMap2D.h:244
constexpr yarp::conf::vocab32_t VOCAB_IMAP_SET_MAP
Definition: IMap2D.h:243
constexpr yarp::conf::vocab32_t VOCAB_IMAP_LOCATIONS_COLLECTION
Definition: IMap2D.h:251
constexpr yarp::conf::vocab32_t VOCAB_IMAP_MAPS_COLLECTION
Definition: IMap2D.h:250
constexpr yarp::conf::vocab32_t VOCAB_IMAP_CLEAR_ALL_MAPS
Definition: IMap2D.h:246
constexpr yarp::conf::vocab32_t VOCAB_IMAP_ERROR
Definition: IMap2D.h:253
constexpr yarp::conf::vocab32_t VOCAB_IMAP_REMOVE
Definition: IMap2D.h:247
constexpr yarp::conf::vocab32_t VOCAB_IMAP_GET_NAMES
Definition: IMap2D.h:245
Map2D_nws_yarp()
Map2D_nws_yarp.
bool detach() override
Detach the object (you must have first called attach).
bool attach(yarp::dev::PolyDriver *driver) override
Attach to another object.
bool open(yarp::os::Searchable &params) override
Open the DeviceDriver.
bool close() override
Close the DeviceDriver.
bool view(T *&x)
Get an interface to the device driver.
Definition: DeviceDriver.h:74
std::string toString() const
Returns text representation of the area.
Definition: Map2DArea.cpp:133
std::string toString() const
Returns text representation of the path.
Definition: Map2DPath.cpp:42
bool enable_map_compression_over_network(bool val)
Definition: MapGrid2D.cpp:1229
bool loadFromFile(std::string map_filename)
Loads a yarp map file from disk.
Definition: MapGrid2D.cpp:556
bool saveToFile(std::string map_filename) const
Store a yarp map file to disk.
Definition: MapGrid2D.cpp:811
A container for a device driver.
Definition: PolyDriver.h:24
bool isValid() const
Check if device is valid.
Definition: PolyDriver.cpp:196
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
void addVocab32(yarp::conf::vocab32_t x)
Places a vocabulary item in the bottle, at the end of the list.
Definition: Bottle.cpp:164
Bottle & addList()
Places an empty nested list in the bottle, at the end of the list.
Definition: Bottle.cpp:182
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Definition: Bottle.cpp:240
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:158
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:246
void clear()
Empties the bottle of any objects it contains.
Definition: Bottle.cpp:121
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
Definition: Bottle.cpp:230
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition: Bottle.cpp:170
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:211
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 class for storing options and configuration information.
Definition: Property.h:34
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:66
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.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
A single value (typically within a Bottle).
Definition: Value.h:45
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
Definition: Value.cpp:222
virtual bool isString() const
Checks if value is a string.
Definition: Value.cpp:156
virtual yarp::conf::vocab32_t asVocab32() const
Get vocabulary identifier as an integer.
Definition: Value.cpp:228
virtual bool asBool() const
Get boolean value.
Definition: Value.cpp:186
virtual bool isVocab32() const
Checks if value is a vocabulary identifier.
Definition: Value.cpp:174
virtual std::string asString() const
Get string value.
Definition: Value.cpp:234
#define yCInfo(component,...)
Definition: LogComponent.h:132
#define yCError(component,...)
Definition: LogComponent.h:154
#define yCTrace(component,...)
Definition: LogComponent.h:85
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:77
An interface for the device drivers.
An interface to the operating system, including Port based communication.
Signal processing.
Definition: Image.h:22
std::string toString() const
Returns text representation of the location.
Definition: Map2DLocation.h:74