33 #include <console_bridge/console.h>
41 const YAML::Node& config)
const
46 Eigen::MatrixX2d sample_range;
47 Eigen::VectorXd sample_res;
52 if (YAML::Node n = config[
"manipulator_reach"])
53 m_reach = n.as<
double>();
55 throw std::runtime_error(
"ROPInvKinFactory, missing 'manipulator_reach' entry!");
58 std::map<std::string, std::array<double, 3>> sample_res_map;
59 if (YAML::Node sample_res_node = config[
"positioner_sample_resolution"])
61 for (
auto it = sample_res_node.begin(); it != sample_res_node.end(); ++it)
63 const YAML::Node& joint = *it;
64 std::array<double, 3> values{ 0, 0, 0 };
66 std::string joint_name;
67 if (YAML::Node n = joint[
"name"])
68 joint_name = n.as<std::string>();
70 throw std::runtime_error(
"ROPInvKinFactory, 'positioner_sample_resolution' missing 'name' entry!");
72 if (YAML::Node n = joint[
"value"])
73 values[0] = n.as<
double>();
75 throw std::runtime_error(
"ROPInvKinFactory, 'positioner_sample_resolution' missing 'value' entry!");
77 auto jnt = scene_graph.
getJoint(joint_name);
79 throw std::runtime_error(
"ROPInvKinFactory, 'positioner_sample_resolution' failed to find joint in scene "
82 values[1] = jnt->limits->lower;
83 values[2] = jnt->limits->upper;
85 if (YAML::Node min = joint[
"min"])
86 values[1] = min.as<
double>();
88 if (YAML::Node max = joint[
"max"])
89 values[2] = max.as<
double>();
91 if (values[1] < jnt->limits->lower)
92 throw std::runtime_error(
"ROPInvKinFactory, sample range minimum is less than joint minimum!");
94 if (values[2] > jnt->limits->upper)
95 throw std::runtime_error(
"ROPInvKinFactory, sample range maximum is greater than joint maximum!");
97 if (values[1] > values[2])
98 throw std::runtime_error(
"ROPInvKinFactory, sample range is not valid!");
100 sample_res_map[joint_name] = values;
105 throw std::runtime_error(
"ROPInvKinFactory, missing 'positioner_sample_resolution' entry!");
109 if (YAML::Node positioner = config[
"positioner"])
112 if (YAML::Node n = positioner[
"class"])
115 throw std::runtime_error(
"ROPInvKinFactory, 'positioner' missing 'class' entry!");
117 if (YAML::Node n = positioner[
"config"])
121 if (fwd_kin ==
nullptr)
122 throw std::runtime_error(
"ROPInvKinFactory, failed to create positioner forward kinematics!");
124 if (sample_res_map.size() !=
static_cast<std::size_t
>(fwd_kin->numJoints()))
125 throw std::runtime_error(
"ROPInvKinFactory, positioner sample resolution has incorrect number of joints!");
129 throw std::runtime_error(
"ROPInvKinFactory, missing 'positioner' entry!");
133 sample_range.resize(fwd_kin->numJoints(), 2);
134 sample_res.resize(fwd_kin->numJoints());
135 std::vector<std::string> joint_names = fwd_kin->getJointNames();
136 for (Eigen::Index i = 0; i < static_cast<Eigen::Index>(joint_names.size()); ++i)
138 const auto& jn = joint_names[
static_cast<std::size_t
>(i)];
139 auto it = sample_res_map.find(jn);
140 if (it == sample_res_map.end())
141 throw std::runtime_error(
"ROPInvKinFactory, positioner sample resolution missing joint '" + jn +
"'!");
143 sample_res(i) = it->second[0];
144 sample_range(i, 0) = it->second[1];
145 sample_range(i, 1) = it->second[2];
149 if (YAML::Node manipulator = config[
"manipulator"])
152 if (YAML::Node n = manipulator[
"class"])
155 throw std::runtime_error(
"ROPInvKinFactory, 'manipulator' missing 'class' entry!");
157 if (YAML::Node n = manipulator[
"config"])
161 if (inv_kin ==
nullptr)
162 throw std::runtime_error(
"ROPInvKinFactory, failed to create positioner inverse kinematics!");
166 throw std::runtime_error(
"ROPInvKinFactory, missing 'manipulator' entry!");
169 catch (
const std::exception& e)
171 CONSOLE_BRIDGE_logError(
"ROPInvKinFactory: Failed to parse yaml config data! Details: %s", e.what());
175 return std::make_unique<ROPInvKin>(
176 scene_graph, scene_state, std::move(inv_kin), m_reach, std::move(fwd_kin), sample_range, sample_res, solver_name);
179 PLUGIN_ANCHOR_IMPL(ROPInvKinFactoriesAnchor)