Go to the documentation of this file.00001
00032 #include <constrained_ik/moveit_interface/joint_interpolation_planner.h>
00033 #include <ros/ros.h>
00034 #include <eigen3/Eigen/Core>
00035 #include <eigen_conversions/eigen_msg.h>
00036 #include <moveit/robot_state/conversions.h>
00037
00038
00039 namespace constrained_ik
00040 {
00041 bool JointInterpolationPlanner::solve(planning_interface::MotionPlanDetailedResponse &res)
00042 {
00043 planning_interface::MotionPlanResponse response;
00044 bool success = solve(response);
00045
00046
00047 res.trajectory_.push_back(response.trajectory_);
00048 res.processing_time_.push_back(response.planning_time_);
00049 res.description_.push_back("Joint interpolation Planner");
00050 res.error_code_ = response.error_code_;
00051
00052 return success;
00053 }
00054
00055 bool JointInterpolationPlanner::solve(planning_interface::MotionPlanResponse &res)
00056 {
00057 ros::WallTime start_time = ros::WallTime::now();
00058 robot_model::RobotModelConstPtr rob_model = planning_scene_->getRobotModel();
00059 robot_state::RobotState start_state(rob_model);
00060 robot_state::robotStateMsgToRobotState(request_.start_state, start_state);
00061 robot_state::RobotState goal_state = start_state;
00062 robot_state::RobotStatePtr mid_state;
00063 const robot_model::JointModelGroup *group_model = rob_model->getJointModelGroup(request_.group_name);
00064 std::vector<std::string> joint_names = group_model->getActiveJointModelNames();
00065 std::vector<std::string> link_names = group_model->getLinkModelNames();
00066 Eigen::Affine3d goal_pose;
00067 std::vector<double> pos(1);
00068 robot_trajectory::RobotTrajectoryPtr traj(new robot_trajectory::RobotTrajectory(rob_model, request_.group_name));
00069
00070
00071 ROS_INFO_STREAM("Joint Interpolated Planner will plan for group: " << request_.group_name <<" with tip link '"<<link_names.back() <<"'");
00072
00073
00074 if (!request_.goal_constraints[0].joint_constraints.empty())
00075 {
00076 for(unsigned int i = 0; i < request_.goal_constraints[0].joint_constraints.size(); i++)
00077 {
00078 pos[0]=request_.goal_constraints[0].joint_constraints[i].position;
00079 goal_state.setJointPositions(joint_names[i], pos);
00080
00081 ROS_DEBUG("Setting joint %s from %f to position %f", request_.goal_constraints[0].joint_constraints[i].joint_name.c_str(),
00082 *start_state.getJointPositions(joint_names[i]), request_.goal_constraints[0].joint_constraints[i].position);
00083 }
00084 }
00085 else
00086 {
00087 geometry_msgs::Pose pose;
00088 if (!request_.goal_constraints[0].position_constraints.empty() && !request_.goal_constraints[0].orientation_constraints.empty())
00089 {
00090 pose.position = request_.goal_constraints[0].position_constraints[0].constraint_region.primitive_poses[0].position;
00091 pose.orientation = request_.goal_constraints[0].orientation_constraints[0].orientation;
00092 }
00093 else if (!request_.goal_constraints[0].position_constraints.empty() && request_.goal_constraints[0].orientation_constraints.empty())
00094 {
00095 tf::poseEigenToMsg(start_state.getFrameTransform(link_names.back()), pose);
00096 pose.position = request_.goal_constraints[0].position_constraints[0].constraint_region.primitive_poses[0].position;
00097 }
00098 else if (request_.goal_constraints[0].position_constraints.empty() && !request_.goal_constraints[0].orientation_constraints.empty())
00099 {
00100 tf::poseEigenToMsg(start_state.getFrameTransform(link_names.back()), pose);
00101 pose.orientation = request_.goal_constraints[0].orientation_constraints[0].orientation;
00102 }
00103 else
00104 {
00105 ROS_ERROR("No constraint was passed with request!");
00106 res.error_code_.val = moveit_msgs::MoveItErrorCodes::INVALID_GOAL_CONSTRAINTS;
00107 return false;
00108 }
00109
00110 tf::poseMsgToEigen(pose, goal_pose);
00111 if(!goal_state.setFromIK(group_model, goal_pose, link_names.back()))
00112 {
00113 ROS_ERROR("Joint Interpolated Planner goal pose is out of reach");
00114 res.error_code_.val = moveit_msgs::MoveItErrorCodes::NO_IK_SOLUTION;
00115 return false;
00116 }
00117 }
00118
00119
00120 double dt;
00121 Eigen::VectorXd jv_step;
00122 Eigen::VectorXd jv_start;
00123 Eigen::VectorXd delta;
00124
00125 start_state.copyJointGroupPositions(request_.group_name, jv_start);
00126 mid_state = robot_model::RobotStatePtr(new robot_model::RobotState(start_state));
00127 start_state.interpolate(goal_state, 0.1, *mid_state);
00128 mid_state->copyJointGroupPositions(request_.group_name, jv_step);
00129 delta = (jv_step - jv_start).cwiseAbs();
00130 dt = config_.joint_discretization_step*(0.1/delta.maxCoeff());
00131
00132
00133 int steps = (1.0/dt) + 1;
00134 dt = 1.0/steps;
00135
00136 for (int j=0; j<=steps; j++)
00137 {
00138 if (j!=steps)
00139 start_state.interpolate(goal_state, j*dt, *mid_state);
00140 else
00141 start_state.interpolate(goal_state, 1, *mid_state);
00142
00143 traj->addSuffixWayPoint(*mid_state, 0.0);
00144
00145 if (terminate_)
00146 break;
00147
00148 res.planning_time_ = (ros::WallTime::now() - start_time).toSec();
00149 if (res.planning_time_ > request_.allowed_planning_time)
00150 {
00151 ROS_ERROR("Joint Interpolated Planner timed out. :(");
00152 res.error_code_.val = moveit_msgs::MoveItErrorCodes::TIMED_OUT;
00153 return false;
00154 }
00155 }
00156
00157
00158 if (terminate_)
00159 {
00160 ROS_INFO("Joint Interpolated Planner was terminated!");
00161 res.error_code_.val = moveit_msgs::MoveItErrorCodes::INVALID_MOTION_PLAN;
00162 return false;
00163 }
00164
00165
00166 if (planning_scene_->isPathValid(*traj, request_.group_name))
00167 {
00168 ROS_INFO("Joint Interpolated Planner generated a collision-free trajectory with %i points! :)",steps);
00169 res.trajectory_=traj;
00170 res.error_code_.val = moveit_msgs::MoveItErrorCodes::SUCCESS;
00171 return true;
00172 }
00173 else
00174 {
00175 ROS_INFO("Joint interpolated trajectory is not collision free. :(");
00176 res.error_code_.val = moveit_msgs::MoveItErrorCodes::INVALID_MOTION_PLAN;
00177 return false;
00178 }
00179 }
00180 }