yaml_parser.cpp
Go to the documentation of this file.
1 #include <yocs_msgs/Trajectory.h>
2 #include <yocs_msgs/Waypoint.h>
4 
5 namespace yocs
6 {
7  bool loadWaypointsAndTrajectoriesFromYaml(const std::string& filename,
8  yocs_msgs::WaypointList& wps,
9  yocs_msgs::TrajectoryList& trajs)
10  {
11  wps.waypoints.clear();
12  trajs.trajectories.clear();
13 
14  // Yaml File Parsing
15  try
16  {
17  YAML::Node doc;
18 
19  getYamlNode(filename, doc);
20  parseWaypoints(doc, wps);
21  parseTrajectories(doc, wps, trajs);
22  }
23  catch(YAML::ParserException& e)
24  {
25  ROS_ERROR("Parsing waypoints file failed: %s", e.what());
26  return false;
27  }
28  catch(YAML::RepresentationException& e)
29  {
30  ROS_ERROR("Parsing waypoints file failed: %s", e.what());
31  return false;
32  }
33  catch(std::string& e) {
34  ROS_ERROR("Parsing waypoints file failed: %s",e.c_str());
35  return false;
36  }
37  return true;
38  }
39 
40  void getYamlNode(const std::string& filename, YAML::Node& node)
41  {
42  std::ifstream ifs(filename.c_str(), std::ifstream::in);
43  if (ifs.good() == false)
44  {
45  throw std::string("Waypoints file not found");
46  }
47 
48  #ifdef HAVE_NEW_YAMLCPP
49  node = YAML::Load(ifs);
50  #else
51  YAML::Parser parser(ifs);
52  parser.GetNextDocument(node);
53  #endif
54  }
55 
56  void parseWaypoints(const YAML::Node& node, yocs_msgs::WaypointList& wps)
57  {
58  #ifdef HAVE_NEW_YAMLCPP
59  const YAML::Node& wp_node_tmp = node["waypoints"];
60  const YAML::Node* wp_node = wp_node_tmp ? &wp_node_tmp : NULL;
61  #else
62  const YAML::Node* wp_node = node.FindValue("waypoints");
63  #endif
64 
65  if(wp_node != NULL)
66  {
67  for(unsigned int i = 0; i < wp_node->size(); ++i)
68  {
69  // Parse waypoint entries on YAML
70  yocs_msgs::Waypoint wp;
71 
72  (*wp_node)[i]["name"] >> wp.name;
73  (*wp_node)[i]["frame_id"] >> wp.header.frame_id;
74  (*wp_node)[i]["pose"]["position"]["x"] >> wp.pose.position.x;
75  (*wp_node)[i]["pose"]["position"]["y"] >> wp.pose.position.y;
76  (*wp_node)[i]["pose"]["position"]["z"] >> wp.pose.position.z;
77  (*wp_node)[i]["pose"]["orientation"]["x"] >> wp.pose.orientation.x;
78  (*wp_node)[i]["pose"]["orientation"]["y"] >> wp.pose.orientation.y;
79  (*wp_node)[i]["pose"]["orientation"]["z"] >> wp.pose.orientation.z;
80  (*wp_node)[i]["pose"]["orientation"]["w"] >> wp.pose.orientation.w;
81 
82  wps.waypoints.push_back(wp);
83  }
84  ROS_INFO_STREAM("Parsed " << wps.waypoints.size() << " waypoints.");
85  }
86  else
87  {
88  ROS_WARN_STREAM("Couldn't find any waypoints in the provided yaml file.");
89  }
90  }
91 
92  void parseTrajectories(const YAML::Node& node,
93  const yocs_msgs::WaypointList& wps,
94  yocs_msgs::TrajectoryList& trajs)
95  {
96  unsigned int i;
97 
98  #ifdef HAVE_NEW_YAMLCPP
99  const YAML::Node& wp_node_tmp = node["trajectories"];
100  const YAML::Node* wp_node = wp_node_tmp ? &wp_node_tmp : NULL;
101  #else
102  const YAML::Node* wp_node = node.FindValue("trajectories");
103  #endif
104  if(wp_node != NULL)
105  {
106  for(i = 0; i < wp_node->size(); ++i)
107  {
108  // Parse trajectory entries on YAML
109  yocs_msgs::Trajectory traj;
110 
111  // check if all specified waypoints are configured
112  bool all_waypoints_found = true;
113 
114  for(unsigned int wp = 0; wp < (*wp_node)[i]["waypoints"].size(); ++wp)
115  {
116  bool wp_found = false;
117  std::string wp_name;
118  (*wp_node)[i]["waypoints"][wp] >> wp_name;
119  for(unsigned int known_wp = 0; known_wp < wps.waypoints.size(); ++known_wp)
120  {
121  if (wp_name == wps.waypoints[known_wp].name)
122  {
123  traj.waypoints.push_back(wps.waypoints[known_wp]);
124  wp_found = true;
125  break;
126  }
127  }
128  if (!wp_found)
129  {
130  all_waypoints_found = false;
131  break;
132  }
133  }
134  if (all_waypoints_found)
135  {
136  (*wp_node)[i]["name"] >> traj.name;
137  trajs.trajectories.push_back(traj);
138  }
139  }
140  ROS_INFO_STREAM("Parsed " << trajs.trajectories.size() << " trajectories.");
141  }
142  else
143  {
144  ROS_WARN_STREAM("Couldn't find any trajectories in the provided yaml file.");
145  }
146  }
147 }
bool loadWaypointsAndTrajectoriesFromYaml(const std::string &filename, yocs_msgs::WaypointList &wps, yocs_msgs::TrajectoryList &trajs)
Definition: yaml_parser.cpp:7
void getYamlNode(const std::string &filename, YAML::Node &node)
Definition: yaml_parser.cpp:40
#define ROS_WARN_STREAM(args)
#define ROS_INFO_STREAM(args)
#define ROS_ERROR(...)
void parseTrajectories(const YAML::Node &node, const yocs_msgs::WaypointList &wps, yocs_msgs::TrajectoryList &trajs)
Definition: yaml_parser.cpp:92
void parseWaypoints(const YAML::Node &node, yocs_msgs::WaypointList &wps)
Definition: yaml_parser.cpp:56


yocs_waypoint_provider
Author(s): Jihoon Lee
autogenerated on Mon Jun 10 2019 15:54:09