cmd_vel_subscribers.cpp
Go to the documentation of this file.
1 
9 /*****************************************************************************
10  ** Includes
11  *****************************************************************************/
12 
13 #include <fstream>
14 
17 
18 /*****************************************************************************
19 ** Namespaces
20 *****************************************************************************/
21 
22 namespace yocs_cmd_vel_mux {
23 
24 /*****************************************************************************
25  ** Implementation
26  *****************************************************************************/
27 
28 void CmdVelSubscribers::CmdVelSubs::operator << (const YAML::Node& node)
29 {
30  // Fill attributes with a YAML node content
31  double new_timeout;
32  std::string new_topic;
33  node["name"] >> name;
34  node["topic"] >> new_topic;
35  node["timeout"] >> new_timeout;
36  node["priority"] >> priority;
37 #ifdef HAVE_NEW_YAMLCPP
38  if (node["short_desc"]) {
39 #else
40  if (node.FindValue("short_desc") != NULL) {
41 #endif
42  node["short_desc"] >> short_desc;
43  }
44 
45  if (new_topic != topic)
46  {
47  // Shutdown the topic if the name has changed so it gets recreated on configuration reload
48  // In the case of new subscribers, topic is empty and shutdown has just no effect
49  topic = new_topic;
50  subs.shutdown();
51  }
52 
53  if (new_timeout != timeout)
54  {
55  // Change timer period if the timeout changed
56  timeout = new_timeout;
58  }
59 }
60 
61 void CmdVelSubscribers::configure(const YAML::Node& node)
62 {
63  try
64  {
65  if (node.size() == 0)
66  {
67  throw EmptyCfgException("Configuration is empty");
68  }
69 
70  std::vector<std::shared_ptr<CmdVelSubs>> new_list(node.size());
71  for (unsigned int i = 0; i < node.size(); i++)
72  {
73  // Parse entries on YAML
74  std::string new_subs_name = node[i]["name"].Scalar();
75  auto old_subs = std::find_if(list.begin(), list.end(),
76  [&new_subs_name](const std::shared_ptr<CmdVelSubs>& subs)
77  {return subs->name == new_subs_name;});
78  if (old_subs != list.end())
79  {
80  // For names already in the subscribers list, retain current object so we don't re-subscribe to the topic
81  new_list[i] = *old_subs;
82  }
83  else
84  {
85  new_list[i] = std::make_shared<CmdVelSubs>(i);
86  }
87  // update existing or new object with the new configuration
88  *new_list[i] << node[i];
89  }
90 
91  list = new_list;
92  }
93  catch(EmptyCfgException& e) {
94  throw e;
95  }
96  catch(YAML::ParserException& e)
97  {
98  throw YamlException(e.what());
99  }
100  catch(YAML::RepresentationException& e)
101  {
102  throw YamlException(e.what());
103  }
104 }
105 
106 
107 } // namespace yocs_cmd_vel_mux
void configure(const YAML::Node &node)
Configures the subscribers from a yaml file.
std::vector< std::shared_ptr< CmdVelSubs > > list
Structure for the cmd_vel_mux.
void setPeriod(const Duration &period, bool reset=true)


yocs_cmd_vel_mux
Author(s): Jorge Santos Simon
autogenerated on Mon Jun 10 2019 15:53:29