6 : chain_base_link_(chain_base_link), nh_(nh)
10 throw std::runtime_error(
11 "ERROR getting robot description (/robot_description)");
23 ROS_WARN(
"KDLManager: Missing eps parameter, setting default");
29 ROS_WARN(
"KDLManager: Missing max_tf_attempts parameter, setting default");
35 ROS_WARN(
"KDLManager: Missing ikvel_solver parameter, setting default");
42 "KDLManager: Missing ik_angle_tolerance parameter, setting default");
48 ROS_WARN(
"KDLManager: Missing ik_pos_tolerance parameter, setting default");
65 ROS_WARN(
"KDLManager: Missing nso_weight parameter, setting default");
70 std::vector<double> gravity;
71 if (!
nh_.
getParam(
"kdl_manager/gravity_in_base_link", gravity))
74 "KDLManager: Missing kdl_manager/gravity_in_base_link parameter. This " 75 "will affect the dynamic solvers");
80 if (gravity.size() != 3)
83 << gravity.size() <<
". Should have size 3");
115 unsigned int joint_n =
chain_.at(end_effector_link).getNrOfJoints();
120 "Number of joints for kinematic chain is smaller than 6 (%d). The " 121 "NSO ik solver vel has issues with under-actuated chains. Using WDLS",
129 q_vel_lim(joint_n), q_desired(joint_n);
132 for (
unsigned int i = 0; i < joint_n; i++)
135 q_desired(i) = (q_max(i) + q_min(i)) / 2;
138 ikvel_[end_effector_link] =
140 chain_.at(end_effector_link), q_desired, w,
eps_));
149 return (
chain_.find(end_effector_link) !=
chain_.end());
158 <<
", but it was already initialized");
171 <<
"> in the kinematic tree");
174 for (
auto it = tree_map.begin(); it != tree_map.end(); it++)
182 chain_[end_effector_link] = chain;
185 std::vector<std::string> new_vector;
199 new_vector.push_back(kdl_joint.
getName());
205 fkpos_[end_effector_link] =
207 fkvel_[end_effector_link] =
209 ikpos_[end_effector_link] =
222 const std::string &gripping_point_frame)
232 eef_to_gripping_point))
238 eef_to_gripping_point.
Inverse();
243 const std::string &sensor_point_frame)
253 eef_to_sensor_point))
263 const Eigen::VectorXd &qdot,
264 sensor_msgs::JointState &state)
const 271 if (
chain_.at(end_effector_link).getNrOfJoints() != qdot.rows())
274 "Joint chain for eef %s has a different number of joints than the " 276 end_effector_link.c_str());
280 Eigen::VectorXd q(
chain_.at(end_effector_link).getNrOfJoints());
283 for (
unsigned long i = 0; i < state.name.size(); i++)
287 q[joint_index] = state.position[i];
291 if (joint_index ==
chain_.at(end_effector_link).getNrOfJoints())
297 if (joint_index !=
chain_.at(end_effector_link).getNrOfJoints())
300 "Provided joint state does not have all of the required chain joints");
308 const Eigen::VectorXd &q,
309 const Eigen::VectorXd &qdot,
310 sensor_msgs::JointState &state)
const 312 sensor_msgs::JointState ret;
313 if (q.rows() != qdot.rows())
316 "Given joint state with a different number of joint positions and " 326 if (
chain_.at(end_effector_link).getNrOfJoints() != qdot.rows())
329 "Joint chain for eef %s has a different number of joints than the " 331 end_effector_link.c_str());
339 for (
unsigned long i = 0;
343 state.position[i] = q[i];
344 state.velocity[i] = qdot[i];
351 const Eigen::VectorXd &q,
352 const Eigen::VectorXd &qdot,
353 sensor_msgs::JointState &state)
const 355 if (q.rows() != qdot.rows())
358 "Given joint state with a different number of joint positions and " 368 if (
chain_.at(end_effector_link).getNrOfJoints() != qdot.rows())
371 "Joint chain for eef %s has a different number of joints than the " 373 end_effector_link.c_str());
379 for (
unsigned long i = 0;
383 for (
unsigned long j = 0; j < state.name.size(); j++)
387 state.position[j] = q[i];
388 state.velocity[j] = qdot[i];
398 <<
" from given joint state");
407 const Eigen::VectorXd &q,
408 const Eigen::VectorXd &qdot,
409 const Eigen::VectorXd &effort,
410 sensor_msgs::JointState &state)
const 422 if (
chain_.at(end_effector_link).getNrOfJoints() != effort.rows())
426 <<
" has a different number of joints than the provided");
431 for (
unsigned long i = 0;
435 for (
unsigned long j = 0; j < state.name.size(); j++)
439 state.effort[j] = effort[i];
449 <<
" from given joint state");
458 const sensor_msgs::JointState &state,
467 if (!
getEefPose(end_effector_link, state, eef_pose))
477 const sensor_msgs::JointState &state,
486 if (!
getEefPose(end_effector_link, state, eef_pose))
496 const sensor_msgs::JointState &state,
511 if (!
getEefPose(end_effector_link, state, eef_pose))
517 if (!
getEefTwist(end_effector_link, state, eef_twist))
522 Eigen::Vector3d vel_eig, rot_eig, converted_vel, r_eig;
532 out.
vel =
KDL::Vector(converted_vel[0], converted_vel[1], converted_vel[2]);
539 const sensor_msgs::JointState &state,
555 fkpos_.at(end_effector_link)->JntToCart(positions, out);
560 const sensor_msgs::JointState &state,
575 fkvel_.at(end_effector_link)->JntToCart(velocities, out);
588 unsigned int joint_n =
chain_.at(end_effector_link).getNrOfJoints();
589 if (q_min.
rows() != joint_n || q_max.
rows() != joint_n ||
590 q_vel_lim.
rows() != joint_n)
593 "KDLManager::getJointPositionLimits requires initialized joint arrays");
597 urdf::JointConstSharedPtr joint;
598 urdf::JointLimitsSharedPtr limits;
602 for (
unsigned int i = 0; i <
chain_.at(end_effector_link).getNrOfSegments();
605 if (
chain_.at(end_effector_link).getSegment(i).getJoint().getType() ==
606 KDL::Joint::JointType::None)
612 chain_.at(end_effector_link).getSegment(i).getJoint().getName());
613 limits = joint->limits;
614 q_min(j) = limits->lower;
615 q_max(j) = limits->upper;
616 q_vel_lim(j) = limits->velocity;
624 const sensor_msgs::JointState &state,
643 const sensor_msgs::JointState &state,
644 Eigen::VectorXd &q)
const 663 const sensor_msgs::JointState &state,
671 q_dot.
resize(
chain_.at(end_effector_link).getNrOfJoints());
684 const sensor_msgs::JointState &state,
694 KDL::JntSpaceInertiaMatrix B(
chain_.at(end_effector_link).getNrOfJoints());
708 const sensor_msgs::JointState &state,
732 const sensor_msgs::JointState &state,
733 Eigen::MatrixXd &coriolis)
748 dynamic_chain_.at(end_effector_link)->JntToCoriolis(q, q_dot.qdot, cor);
754 const std::string &target_frame,
757 geometry_msgs::PoseStamped base_to_target;
758 base_to_target.header.frame_id = base_frame;
759 base_to_target.header.stamp =
ros::Time(0);
760 base_to_target.pose.position.x = 0;
761 base_to_target.pose.position.y = 0;
762 base_to_target.pose.position.z = 0;
763 base_to_target.pose.orientation.x = 0;
764 base_to_target.pose.orientation.y = 0;
765 base_to_target.pose.orientation.z = 0;
766 base_to_target.pose.orientation.w = 1;
778 ROS_WARN(
"TF exception in kdl manager: %s", ex.what());
783 if (attempts >= max_tf_attempts_)
786 "KDL manager could not find the transform between frames %s and %s",
787 base_frame.c_str(), target_frame.c_str());
803 sensor_msgs::JointState dummy_state;
805 for (
unsigned int i = 0;
809 dummy_state.position.push_back(0);
810 dummy_state.velocity.push_back(0);
811 dummy_state.effort.push_back(0);
815 if (
getPoseIK(end_effector_link, dummy_state, in, dummy_out))
824 const sensor_msgs::JointState &state,
840 out.
resize(
chain_.at(end_effector_link).getNrOfJoints());
841 ikpos_.at(end_effector_link)->CartToJnt(positions, in, out);
846 difference = computedPose.Inverse() * in;
847 Eigen::Vector3d quat_v;
850 difference.
M.
GetQuaternion(quat_v[0], quat_v[1], quat_v[2], quat_w);
851 double angle = 2 *
atan2(quat_v.norm(), quat_w);
856 "KDL manager could not compute pose ik for end-effector %s. Final " 857 "orientation error was %.2f",
858 end_effector_link.c_str(), angle);
865 "KDL manager could not compute pose ik for end-effector %s. Final " 866 "position error was %.2f",
867 end_effector_link.c_str(), difference.p.Norm());
875 const sensor_msgs::JointState &state,
887 return getPoseIK(end_effector_link, state, pose_in_eef, out);
891 const sensor_msgs::JointState &state,
899 fkpos_.at(end_effector_link)->JntToCart(in, out);
904 const sensor_msgs::JointState &state,
915 if (!
getVelIK(end_effector_link, state, modified_in, out))
924 const sensor_msgs::JointState &state,
939 if (!
getEefPose(end_effector_link, state, peef))
945 Eigen::Vector3d vel_eig, rot_eig, peef_eig, pgrip_eig;
961 const sensor_msgs::JointState &state,
976 out.
resize(
chain_.at(end_effector_link).getNrOfJoints());
977 ikvel_.at(end_effector_link)->CartToJnt(positions, in, out);
982 const sensor_msgs::JointState &state,
997 out.
resize(positions.rows());
998 jac_solver_.at(end_effector_link)->JntToJac(positions, out);
1003 const sensor_msgs::JointState &state)
const 1010 unsigned int processed_joints = 0, name_size, pos_size, vel_size;
1011 name_size = state.name.size();
1012 pos_size = state.position.size();
1013 vel_size = state.velocity.size();
1015 if (name_size != pos_size || name_size != vel_size)
1018 "Got joint state where the name, position and velocity dimensions " 1019 "(resp. %d, %d, %d) are different",
1020 name_size, pos_size, vel_size);
1024 for (
unsigned long i = 0;
1027 for (
unsigned long j = 0; j < state.name.size(); j++)
1045 const sensor_msgs::JointState ¤t_state,
1046 const std::string &end_effector_link,
KDL::JntArray &positions,
1049 unsigned int processed_joints = 0;
1050 unsigned int name_size, pos_size, vel_size;
1052 name_size = current_state.name.size();
1053 pos_size = current_state.position.size();
1054 vel_size = current_state.velocity.size();
1056 if (name_size != pos_size || name_size != vel_size)
1059 "Got joint state where the name, position and velocity dimensions " 1060 "(resp. %d, %d, %d) are different",
1061 name_size, pos_size, vel_size);
1065 for (
unsigned long i = 0;
1068 for (
unsigned long j = 0; j < current_state.name.size(); j++)
1071 current_state.name[j])
1073 positions(processed_joints) = current_state.position[j];
1074 velocities.
q(processed_joints) = current_state.position[j];
1075 velocities.
qdot(processed_joints) = current_state.velocity[j];
1084 ROS_ERROR(
"Failed to acquire chain joint state");
1092 const std::string &joint_name)
const 1096 if (chain.
segments[i].getJoint().getName() == joint_name)
1106 unsigned int &num_joints)
const 1113 num_joints =
chain_.at(end_effector_link).getNrOfJoints();
1125 chain_.at(end_effector_link).addSegment(new_segment);
1132 for (
auto const &item :
chain_)
1134 ikvel_[item.first]->updateInternalDataStructures();
1135 fkpos_[item.first]->updateInternalDataStructures();
1136 jac_solver_[item.first]->updateInternalDataStructures();
1141 bool setKDLManager(
const ArmInfo &arm_info, std::shared_ptr<KDLManager> manager)
1143 if (!manager->initializeArm(arm_info.kdl_eef_frame))
1148 if (!manager->setGrippingPoint(arm_info.kdl_eef_frame,
1149 arm_info.gripping_frame))
1155 "Successfully set up arm %s with eef_frame %s and gripping_frame %s",
1156 arm_info.name.c_str(), arm_info.kdl_eef_frame.c_str(),
1157 arm_info.gripping_frame.c_str());
const std::string WDLS_SOLVER("wdls")
KDL_PARSER_PUBLIC bool treeFromUrdfModel(const urdf::ModelInterface &robot_model, KDL::Tree &tree)
void resize(unsigned int newNrOfColumns)
const std::string NSO_SOLVER("nso")
unsigned int getNrOfSegments() const
IMETHOD Twist GetTwist() const
const SegmentMap & getSegments() const
const Segment & getSegment(unsigned int nr) const
const std::string getTypeName() const
unsigned int rows() const
TFSIMD_FORCE_INLINE tfScalar angle(const Quaternion &q1, const Quaternion &q2)
const Joint & getJoint() const
bool getParam(const std::string &key, std::string &s) const
void poseMsgToKDL(const geometry_msgs::Pose &m, KDL::Frame &k)
bool getChain(const std::string &chain_root, const std::string &chain_tip, Chain &chain) const
URDF_EXPORT bool initParam(const std::string ¶m)
void vectorEigenToKDL(const Eigen::Matrix< double, 3, 1 > &e, KDL::Vector &k)
#define ROS_WARN_STREAM(args)
#define ROS_DEBUG_STREAM(args)
void resize(unsigned int newSize)
void vectorKDLToEigen(const KDL::Vector &k, Eigen::Matrix< double, 3, 1 > &e)
std::vector< Segment > segments
void GetQuaternion(double &x, double &y, double &z, double &w) const
INLINE Rall1d< T, V, S > atan2(const Rall1d< T, V, S > &y, const Rall1d< T, V, S > &x)
const std::string & getName() const
#define ROS_ERROR_STREAM(args)