yaml_parser.cpp
Go to the documentation of this file.
00001 #include <yocs_msgs/Trajectory.h>
00002 #include <yocs_msgs/Waypoint.h>
00003 #include "yocs_waypoint_provider/yaml_parser.hpp"
00004 
00005 namespace yocs
00006 {
00007   bool loadWaypointsAndTrajectoriesFromYaml(const std::string& filename,
00008                                             yocs_msgs::WaypointList& wps,
00009                                             yocs_msgs::TrajectoryList& trajs)
00010   {
00011     wps.waypoints.clear();
00012     trajs.trajectories.clear();
00013 
00014     // Yaml File Parsing
00015     try
00016     {
00017       YAML::Node doc;
00018 
00019       getYamlNode(filename, doc);
00020       parseWaypoints(doc, wps);
00021       parseTrajectories(doc, wps, trajs);
00022     }
00023     catch(YAML::ParserException& e)
00024     {
00025       ROS_ERROR("Parsing waypoints file failed: %s", e.what());
00026       return false;
00027     }
00028     catch(YAML::RepresentationException& e)
00029     {
00030       ROS_ERROR("Parsing waypoints file failed: %s", e.what());
00031       return false;
00032     }
00033     catch(std::string& e) {
00034       ROS_ERROR("Parsing waypoints file failed: %s",e.c_str());
00035       return false;
00036     }
00037     return true;
00038   }
00039 
00040   void getYamlNode(const std::string& filename, YAML::Node& node)
00041   {
00042     std::ifstream ifs(filename.c_str(), std::ifstream::in);
00043     if (ifs.good() == false)
00044     {
00045       throw std::string("Waypoints file not found");
00046     }
00047 
00048     #ifdef HAVE_NEW_YAMLCPP
00049       node = YAML::Load(ifs);
00050     #else
00051       YAML::Parser parser(ifs);
00052       parser.GetNextDocument(node);
00053     #endif
00054   }
00055 
00056   void parseWaypoints(const YAML::Node& node, yocs_msgs::WaypointList& wps)
00057   {
00058     #ifdef HAVE_NEW_YAMLCPP
00059       const YAML::Node& wp_node_tmp = node["waypoints"];
00060       const YAML::Node* wp_node = wp_node_tmp ? &wp_node_tmp : NULL;
00061     #else
00062       const YAML::Node* wp_node = node.FindValue("waypoints");
00063     #endif
00064 
00065     if(wp_node != NULL)
00066     {
00067       for(unsigned int i = 0; i < wp_node->size(); ++i)
00068       {
00069         // Parse waypoint entries on YAML
00070         yocs_msgs::Waypoint wp;
00071 
00072         (*wp_node)[i]["name"] >> wp.name;
00073         (*wp_node)[i]["frame_id"] >> wp.header.frame_id;
00074         (*wp_node)[i]["pose"]["position"]["x"] >> wp.pose.position.x;
00075         (*wp_node)[i]["pose"]["position"]["y"] >> wp.pose.position.y;
00076         (*wp_node)[i]["pose"]["position"]["z"] >> wp.pose.position.z;
00077         (*wp_node)[i]["pose"]["orientation"]["x"] >> wp.pose.orientation.x;
00078         (*wp_node)[i]["pose"]["orientation"]["y"] >> wp.pose.orientation.y;
00079         (*wp_node)[i]["pose"]["orientation"]["z"] >> wp.pose.orientation.z;
00080         (*wp_node)[i]["pose"]["orientation"]["w"] >> wp.pose.orientation.w;
00081 
00082         wps.waypoints.push_back(wp);
00083       }
00084       ROS_INFO_STREAM("Parsed " << wps.waypoints.size() << " waypoints.");
00085     }
00086     else
00087     {
00088       ROS_WARN_STREAM("Couldn't find any waypoints in the provided yaml file.");
00089     }
00090   }
00091 
00092   void parseTrajectories(const YAML::Node& node,
00093                          const yocs_msgs::WaypointList& wps,
00094                          yocs_msgs::TrajectoryList& trajs)
00095   {
00096     unsigned int i;
00097 
00098     #ifdef HAVE_NEW_YAMLCPP
00099     const YAML::Node& wp_node_tmp = node["trajectories"];
00100     const YAML::Node* wp_node = wp_node_tmp ? &wp_node_tmp : NULL;
00101     #else
00102     const YAML::Node* wp_node = node.FindValue("trajectories");
00103     #endif
00104     if(wp_node != NULL)
00105     {
00106       for(i = 0; i < wp_node->size(); ++i)
00107       {
00108         // Parse trajectory entries on YAML
00109         yocs_msgs::Trajectory traj;
00110 
00111         // check if all specified waypoints are configured
00112         bool all_waypoints_found = true;
00113 
00114         for(unsigned int wp = 0; wp < (*wp_node)[i]["waypoints"].size(); ++wp)
00115         {
00116           bool wp_found = false;
00117           std::string wp_name;
00118           (*wp_node)[i]["waypoints"][wp] >> wp_name;
00119           for(unsigned int known_wp = 0; known_wp < wps.waypoints.size(); ++known_wp)
00120           {
00121             if (wp_name == wps.waypoints[known_wp].name)
00122             {
00123               traj.waypoints.push_back(wps.waypoints[known_wp]);
00124               wp_found = true;
00125               break;
00126             }
00127           }
00128           if (!wp_found)
00129           {
00130             all_waypoints_found = false;
00131             break;
00132           }
00133         }
00134         if (all_waypoints_found)
00135         {
00136           (*wp_node)[i]["name"] >> traj.name;
00137           trajs.trajectories.push_back(traj);
00138         }
00139       }
00140       ROS_INFO_STREAM("Parsed " << trajs.trajectories.size() << " trajectories.");
00141     }
00142     else
00143     {
00144       ROS_WARN_STREAM("Couldn't find any trajectories in the provided yaml file.");
00145     }
00146   }
00147 }


yocs_waypoint_provider
Author(s): Jihoon Lee
autogenerated on Thu Jun 6 2019 21:47:44