SMACC2
Loading...
Searching...
No Matches
cb_move_known_state.cpp
Go to the documentation of this file.
1// Copyright 2021 RobosoftAI Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20
21#include <yaml-cpp/yaml.h>
22#include <ament_index_cpp/get_package_share_directory.hpp>
23#include <filesystem>
24#include <fstream>
26
28{
29CbMoveKnownState::CbMoveKnownState(std::string pkg, std::string config_path)
30: pkg_(pkg), config_path_(config_path)
31{
32}
33
35
37{
40}
41
42#define HAVE_NEW_YAMLCPP
43std::map<std::string, double> CbMoveKnownState::loadJointStatesFromFile(
44 std::string pkg, std::string filepath)
45{
46 //auto pkgpath = ros::package::getPath(pkg);
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(
79 getLogger(),
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
89 YAML::Parser parser(ifs);
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}
133} // namespace cl_move_group_interface
std::map< std::string, double > jointValueTarget_
std::map< std::string, double > loadJointStatesFromFile(std::string pkg, std::string filepath)
CbMoveKnownState(std::string pkg, std::string config_path)
virtual rclcpp::Logger getLogger() const