yaml_parser.cpp
Go to the documentation of this file.
00001 
00002 #include "yocs_waypoint_manager/yaml_parser.hpp"
00003 
00004 namespace yocs {
00005   bool loadWaypointListFromYaml(const std::string& filename,yocs_msgs::WaypointList& wps) {
00006 
00007     wps.waypoints.clear();
00008 
00009     // Yaml File Parsing
00010     try
00011     {
00012       YAML::Node doc;
00013 
00014       getYamlNode(filename, doc);
00015       parseWaypoints(doc, wps);
00016     }
00017     catch(YAML::ParserException& e)
00018     {
00019       ROS_ERROR("Parse waypoints file failed: %s", e.what());
00020       return false;
00021     }
00022     catch(YAML::RepresentationException& e)
00023     {
00024       ROS_ERROR("Parse waypoints file failed: %s", e.what());
00025       return false;
00026     }
00027     catch(std::string& e) {
00028       ROS_ERROR("Parse waypoints file failed: %s",e.c_str());
00029       return false;
00030     }
00031     return true;
00032   }
00033 
00034   void getYamlNode(const std::string& filename, YAML::Node& node) 
00035   {
00036     std::ifstream ifs(filename.c_str(), std::ifstream::in);
00037     if (ifs.good() == false)
00038     {                                                            
00039       throw std::string("Waypoints file not found");
00040     }
00041 
00042     YAML::Parser parser(ifs);
00043     #ifdef HAVE_NEW_YAMLCPP
00044       node = YAML::Load(ifs);
00045     #else
00046       parser.GetNextDocument(node);
00047     #endif
00048   }
00049 
00050   void parseWaypoints(const YAML::Node& node, yocs_msgs::WaypointList& wps) 
00051   {
00052     
00053     unsigned int i;
00054 
00055     #ifdef HAVE_NEW_YAMLCPP
00056     const YAML::Node& wp_node_tmp = node["waypoints"];   
00057     const YAML::Node* wp_node = wp_node_tmp ? &wp_node_tmp : NULL;   
00058     #else
00059     const YAML::Node* wp_node = node.FindValue("waypoints");   
00060     #endif
00061     if(wp_node != NULL)
00062     {
00063       for(i = 0; i < wp_node->size(); i++) 
00064       {
00065         // Parse waypoint entries on YAML
00066         yocs_msgs::Waypoint wp;
00067 
00068         (*wp_node)[i]["name"] >> wp.name;
00069         (*wp_node)[i]["frame_id"] >> wp.header.frame_id;
00070         (*wp_node)[i]["pose"]["position"]["x"] >> wp.pose.position.x;
00071         (*wp_node)[i]["pose"]["position"]["y"] >> wp.pose.position.y;
00072         (*wp_node)[i]["pose"]["position"]["z"] >> wp.pose.position.z;
00073         (*wp_node)[i]["pose"]["orientation"]["x"] >> wp.pose.orientation.x;
00074         (*wp_node)[i]["pose"]["orientation"]["y"] >> wp.pose.orientation.y;
00075         (*wp_node)[i]["pose"]["orientation"]["z"] >> wp.pose.orientation.z;
00076         (*wp_node)[i]["pose"]["orientation"]["w"] >> wp.pose.orientation.w;
00077 
00078         wps.waypoints.push_back(wp);
00079       }
00080     }
00081   }
00082 }


yocs_waypoint_manager
Author(s): Jihoon Lee
autogenerated on Sun Nov 23 2014 12:41:12