68rclcpp::Logger getLogger()
161#include "_ROBOT_NAME___GROUP_NAME__ikfast_solver.cpp"
165 std::vector<std::string> joint_names_;
166 std::vector<double> joint_min_vector_;
167 std::vector<double> joint_max_vector_;
168 std::vector<bool> joint_has_limits_vector_;
169 std::vector<std::string> link_names_;
170 const size_t num_joints_;
171 std::vector<int> free_params_;
173 std::shared_ptr<ikfast_kinematics::ParamListener> param_listener_;
174 ikfast_kinematics::Params params_;
178 const std::string IKFAST_TIP_FRAME_ =
"_EEF_LINK_";
179 const std::string IKFAST_BASE_FRAME_ =
"_BASE_LINK_";
185 bool tip_transform_required_;
186 bool base_transform_required_;
190 Eigen::Isometry3d chain_base_to_group_base_;
191 Eigen::Isometry3d group_tip_to_chain_tip_;
195 const std::vector<std::string>& getJointNames()
const override
199 const std::vector<std::string>& getLinkNames()
const override
210 srand(time(
nullptr));
227 getPositionIK(
const geometry_msgs::msg::Pose& ik_pose,
const std::vector<double>& ik_seed_state,
228 std::vector<double>& solution, moveit_msgs::msg::MoveItErrorCodes& error_code,
246 bool getPositionIK(
const std::vector<geometry_msgs::msg::Pose>& ik_poses,
const std::vector<double>& ik_seed_state,
259 const geometry_msgs::msg::Pose& ik_pose,
const std::vector<double>& ik_seed_state,
double timeout,
260 std::vector<double>& solution, moveit_msgs::msg::MoveItErrorCodes& error_code,
273 const geometry_msgs::msg::Pose& ik_pose,
const std::vector<double>& ik_seed_state,
double timeout,
274 const std::vector<double>& consistency_limits, std::vector<double>& solution,
275 moveit_msgs::msg::MoveItErrorCodes& error_code,
287 const geometry_msgs::msg::Pose& ik_pose,
const std::vector<double>& ik_seed_state,
double timeout,
288 std::vector<double>& solution,
const IKCallbackFn& solution_callback,
289 moveit_msgs::msg::MoveItErrorCodes& error_code,
303 const geometry_msgs::msg::Pose& ik_pose,
const std::vector<double>& ik_seed_state,
double timeout,
304 const std::vector<double>& consistency_limits, std::vector<double>& solution,
305 const IKCallbackFn& solution_callback, moveit_msgs::msg::MoveItErrorCodes& error_code,
316 bool getPositionFK(
const std::vector<std::string>& link_names,
const std::vector<double>& joint_angles,
317 std::vector<geometry_msgs::msg::Pose>& poses)
const override;
333 bool setRedundantJoints(
const std::vector<unsigned int>& redundant_joint_indices)
override;
337 const std::string& group_name,
const std::string& base_frame,
338 const std::vector<std::string>& tip_frames,
double search_discretization)
override;
344 size_t solve(KDL::Frame& pose_frame,
const std::vector<double>& vfree, IkSolutionList<IkReal>& solutions)
const;
349 void getSolution(
const IkSolutionList<IkReal>& solutions,
int i, std::vector<double>& solution)
const;
354 void getSolution(
const IkSolutionList<IkReal>& solutions,
const std::vector<double>& ik_seed_state,
int i,
355 std::vector<double>& solution)
const;
360 double enforceLimits(
double val,
double min,
double max)
const;
362 void fillFreeParams(
int count,
int* array);
363 bool getCount(
int& count,
int max_count,
int min_count)
const;
374 bool computeRelativeTransform(
const std::string& from,
const std::string& to, Eigen::Isometry3d& transform,
375 bool& differs_from_identity);
382 void transformToChainFrame(
const geometry_msgs::msg::Pose& ik_pose, KDL::Frame& ik_pose_chain)
const;
385bool IKFastKinematicsPlugin::computeRelativeTransform(
const std::string& from,
const std::string& to,
386 Eigen::Isometry3d& transform,
bool& differs_from_identity)
388 RobotStatePtr robot_state;
389 robot_state = std::make_shared<RobotState>(
robot_model_);
390 robot_state->setToDefaultValues();
393 auto* from_link =
robot_model_->getLinkModel(from, &has_link);
394 auto* to_link =
robot_model_->getLinkModel(to, &has_link);
395 if (!from_link || !to_link)
398 if (
robot_model_->getRigidlyConnectedParentLinkModel(from_link) !=
399 robot_model_->getRigidlyConnectedParentLinkModel(to_link))
401 RCLCPP_ERROR_STREAM(getLogger(),
"Link frames " << from <<
" and " << to <<
" are not rigidly connected.");
405 transform = robot_state->getGlobalLinkTransform(from_link).inverse() * robot_state->getGlobalLinkTransform(to_link);
406 differs_from_identity = !transform.matrix().isIdentity();
410bool IKFastKinematicsPlugin::initialize(
const rclcpp::Node::SharedPtr& node,
412 const std::string& base_frame,
const std::vector<std::string>& tip_frames,
413 double search_discretization)
415 if (tip_frames.size() != 1)
417 RCLCPP_ERROR(
getLogger(),
"Expecting exactly one tip frame.");
422 std::string kinematics_param_prefix =
"robot_description_kinematics." + group_name;
423 param_listener_ = std::make_shared<ikfast_kinematics::ParamListener>(node, kinematics_param_prefix);
424 params_ = param_listener_->get_params();
426 storeValues(robot_model, group_name, base_frame, tip_frames, search_discretization);
428 RCLCPP_INFO_STREAM(
getLogger(),
"Using link_prefix: '" << params_.link_prefix <<
'\'');
436 if (!robot_model.
hasLinkModel(params_.link_prefix + IKFAST_TIP_FRAME_))
437 RCLCPP_ERROR_STREAM(
getLogger(),
"prefixed tip frame '" << params_.link_prefix + IKFAST_TIP_FRAME_
438 <<
"' does not exist. "
439 "Please check your link_prefix parameter.");
440 if (!robot_model.
hasLinkModel(params_.link_prefix + IKFAST_BASE_FRAME_))
441 RCLCPP_ERROR_STREAM(
getLogger(),
"prefixed base frame '" << params_.link_prefix + IKFAST_BASE_FRAME_
442 <<
"' does not exist. "
443 "Please check your link_prefix parameter.");
449 if (!computeRelativeTransform(
tip_frames_[0], params_.link_prefix + IKFAST_TIP_FRAME_, group_tip_to_chain_tip_,
450 tip_transform_required_) ||
451 !computeRelativeTransform(params_.link_prefix + IKFAST_BASE_FRAME_,
base_frame_, chain_base_to_group_base_,
452 base_transform_required_))
460 if (free_params_.size() > 1)
462 RCLCPP_ERROR(
getLogger(),
"Only one free joint parameter supported!");
465 else if (free_params_.size() == 1)
469 KinematicsBase::setSearchDiscretization(search_discretization);
475 RCLCPP_ERROR_STREAM(
getLogger(),
"Unknown planning group: " << group_name);
479 RCLCPP_DEBUG(
getLogger(),
"Registering joints and links");
482 while (link && link != base_link)
485 link_names_.push_back(link->
getName());
491 joint_names_.push_back(joint->
getName());
500 if (joint_names_.size() != num_joints_)
502 RCLCPP_FATAL(
getLogger(),
"Joint numbers of RobotModel (%zd) and IKFast solver (%zd) do not match",
503 joint_names_.size(), num_joints_);
507 std::reverse(link_names_.begin(), link_names_.end());
508 std::reverse(joint_names_.begin(), joint_names_.end());
509 std::reverse(joint_min_vector_.begin(), joint_min_vector_.end());
510 std::reverse(joint_max_vector_.begin(), joint_max_vector_.end());
511 std::reverse(joint_has_limits_vector_.begin(), joint_has_limits_vector_.end());
513 for (
size_t joint_id = 0; joint_id < num_joints_; ++joint_id)
514 RCLCPP_DEBUG_STREAM(
getLogger(), joint_names_[joint_id] <<
' ' << joint_min_vector_[joint_id] <<
' '
515 << joint_max_vector_[joint_id] <<
' '
516 << joint_has_limits_vector_[joint_id]);
524 if (discretization.empty())
526 RCLCPP_ERROR(getLogger(),
"The 'discretization' map is empty");
532 RCLCPP_ERROR(getLogger(),
"This group's solver doesn't support redundant joints");
538 std::string redundant_joint = joint_names_[free_params_[0]];
539 RCLCPP_ERROR_STREAM(getLogger(),
"Attempted to discretize a non-redundant joint "
540 << discretization.begin()->first <<
", only joint '" << redundant_joint
545 if (discretization.begin()->second <= 0.0)
547 RCLCPP_ERROR_STREAM(getLogger(),
"Discretization can not takes values that are <= 0");
557 RCLCPP_ERROR(getLogger(),
"Changing the redundant joints isn't permitted by this group's solver ");
561size_t IKFastKinematicsPlugin::solve(KDL::Frame& pose_frame,
const std::vector<double>& vfree,
562 IkSolutionList<IkReal>& solutions)
const
568 trans[0] = pose_frame.p[0];
569 trans[1] = pose_frame.p[1];
570 trans[2] = pose_frame.p[2];
573 KDL::Vector direction;
584 vals[0] = mult(0, 0);
585 vals[1] = mult(0, 1);
586 vals[2] = mult(0, 2);
587 vals[3] = mult(1, 0);
588 vals[4] = mult(1, 1);
589 vals[5] = mult(1, 2);
590 vals[6] = mult(2, 0);
591 vals[7] = mult(2, 1);
592 vals[8] = mult(2, 2);
595 ComputeIk(trans, vals, vfree.size() > 0 ? &vfree[0] : nullptr, solutions);
596 return solutions.GetNumSolutions();
604 direction = pose_frame.M * KDL::Vector(0, 0, 1);
605 ComputeIk(trans, direction.data, vfree.size() > 0 ? &vfree[0] : nullptr, solutions);
606 return solutions.GetNumSolutions();
611 RCLCPP_ERROR(getLogger(),
"IK for this IkParameterizationType not implemented yet.");
615 ComputeIk(trans, direction.data, vfree.size() > 0 ? &vfree[0] : nullptr, solutions);
616 return solutions.GetNumSolutions();
621 RCLCPP_ERROR(getLogger(),
"IK for this IkParameterizationType not implemented yet.");
629 double roll, pitch, yaw;
633 pose_frame.M.GetRPY(roll, pitch, yaw);
634 ComputeIk(trans, &yaw, vfree.size() > 0 ? &vfree[0] : nullptr, solutions);
635 return solutions.GetNumSolutions();
642 pose_frame.M.GetRPY(roll, pitch, yaw);
643 ComputeIk(trans, &roll, vfree.size() > 0 ? &vfree[0] : nullptr, solutions);
644 return solutions.GetNumSolutions();
651 pose_frame.M.GetRPY(roll, pitch, yaw);
652 ComputeIk(trans, &pitch, vfree.size() > 0 ? &vfree[0] : nullptr, solutions);
653 return solutions.GetNumSolutions();
656 RCLCPP_ERROR(getLogger(),
"Unknown IkParameterizationType! "
657 "Was the solver generated with an incompatible version of Openrave?");
662void IKFastKinematicsPlugin::getSolution(
const IkSolutionList<IkReal>& solutions,
int i,
663 std::vector<double>& solution)
const
666 solution.resize(num_joints_);
669 const IkSolutionBase<IkReal>& sol = solutions.GetSolution(i);
670 std::vector<IkReal> vsolfree(sol.GetFree().size());
671 sol.GetSolution(&solution[0], vsolfree.size() > 0 ? &vsolfree[0] : nullptr);
673 for (std::size_t joint_id = 0; joint_id < num_joints_; ++joint_id)
675 if (joint_has_limits_vector_[joint_id])
677 solution[joint_id] = enforceLimits(solution[joint_id], joint_min_vector_[joint_id], joint_max_vector_[joint_id]);
682void IKFastKinematicsPlugin::getSolution(
const IkSolutionList<IkReal>& solutions,
683 const std::vector<double>& ik_seed_state,
int i,
684 std::vector<double>& solution)
const
687 solution.resize(num_joints_);
690 const IkSolutionBase<IkReal>& sol = solutions.GetSolution(i);
691 std::vector<IkReal> vsolfree(sol.GetFree().size());
692 sol.GetSolution(&solution[0], vsolfree.size() > 0 ? &vsolfree[0] : nullptr);
695 for (std::size_t i = 0; i < num_joints_; ++i)
697 if (joint_has_limits_vector_[i])
699 solution[i] = enforceLimits(solution[i], joint_min_vector_[i], joint_max_vector_[i]);
700 double signed_distance = solution[i] - ik_seed_state[i];
701 while (signed_distance > M_PI && solution[i] - 2 * M_PI > (joint_min_vector_[i] -
LIMIT_TOLERANCE))
703 signed_distance -= 2 * M_PI;
704 solution[i] -= 2 * M_PI;
706 while (signed_distance < -M_PI && solution[i] + 2 * M_PI < (joint_max_vector_[i] +
LIMIT_TOLERANCE))
708 signed_distance += 2 * M_PI;
709 solution[i] += 2 * M_PI;
715double IKFastKinematicsPlugin::enforceLimits(
double joint_value,
double min,
double max)
const
718 while (joint_value > max)
720 joint_value -= 2 * M_PI;
724 while (joint_value < min)
726 joint_value += 2 * M_PI;
731void IKFastKinematicsPlugin::fillFreeParams(
int count,
int* array)
733 free_params_.clear();
734 for (
int i = 0; i < count; ++i)
735 free_params_.push_back(array[i]);
738bool IKFastKinematicsPlugin::getCount(
int& count,
int max_count,
int min_count)
const
742 if (-count >= min_count)
747 else if (count + 1 <= max_count)
759 if (1 - count <= max_count)
764 else if (count - 1 >= min_count)
775 const std::vector<double>& joint_angles,
776 std::vector<geometry_msgs::msg::Pose>& poses)
const
784 RCLCPP_ERROR(getLogger(),
"Can only compute FK for Transform6D IK type!");
789 if (link_names.size() == 0)
791 RCLCPP_WARN_STREAM(getLogger(),
"Link names with nothing");
795 if (link_names.size() != 1 || link_names[0] !=
getTipFrame())
797 RCLCPP_ERROR(getLogger(),
"Can compute FK for %s only",
getTipFrame().c_str());
803 IkReal eerot[9], eetrans[3];
805 if (joint_angles.size() != num_joints_)
807 RCLCPP_ERROR(getLogger(),
"Unexpected number of joint angles");
811#pragma clang diagnostic push
812#pragma clang diagnostic ignored "-Wvla-cxx-extension"
813 IkReal angles[num_joints_];
814#pragma clang diagnostic pop
815 for (
unsigned char i = 0; i < num_joints_; ++i)
816 angles[i] = joint_angles[i];
821 for (
int i = 0; i < 3; ++i)
822 p_out.p.data[i] = eetrans[i];
824 for (
int i = 0; i < 9; ++i)
825 p_out.M.data[i] = eerot[i];
828 poses[0] = tf2::toMsg(p_out);
834 const std::vector<double>& ik_seed_state,
double timeout,
835 std::vector<double>& solution,
836 moveit_msgs::msg::MoveItErrorCodes& error_code,
839 std::vector<double> consistency_limits;
845 const std::vector<double>& ik_seed_state,
double timeout,
846 const std::vector<double>& consistency_limits,
847 std::vector<double>& solution,
848 moveit_msgs::msg::MoveItErrorCodes& error_code,
856 const std::vector<double>& ik_seed_state,
double timeout,
857 std::vector<double>& solution,
const IKCallbackFn& solution_callback,
858 moveit_msgs::msg::MoveItErrorCodes& error_code,
861 std::vector<double> consistency_limits;
862 return searchPositionIK(ik_pose, ik_seed_state, timeout, consistency_limits, solution, solution_callback, error_code,
867 const std::vector<double>& ik_seed_state,
double timeout,
868 const std::vector<double>& consistency_limits,
869 std::vector<double>& solution,
const IKCallbackFn& solution_callback,
870 moveit_msgs::msg::MoveItErrorCodes& error_code,
877 if (free_params_.size() == 0)
879 RCLCPP_DEBUG_STREAM(getLogger(),
"No need to search since no free params/redundant joints");
881 std::vector<geometry_msgs::msg::Pose> ik_poses(1, ik_pose);
882 std::vector<std::vector<double>> solutions;
885 if (!
getPositionIK(ik_poses, ik_seed_state, solutions, kinematic_result, options))
887 RCLCPP_DEBUG(getLogger(),
"No solution whatsoever");
888 error_code.val = moveit_msgs::msg::MoveItErrorCodes::NO_IK_SOLUTION;
893 std::vector<LimitObeyingSol> solutions_obey_limits;
894 for (std::size_t i = 0; i < solutions.size(); ++i)
896 double dist_from_seed = 0.0;
897 for (std::size_t j = 0; j < ik_seed_state.size(); ++j)
899 dist_from_seed += fabs(ik_seed_state[j] - solutions[i][j]);
902 solutions_obey_limits.push_back({ solutions[i], dist_from_seed });
904 std::sort(solutions_obey_limits.begin(), solutions_obey_limits.end());
907 if (solution_callback)
909 for (std::size_t i = 0; i < solutions_obey_limits.size(); ++i)
911 solution_callback(ik_pose, solutions_obey_limits[i].value, error_code);
912 if (error_code.val == moveit_msgs::msg::MoveItErrorCodes::SUCCESS)
914 solution = solutions_obey_limits[i].value;
915 RCLCPP_DEBUG(getLogger(),
"Solution passes callback");
920 RCLCPP_DEBUG_STREAM(getLogger(),
"Solution has error code " << error_code.val);
925 solution = solutions_obey_limits[0].value;
926 error_code.val = moveit_msgs::msg::MoveItErrorCodes::SUCCESS;
935 RCLCPP_ERROR(getLogger(),
"Kinematics not active");
936 error_code.val = error_code.NO_IK_SOLUTION;
940 if (ik_seed_state.size() != num_joints_)
942 RCLCPP_ERROR_STREAM(getLogger(),
943 "Seed state must have size " << num_joints_ <<
" instead of size " << ik_seed_state.size());
944 error_code.val = error_code.NO_IK_SOLUTION;
948 if (!consistency_limits.empty() && consistency_limits.size() != num_joints_)
950 RCLCPP_ERROR_STREAM(getLogger(),
"Consistency limits be empty or must have size "
951 << num_joints_ <<
" instead of size " << consistency_limits.size());
952 error_code.val = error_code.NO_IK_SOLUTION;
960 transformToChainFrame(ik_pose, frame);
962 std::vector<double> vfree(free_params_.size());
966 double initial_guess = ik_seed_state[free_params_[0]];
967 vfree[0] = initial_guess;
971 int num_positive_increments;
972 int num_negative_increments;
975 if (!consistency_limits.empty())
979 double max_limit = fmin(joint_max_vector_[free_params_[0]], initial_guess + consistency_limits[free_params_[0]]);
980 double min_limit = fmax(joint_min_vector_[free_params_[0]], initial_guess - consistency_limits[free_params_[0]]);
982 num_positive_increments =
static_cast<int>((max_limit - initial_guess) / search_discretization);
983 num_negative_increments =
static_cast<int>((initial_guess - min_limit) / search_discretization);
987 num_positive_increments = (joint_max_vector_[free_params_[0]] - initial_guess) / search_discretization;
988 num_negative_increments = (initial_guess - joint_min_vector_[free_params_[0]]) / search_discretization;
994 RCLCPP_DEBUG_STREAM(getLogger(),
"Free param is " << free_params_[0] <<
" initial guess is " << initial_guess
995 <<
", # positive increments: " << num_positive_increments
996 <<
", # negative increments: " << num_negative_increments);
997 if ((search_mode &
OPTIMIZE_MAX_JOINT) && (num_positive_increments + num_negative_increments) > 1000)
998 RCLCPP_WARN_STREAM_ONCE(getLogger(),
"Large search space, consider increasing the search discretization");
1000 double best_costs = -1.0;
1001 std::vector<double> best_solution;
1002 int nattempts = 0, nvalid = 0;
1006 IkSolutionList<IkReal> solutions;
1007 size_t numsol = solve(frame, vfree, solutions);
1009 RCLCPP_DEBUG_STREAM(getLogger(),
"Found " << numsol <<
" solutions from IKFast");
1013 for (
size_t s = 0; s < numsol; ++s)
1016 std::vector<double> sol;
1017 getSolution(solutions, ik_seed_state, s, sol);
1019 bool obeys_limits =
true;
1020 for (
size_t i = 0; i < sol.size(); ++i)
1022 if (joint_has_limits_vector_[i] && (sol[i] < joint_min_vector_[i] || sol[i] > joint_max_vector_[i]))
1024 obeys_limits =
false;
1032 getSolution(solutions, ik_seed_state, s, solution);
1035 if (solution_callback)
1037 solution_callback(ik_pose, solution, error_code);
1041 error_code.val = error_code.SUCCESS;
1044 if (error_code.val == error_code.SUCCESS)
1051 for (
unsigned int i = 0; i < solution.size(); ++i)
1053 double d = fabs(ik_seed_state[i] - solution[i]);
1057 if (costs < best_costs || best_costs == -1.0)
1060 best_solution = solution;
1071 if (!getCount(counter, num_positive_increments, -num_negative_increments))
1074 error_code.val = moveit_msgs::msg::MoveItErrorCodes::NO_IK_SOLUTION;
1078 vfree[0] = initial_guess + search_discretization * counter;
1082 RCLCPP_DEBUG_STREAM(getLogger(),
"Valid solutions: " << nvalid <<
'/' << nattempts);
1086 solution = best_solution;
1087 error_code.val = error_code.SUCCESS;
1092 error_code.val = moveit_msgs::msg::MoveItErrorCodes::NO_IK_SOLUTION;
1098 const std::vector<double>& ik_seed_state, std::vector<double>& solution,
1099 moveit_msgs::msg::MoveItErrorCodes& error_code,
1102 RCLCPP_DEBUG_STREAM(getLogger(),
"getPositionIK");
1106 RCLCPP_ERROR(getLogger(),
"kinematics not active");
1110 if (ik_seed_state.size() < num_joints_)
1112 RCLCPP_ERROR_STREAM(getLogger(),
"ik_seed_state only has " << ik_seed_state.size()
1113 <<
" entries, this ikfast solver requires "
1119 for (std::size_t i = 0; i < ik_seed_state.size(); ++i)
1122 if (joint_has_limits_vector_[i] && ((ik_seed_state[i] < (joint_min_vector_[i] -
LIMIT_TOLERANCE)) ||
1125 RCLCPP_DEBUG_STREAM(getLogger(),
"IK seed not in limits! " <<
static_cast<int>(i) <<
" value " << ik_seed_state[i]
1126 <<
" has limit: " << joint_has_limits_vector_[i]
1127 <<
" being " << joint_min_vector_[i] <<
" to "
1128 << joint_max_vector_[i]);
1133 std::vector<double> vfree(free_params_.size());
1134 for (std::size_t i = 0; i < free_params_.size(); ++i)
1136 int p = free_params_[i];
1137 RCLCPP_ERROR(getLogger(),
"%u is %f", p, ik_seed_state[p]);
1138 vfree[i] = ik_seed_state[p];
1142 transformToChainFrame(ik_pose, frame);
1144 IkSolutionList<IkReal> solutions;
1145 size_t numsol = solve(frame, vfree, solutions);
1146 RCLCPP_DEBUG_STREAM(getLogger(),
"Found " << numsol <<
" solutions from IKFast");
1148 std::vector<LimitObeyingSol> solutions_obey_limits;
1152 std::vector<double> solution_obey_limits;
1153 for (std::size_t s = 0; s < numsol; ++s)
1155 std::vector<double> sol;
1156 getSolution(solutions, ik_seed_state, s, sol);
1157 RCLCPP_DEBUG(getLogger(),
"Sol %d: %e %e %e %e %e %e",
static_cast<int>(s), sol[0], sol[1], sol[2],
1158 sol[3], sol[4], sol[5]);
1160 bool obeys_limits =
true;
1161 for (std::size_t i = 0; i < sol.size(); ++i)
1164 if (joint_has_limits_vector_[i] && ((sol[i] < (joint_min_vector_[i] -
LIMIT_TOLERANCE)) ||
1168 obeys_limits =
false;
1169 RCLCPP_DEBUG_STREAM(getLogger(),
"Not in limits! " <<
static_cast<int>(i) <<
" value " << sol[i]
1170 <<
" has limit: " << joint_has_limits_vector_[i]
1171 <<
" being " << joint_min_vector_[i] <<
" to "
1172 << joint_max_vector_[i]);
1179 getSolution(solutions, ik_seed_state, s, solution_obey_limits);
1180 double dist_from_seed = 0.0;
1181 for (std::size_t i = 0; i < ik_seed_state.size(); ++i)
1183 dist_from_seed += fabs(ik_seed_state[i] - solution_obey_limits[i]);
1186 solutions_obey_limits.push_back({ solution_obey_limits, dist_from_seed });
1192 RCLCPP_DEBUG(getLogger(),
"No IK solution");
1196 if (!solutions_obey_limits.empty())
1198 std::sort(solutions_obey_limits.begin(), solutions_obey_limits.end());
1199 solution = solutions_obey_limits[0].value;
1200 error_code.val = moveit_msgs::msg::MoveItErrorCodes::SUCCESS;
1204 error_code.val = moveit_msgs::msg::MoveItErrorCodes::NO_IK_SOLUTION;
1209 const std::vector<double>& ik_seed_state,
1210 std::vector<std::vector<double>>& solutions,
1214 RCLCPP_DEBUG_STREAM(getLogger(),
"getPositionIK with multiple solutions");
1218 RCLCPP_ERROR(getLogger(),
"kinematics not active");
1223 if (ik_poses.empty())
1225 RCLCPP_ERROR(getLogger(),
"ik_poses is empty");
1230 if (ik_poses.size() > 1)
1232 RCLCPP_ERROR(getLogger(),
"ik_poses contains multiple entries, only one is allowed");
1237 if (ik_seed_state.size() < num_joints_)
1239 RCLCPP_ERROR_STREAM(getLogger(),
"ik_seed_state only has " << ik_seed_state.size()
1240 <<
" entries, this ikfast solver requires "
1246 transformToChainFrame(ik_poses[0], frame);
1249 std::vector<IkSolutionList<IkReal>> solution_set;
1250 IkSolutionList<IkReal> ik_solutions;
1251 std::vector<double> vfree;
1253 std::vector<double> sampled_joint_vals;
1266 double jv = sampled_joint_vals[0];
1270 RCLCPP_ERROR_STREAM(getLogger(),
"ik seed is out of bounds");
1276 if (!sampleRedundantJoint(options.discretization_method, sampled_joint_vals))
1282 for (
unsigned int i = 0; i < sampled_joint_vals.size(); ++i)
1285 vfree.push_back(sampled_joint_vals[i]);
1286 numsol += solve(frame, vfree, ik_solutions);
1287 solution_set.push_back(ik_solutions);
1293 numsol = solve(frame, vfree, ik_solutions);
1294 solution_set.push_back(ik_solutions);
1297 RCLCPP_DEBUG_STREAM(getLogger(),
"Found " << numsol <<
" solutions from IKFast");
1298 bool solutions_found =
false;
1304 for (
unsigned int r = 0; r < solution_set.size(); ++r)
1306 ik_solutions = solution_set[r];
1307 numsol = ik_solutions.GetNumSolutions();
1308 for (
int s = 0; s < numsol; ++s)
1310 std::vector<double> sol;
1311 getSolution(ik_solutions, ik_seed_state, s, sol);
1313 bool obeys_limits =
true;
1314 for (
unsigned int i = 0; i < sol.size(); ++i)
1317 if (joint_has_limits_vector_[i] && ((sol[i] < (joint_min_vector_[i] -
LIMIT_TOLERANCE)) ||
1321 obeys_limits =
false;
1322 RCLCPP_DEBUG_STREAM(getLogger(),
"Not in limits! " << i <<
" value " << sol[i]
1323 <<
" has limit: " << joint_has_limits_vector_[i]
1324 <<
" being " << joint_min_vector_[i] <<
" to "
1325 << joint_max_vector_[i]);
1332 solutions_found =
true;
1333 solutions.push_back(sol);
1338 if (solutions_found)
1346 RCLCPP_DEBUG_STREAM(getLogger(),
"No IK solution");
1354 std::vector<double>& sampled_joint_vals)
const
1358 double joint_min = joint_min_vector_[index];
1359 double joint_max = joint_max_vector_[index];
1365 size_t steps = std::ceil((joint_max - joint_min) / joint_dscrt);
1366 for (
size_t i = 0; i < steps; ++i)
1368 sampled_joint_vals.push_back(joint_min + joint_dscrt * i);
1370 sampled_joint_vals.push_back(joint_max);
1375 int steps = std::ceil((joint_max - joint_min) / joint_dscrt);
1376 steps = steps > 0 ? steps : 1;
1377 double diff = joint_max - joint_min;
1378 for (
int i = 0; i < steps; ++i)
1380 sampled_joint_vals.push_back(((diff * std::rand()) / (
static_cast<double>(RAND_MAX))) + joint_min);
1389 RCLCPP_ERROR_STREAM(
getLogger(),
"Discretization method " << method <<
" is not supported");
1396void IKFastKinematicsPlugin::transformToChainFrame(
const geometry_msgs::msg::Pose& ik_pose,
1397 KDL::Frame& ik_pose_chain)
const
1399 if (tip_transform_required_ || base_transform_required_)
1401 Eigen::Isometry3d ik_eigen_pose;
1402 tf2::fromMsg(ik_pose, ik_eigen_pose);
1403 if (tip_transform_required_)
1404 ik_eigen_pose = ik_eigen_pose * group_tip_to_chain_tip_;
1406 if (base_transform_required_)
1407 ik_eigen_pose = chain_base_to_group_base_ * ik_eigen_pose;
1409 tf2::fromMsg(tf2::toMsg(ik_eigen_pose), ik_pose_chain);
1413 tf2::fromMsg(ik_pose, ik_pose_chain);