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