60 std::vector<kinematic_constraints::JointConstraint> jc;
61 for (
const moveit_msgs::JointConstraint& joint_constraint : constr.joint_constraints)
64 if (j.configure(joint_constraint))
68 return jc.empty() ? false :
configure(jc);
77 ROS_ERROR_NAMED(
"constraint_samplers",
"NULL group specified for constraint sampler");
83 std::map<std::string, JointInfo> bound_data;
84 bool some_valid_constraint =
false;
87 if (!joint_constraint.enabled())
94 some_valid_constraint =
true;
98 std::map<std::string, JointInfo>::iterator it = bound_data.find(joint_constraint.getJointVariableName());
99 if (it != bound_data.end())
105 joint_constraint.getDesiredJointPosition() - joint_constraint.getJointToleranceBelow()),
107 joint_constraint.getDesiredJointPosition() + joint_constraint.getJointToleranceAbove()));
109 ROS_DEBUG_NAMED(
"constraint_samplers",
"Bounds for %s JointConstraint are %g %g",
114 std::stringstream cs;
115 joint_constraint.print(cs);
117 "The constraints for joint '%s' are such that "
118 "there are no possible values for the joint: min_bound: %g, max_bound: %g. Failing.\n",
123 bound_data[joint_constraint.getJointVariableName()] = ji;
126 if (!some_valid_constraint)
128 ROS_WARN_NAMED(
"constraint_samplers",
"No valid joint constraints");
132 for (std::pair<const std::string, JointInfo>& it : bound_data)
138 if (bound_data.find(joint->getName()) == bound_data.end() && joint->getVariableCount() > 0 &&
139 joint->getMimic() ==
nullptr)
142 const std::vector<std::string>& vars = joint->getVariableNames();
145 bool all_found =
true;
146 for (
const std::string& var : vars)
147 if (bound_data.find(var) == bound_data.end())
170 ROS_WARN_NAMED(
"constraint_samplers",
"JointConstraintSampler not configured, won't sample");
175 std::vector<double> v;
176 for (std::size_t i = 0; i <
unbounded_.size(); ++i)
180 for (std::size_t j = 0; j < v.size(); ++j)
185 for (
const JointInfo& bound :
bounds_)
233 const kinematic_constraints::OrientationConstraintPtr& oc)
234 : position_constraint_(pc), orientation_constraint_(oc)
258 ROS_WARN_NAMED(
"constraint_samplers",
"No enabled constraints in sampling pose");
269 "Position and orientation constraints need to be specified for the same link "
270 "in order to use IK-based sampling");
281 ROS_WARN_NAMED(
"constraint_samplers",
"No solver instance in setup");
291 for (std::size_t p = 0; p < constr.position_constraints.size(); ++p)
292 for (std::size_t o = 0; o < constr.orientation_constraints.size(); ++o)
293 if (constr.position_constraints[p].link_name == constr.orientation_constraints[o].link_name)
295 kinematic_constraints::PositionConstraintPtr pc(
297 kinematic_constraints::OrientationConstraintPtr oc(
299 if (pc->configure(constr.position_constraints[p],
scene_->getTransforms()) &&
300 oc->configure(constr.orientation_constraints[o],
scene_->getTransforms()))
301 return configure(IKSamplingPose(pc, oc));
304 for (
const moveit_msgs::PositionConstraint& position_constraint : constr.position_constraints)
306 kinematic_constraints::PositionConstraintPtr pc(
308 if (pc->configure(position_constraint,
scene_->getTransforms()))
312 for (
const moveit_msgs::OrientationConstraint& orientation_constraint : constr.orientation_constraints)
314 kinematic_constraints::OrientationConstraintPtr oc(
316 if (oc->configure(orientation_constraint,
scene_->getTransforms()))
327 const std::vector<bodies::BodyPtr>& constraint_regions =
331 vol += constraint_region->computeVolume();
332 if (!constraint_regions.empty())
367 "The IK solver expects requests in frame '%s' but this frame is not known to the sampler. "
368 "Ignoring transformation (IK may fail)",
374 bool wrong_link =
false;
382 for (
const std::pair<const moveit::core::LinkModel* const, Eigen::Isometry3d>& fixed_link : fixed_links)
400 for (
const std::pair<const moveit::core::LinkModel* const, Eigen::Isometry3d>& fixed_link : fixed_links)
414 "IK cannot be performed for link '%s'. The solver can report IK solutions for link '%s'.",
418 kb_->getTipFrame().c_str());
425 unsigned int max_attempts)
431 "IKConstraintSampler received dirty robot state, but valid transforms are required. "
443 for (std::size_t i = 0; i < b.size(); ++i)
451 ROS_ERROR_NAMED(
"constraint_samplers",
"Unable to sample a point inside the constraint region");
457 ROS_ERROR_NAMED(
"constraint_samplers",
"Unable to sample a point inside the constraint region. "
458 "Constraint region is empty when it should not be.");
471 temp_state.setToRandomPositions(
jmg_);
488 Eigen::Isometry3d diff;
490 moveit_msgs::OrientationConstraint::XYZ_EULER_ANGLES)
492 diff = Eigen::Isometry3d(Eigen::AngleAxisd(angle_x, Eigen::Vector3d::UnitX()) *
493 Eigen::AngleAxisd(angle_y, Eigen::Vector3d::UnitY()) *
494 Eigen::AngleAxisd(angle_z, Eigen::Vector3d::UnitZ()));
497 moveit_msgs::OrientationConstraint::ROTATION_VECTOR)
503 diff = Eigen::Isometry3d(Eigen::AngleAxisd(rotation_vector.norm(), rotation_vector.normalized()));
509 "The parameterization type for the orientation constraints is invalid.");
520 Eigen::Isometry3d rt(
t.linear() * quat);
521 quat = Eigen::Quaterniond(rt.linear());
529 quat = Eigen::Quaterniond(q[3], q[0], q[1], q[2]);
544 const std::vector<double>& ik_sol, moveit_msgs::MoveItErrorCodes& error_code)
547 std::vector<double> solution(bij.size());
548 for (std::size_t i = 0; i < bij.size(); ++i)
549 solution[i] = ik_sol[bij[i]];
550 if (constraint(state, jmg, &solution[0]))
551 error_code.val = moveit_msgs::MoveItErrorCodes::SUCCESS;
558 unsigned int max_attempts)
560 return sampleHelper(state, reference_state, max_attempts);
564 unsigned int max_attempts)
568 ROS_WARN_NAMED(
"constraint_samplers",
"IKConstraintSampler not configured, won't sample");
574 adapted_ik_validity_callback = [
this, state_ptr = &state](
const geometry_msgs::Pose& ,
575 const std::vector<double>& joints,
576 moveit_msgs::MoveItErrorCodes& error_code) {
580 for (
unsigned int a = 0; a < max_attempts; ++a)
584 Eigen::Quaterniond quat;
588 ROS_INFO_NAMED(
"constraint_samplers",
"IK constraint sampler was unable to produce a pose to run IK for");
597 Eigen::Isometry3d ikq(Eigen::Translation3d(
point) * quat);
600 point = ikq.translation();
601 quat = Eigen::Quaterniond(ikq.linear());
607 Eigen::Isometry3d ikq(Eigen::Translation3d(
point) * quat);
609 point = ikq.translation();
610 quat = Eigen::Quaterniond(ikq.linear());
613 geometry_msgs::Pose ik_query;
614 ik_query.position.x =
point.x();
615 ik_query.position.y =
point.y();
616 ik_query.position.z =
point.z();
617 ik_query.orientation.x = quat.x();
618 ik_query.orientation.y = quat.y();
619 ik_query.orientation.z = quat.z();
620 ik_query.orientation.w = quat.w();
642 std::vector<double> seed(ik_joint_bijection.size(), 0.0);
643 std::vector<double> vals;
651 assert(vals.size() == ik_joint_bijection.size());
652 for (std::size_t i = 0; i < ik_joint_bijection.size(); ++i)
653 seed[i] = vals[ik_joint_bijection[i]];
655 std::vector<double> ik_sol;
656 moveit_msgs::MoveItErrorCodes error;
658 if (adapted_ik_validity_callback ?
659 kb_->searchPositionIK(ik_query, seed, timeout, ik_sol, adapted_ik_validity_callback, error) :
660 kb_->searchPositionIK(ik_query, seed, timeout, ik_sol, error))
662 assert(ik_sol.size() == ik_joint_bijection.size());
663 std::vector<double> solution(ik_joint_bijection.size());
664 for (std::size_t i = 0; i < ik_joint_bijection.size(); ++i)
665 solution[ik_joint_bijection[i]] = ik_sol[i];
673 error.val != moveit_msgs::MoveItErrorCodes::INVALID_ROBOT_STATE &&
675 ROS_ERROR_NAMED(
"constraint_samplers",
"IK solver failed with error %d", error.val);