-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
139 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#ifndef LBR_FRI_ROS2__KINEMATICS_HPP_ | ||
#define LBR_FRI_ROS2__KINEMATICS_HPP_ | ||
|
||
#include <string> | ||
|
||
#include "eigen3/Eigen/Core" | ||
#include "kdl/chain.hpp" | ||
#include "kdl/chainfksolverpos_recursive.hpp" | ||
#include "kdl/chainjnttojacsolver.hpp" | ||
#include "kdl/tree.hpp" | ||
#include "kdl_parser/kdl_parser.hpp" | ||
#include "rclcpp/logger.hpp" | ||
#include "rclcpp/logging.hpp" | ||
|
||
#include "friLBRState.h" | ||
|
||
#include "lbr_fri_idl/msg/lbr_state.hpp" | ||
|
||
namespace lbr_fri_ros2 { | ||
class Kinematics { | ||
protected: | ||
static constexpr char LOGGER_NAME[] = "lbr_fri_ros2::Kinematics"; | ||
|
||
public: | ||
static constexpr uint8_t CARTESIAN_DOF = 6; | ||
using jnt_pos_array_t = lbr_fri_idl::msg::LBRState::_measured_joint_position_type; | ||
using const_jnt_pos_array_t_ref = const jnt_pos_array_t &; | ||
|
||
Kinematics(const std::string &robot_description, const std::string &chain_root = "lbr_link_0", | ||
const std::string &chain_tip = "lbr_link_ee"); | ||
|
||
// internally computes the jacobian and return a reference to it | ||
const KDL::Jacobian &compute_jacobian(const_jnt_pos_array_t_ref q); | ||
const KDL::Jacobian &compute_jacobian(const std::vector<double> &q); | ||
|
||
// forward kinematics | ||
const KDL::Frame &compute_fk(const_jnt_pos_array_t_ref q); | ||
const KDL::Frame &compute_fk(const std::vector<double> &q); | ||
|
||
protected: | ||
KDL::Tree tree_; | ||
KDL::Chain chain_; | ||
|
||
// solvers | ||
std::unique_ptr<KDL::ChainJntToJacSolver> jacobian_solver_; | ||
std::unique_ptr<KDL::ChainFkSolverPos_recursive> fk_solver_; | ||
|
||
// robot state | ||
KDL::JntArray q_; | ||
|
||
// forward kinematics | ||
KDL::Frame chain_tip_frame_; | ||
|
||
// jacobian | ||
KDL::Jacobian jacobian_; | ||
}; | ||
} // namespace lbr_fri_ros2 | ||
#endif // LBR_FRI_ROS2__KINEMATICS_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include "lbr_fri_ros2/kinematics.hpp" | ||
|
||
namespace lbr_fri_ros2 { | ||
Kinematics::Kinematics(const std::string &robot_description, const std::string &chain_root, | ||
const std::string &chain_tip) { | ||
if (!kdl_parser::treeFromString(robot_description, tree_)) { | ||
std::string err = "Failed to construct kdl tree from robot description."; | ||
RCLCPP_ERROR(rclcpp::get_logger(LOGGER_NAME), err.c_str()); | ||
throw std::runtime_error(err); | ||
} | ||
if (!tree_.getChain(chain_root, chain_tip, chain_)) { | ||
std::string err = "Failed to construct kdl chain from robot description."; | ||
RCLCPP_ERROR(rclcpp::get_logger(LOGGER_NAME), err.c_str()); | ||
throw std::runtime_error(err); | ||
} | ||
if (chain_.getNrOfJoints() != KUKA::FRI::LBRState::NUMBER_OF_JOINTS) { | ||
std::string err = "Invalid number of joints in chain."; | ||
RCLCPP_ERROR(rclcpp::get_logger(LOGGER_NAME), err.c_str()); | ||
throw std::runtime_error(err); | ||
} | ||
jacobian_solver_ = std::make_unique<KDL::ChainJntToJacSolver>(chain_); | ||
fk_solver_ = std::make_unique<KDL::ChainFkSolverPos_recursive>(chain_); | ||
jacobian_.resize(KUKA::FRI::LBRState::NUMBER_OF_JOINTS); | ||
q_.resize(KUKA::FRI::LBRState::NUMBER_OF_JOINTS); | ||
q_.data.setZero(); | ||
} | ||
|
||
const KDL::Jacobian &Kinematics::compute_jacobian(const_jnt_pos_array_t_ref q) { | ||
q_.data = | ||
Eigen::Map<const Eigen::Matrix<double, KUKA::FRI::LBRState::NUMBER_OF_JOINTS, 1>>(q.data()); | ||
jacobian_solver_->JntToJac(q_, jacobian_); | ||
return jacobian_; | ||
} | ||
|
||
const KDL::Jacobian &Kinematics::compute_jacobian(const std::vector<double> &q) { | ||
if (q.size() != KUKA::FRI::LBRState::NUMBER_OF_JOINTS) { | ||
std::string err = "Invalid number of joint positions."; | ||
RCLCPP_ERROR(rclcpp::get_logger(LOGGER_NAME), err.c_str()); | ||
throw std::runtime_error(err); | ||
} | ||
q_.data = | ||
Eigen::Map<const Eigen::Matrix<double, KUKA::FRI::LBRState::NUMBER_OF_JOINTS, 1>>(q.data()); | ||
jacobian_solver_->JntToJac(q_, jacobian_); | ||
return jacobian_; | ||
} | ||
|
||
const KDL::Frame &Kinematics::compute_fk(const_jnt_pos_array_t_ref q) { | ||
q_.data = | ||
Eigen::Map<const Eigen::Matrix<double, KUKA::FRI::LBRState::NUMBER_OF_JOINTS, 1>>(q.data()); | ||
fk_solver_->JntToCart(q_, chain_tip_frame_); | ||
return chain_tip_frame_; | ||
} | ||
|
||
const KDL::Frame &Kinematics::compute_fk(const std::vector<double> &q) { | ||
if (q.size() != KUKA::FRI::LBRState::NUMBER_OF_JOINTS) { | ||
std::string err = "Invalid number of joint positions."; | ||
RCLCPP_ERROR(rclcpp::get_logger(LOGGER_NAME), err.c_str()); | ||
throw std::runtime_error(err); | ||
} | ||
q_.data = | ||
Eigen::Map<const Eigen::Matrix<double, KUKA::FRI::LBRState::NUMBER_OF_JOINTS, 1>>(q.data()); | ||
fk_solver_->JntToCart(q_, chain_tip_frame_); | ||
return chain_tip_frame_; | ||
} | ||
} // namespace lbr_fri_ros2 |