37 #include <geometry_msgs/PoseStamped.h> 39 #include <tf2_kdl/tf2_kdl.h> 50 bool PR2ArmIKSolver::getCount(
int& count,
const int& max_count,
const int& min_count)
54 if (-count >= min_count)
59 else if (count + 1 <= max_count)
71 if (1 - count <= max_count)
76 else if (count - 1 >= min_count)
86 PR2ArmIKSolver::PR2ArmIKSolver(
const urdf::ModelInterface&
robot_model,
const std::string& root_frame_name,
87 const std::string& tip_frame_name,
const double& search_discretization_angle,
88 const int& free_angle)
110 std::vector<std::vector<double> > solution_ik;
114 ROS_DEBUG_NAMED(
"pr2_arm_kinematics_plugin",
"Solving with %f", q_init(0));
122 if (solution_ik.empty())
125 double min_distance = 1e6;
128 for (
int i = 0; i < (int)solution_ik.size(); i++)
132 ROS_DEBUG_NAMED(
"pr2_arm_kinematics_plugin",
"Solution : %d", (
int)solution_ik.size());
134 for (
int j = 0; j < (int)solution_ik[i].size(); j++)
136 ROS_DEBUG_NAMED(
"pr2_arm_kinematics_plugin",
"%d: %f", j, solution_ik[i][j]);
142 if (tmp_distance < min_distance)
144 min_distance = tmp_distance;
151 q_out.
resize((
int)solution_ik[min_index].size());
152 for (
int i = 0; i < (int)solution_ik[min_index].size(); i++)
154 q_out(i) = solution_ik[min_index][i];
163 const double& timeout)
170 double loop_time = 0;
173 int num_positive_increments =
175 int num_negative_increments =
178 ROS_DEBUG_NAMED(
"pr2_arm_kinematics_plugin",
"%f %f %f %d %d \n\n", initial_guess,
181 num_negative_increments);
182 while (loop_time < timeout)
186 if (!
getCount(count, num_positive_increments, -num_negative_increments))
193 if (loop_time >= timeout)
195 ROS_DEBUG_NAMED(
"pr2_arm_kinematics_plugin",
"IK Timed out in %f seconds", timeout);
200 ROS_DEBUG_NAMED(
"pr2_arm_kinematics_plugin",
"No IK solution was found");
206 bool getKDLChain(
const urdf::ModelInterface& model,
const std::string& root_name,
const std::string& tip_name,
213 ROS_ERROR(
"Could not initialize tree object");
216 if (!tree.
getChain(root_name, tip_name, kdl_chain))
218 ROS_ERROR_STREAM(
"Could not initialize chain object for base " << root_name <<
" tip " << tip_name);
226 Eigen::Isometry3f b = Eigen::Isometry3f::Identity();
227 for (
int i = 0; i < 3; i++)
229 for (
int j = 0; j < 3; j++)
241 for (
int i = 0; i < (int)array_1.size(); i++)
243 distance += (array_1[i] - array_2(i)) * (array_1[i] - array_2(i));
245 return sqrt(distance);
268 const std::string& base_frame,
const std::vector<std::string>& tip_frames,
269 double search_discretization)
271 storeValues(robot_model, group_name, base_frame, tip_frames, search_discretization);
302 ROS_DEBUG_NAMED(
"pr2_arm_kinematics_plugin",
"PR2Kinematics:: joint name: %s",
307 ROS_DEBUG_NAMED(
"pr2_arm_kinematics_plugin",
"PR2Kinematics can solve IK for %s",
312 ROS_DEBUG_NAMED(
"pr2_arm_kinematics_plugin",
"PR2Kinematics can solve FK for %s",
315 ROS_DEBUG_NAMED(
"pr2_arm_kinematics_plugin",
"PR2KinematicsPlugin::active for %s", group_name.c_str());
323 std::vector<double>& solution, moveit_msgs::MoveItErrorCodes& error_code,
330 const std::vector<double>& ik_seed_state,
double timeout,
331 std::vector<double>& solution, moveit_msgs::MoveItErrorCodes& error_code,
337 error_code.val = error_code.PLANNING_FAILED;
349 jnt_pos_in(i) = ik_seed_state[i];
352 int ik_valid =
pr2_arm_ik_solver_->cartToJntSearch(jnt_pos_in, pose_desired, jnt_pos_out, timeout);
355 error_code.val = error_code.NO_IK_SOLUTION;
361 solution.resize(dimension_);
364 solution[i] = jnt_pos_out(i);
366 error_code.val = error_code.SUCCESS;
371 ROS_DEBUG_NAMED(
"pr2_arm_kinematics_plugin",
"An IK solution could not be found");
372 error_code.val = error_code.NO_IK_SOLUTION;
378 const std::vector<double>& ik_seed_state,
double timeout,
379 const std::vector<double>& consistency_limit,
380 std::vector<double>& solution, moveit_msgs::MoveItErrorCodes& error_code,
387 const std::vector<double>& ik_seed_state,
double timeout,
388 std::vector<double>& solution,
const IKCallbackFn& solution_callback,
389 moveit_msgs::MoveItErrorCodes& error_code,
396 const std::vector<double>& ik_seed_state,
double timeout,
397 const std::vector<double>& consistency_limit,
398 std::vector<double>& solution,
const IKCallbackFn& solution_callback,
399 moveit_msgs::MoveItErrorCodes& error_code,
406 const std::vector<double>& joint_angles,
407 std::vector<geometry_msgs::Pose>& poses)
const double search_discretization_angle_
A set of options for the kinematics solver.
Core components of MoveIt!
KDL_PARSER_PUBLIC bool treeFromUrdfModel(const urdf::ModelInterface &robot_model, KDL::Tree &tree)
unsigned int getNrOfSegments() const
bool getKDLChain(const urdf::ModelInterface &model, const std::string &root_name, const std::string &tip_name, KDL::Chain &kdl_chain)
pr2_arm_kinematics::PR2ArmIKSolverPtr pr2_arm_ik_solver_
std::vector< std::string > tip_frames_
const Segment & getSegment(unsigned int nr) const
bool getCount(int &count, const int &max_count, const int &min_count)
moveit_msgs::KinematicSolverInfo fk_solver_info_
std::string root_frame_name_
static const int NO_IK_SOLUTION
std::shared_ptr< KDL::ChainFkSolverPos_recursive > jnt_to_pose_solver_
Definition of a kinematic model. This class is not thread safe, however multiple instances can be cre...
void getKDLChainInfo(const KDL::Chain &chain, moveit_msgs::KinematicSolverInfo &chain_info)
void updateInternalDataStructures()
#define ROS_DEBUG_NAMED(name,...)
bool getPositionIK(const geometry_msgs::Pose &ik_pose, const std::vector< double > &ik_seed_state, std::vector< double > &solution, moveit_msgs::MoveItErrorCodes &error_code, const kinematics::KinematicsQueryOptions &options=kinematics::KinematicsQueryOptions()) const override
Given a desired pose of the end-effector, compute the joint angles to reach it.
void storeValues(const moveit::core::RobotModel &robot_model, const std::string &group_name, const std::string &base_frame, const std::vector< std::string > &tip_frames, double search_discretization)
void fromMsg(const A &, B &b)
PR2ArmIK pr2_arm_ik_
The PR2 inverse kinematics solver.
bool searchPositionIK(const geometry_msgs::Pose &ik_pose, const std::vector< double > &ik_seed_state, double timeout, std::vector< double > &solution, moveit_msgs::MoveItErrorCodes &error_code, const kinematics::KinematicsQueryOptions &options=kinematics::KinematicsQueryOptions()) const override
Given a desired pose of the end-effector, search for the joint angles required to reach it...
const urdf::ModelInterfaceSharedPtr & getURDF() const
Get the parsed URDF model.
bool getChain(const std::string &chain_root, const std::string &chain_tip, Chain &chain) const
INLINE Rall1d< T, V, S > sqrt(const Rall1d< T, V, S > &arg)
def xml_string(rootXml, addHeader=True)
bool active_
Indicates whether the solver has been successfully initialized.
boost::function< void(const geometry_msgs::Pose &ik_pose, const std::vector< double > &ik_solution, moveit_msgs::MoveItErrorCodes &error_code)> IKCallbackFn
Signature for a callback to validate an IK solution. Typically used for collision checking...
moveit_msgs::KinematicSolverInfo ik_solver_info_
const std::vector< std::string > & getJointNames() const override
Return all the joint names in the order they are used internally.
void resize(unsigned int newSize)
bool initialize(const moveit::core::RobotModel &robot_model, const std::string &group_name, const std::string &base_frame, const std::vector< std::string > &tip_frames, double search_discretization) override
Initialization function for the kinematics.
const std::vector< std::string > & getLinkNames() const override
Return all the link names in the order they are represented internally.
bool getPositionFK(const std::vector< std::string > &link_names, const std::vector< double > &joint_angles, std::vector< geometry_msgs::Pose > &poses) const override
Given a set of joint angles and a set of links, compute their pose.
int cartToJntSearch(const KDL::JntArray &q_in, const KDL::Frame &p_in, KDL::JntArray &q_out, const double &timeout)
const std::string & getName() const
static const int TIMED_OUT
bool init(const urdf::ModelInterface &robot_model, const std::string &root_name, const std::string &tip_name)
Initialize the solver by providing a urdf::Model and a root and tip name.
moveit_msgs::KinematicSolverInfo solver_info_
get chain information about the arm.
void computeIKShoulderPan(const Eigen::Isometry3f &g_in, const double &shoulder_pan_initial_guess, std::vector< std::vector< double > > &solution) const
compute IK based on an initial guess for the shoulder pan angle.
#define ROS_ERROR_STREAM(args)
bool isActive()
Specifies if the node is active or not.
int CartToJnt(const KDL::JntArray &q_init, const KDL::Frame &p_in, KDL::JntArray &q_out) override
double distance(const urdf::Pose &transform)
void computeIKShoulderRoll(const Eigen::Isometry3f &g_in, const double &shoulder_roll_initial_guess, std::vector< std::vector< double > > &solution) const
compute IK based on an initial guess for the shoulder roll angle. h
double computeEuclideanDistance(const std::vector< double > &array_1, const KDL::JntArray &array_2)
Eigen::Isometry3f KDLToEigenMatrix(const KDL::Frame &p)