45{
46
47 std::string pkgpath = ament_index_cpp::get_package_share_directory(pkg);
48
49 std::map<std::string, double> jointStates;
50
51 if (pkgpath == "")
52 {
53 RCLCPP_ERROR_STREAM(
54 getLogger(),
"[" <<
getName() <<
"] package not found for the known poses file: " << pkg
55 << std::endl
56 << " [IGNORING BEHAVIOR]");
57 return jointStates;
58 }
59
60 filepath = pkgpath + "/" + filepath;
61
62 RCLCPP_INFO_STREAM(
63 getLogger(),
"[" <<
getName() <<
"] Opening file with joint known state: " << filepath);
64
65 if (std::filesystem::exists(filepath))
66 {
67 RCLCPP_INFO_STREAM(
getLogger(),
"[" <<
getName() <<
"] known state file exists: " << filepath);
68 }
69 else
70 {
71 RCLCPP_ERROR_STREAM(
72 getLogger(),
"[" <<
getName() <<
"] known state file does not exists: " << filepath);
73 }
74
75 std::ifstream ifs(filepath.c_str(), std::ifstream::in);
76 if (ifs.good() == false)
77 {
78 RCLCPP_ERROR_STREAM(
80 "[" <<
getName() <<
"] Error opening file with joint known states: " << filepath);
81 throw std::string("joint state files not found");
82 }
83
84 try
85 {
86#ifdef HAVE_NEW_YAMLCPP
87 YAML::Node node = YAML::Load(ifs);
88#else
90 parser.GetNextDocument(node);
91#endif
92
93#ifdef HAVE_NEW_YAMLCPP
94 const YAML::Node & wp_node_tmp = node["joint_states"];
95 const YAML::Node * wp_node = wp_node_tmp ? &wp_node_tmp : NULL;
96#else
97 const YAML::Node * wp_node = node.FindValue("waypoints");
98#endif
99
100 if (wp_node != NULL)
101 {
102 try
103 {
104 for (YAML::const_iterator it = wp_node->begin(); it != wp_node->end(); ++it)
105 {
106 std::string key = it->first.as<std::string>();
107 double value = it->second.as<double>();
108 RCLCPP_DEBUG_STREAM(
getLogger(),
" joint - " << key <<
": " << value);
109 jointStates[key] = value;
110 }
111
112 return jointStates;
113 }
114 catch (std::exception & ex)
115 {
116 RCLCPP_ERROR(
getLogger(),
"trying to convert to map, failed, errormsg: %s", ex.what());
117 }
118
119 RCLCPP_INFO_STREAM(
getLogger(),
"Parsed " << jointStates.size() <<
" joint entries.");
120 }
121 else
122 {
123 RCLCPP_WARN_STREAM(
getLogger(),
"Couldn't find any jointStates in the provided yaml file.");
124 }
125 }
126 catch (const YAML::ParserException & ex)
127 {
128 RCLCPP_ERROR_STREAM(
129 getLogger(),
"Error loading the Waypoints YAML file. Incorrect syntax: " << ex.what());
130 }
131 return jointStates;
132}
std::string getName() const
virtual rclcpp::Logger getLogger()