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)
107 std::vector<std::vector<double> > solution_ik;
111 ROS_DEBUG_NAMED(
"pr2_arm_kinematics_plugin",
"Solving with %f", q_init(0));
119 if (solution_ik.empty())
122 double min_distance = 1e6;
125 for (
int i = 0; i < (int)solution_ik.size(); i++)
129 ROS_DEBUG_NAMED(
"pr2_arm_kinematics_plugin",
"Solution : %d", (
int)solution_ik.size());
131 for (
int j = 0; j < (int)solution_ik[i].size(); j++)
133 ROS_DEBUG_NAMED(
"pr2_arm_kinematics_plugin",
"%d: %f", j, solution_ik[i][j]);
139 if (tmp_distance < min_distance)
141 min_distance = tmp_distance;
148 q_out.resize((
int)solution_ik[min_index].size());
149 for (
int i = 0; i < (int)solution_ik[min_index].size(); i++)
151 q_out(i) = solution_ik[min_index][i];
160 const double& timeout)
163 KDL::JntArray q_init = q_in;
167 double loop_time = 0;
170 int num_positive_increments =
172 int num_negative_increments =
175 ROS_DEBUG_NAMED(
"pr2_arm_kinematics_plugin",
"%f %f %f %d %d \n\n", initial_guess,
178 num_negative_increments);
179 while (loop_time < timeout)
183 if (!
getCount(count, num_positive_increments, -num_negative_increments))
190 if (loop_time >= timeout)
192 ROS_DEBUG_NAMED(
"pr2_arm_kinematics_plugin",
"IK Timed out in %f seconds", timeout);
197 ROS_DEBUG_NAMED(
"pr2_arm_kinematics_plugin",
"No IK solution was found");
203 bool getKDLChain(
const urdf::ModelInterface& model,
const std::string& root_name,
const std::string& tip_name,
204 KDL::Chain& kdl_chain)
210 ROS_ERROR(
"Could not initialize tree object");
213 if (!tree.getChain(root_name, tip_name, kdl_chain))
215 ROS_ERROR_STREAM(
"Could not initialize chain object for base " << root_name <<
" tip " << tip_name);
223 Eigen::Isometry3f b = Eigen::Isometry3f::Identity();
224 for (
int i = 0; i < 3; i++)
226 for (
int j = 0; j < 3; j++)
238 for (
int i = 0; i < (int)array_1.size(); i++)
240 distance += (array_1[i] - array_2(i)) * (array_1[i] - array_2(i));
245 void getKDLChainInfo(
const KDL::Chain& chain, moveit_msgs::KinematicSolverInfo& chain_info)
248 while (i < (
int)chain.getNrOfSegments())
250 chain_info.link_names.push_back(chain.getSegment(i).getName());
265 const std::string& base_frame,
const std::vector<std::string>& tip_frames,
266 double search_discretization)
299 ROS_DEBUG_NAMED(
"pr2_arm_kinematics_plugin",
"PR2Kinematics:: joint name: %s", joint_name.c_str());
303 ROS_DEBUG_NAMED(
"pr2_arm_kinematics_plugin",
"PR2Kinematics can solve IK for %s", link_name.c_str());
307 ROS_DEBUG_NAMED(
"pr2_arm_kinematics_plugin",
"PR2Kinematics can solve FK for %s", link_name.c_str());
309 ROS_DEBUG_NAMED(
"pr2_arm_kinematics_plugin",
"PR2KinematicsPlugin::active for %s", group_name.c_str());
317 const std::vector<double>& ,
318 std::vector<double>& ,
319 moveit_msgs::MoveItErrorCodes& ,
326 const std::vector<double>& ik_seed_state,
double timeout,
327 std::vector<double>& solution, moveit_msgs::MoveItErrorCodes& error_code,
333 error_code.val = error_code.PLANNING_FAILED;
336 KDL::Frame pose_desired;
340 KDL::JntArray jnt_pos_in;
341 KDL::JntArray jnt_pos_out;
345 jnt_pos_in(i) = ik_seed_state[i];
348 int ik_valid =
pr2_arm_ik_solver_->cartToJntSearch(jnt_pos_in, pose_desired, jnt_pos_out, timeout);
351 error_code.val = error_code.NO_IK_SOLUTION;
360 solution[i] = jnt_pos_out(i);
362 error_code.val = error_code.SUCCESS;
367 ROS_DEBUG_NAMED(
"pr2_arm_kinematics_plugin",
"An IK solution could not be found");
368 error_code.val = error_code.NO_IK_SOLUTION;
374 const std::vector<double>& ,
double ,
375 const std::vector<double>& ,
376 std::vector<double>& ,
377 moveit_msgs::MoveItErrorCodes& ,
384 const std::vector<double>& ,
double ,
385 std::vector<double>& ,
387 moveit_msgs::MoveItErrorCodes& ,
394 const std::vector<double>& ,
double ,
395 const std::vector<double>& ,
396 std::vector<double>& ,
398 moveit_msgs::MoveItErrorCodes& ,
405 const std::vector<double>& ,
406 std::vector<geometry_msgs::Pose>& )
const