19 #include "../include/open_manipulator_libs/kinematics.h" 28 void SolverUsingCRAndJacobian::setOption(
const void *arg){}
30 Eigen::MatrixXd SolverUsingCRAndJacobian::jacobian(
Manipulator *manipulator,
Name tool_name)
32 Eigen::MatrixXd jacobian = Eigen::MatrixXd::Identity(6, manipulator->
getDOF());
34 Eigen::Vector3d joint_axis = Eigen::Vector3d::Zero(3);
36 Eigen::Vector3d position_changed = Eigen::Vector3d::Zero(3);
37 Eigen::Vector3d orientation_changed = Eigen::Vector3d::Zero(3);
38 Eigen::VectorXd pose_changed = Eigen::VectorXd::Zero(6);
45 for (int8_t size = 0; size < manipulator->
getDOF(); size++)
59 orientation_changed = joint_axis;
61 pose_changed << position_changed(0),
64 orientation_changed(0),
65 orientation_changed(1),
66 orientation_changed(2);
68 jacobian.col(index) = pose_changed;
75 void SolverUsingCRAndJacobian::solveForwardKinematics(
Manipulator *manipulator)
80 bool SolverUsingCRAndJacobian::solveInverseKinematics(
Manipulator *manipulator,
Name tool_name,
Pose target_pose, std::vector<JointValue> *goal_joint_value)
82 return inverseSolverUsingJacobian(manipulator, tool_name, target_pose, goal_joint_value);
87 void SolverUsingCRAndJacobian::forwardSolverUsingChainRule(
Manipulator *manipulator,
Name component_name)
89 Name my_name = component_name;
93 Pose parent_pose_value;
122 for (int8_t index = 0; index < number_of_child; index++)
125 forwardSolverUsingChainRule(manipulator, child_name);
129 bool SolverUsingCRAndJacobian::inverseSolverUsingJacobian(
Manipulator *manipulator,
Name tool_name,
Pose target_pose, std::vector<JointValue> *goal_joint_value)
131 const double lambda = 0.7;
132 const int8_t iteration = 10;
136 Eigen::MatrixXd jacobian = Eigen::MatrixXd::Identity(6, _manipulator.
getDOF());
138 Eigen::VectorXd pose_changed = Eigen::VectorXd::Zero(6);
139 Eigen::VectorXd delta_angle = Eigen::VectorXd::Zero(_manipulator.
getDOF());
141 for (int8_t count = 0; count < iteration; count++)
144 solveForwardKinematics(&_manipulator);
146 jacobian = this->jacobian(&_manipulator, tool_name);
153 if (pose_changed.norm() < 1E-6)
156 for(int8_t index = 0; index < _manipulator.
getDOF(); index++)
158 goal_joint_value->at(index).velocity = 0.0;
159 goal_joint_value->at(index).acceleration = 0.0;
160 goal_joint_value->at(index).effort = 0.0;
166 ColPivHouseholderQR<MatrixXd> dec(jacobian);
167 delta_angle = lambda * dec.solve(pose_changed);
170 std::vector<double> changed_angle;
171 for(int8_t index = 0; index < _manipulator.
getDOF(); index++)
175 *goal_joint_value = {};
183 void SolverUsingCRAndSRJacobian::setOption(
const void *arg){}
185 Eigen::MatrixXd SolverUsingCRAndSRJacobian::jacobian(
Manipulator *manipulator,
Name tool_name)
187 Eigen::MatrixXd jacobian = Eigen::MatrixXd::Identity(6, manipulator->
getDOF());
189 Eigen::Vector3d joint_axis = Eigen::Vector3d::Zero(3);
191 Eigen::Vector3d position_changed = Eigen::Vector3d::Zero(3);
192 Eigen::Vector3d orientation_changed = Eigen::Vector3d::Zero(3);
193 Eigen::VectorXd pose_changed = Eigen::VectorXd::Zero(6);
200 for (int8_t size = 0; size < manipulator->
getDOF(); size++)
214 orientation_changed = joint_axis;
216 pose_changed << position_changed(0),
219 orientation_changed(0),
220 orientation_changed(1),
221 orientation_changed(2);
223 jacobian.col(index) = pose_changed;
230 void SolverUsingCRAndSRJacobian::solveForwardKinematics(
Manipulator *manipulator)
235 bool SolverUsingCRAndSRJacobian::solveInverseKinematics(
Manipulator *manipulator,
Name tool_name,
Pose target_pose, std::vector<JointValue> *goal_joint_value)
237 return inverseSolverUsingSRJacobian(manipulator, tool_name, target_pose, goal_joint_value);
242 void SolverUsingCRAndSRJacobian::forwardSolverUsingChainRule(
Manipulator *manipulator,
Name component_name)
244 Name my_name = component_name;
248 Pose parent_pose_value;
277 for (int8_t index = 0; index < number_of_child; index++)
280 forwardSolverUsingChainRule(manipulator, child_name);
284 bool SolverUsingCRAndSRJacobian::inverseSolverUsingSRJacobian(
Manipulator *manipulator,
Name tool_name,
Pose target_pose, std::vector<JointValue> *goal_joint_value)
291 const double param = 0.002;
292 const int8_t iteration = 10;
294 const double gamma = 0.5;
297 double wn_pos = 1 / 0.3;
298 double wn_ang = 1 / (2 * M_PI);
302 Eigen::MatrixXd We(6, 6);
303 We << wn_pos, 0, 0, 0, 0, 0,
304 0, wn_pos, 0, 0, 0, 0,
305 0, 0, wn_pos, 0, 0, 0,
306 0, 0, 0, wn_ang, 0, 0,
307 0, 0, 0, 0, wn_ang, 0,
308 0, 0, 0, 0, 0, wn_ang;
310 Eigen::MatrixXd Wn = Eigen::MatrixXd::Identity(_manipulator.
getDOF(), _manipulator.
getDOF());
313 Eigen::MatrixXd jacobian = Eigen::MatrixXd::Identity(6, _manipulator.
getDOF());
314 Eigen::MatrixXd sr_jacobian = Eigen::MatrixXd::Identity(_manipulator.
getDOF(), _manipulator.
getDOF());
317 Eigen::VectorXd pose_changed = Eigen::VectorXd::Zero(6);
318 Eigen::VectorXd angle_changed = Eigen::VectorXd::Zero(_manipulator.
getDOF());
319 Eigen::VectorXd gerr(_manipulator.
getDOF());
322 std::vector<double> present_angle;
323 std::vector<double> set_angle;
327 solveForwardKinematics(&_manipulator);
330 pre_Ek = pose_changed.transpose() * We * pose_changed;
334 #if defined(KINEMATICS_DEBUG) 335 Eigen::Vector3d target_orientation_rpy = math::convertRotationToRPY(target_pose.orientation);
336 Eigen::VectorXd debug_target_pose(6);
337 for(
int t=0; t<3; t++)
338 debug_target_pose(t) = target_pose.position(t);
339 for(
int t=0; t<3; t++)
340 debug_target_pose(t+3) = target_orientation_rpy(t);
344 Eigen::Vector3d present_orientation_rpy = math::convertRotationToRPY(present_orientation);
345 Eigen::VectorXd debug_present_pose(6);
346 for(
int t=0; t<3; t++)
347 debug_present_pose(t) = present_position(t);
348 for(
int t=0; t<3; t++)
349 debug_present_pose(t+3) = present_orientation_rpy(t);
353 log::warn(
"Ek : ", pre_Ek*1000000000000);
355 log::println_VECTOR(debug_target_pose,16);
357 log::println_VECTOR(debug_present_pose,16);
359 log::println_VECTOR(debug_target_pose-debug_present_pose,16);
364 for (int8_t count = 0; count < iteration; count++)
367 jacobian = this->jacobian(&_manipulator, tool_name);
368 lambda = pre_Ek + param;
370 sr_jacobian = (jacobian.transpose() * We * jacobian) + (lambda * Wn);
371 gerr = jacobian.transpose() * We * pose_changed;
373 ColPivHouseholderQR<Eigen::MatrixXd> dec(sr_jacobian);
374 angle_changed = dec.solve(gerr);
378 for (int8_t index = 0; index < _manipulator.
getDOF(); index++)
379 set_angle.push_back(present_angle.at(index) + angle_changed(index));
381 solveForwardKinematics(&_manipulator);
386 new_Ek = pose_changed.transpose() * We * pose_changed;
390 #if defined(KINEMATICS_DEBUG) 393 present_orientation_rpy = math::convertRotationToRPY(present_orientation);
394 for(
int t=0; t<3; t++)
395 debug_present_pose(t) = present_position(t);
396 for(
int t=0; t<3; t++)
397 debug_present_pose(t+3) = present_orientation_rpy(t);
399 log::warn(
"Ek : ", new_Ek*1000000000000);
401 log::println_VECTOR(debug_target_pose,16);
403 log::println_VECTOR(debug_present_pose,16);
405 log::println_VECTOR(debug_target_pose-debug_present_pose,16);
412 #if defined(KINEMATICS_DEBUG) 414 log::warn(
"Ek : ", new_Ek*1000000000000);
420 for(int8_t index = 0; index < _manipulator.
getDOF(); index++)
422 goal_joint_value->at(index).velocity = 0.0;
423 goal_joint_value->at(index).acceleration = 0.0;
424 goal_joint_value->at(index).effort = 0.0;
428 else if (new_Ek < pre_Ek)
435 for (int8_t index = 0; index < _manipulator.
getDOF(); index++)
436 set_angle.push_back(present_angle.at(index) - (gamma * angle_changed(index)));
439 solveForwardKinematics(&_manipulator);
442 log::error(
"[sr]fail to solve inverse kinematics (please change the solver)");
443 *goal_joint_value = {};
451 void SolverUsingCRAndSRPositionOnlyJacobian::setOption(
const void *arg){}
453 Eigen::MatrixXd SolverUsingCRAndSRPositionOnlyJacobian::jacobian(
Manipulator *manipulator,
Name tool_name)
455 Eigen::MatrixXd jacobian = Eigen::MatrixXd::Identity(6, manipulator->
getDOF());
457 Eigen::Vector3d joint_axis = Eigen::Vector3d::Zero(3);
459 Eigen::Vector3d position_changed = Eigen::Vector3d::Zero(3);
460 Eigen::Vector3d orientation_changed = Eigen::Vector3d::Zero(3);
461 Eigen::VectorXd pose_changed = Eigen::VectorXd::Zero(6);
468 for (int8_t size = 0; size < manipulator->
getDOF(); size++)
482 orientation_changed = joint_axis;
484 pose_changed << position_changed(0),
487 orientation_changed(0),
488 orientation_changed(1),
489 orientation_changed(2);
491 jacobian.col(index) = pose_changed;
498 void SolverUsingCRAndSRPositionOnlyJacobian::solveForwardKinematics(
Manipulator *manipulator)
503 bool SolverUsingCRAndSRPositionOnlyJacobian::solveInverseKinematics(
Manipulator *manipulator,
Name tool_name,
Pose target_pose, std::vector<JointValue> *goal_joint_value)
505 return inverseSolverUsingPositionOnlySRJacobian(manipulator, tool_name, target_pose, goal_joint_value);
510 void SolverUsingCRAndSRPositionOnlyJacobian::forwardSolverUsingChainRule(
Manipulator *manipulator,
Name component_name)
512 Name my_name = component_name;
516 Pose parent_pose_value;
545 for (int8_t index = 0; index < number_of_child; index++)
548 forwardSolverUsingChainRule(manipulator, child_name);
552 bool SolverUsingCRAndSRPositionOnlyJacobian::inverseSolverUsingPositionOnlySRJacobian(
Manipulator *manipulator,
Name tool_name,
Pose target_pose, std::vector<JointValue> *goal_joint_value)
559 const double param = 0.002;
560 const int8_t iteration = 10;
562 const double gamma = 0.5;
565 Eigen::MatrixXd jacobian = Eigen::MatrixXd::Identity(6, _manipulator.
getDOF());
566 Eigen::MatrixXd position_jacobian = Eigen::MatrixXd::Identity(3, _manipulator.
getDOF());
567 Eigen::MatrixXd sr_jacobian = Eigen::MatrixXd::Identity(_manipulator.
getDOF(), _manipulator.
getDOF());
570 Eigen::Vector3d position_changed = Eigen::VectorXd::Zero(3);
571 Eigen::VectorXd angle_changed = Eigen::VectorXd::Zero(_manipulator.
getDOF());
572 Eigen::VectorXd gerr(_manipulator.
getDOF());
575 double wn_pos = 1 / 0.3;
579 Eigen::MatrixXd We(3, 3);
584 Eigen::MatrixXd Wn = Eigen::MatrixXd::Identity(_manipulator.
getDOF(), _manipulator.
getDOF());
587 std::vector<double> present_angle;
588 std::vector<double> set_angle;
592 solveForwardKinematics(&_manipulator);
595 pre_Ek = position_changed.transpose() * We * position_changed;
599 #if defined(KINEMATICS_DEBUG) 600 Eigen::Vector3d target_orientation_rpy = math::convertRotationToRPY(target_pose.orientation);
601 Eigen::VectorXd debug_target_pose(6);
602 for(
int t=0; t<3; t++)
603 debug_target_pose(t) = target_pose.position(t);
604 for(
int t=0; t<3; t++)
605 debug_target_pose(t+3) = target_orientation_rpy(t);
609 Eigen::Vector3d present_orientation_rpy = math::convertRotationToRPY(present_orientation);
610 Eigen::VectorXd debug_present_pose(6);
611 for(
int t=0; t<3; t++)
612 debug_present_pose(t) = present_position(t);
613 for(
int t=0; t<3; t++)
614 debug_present_pose(t+3) = present_orientation_rpy(t);
618 log::warn(
"Ek : ", pre_Ek*1000000000000);
620 log::println_VECTOR(debug_target_pose,16);
622 log::println_VECTOR(debug_present_pose,16);
624 log::println_VECTOR(debug_target_pose-debug_present_pose,16);
629 for (int8_t count = 0; count < iteration; count++)
632 jacobian = this->jacobian(&_manipulator, tool_name);
633 position_jacobian.row(0) = jacobian.row(0);
634 position_jacobian.row(1) = jacobian.row(1);
635 position_jacobian.row(2) = jacobian.row(2);
636 lambda = pre_Ek + param;
638 sr_jacobian = (position_jacobian.transpose() * We * position_jacobian) + (lambda * Wn);
639 gerr = position_jacobian.transpose() * We * position_changed;
641 ColPivHouseholderQR<Eigen::MatrixXd> dec(sr_jacobian);
642 angle_changed = dec.solve(gerr);
646 for (int8_t index = 0; index < _manipulator.
getDOF(); index++)
649 solveForwardKinematics(&_manipulator);
654 new_Ek = position_changed.transpose() * We * position_changed;
658 #if defined(KINEMATICS_DEBUG) 661 present_orientation_rpy = math::convertRotationToRPY(present_orientation);
662 for(
int t=0; t<3; t++)
663 debug_present_pose(t) = present_position(t);
664 for(
int t=0; t<3; t++)
665 debug_present_pose(t+3) = present_orientation_rpy(t);
667 log::warn(
"Ek : ", new_Ek*1000000000000);
669 log::println_VECTOR(debug_target_pose,16);
671 log::println_VECTOR(debug_present_pose,16);
673 log::println_VECTOR(debug_target_pose-debug_present_pose,16);
680 #if defined(KINEMATICS_DEBUG) 682 log::warn(
"Ek : ", new_Ek*1000000000000);
688 for(int8_t index = 0; index < _manipulator.
getDOF(); index++)
690 goal_joint_value->at(index).velocity = 0.0;
691 goal_joint_value->at(index).acceleration = 0.0;
692 goal_joint_value->at(index).effort = 0.0;
696 else if (new_Ek < pre_Ek)
703 for (int8_t index = 0; index < _manipulator.
getDOF(); index++)
707 solveForwardKinematics(&_manipulator);
710 log::error(
"[position_only]fail to solve inverse kinematics (please change the solver)");
711 *goal_joint_value = {};
719 void SolverCustomizedforOMChain::setOption(
const void *arg){}
721 Eigen::MatrixXd SolverCustomizedforOMChain::jacobian(
Manipulator *manipulator,
Name tool_name)
723 Eigen::MatrixXd jacobian = Eigen::MatrixXd::Identity(6, manipulator->
getDOF());
725 Eigen::Vector3d joint_axis = Eigen::Vector3d::Zero(3);
727 Eigen::Vector3d position_changed = Eigen::Vector3d::Zero(3);
728 Eigen::Vector3d orientation_changed = Eigen::Vector3d::Zero(3);
729 Eigen::VectorXd pose_changed = Eigen::VectorXd::Zero(6);
736 for (int8_t size = 0; size < manipulator->
getDOF(); size++)
750 orientation_changed = joint_axis;
752 pose_changed << position_changed(0),
755 orientation_changed(0),
756 orientation_changed(1),
757 orientation_changed(2);
759 jacobian.col(index) = pose_changed;
766 void SolverCustomizedforOMChain::solveForwardKinematics(
Manipulator *manipulator)
771 bool SolverCustomizedforOMChain::solveInverseKinematics(
Manipulator *manipulator,
Name tool_name,
Pose target_pose, std::vector<JointValue> *goal_joint_value)
773 return chainCustomInverseKinematics(manipulator, tool_name, target_pose, goal_joint_value);
778 void SolverCustomizedforOMChain::forwardSolverUsingChainRule(
Manipulator *manipulator,
Name component_name)
780 Name my_name = component_name;
784 Pose parent_pose_value;
813 for (int8_t index = 0; index < number_of_child; index++)
816 forwardSolverUsingChainRule(manipulator, child_name);
819 bool SolverCustomizedforOMChain::chainCustomInverseKinematics(
Manipulator *manipulator,
Name tool_name,
Pose target_pose, std::vector<JointValue> *goal_joint_value)
826 const double param = 0.002;
827 const int8_t iteration = 10;
829 const double gamma = 0.5;
832 double wn_pos = 1 / 0.3;
833 double wn_ang = 1 / (2 * M_PI);
837 Eigen::MatrixXd We(6, 6);
838 We << wn_pos, 0, 0, 0, 0, 0,
839 0, wn_pos, 0, 0, 0, 0,
840 0, 0, wn_pos, 0, 0, 0,
841 0, 0, 0, wn_ang, 0, 0,
842 0, 0, 0, 0, wn_ang, 0,
843 0, 0, 0, 0, 0, wn_ang;
845 Eigen::MatrixXd Wn = Eigen::MatrixXd::Identity(_manipulator.
getDOF(), _manipulator.
getDOF());
848 Eigen::MatrixXd jacobian = Eigen::MatrixXd::Identity(6, _manipulator.
getDOF());
849 Eigen::MatrixXd sr_jacobian = Eigen::MatrixXd::Identity(_manipulator.
getDOF(), _manipulator.
getDOF());
852 Eigen::VectorXd pose_changed = Eigen::VectorXd::Zero(6);
853 Eigen::VectorXd angle_changed = Eigen::VectorXd::Zero(_manipulator.
getDOF());
854 Eigen::VectorXd gerr(_manipulator.
getDOF());
857 std::vector<double> present_angle;
858 std::vector<double> set_angle;
862 solveForwardKinematics(&_manipulator);
871 Eigen::Vector3d target_position_from_joint1 = target_pose.
kinematic.
position - joint1_rlative_position;
873 target_orientation_rpy(0) = present_orientation_rpy(0);
874 target_orientation_rpy(1) = target_orientation_rpy(1);
875 target_orientation_rpy(2) = atan2(target_position_from_joint1(1) ,target_position_from_joint1(0));
882 pre_Ek = pose_changed.transpose() * We * pose_changed;
886 #if defined(KINEMATICS_DEBUG) 887 Eigen::VectorXd debug_target_pose(6);
888 for(
int t=0; t<3; t++)
889 debug_target_pose(t) = target_pose.position(t);
890 for(
int t=0; t<3; t++)
891 debug_target_pose(t+3) = target_orientation_rpy(t);
894 Eigen::VectorXd debug_present_pose(6);
895 for(
int t=0; t<3; t++)
896 debug_present_pose(t) = present_position(t);
897 for(
int t=0; t<3; t++)
898 debug_present_pose(t+3) = present_orientation_rpy(t);
902 log::warn(
"Ek : ", pre_Ek*1000000000000);
904 log::println_VECTOR(debug_target_pose,16);
906 log::println_VECTOR(debug_present_pose,16);
908 log::println_VECTOR(debug_target_pose-debug_present_pose,16);
913 for (int8_t count = 0; count < iteration; count++)
916 jacobian = this->jacobian(&_manipulator, tool_name);
917 lambda = pre_Ek + param;
919 sr_jacobian = (jacobian.transpose() * We * jacobian) + (lambda * Wn);
920 gerr = jacobian.transpose() * We * pose_changed;
922 ColPivHouseholderQR<Eigen::MatrixXd> dec(sr_jacobian);
923 angle_changed = dec.solve(gerr);
927 for (int8_t index = 0; index < _manipulator.
getDOF(); index++)
928 set_angle.push_back(present_angle.at(index) + angle_changed(index));
930 solveForwardKinematics(&_manipulator);
935 new_Ek = pose_changed.transpose() * We * pose_changed;
939 #if defined(KINEMATICS_DEBUG) 942 present_orientation_rpy = math::convertRotationToRPY(present_orientation);
943 for(
int t=0; t<3; t++)
944 debug_present_pose(t) = present_position(t);
945 for(
int t=0; t<3; t++)
946 debug_present_pose(t+3) = present_orientation_rpy(t);
948 log::warn(
"Ek : ", new_Ek*1000000000000);
950 log::println_VECTOR(debug_target_pose,16);
952 log::println_VECTOR(debug_present_pose,16);
954 log::println_VECTOR(debug_target_pose-debug_present_pose,16);
961 #if defined(KINEMATICS_DEBUG) 963 log::warn(
"Ek : ", new_Ek*1000000000000);
969 for(int8_t index = 0; index < _manipulator.
getDOF(); index++)
971 goal_joint_value->at(index).velocity = 0.0;
972 goal_joint_value->at(index).acceleration = 0.0;
973 goal_joint_value->at(index).effort = 0.0;
977 else if (new_Ek < pre_Ek)
984 for (int8_t index = 0; index < _manipulator.
getDOF(); index++)
985 set_angle.push_back(present_angle.at(index) - (gamma * angle_changed(index)));
988 solveForwardKinematics(&_manipulator);
991 log::error(
"[OpenManipulator Chain Custom]fail to solve inverse kinematics");
992 *goal_joint_value = {};
double getJointPosition(Name component_name)
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val)
Pose getComponentPoseFromWorld(Name component_name)
void setAllActiveJointPosition(std::vector< double > joint_position_vector)
Eigen::Matrix3d getComponentOrientationFromWorld(Name component_name)
std::vector< double > getAllActiveJointPosition()
Eigen::Vector3d getAxis(Name component_name)
Eigen::Vector3d getComponentRelativePositionFromParent(Name component_name)
Eigen::VectorXd poseDifference(Eigen::Vector3d desired_position, Eigen::Vector3d present_position, Eigen::Matrix3d desired_orientation, Eigen::Matrix3d present_orientation)
Name getComponentParentName(Name component_name)
void println(STRING str, STRING color="DEFAULT")
std::vector< JointValue > getAllActiveJointValue()
Eigen::Vector3d vector3(double v1, double v2, double v3)
Eigen::Matrix3d skewSymmetricMatrix(Eigen::Vector3d v)
Eigen::Vector3d positionDifference(Eigen::Vector3d desired_position, Eigen::Vector3d present_position)
void setComponentPoseFromWorld(Name component_name, Pose pose_to_world)
Eigen::Matrix3d rodriguesRotationMatrix(Eigen::Vector3d axis, double angle)
std::vector< Name > getComponentChildName(Name component_name)
Eigen::Matrix3d getWorldOrientation()
Eigen::Vector3d acceleration
Eigen::Vector3d convertRotationMatrixToRPYVector(const Eigen::Matrix3d &rotation_matrix)
Eigen::Vector3d getComponentPositionFromWorld(Name component_name)
Eigen::Matrix3d orientation
Eigen::Matrix3d convertRPYToRotationMatrix(double roll, double pitch, double yaw)