Skip to content

Commit

Permalink
Refactor convert_cartesian_deltas_to_joint_deltas to use compute_jaco…
Browse files Browse the repository at this point in the history
…bian_inverse method
  • Loading branch information
francescodonofrio committed Nov 15, 2024
1 parent 64085ec commit 5ac1077
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class KinematicsInterfaceKDL : public kinematics_interface::KinematicsInterface
KDL::JntArray q_;
KDL::Frame frame_;
std::shared_ptr<KDL::Jacobian> jacobian_;
std::shared_ptr<Eigen::Matrix<double, Eigen::Dynamic, 6>> jacobian_inverse_;
std::shared_ptr<KDL::ChainJntToJacSolver> jac_solver_;
std::shared_ptr<rclcpp::node_interfaces::NodeParametersInterface> parameters_interface_;
std::unordered_map<std::string, int> link_name_map_;
Expand Down
22 changes: 11 additions & 11 deletions kinematics_interface_kdl/src/kinematics_interface_kdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ bool KinematicsInterfaceKDL::initialize(
fk_pos_solver_ = std::make_shared<KDL::ChainFkSolverPos_recursive>(chain_);
jac_solver_ = std::make_shared<KDL::ChainJntToJacSolver>(chain_);
jacobian_ = std::make_shared<KDL::Jacobian>(num_joints_);
jacobian_inverse_ = std::make_shared<Eigen::Matrix<double, Eigen::Dynamic, 6>>(num_joints_, 6);

return true;
}
Expand Down Expand Up @@ -145,17 +146,13 @@ bool KinematicsInterfaceKDL::convert_cartesian_deltas_to_joint_deltas(
return false;
}

// get joint array
q_.data = joint_pos;
// calculate Jacobian inverse
if (!calculate_jacobian_inverse(joint_pos, link_name, *jacobian_inverse_))
{
return false;
}

// calculate Jacobian
jac_solver_->JntToJac(q_, *jacobian_, link_name_map_[link_name]);
// TODO(anyone): this dynamic allocation needs to be replaced
Eigen::Matrix<double, 6, Eigen::Dynamic> J = jacobian_->data;
// damped inverse
Eigen::Matrix<double, Eigen::Dynamic, 6> J_inverse =
(J.transpose() * J + alpha * I).inverse() * J.transpose();
delta_theta = J_inverse * delta_x;
delta_theta = *jacobian_inverse_ * delta_x;

return true;
}
Expand Down Expand Up @@ -202,7 +199,10 @@ bool KinematicsInterfaceKDL::calculate_jacobian_inverse(
Eigen::Matrix<double, 6, Eigen::Dynamic> jacobian = jacobian_->data;

// damped inverse
jacobian_inverse = (jacobian.transpose() * jacobian + alpha * I).inverse() * jacobian.transpose();
*jacobian_inverse_ =
(jacobian.transpose() * jacobian + alpha * I).inverse() * jacobian.transpose();

jacobian_inverse = *jacobian_inverse_;

return true;
}
Expand Down

0 comments on commit 5ac1077

Please sign in to comment.