28 #include <console_bridge/console.h>
40 using Eigen::MatrixXd;
41 using Eigen::VectorXd;
51 throw std::runtime_error(
"Cannot create a state solver form empty scene!");
58 : data_(std::move(data))
92 for (
const auto& joint : joint_values)
102 const Eigen::Ref<const Eigen::VectorXd>& joint_values,
105 assert(
static_cast<Eigen::Index
>(joint_names.size()) == joint_values.size());
106 for (
auto i = 0U; i < joint_names.size(); ++i)
117 throw std::runtime_error(
"KDLStateSolver, not supported!");
144 for (
const auto& joint : joint_values)
147 state.joints[joint.first] = joint.second;
157 const Eigen::Ref<const Eigen::VectorXd>& joint_values,
163 for (
auto i = 0U; i < joint_names.size(); ++i)
166 state.joints[joint_names[i]] = joint_values[i];
169 Eigen::Isometry3d parent_frame{ Eigen::Isometry3d::Identity() };
177 throw std::runtime_error(
"KDLStateSolver, not supported!");
189 const std::string& link_name,
192 assert(joint_values.size() ==
data_.
tree.getNrOfJoints());
194 KDL::Jacobian kdl_jacobian;
198 throw std::runtime_error(
"KDLStateSolver: Failed to calculate jacobian.");
202 const std::string& link_name,
206 KDL::Jacobian kdl_jacobian;
210 throw std::runtime_error(
"KDLStateSolver: Failed to calculate jacobian.");
214 const Eigen::Ref<const Eigen::VectorXd>& joint_values,
215 const std::string& link_name,
218 KDL::JntArray kdl_joint_vals =
getKDLJntArray(joint_names, joint_values);
219 KDL::Jacobian kdl_jacobian;
223 throw std::runtime_error(
"KDLStateSolver: Failed to calculate jacobian.");
267 const std::string& to_link_name)
const
286 for (
const auto& seg :
data_.
tree.getSegments())
288 const KDL::Joint& jnt = seg.second.segment.getJoint();
290 if (jnt.getType() == KDL::Joint::None)
293 joint_to_qnr_.insert(std::make_pair(jnt.getName(), seg.second.q_nr));
297 joint_qnr_[j] =
static_cast<int>(seg.second.q_nr);
300 const auto& sj = scene_graph.
getJoint(jnt.getName());
320 const std::string& joint_name,
321 const double& joint_value)
const
326 q(qnr->second) = joint_value;
330 CONSOLE_BRIDGE_logError(
"Tried to set joint name %s which does not exist!", joint_name.c_str());
335 const KDL::JntArray& q_in,
336 const KDL::SegmentMap::const_iterator& it,
337 const Eigen::Isometry3d& parent_frame)
const
339 if (it !=
data_.
tree.getSegments().end())
341 const KDL::TreeElementType& current_element = it->second;
342 KDL::Frame current_frame;
343 if (q_in.data.size() > 0)
344 current_frame = GetTreeElementSegment(current_element).pose(q_in(GetTreeElementQNr(current_element)));
346 current_frame = GetTreeElementSegment(current_element).pose(0);
348 Eigen::Isometry3d local_frame =
convert(current_frame);
349 Eigen::Isometry3d global_frame{ parent_frame * local_frame };
350 state.
link_transforms[current_element.segment.getName()] = global_frame;
351 if (current_element.segment.getName() !=
data_.
tree.getRootSegment()->first)
352 state.
joint_transforms[current_element.segment.getJoint().getName()] = global_frame;
354 for (
const auto& child : current_element.children)
362 const KDL::JntArray& q_in,
363 const KDL::SegmentMap::const_iterator& it,
364 const Eigen::Isometry3d& parent_frame)
const
366 std::lock_guard<std::mutex> guard(
mutex_);
371 const KDL::JntArray& kdl_joints,
372 const std::string& link_name)
const
374 jacobian.resize(
static_cast<unsigned>(kdl_joints.data.size()));
375 if (
jac_solver_->JntToJac(kdl_joints, jacobian, link_name) < 0)
377 CONSOLE_BRIDGE_logError(
"Failed to calculate jacobian");
385 const Eigen::Ref<const Eigen::VectorXd>& joint_values)
const
390 for (
unsigned i = 0; i < joint_names.size(); ++i)
391 kdl_joints.data(
joint_to_qnr_.at(joint_names[i])) = joint_values[i];
401 for (
const auto& joint : joint_values)
402 kdl_joints.data(
joint_to_qnr_.at(joint.first)) = joint.second;