kinematics.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2019 ROBOTIS CO., LTD.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16 
17 /* Authors: Darby Lim, Hye-Jong KIM, Ryan Shim, Yong-Ho Na */
18 
19 #include "../include/open_manipulator_p_libs/kinematics.h"
20 
21 using namespace robotis_manipulator;
22 using namespace kinematics;
23 
24 
25 /*****************************************************************************
26 ** Kinematics Solver Using Chain Rule and Jacobian
27 *****************************************************************************/
28 void SolverUsingCRAndJacobian::setOption(const void *arg){}
29 
30 Eigen::MatrixXd SolverUsingCRAndJacobian::jacobian(Manipulator *manipulator, Name tool_name)
31 {
32  Eigen::MatrixXd jacobian = Eigen::MatrixXd::Identity(6, manipulator->getDOF());
33 
34  Eigen::Vector3d joint_axis = Eigen::Vector3d::Zero(3);
35 
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);
39 
41 
42  int8_t index = 0;
43  Name my_name = manipulator->getWorldChildName();
44 
45  for (int8_t size = 0; size < manipulator->getDOF(); size++)
46  {
47  Name parent_name = manipulator->getComponentParentName(my_name);
48  if (parent_name == manipulator->getWorldName())
49  {
50  joint_axis = manipulator->getWorldOrientation() * manipulator->getAxis(my_name);
51  }
52  else
53  {
54  joint_axis = manipulator->getComponentOrientationFromWorld(parent_name) * manipulator->getAxis(my_name);
55  }
56 
57  position_changed = math::skewSymmetricMatrix(joint_axis) *
58  (manipulator->getComponentPositionFromWorld(tool_name) - manipulator->getComponentPositionFromWorld(my_name));
59  orientation_changed = joint_axis;
60 
61  pose_changed << position_changed(0),
62  position_changed(1),
63  position_changed(2),
64  orientation_changed(0),
65  orientation_changed(1),
66  orientation_changed(2);
67 
68  jacobian.col(index) = pose_changed;
69  index++;
70  my_name = manipulator->getComponentChildName(my_name).at(0); // Get Child name which has active joint
71  }
72  return jacobian;
73 }
74 
75 void SolverUsingCRAndJacobian::solveForwardKinematics(Manipulator *manipulator)
76 {
77  forwardSolverUsingChainRule(manipulator, manipulator->getWorldChildName());
78 }
79 
80 bool SolverUsingCRAndJacobian::solveInverseKinematics(Manipulator *manipulator, Name tool_name, Pose target_pose, std::vector<JointValue> *goal_joint_value)
81 {
82  return inverseSolverUsingJacobian(manipulator, tool_name, target_pose, goal_joint_value);
83 }
84 
85 
86 //private
87 void SolverUsingCRAndJacobian::forwardSolverUsingChainRule(Manipulator *manipulator, Name component_name)
88 {
89  Name my_name = component_name;
90  Name parent_name = manipulator->getComponentParentName(my_name);
91  int8_t number_of_child = manipulator->getComponentChildName(my_name).size();
92 
93  Pose parent_pose_value;
94  Pose my_pose_value;
95 
96  //Get Parent Pose
97  if (parent_name == manipulator->getWorldName())
98  {
99  parent_pose_value = manipulator->getWorldPose();
100  }
101  else
102  {
103  parent_pose_value = manipulator->getComponentPoseFromWorld(parent_name);
104  }
105 
106  //position
107  my_pose_value.kinematic.position = parent_pose_value.kinematic.position
108  + (parent_pose_value.kinematic.orientation * manipulator->getComponentRelativePositionFromParent(my_name));
109  //orientation
110  my_pose_value.kinematic.orientation = parent_pose_value.kinematic.orientation * manipulator->getComponentRelativeOrientationFromParent(my_name) * math::rodriguesRotationMatrix(manipulator->getAxis(my_name), manipulator->getJointPosition(my_name));
111  //linear velocity
112  my_pose_value.dynamic.linear.velocity = math::vector3(0.0, 0.0, 0.0);
113  //angular velocity
114  my_pose_value.dynamic.angular.velocity = math::vector3(0.0, 0.0, 0.0);
115  //linear acceleration
116  my_pose_value.dynamic.linear.acceleration = math::vector3(0.0, 0.0, 0.0);
117  //angular acceleration
118  my_pose_value.dynamic.angular.acceleration = math::vector3(0.0, 0.0, 0.0);
119 
120  manipulator->setComponentPoseFromWorld(my_name, my_pose_value);
121 
122  for (int8_t index = 0; index < number_of_child; index++)
123  {
124  Name child_name = manipulator->getComponentChildName(my_name).at(index);
125  forwardSolverUsingChainRule(manipulator, child_name);
126  }
127 }
128 
129 bool SolverUsingCRAndJacobian::inverseSolverUsingJacobian(Manipulator *manipulator, Name tool_name, Pose target_pose, std::vector<JointValue> *goal_joint_value)
130 {
131  const double lambda = 0.7;
132  const int8_t iteration = 10;
133 
134  Manipulator _manipulator = *manipulator;
135 
136  Eigen::MatrixXd jacobian = Eigen::MatrixXd::Identity(6, _manipulator.getDOF());
137 
138  Eigen::VectorXd pose_changed = Eigen::VectorXd::Zero(6);
139  Eigen::VectorXd delta_angle = Eigen::VectorXd::Zero(_manipulator.getDOF());
140 
141  for (int8_t count = 0; count < iteration; count++)
142  {
143  // Forward kinematics solve
144  solveForwardKinematics(&_manipulator);
145  // Get jacobian
146  jacobian = this->jacobian(&_manipulator, tool_name);
147 
148  // Pose Difference
149  pose_changed = math::poseDifference(target_pose.kinematic.position, _manipulator.getComponentPositionFromWorld(tool_name),
150  target_pose.kinematic.orientation, _manipulator.getComponentOrientationFromWorld(tool_name));
151 
152  // Pose sovler success
153  if (pose_changed.norm() < 1E-6)
154  {
155  *goal_joint_value = _manipulator.getAllActiveJointValue();
156  for(int8_t index = 0; index < _manipulator.getDOF(); index++)
157  {
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;
161  }
162  return true;
163  }
164 
165  // Get delta angle
166  ColPivHouseholderQR<MatrixXd> dec(jacobian);
167  delta_angle = lambda * dec.solve(pose_changed);
168 
169  // Set changed angle
170  std::vector<double> changed_angle;
171  for(int8_t index = 0; index < _manipulator.getDOF(); index++)
172  changed_angle.push_back(_manipulator.getAllActiveJointPosition().at(index) + delta_angle(index));
173  _manipulator.setAllActiveJointPosition(changed_angle);
174  }
175  *goal_joint_value = {};
176  return false;
177 }
178 
179 
180 /*****************************************************************************
181 ** Kinematics Solver Using Chain Rule and Singularity Robust Jacobian
182 *****************************************************************************/
183 void SolverUsingCRAndSRJacobian::setOption(const void *arg){}
184 
185 Eigen::MatrixXd SolverUsingCRAndSRJacobian::jacobian(Manipulator *manipulator, Name tool_name)
186 {
187  Eigen::MatrixXd jacobian = Eigen::MatrixXd::Identity(6, manipulator->getDOF());
188 
189  Eigen::Vector3d joint_axis = Eigen::Vector3d::Zero(3);
190 
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);
194 
196 
197  int8_t index = 0;
198  Name my_name = manipulator->getWorldChildName();
199 
200  for (int8_t size = 0; size < manipulator->getDOF(); size++)
201  {
202  Name parent_name = manipulator->getComponentParentName(my_name);
203  if (parent_name == manipulator->getWorldName())
204  {
205  joint_axis = manipulator->getWorldOrientation() * manipulator->getAxis(my_name);
206  }
207  else
208  {
209  joint_axis = manipulator->getComponentOrientationFromWorld(parent_name) * manipulator->getAxis(my_name);
210  }
211 
212  position_changed = math::skewSymmetricMatrix(joint_axis) *
213  (manipulator->getComponentPositionFromWorld(tool_name) - manipulator->getComponentPositionFromWorld(my_name));
214  orientation_changed = joint_axis;
215 
216  pose_changed << position_changed(0),
217  position_changed(1),
218  position_changed(2),
219  orientation_changed(0),
220  orientation_changed(1),
221  orientation_changed(2);
222 
223  jacobian.col(index) = pose_changed;
224  index++;
225  my_name = manipulator->getComponentChildName(my_name).at(0); // Get Child name which has active joint
226  }
227  return jacobian;
228 }
229 
230 void SolverUsingCRAndSRJacobian::solveForwardKinematics(Manipulator *manipulator)
231 {
232  forwardSolverUsingChainRule(manipulator, manipulator->getWorldChildName());
233 }
234 
235 bool SolverUsingCRAndSRJacobian::solveInverseKinematics(Manipulator *manipulator, Name tool_name, Pose target_pose, std::vector<JointValue> *goal_joint_value)
236 {
237  return inverseSolverUsingSRJacobian(manipulator, tool_name, target_pose, goal_joint_value);
238 }
239 
240 
241 // Private
242 void SolverUsingCRAndSRJacobian::forwardSolverUsingChainRule(Manipulator *manipulator, Name component_name)
243 {
244  Name my_name = component_name;
245  Name parent_name = manipulator->getComponentParentName(my_name);
246  int8_t number_of_child = manipulator->getComponentChildName(my_name).size();
247 
248  Pose parent_pose_value;
249  Pose my_pose_value;
250 
251  // Get Parent Pose
252  if (parent_name == manipulator->getWorldName())
253  {
254  parent_pose_value = manipulator->getWorldPose();
255  }
256  else
257  {
258  parent_pose_value = manipulator->getComponentPoseFromWorld(parent_name);
259  }
260 
261  // Position
262  my_pose_value.kinematic.position = parent_pose_value.kinematic.position
263  + (parent_pose_value.kinematic.orientation * manipulator->getComponentRelativePositionFromParent(my_name));
264  // Orientation
265  my_pose_value.kinematic.orientation = parent_pose_value.kinematic.orientation * manipulator->getComponentRelativeOrientationFromParent(my_name) * math::rodriguesRotationMatrix(manipulator->getAxis(my_name), manipulator->getJointPosition(my_name));
266  // Linear velocity
267  my_pose_value.dynamic.linear.velocity = math::vector3(0.0, 0.0, 0.0);
268  // Angular velocity
269  my_pose_value.dynamic.angular.velocity = math::vector3(0.0, 0.0, 0.0);
270  // Linear acceleration
271  my_pose_value.dynamic.linear.acceleration = math::vector3(0.0, 0.0, 0.0);
272  // Angular acceleration
273  my_pose_value.dynamic.angular.acceleration = math::vector3(0.0, 0.0, 0.0);
274 
275  manipulator->setComponentPoseFromWorld(my_name, my_pose_value);
276 
277  for (int8_t index = 0; index < number_of_child; index++)
278  {
279  Name child_name = manipulator->getComponentChildName(my_name).at(index);
280  forwardSolverUsingChainRule(manipulator, child_name);
281  }
282 }
283 
284 bool SolverUsingCRAndSRJacobian::inverseSolverUsingSRJacobian(Manipulator *manipulator, Name tool_name, Pose target_pose, std::vector<JointValue> *goal_joint_value)
285 {
286  //manipulator
287  Manipulator _manipulator = *manipulator;
288 
289  //solver parameter
290  double lambda = 0.0;
291  const double param = 0.002;
292  const int8_t iteration = 5;
293 
294  const double gamma = 0.5; //rollback delta
295 
296  //sr sovler parameter
297  double wn_pos = 1 / 0.3;
298  double wn_ang = 1 / (2 * M_PI);
299  double pre_Ek = 0.0;
300  double new_Ek = 0.0;
301 
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;
309 
310  Eigen::MatrixXd Wn = Eigen::MatrixXd::Identity(_manipulator.getDOF(), _manipulator.getDOF());
311 
312  //jacobian
313  Eigen::MatrixXd jacobian = Eigen::MatrixXd::Identity(6, _manipulator.getDOF());
314  Eigen::MatrixXd sr_jacobian = Eigen::MatrixXd::Identity(_manipulator.getDOF(), _manipulator.getDOF());
315 
316  //delta parameter
317  Eigen::VectorXd pose_changed = Eigen::VectorXd::Zero(6);
318  Eigen::VectorXd angle_changed = Eigen::VectorXd::Zero(_manipulator.getDOF()); //delta angle (dq)
319  Eigen::VectorXd gerr(_manipulator.getDOF());
320 
321  //angle parameter
322  std::vector<double> present_angle; //angle (q)
323  std::vector<double> set_angle; //set angle (q + dq)
324 
326 
327  solveForwardKinematics(&_manipulator);
329  pose_changed = math::poseDifference(target_pose.kinematic.position, _manipulator.getComponentPositionFromWorld(tool_name), target_pose.kinematic.orientation, _manipulator.getComponentOrientationFromWorld(tool_name));
330  pre_Ek = pose_changed.transpose() * We * pose_changed;
332 
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);
341 
342  Eigen::Vector3d present_position = _manipulator.getComponentPositionFromWorld(tool_name);
343  Eigen::MatrixXd present_orientation = _manipulator.getComponentOrientationFromWorld(tool_name);
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);
350 
351  log::println("------------------------------------");
352  log::warn("iter : first");
353  log::warn("Ek : ", pre_Ek*1000000000000);
354  log::println("tar_pose");
355  log::println_VECTOR(debug_target_pose,16);
356  log::println("pre_pose");
357  log::println_VECTOR(debug_present_pose,16);
358  log::println("delta_pose");
359  log::println_VECTOR(debug_target_pose-debug_present_pose,16);
360  #endif
361 
364  for (int8_t count = 0; count < iteration; count++)
365  {
367  jacobian = this->jacobian(&_manipulator, tool_name);
368  lambda = pre_Ek + param;
369 
370  sr_jacobian = (jacobian.transpose() * We * jacobian) + (lambda * Wn); //calculate sr_jacobian (J^T*we*J + lamda*Wn)
371  gerr = jacobian.transpose() * We * pose_changed; //calculate gerr (J^T*we) dx
372 
373  ColPivHouseholderQR<Eigen::MatrixXd> dec(sr_jacobian); //solving (get dq)
374  angle_changed = dec.solve(gerr); //(J^T*we) * dx = (J^T*we*J + lamda*Wn) * dq
375 
376  present_angle = _manipulator.getAllActiveJointPosition();
377  set_angle.clear();
378  for (int8_t index = 0; index < _manipulator.getDOF(); index++)
379  set_angle.push_back(present_angle.at(index) + angle_changed(index));
380  _manipulator.setAllActiveJointPosition(set_angle);
381  solveForwardKinematics(&_manipulator);
383 
385  pose_changed = math::poseDifference(target_pose.kinematic.position, _manipulator.getComponentPositionFromWorld(tool_name), target_pose.kinematic.orientation, _manipulator.getComponentOrientationFromWorld(tool_name));
386  new_Ek = pose_changed.transpose() * We * pose_changed;
388 
390  #if defined(KINEMATICS_DEBUG)
391  present_position = _manipulator.getComponentPositionFromWorld(tool_name);
392  present_orientation = _manipulator.getComponentOrientationFromWorld(tool_name);
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);
398  log::warn("iter : ", count,0);
399  log::warn("Ek : ", new_Ek*1000000000000);
400  log::println("tar_pose");
401  log::println_VECTOR(debug_target_pose,16);
402  log::println("pre_pose");
403  log::println_VECTOR(debug_present_pose,16);
404  log::println("delta_pose");
405  log::println_VECTOR(debug_target_pose-debug_present_pose,16);
406  #endif
407 
409  if (new_Ek < 1E-12)
410  {
412  #if defined(KINEMATICS_DEBUG)
413  log::warn("iter : ", count,0);
414  log::warn("Ek : ", new_Ek*1000000000000);
415  log::error("Success");
416  log::println("------------------------------------");
417  #endif
418  *goal_joint_value = _manipulator.getAllActiveJointValue();
420  for(int8_t index = 0; index < _manipulator.getDOF(); index++)
421  {
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;
425  }
426  return true;
427  }
428  else if (new_Ek < pre_Ek)
429  {
430  pre_Ek = new_Ek;
431  }
432  else
433  {
434  present_angle = _manipulator.getAllActiveJointPosition();
435  for (int8_t index = 0; index < _manipulator.getDOF(); index++)
436  set_angle.push_back(present_angle.at(index) - (gamma * angle_changed(index)));
437  _manipulator.setAllActiveJointPosition(set_angle);
438 
439  solveForwardKinematics(&_manipulator);
440  }
441  }
442  log::error("[sr]fail to solve inverse kinematics (please change the solver)");
443  *goal_joint_value = {};
444  return false;
445 }
446 
447 
448 /*****************************************************************************
449 ** Kinematics Solver Using Chain Rule and Singularity Robust Position Only Jacobian
450 *****************************************************************************/
451 void SolverUsingCRAndSRPositionOnlyJacobian::setOption(const void *arg){}
452 
453 Eigen::MatrixXd SolverUsingCRAndSRPositionOnlyJacobian::jacobian(Manipulator *manipulator, Name tool_name)
454 {
455  Eigen::MatrixXd jacobian = Eigen::MatrixXd::Identity(6, manipulator->getDOF());
456 
457  Eigen::Vector3d joint_axis = Eigen::Vector3d::Zero(3);
458 
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);
462 
464 
465  int8_t index = 0;
466  Name my_name = manipulator->getWorldChildName();
467 
468  for (int8_t size = 0; size < manipulator->getDOF(); size++)
469  {
470  Name parent_name = manipulator->getComponentParentName(my_name);
471  if (parent_name == manipulator->getWorldName())
472  {
473  joint_axis = manipulator->getWorldOrientation() * manipulator->getAxis(my_name);
474  }
475  else
476  {
477  joint_axis = manipulator->getComponentOrientationFromWorld(parent_name) * manipulator->getAxis(my_name);
478  }
479 
480  position_changed = math::skewSymmetricMatrix(joint_axis) *
481  (manipulator->getComponentPositionFromWorld(tool_name) - manipulator->getComponentPositionFromWorld(my_name));
482  orientation_changed = joint_axis;
483 
484  pose_changed << position_changed(0),
485  position_changed(1),
486  position_changed(2),
487  orientation_changed(0),
488  orientation_changed(1),
489  orientation_changed(2);
490 
491  jacobian.col(index) = pose_changed;
492  index++;
493  my_name = manipulator->getComponentChildName(my_name).at(0); // Get Child name which has active joint
494  }
495  return jacobian;
496 }
497 
498 void SolverUsingCRAndSRPositionOnlyJacobian::solveForwardKinematics(Manipulator *manipulator)
499 {
500  forwardSolverUsingChainRule(manipulator, manipulator->getWorldChildName());
501 }
502 
503 bool SolverUsingCRAndSRPositionOnlyJacobian::solveInverseKinematics(Manipulator *manipulator, Name tool_name, Pose target_pose, std::vector<JointValue> *goal_joint_value)
504 {
505  return inverseSolverUsingPositionOnlySRJacobian(manipulator, tool_name, target_pose, goal_joint_value);
506 }
507 
508 
509 //private
510 void SolverUsingCRAndSRPositionOnlyJacobian::forwardSolverUsingChainRule(Manipulator *manipulator, Name component_name)
511 {
512  Name my_name = component_name;
513  Name parent_name = manipulator->getComponentParentName(my_name);
514  int8_t number_of_child = manipulator->getComponentChildName(my_name).size();
515 
516  Pose parent_pose_value;
517  Pose my_pose_value;
518 
519  //Get Parent Pose
520  if (parent_name == manipulator->getWorldName())
521  {
522  parent_pose_value = manipulator->getWorldPose();
523  }
524  else
525  {
526  parent_pose_value = manipulator->getComponentPoseFromWorld(parent_name);
527  }
528 
529  //position
530  my_pose_value.kinematic.position = parent_pose_value.kinematic.position
531  + (parent_pose_value.kinematic.orientation * manipulator->getComponentRelativePositionFromParent(my_name));
532  //orientation
533  my_pose_value.kinematic.orientation = parent_pose_value.kinematic.orientation * manipulator->getComponentRelativeOrientationFromParent(my_name) * math::rodriguesRotationMatrix(manipulator->getAxis(my_name), manipulator->getJointPosition(my_name));
534  //linear velocity
535  my_pose_value.dynamic.linear.velocity = math::vector3(0.0, 0.0, 0.0);
536  //angular velocity
537  my_pose_value.dynamic.angular.velocity = math::vector3(0.0, 0.0, 0.0);
538  //linear acceleration
539  my_pose_value.dynamic.linear.acceleration = math::vector3(0.0, 0.0, 0.0);
540  //angular acceleration
541  my_pose_value.dynamic.angular.acceleration = math::vector3(0.0, 0.0, 0.0);
542 
543  manipulator->setComponentPoseFromWorld(my_name, my_pose_value);
544 
545  for (int8_t index = 0; index < number_of_child; index++)
546  {
547  Name child_name = manipulator->getComponentChildName(my_name).at(index);
548  forwardSolverUsingChainRule(manipulator, child_name);
549  }
550 }
551 
552 bool SolverUsingCRAndSRPositionOnlyJacobian::inverseSolverUsingPositionOnlySRJacobian(Manipulator *manipulator, Name tool_name, Pose target_pose, std::vector<JointValue> *goal_joint_value)
553 {
554  //manipulator
555  Manipulator _manipulator = *manipulator;
556 
557  //solver parameter
558  double lambda = 0.0;
559  const double param = 0.002;
560  const int8_t iteration = 10;
561 
562  const double gamma = 0.5; //rollback delta
563 
564  //jacobian
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());
568 
569  //delta parameter
570  Eigen::Vector3d position_changed = Eigen::VectorXd::Zero(3);
571  Eigen::VectorXd angle_changed = Eigen::VectorXd::Zero(_manipulator.getDOF()); //delta angle (dq)
572  Eigen::VectorXd gerr(_manipulator.getDOF());
573 
574  //sr sovler parameter
575  double wn_pos = 1 / 0.3;
576  double pre_Ek = 0.0;
577  double new_Ek = 0.0;
578 
579  Eigen::MatrixXd We(3, 3);
580  We << wn_pos, 0, 0,
581  0, wn_pos, 0,
582  0, 0, wn_pos;
583 
584  Eigen::MatrixXd Wn = Eigen::MatrixXd::Identity(_manipulator.getDOF(), _manipulator.getDOF());
585 
586  //angle parameter
587  std::vector<double> present_angle; //angle (q)
588  std::vector<double> set_angle; //set angle (q + dq)
589 
591 
592  solveForwardKinematics(&_manipulator);
594  position_changed = math::positionDifference(target_pose.kinematic.position, _manipulator.getComponentPositionFromWorld(tool_name));
595  pre_Ek = position_changed.transpose() * We * position_changed;
597 
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);
606 
607  Eigen::Vector3d present_position = _manipulator.getComponentPositionFromWorld(tool_name);
608  Eigen::MatrixXd present_orientation = _manipulator.getComponentOrientationFromWorld(tool_name);
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);
615 
616  log::println("------------------------------------");
617  log::warn("iter : first");
618  log::warn("Ek : ", pre_Ek*1000000000000);
619  log::println("tar_pose");
620  log::println_VECTOR(debug_target_pose,16);
621  log::println("pre_pose");
622  log::println_VECTOR(debug_present_pose,16);
623  log::println("delta_pose");
624  log::println_VECTOR(debug_target_pose-debug_present_pose,16);
625  #endif
626 
629  for (int8_t count = 0; count < iteration; count++)
630  {
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;
637 
638  sr_jacobian = (position_jacobian.transpose() * We * position_jacobian) + (lambda * Wn); //calculate sr_jacobian (J^T*we*J + lamda*Wn)
639  gerr = position_jacobian.transpose() * We * position_changed; //calculate gerr (J^T*we) dx
640 
641  ColPivHouseholderQR<Eigen::MatrixXd> dec(sr_jacobian); //solving (get dq)
642  angle_changed = dec.solve(gerr); //(J^T*we) * dx = (J^T*we*J + lamda*Wn) * dq
643 
644  present_angle = _manipulator.getAllActiveJointPosition();
645  set_angle.clear();
646  for (int8_t index = 0; index < _manipulator.getDOF(); index++)
647  set_angle.push_back(_manipulator.getAllActiveJointPosition().at(index) + angle_changed(index));
648  _manipulator.setAllActiveJointPosition(set_angle);
649  solveForwardKinematics(&_manipulator);
651 
653  position_changed = math::positionDifference(target_pose.kinematic.position, _manipulator.getComponentPositionFromWorld(tool_name));
654  new_Ek = position_changed.transpose() * We * position_changed;
656 
658  #if defined(KINEMATICS_DEBUG)
659  present_position = _manipulator.getComponentPositionFromWorld(tool_name);
660  present_orientation = _manipulator.getComponentOrientationFromWorld(tool_name);
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);
666  log::warn("iter : ", count,0);
667  log::warn("Ek : ", new_Ek*1000000000000);
668  log::println("tar_pose");
669  log::println_VECTOR(debug_target_pose,16);
670  log::println("pre_pose");
671  log::println_VECTOR(debug_present_pose,16);
672  log::println("delta_pose");
673  log::println_VECTOR(debug_target_pose-debug_present_pose,16);
674  #endif
675 
677  if (new_Ek < 1E-12)
678  {
680  #if defined(KINEMATICS_DEBUG)
681  log::warn("iter : ", count,0);
682  log::warn("Ek : ", new_Ek*1000000000000);
683  log::error("IK Success");
684  log::println("------------------------------------");
685  #endif
686  *goal_joint_value = _manipulator.getAllActiveJointValue();
688  for(int8_t index = 0; index < _manipulator.getDOF(); index++)
689  {
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;
693  }
694  return true;
695  }
696  else if (new_Ek < pre_Ek)
697  {
698  pre_Ek = new_Ek;
699  }
700  else
701  {
702  present_angle = _manipulator.getAllActiveJointPosition();
703  for (int8_t index = 0; index < _manipulator.getDOF(); index++)
704  set_angle.push_back(_manipulator.getAllActiveJointPosition().at(index) - (gamma * angle_changed(index)));
705  _manipulator.setAllActiveJointPosition(set_angle);
706 
707  solveForwardKinematics(&_manipulator);
708  }
709  }
710  log::error("[position_only]fail to solve inverse kinematics (please change the solver)");
711  *goal_joint_value = {};
712  return false;
713 }
714 
715 
716 /*****************************************************************************
717 ** Kinematics Solver Customized for OpenManipulator Chain
718 *****************************************************************************/
719 void SolverCustomizedforOMChain::setOption(const void *arg){}
720 
721 Eigen::MatrixXd SolverCustomizedforOMChain::jacobian(Manipulator *manipulator, Name tool_name)
722 {
723  Eigen::MatrixXd jacobian = Eigen::MatrixXd::Identity(6, manipulator->getDOF());
724 
725  Eigen::Vector3d joint_axis = Eigen::Vector3d::Zero(3);
726 
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);
730 
732 
733  int8_t index = 0;
734  Name my_name = manipulator->getWorldChildName();
735 
736  for (int8_t size = 0; size < manipulator->getDOF(); size++)
737  {
738  Name parent_name = manipulator->getComponentParentName(my_name);
739  if (parent_name == manipulator->getWorldName())
740  {
741  joint_axis = manipulator->getWorldOrientation() * manipulator->getAxis(my_name);
742  }
743  else
744  {
745  joint_axis = manipulator->getComponentOrientationFromWorld(parent_name) * manipulator->getAxis(my_name);
746  }
747 
748  position_changed = math::skewSymmetricMatrix(joint_axis) *
749  (manipulator->getComponentPositionFromWorld(tool_name) - manipulator->getComponentPositionFromWorld(my_name));
750  orientation_changed = joint_axis;
751 
752  pose_changed << position_changed(0),
753  position_changed(1),
754  position_changed(2),
755  orientation_changed(0),
756  orientation_changed(1),
757  orientation_changed(2);
758 
759  jacobian.col(index) = pose_changed;
760  index++;
761  my_name = manipulator->getComponentChildName(my_name).at(0); // Get Child name which has active joint
762  }
763  return jacobian;
764 }
765 
766 void SolverCustomizedforOMChain::solveForwardKinematics(Manipulator *manipulator)
767 {
768  forwardSolverUsingChainRule(manipulator, manipulator->getWorldChildName());
769 }
770 
771 bool SolverCustomizedforOMChain::solveInverseKinematics(Manipulator *manipulator, Name tool_name, Pose target_pose, std::vector<JointValue> *goal_joint_value)
772 {
773  return chainCustomInverseKinematics(manipulator, tool_name, target_pose, goal_joint_value);
774 }
775 
776 
777 //private
778 void SolverCustomizedforOMChain::forwardSolverUsingChainRule(Manipulator *manipulator, Name component_name)
779 {
780  Name my_name = component_name;
781  Name parent_name = manipulator->getComponentParentName(my_name);
782  int8_t number_of_child = manipulator->getComponentChildName(my_name).size();
783 
784  Pose parent_pose_value;
785  Pose my_pose_value;
786 
787  //Get Parent Pose
788  if (parent_name == manipulator->getWorldName())
789  {
790  parent_pose_value = manipulator->getWorldPose();
791  }
792  else
793  {
794  parent_pose_value = manipulator->getComponentPoseFromWorld(parent_name);
795  }
796 
797  //position
798  my_pose_value.kinematic.position = parent_pose_value.kinematic.position
799  + (parent_pose_value.kinematic.orientation * manipulator->getComponentRelativePositionFromParent(my_name));
800  //orientation
801  my_pose_value.kinematic.orientation = parent_pose_value.kinematic.orientation * manipulator->getComponentRelativeOrientationFromParent(my_name) * math::rodriguesRotationMatrix(manipulator->getAxis(my_name), manipulator->getJointPosition(my_name));
802  //linear velocity
803  my_pose_value.dynamic.linear.velocity = math::vector3(0.0, 0.0, 0.0);
804  //angular velocity
805  my_pose_value.dynamic.angular.velocity = math::vector3(0.0, 0.0, 0.0);
806  //linear acceleration
807  my_pose_value.dynamic.linear.acceleration = math::vector3(0.0, 0.0, 0.0);
808  //angular acceleration
809  my_pose_value.dynamic.angular.acceleration = math::vector3(0.0, 0.0, 0.0);
810 
811  manipulator->setComponentPoseFromWorld(my_name, my_pose_value);
812 
813  for (int8_t index = 0; index < number_of_child; index++)
814  {
815  Name child_name = manipulator->getComponentChildName(my_name).at(index);
816  forwardSolverUsingChainRule(manipulator, child_name);
817  }
818 }
819 
820 bool SolverCustomizedforOMChain::chainCustomInverseKinematics(Manipulator *manipulator, Name tool_name, Pose target_pose, std::vector<JointValue> *goal_joint_value)
821 {
822  //manipulator
823  Manipulator _manipulator = *manipulator;
824 
825  //solver parameter
826  double lambda = 0.0;
827  const double param = 0.002;
828  const int8_t iteration = 10;
829 
830  const double gamma = 0.5; //rollback delta
831 
832  //sr sovler parameter
833  double wn_pos = 1 / 0.3;
834  double wn_ang = 1 / (2 * M_PI);
835  double pre_Ek = 0.0;
836  double new_Ek = 0.0;
837 
838  Eigen::MatrixXd We(6, 6);
839  We << wn_pos, 0, 0, 0, 0, 0,
840  0, wn_pos, 0, 0, 0, 0,
841  0, 0, wn_pos, 0, 0, 0,
842  0, 0, 0, wn_ang, 0, 0,
843  0, 0, 0, 0, wn_ang, 0,
844  0, 0, 0, 0, 0, wn_ang;
845 
846  Eigen::MatrixXd Wn = Eigen::MatrixXd::Identity(_manipulator.getDOF(), _manipulator.getDOF());
847 
848  //jacobian
849  Eigen::MatrixXd jacobian = Eigen::MatrixXd::Identity(6, _manipulator.getDOF());
850  Eigen::MatrixXd sr_jacobian = Eigen::MatrixXd::Identity(_manipulator.getDOF(), _manipulator.getDOF());
851 
852  //delta parameter
853  Eigen::VectorXd pose_changed = Eigen::VectorXd::Zero(6);
854  Eigen::VectorXd angle_changed = Eigen::VectorXd::Zero(_manipulator.getDOF()); //delta angle (dq)
855  Eigen::VectorXd gerr(_manipulator.getDOF());
856 
857  //angle parameter
858  std::vector<double> present_angle; //angle (q)
859  std::vector<double> set_angle; //set angle (q + dq)
860 
862 
863  solveForwardKinematics(&_manipulator);
864 
866  Eigen::Matrix3d present_orientation = _manipulator.getComponentOrientationFromWorld(tool_name);
867  Eigen::Vector3d present_orientation_rpy = math::convertRotationMatrixToRPYVector(present_orientation);
868  Eigen::Matrix3d target_orientation = target_pose.kinematic.orientation;
869  Eigen::Vector3d target_orientation_rpy = math::convertRotationMatrixToRPYVector(target_orientation);
870 
871  Eigen::Vector3d joint1_rlative_position = _manipulator.getComponentRelativePositionFromParent(_manipulator.getWorldChildName());
872  Eigen::Vector3d target_position_from_joint1 = target_pose.kinematic.position - joint1_rlative_position;
873 
874  target_orientation_rpy(0) = present_orientation_rpy(0);
875  target_orientation_rpy(1) = target_orientation_rpy(1);
876  target_orientation_rpy(2) = atan2(target_position_from_joint1(1) ,target_position_from_joint1(0));
877 
878  target_pose.kinematic.orientation = math::convertRPYToRotationMatrix(target_orientation_rpy(0), target_orientation_rpy(1), target_orientation_rpy(2));
880 
882  pose_changed = math::poseDifference(target_pose.kinematic.position, _manipulator.getComponentPositionFromWorld(tool_name), target_pose.kinematic.orientation, _manipulator.getComponentOrientationFromWorld(tool_name));
883  pre_Ek = pose_changed.transpose() * We * pose_changed;
885 
887  #if defined(KINEMATICS_DEBUG)
888  Eigen::VectorXd debug_target_pose(6);
889  for(int t=0; t<3; t++)
890  debug_target_pose(t) = target_pose.position(t);
891  for(int t=0; t<3; t++)
892  debug_target_pose(t+3) = target_orientation_rpy(t);
893 
894  Eigen::Vector3d present_position = _manipulator.getComponentPositionFromWorld(tool_name);
895  Eigen::VectorXd debug_present_pose(6);
896  for(int t=0; t<3; t++)
897  debug_present_pose(t) = present_position(t);
898  for(int t=0; t<3; t++)
899  debug_present_pose(t+3) = present_orientation_rpy(t);
900 
901  log::println("------------------------------------");
902  log::warn("iter : first");
903  log::warn("Ek : ", pre_Ek*1000000000000);
904  log::println("tar_pose");
905  log::println_VECTOR(debug_target_pose,16);
906  log::println("pre_pose");
907  log::println_VECTOR(debug_present_pose,16);
908  log::println("delta_pose");
909  log::println_VECTOR(debug_target_pose-debug_present_pose,16);
910  #endif
911 
914  for (int8_t count = 0; count < iteration; count++)
915  {
917  jacobian = this->jacobian(&_manipulator, tool_name);
918  lambda = pre_Ek + param;
919 
920  sr_jacobian = (jacobian.transpose() * We * jacobian) + (lambda * Wn); //calculate sr_jacobian (J^T*we*J + lamda*Wn)
921  gerr = jacobian.transpose() * We * pose_changed; //calculate gerr (J^T*we) dx
922 
923  ColPivHouseholderQR<Eigen::MatrixXd> dec(sr_jacobian); //solving (get dq)
924  angle_changed = dec.solve(gerr); //(J^T*we) * dx = (J^T*we*J + lamda*Wn) * dq
925 
926  present_angle = _manipulator.getAllActiveJointPosition();
927  set_angle.clear();
928  for (int8_t index = 0; index < _manipulator.getDOF(); index++)
929  set_angle.push_back(present_angle.at(index) + angle_changed(index));
930  _manipulator.setAllActiveJointPosition(set_angle);
931  solveForwardKinematics(&_manipulator);
933 
935  pose_changed = math::poseDifference(target_pose.kinematic.position, _manipulator.getComponentPositionFromWorld(tool_name), target_pose.kinematic.orientation, _manipulator.getComponentOrientationFromWorld(tool_name));
936  new_Ek = pose_changed.transpose() * We * pose_changed;
938 
940  #if defined(KINEMATICS_DEBUG)
941  present_position = _manipulator.getComponentPositionFromWorld(tool_name);
942  present_orientation = _manipulator.getComponentOrientationFromWorld(tool_name);
943  present_orientation_rpy = math::convertRotationToRPY(present_orientation);
944  for(int t=0; t<3; t++)
945  debug_present_pose(t) = present_position(t);
946  for(int t=0; t<3; t++)
947  debug_present_pose(t+3) = present_orientation_rpy(t);
948  log::warn("iter : ", count,0);
949  log::warn("Ek : ", new_Ek*1000000000000);
950  log::println("tar_pose");
951  log::println_VECTOR(debug_target_pose,16);
952  log::println("pre_pose");
953  log::println_VECTOR(debug_present_pose,16);
954  log::println("delta_pose");
955  log::println_VECTOR(debug_target_pose-debug_present_pose,16);
956  #endif
957 
959  if (new_Ek < 1E-12)
960  {
962  #if defined(KINEMATICS_DEBUG)
963  log::warn("iter : ", count,0);
964  log::warn("Ek : ", new_Ek*1000000000000);
965  log::error("Success");
966  log::println("------------------------------------");
967  #endif
968  *goal_joint_value = _manipulator.getAllActiveJointValue();
970  for(int8_t index = 0; index < _manipulator.getDOF(); index++)
971  {
972  goal_joint_value->at(index).velocity = 0.0;
973  goal_joint_value->at(index).acceleration = 0.0;
974  goal_joint_value->at(index).effort = 0.0;
975  }
976  return true;
977  }
978  else if (new_Ek < pre_Ek)
979  {
980  pre_Ek = new_Ek;
981  }
982  else
983  {
984  present_angle = _manipulator.getAllActiveJointPosition();
985  for (int8_t index = 0; index < _manipulator.getDOF(); index++)
986  set_angle.push_back(present_angle.at(index) - (gamma * angle_changed(index)));
987  _manipulator.setAllActiveJointPosition(set_angle);
988 
989  solveForwardKinematics(&_manipulator);
990  }
991  }
992  log::error("[OpenManipulator Chain Custom]fail to solve inverse kinematics");
993  *goal_joint_value = {};
994  return false;
995 }
996 
997 
998 /*****************************************************************************
999 ** Kinematics Solver Using Geometry Approach
1000 *****************************************************************************/
1001 void SolverUsingCRAndGeometry::setOption(const void *arg)
1002 {
1003  with_gripper_ = arg;
1004 }
1005 
1006 Eigen::MatrixXd SolverUsingCRAndGeometry::jacobian(Manipulator *manipulator, Name tool_name)
1007 {
1008  Eigen::MatrixXd jacobian = Eigen::MatrixXd::Identity(6, manipulator->getDOF());
1009  return jacobian;
1010 }
1011 
1012 void SolverUsingCRAndGeometry::solveForwardKinematics(Manipulator *manipulator)
1013 {
1014  forwardSolverUsingChainRule(manipulator, manipulator->getWorldChildName());
1015 }
1016 
1017 bool SolverUsingCRAndGeometry::solveInverseKinematics(Manipulator *manipulator, Name tool_name, Pose target_pose, std::vector<JointValue> *goal_joint_value)
1018 {
1019  return inverseSolverUsingGeometry(manipulator, tool_name, target_pose, goal_joint_value);
1020 }
1021 
1022 
1023 //private
1024 void SolverUsingCRAndGeometry::forwardSolverUsingChainRule(Manipulator *manipulator, Name component_name)
1025 {
1026  Name my_name = component_name;
1027  Name parent_name = manipulator->getComponentParentName(my_name);
1028  int8_t number_of_child = manipulator->getComponentChildName(my_name).size();
1029 
1030  Pose parent_pose_value;
1031  Pose my_pose_value;
1032 
1033  //Get Parent Pose
1034  if (parent_name == manipulator->getWorldName())
1035  {
1036  parent_pose_value = manipulator->getWorldPose();
1037  }
1038  else
1039  {
1040  parent_pose_value = manipulator->getComponentPoseFromWorld(parent_name);
1041  }
1042 
1043  //position
1044  my_pose_value.kinematic.position = parent_pose_value.kinematic.position
1045  + (parent_pose_value.kinematic.orientation * manipulator->getComponentRelativePositionFromParent(my_name));
1046  //orientation
1047  my_pose_value.kinematic.orientation = parent_pose_value.kinematic.orientation * manipulator->getComponentRelativeOrientationFromParent(my_name) * math::rodriguesRotationMatrix(manipulator->getAxis(my_name), manipulator->getJointPosition(my_name));
1048  //linear velocity
1049  my_pose_value.dynamic.linear.velocity = math::vector3(0.0, 0.0, 0.0);
1050  //angular velocity
1051  my_pose_value.dynamic.angular.velocity = math::vector3(0.0, 0.0, 0.0);
1052  //linear acceleration
1053  my_pose_value.dynamic.linear.acceleration = math::vector3(0.0, 0.0, 0.0);
1054  //angular acceleration
1055  my_pose_value.dynamic.angular.acceleration = math::vector3(0.0, 0.0, 0.0);
1056 
1057  manipulator->setComponentPoseFromWorld(my_name, my_pose_value);
1058 
1059  for (int8_t index = 0; index < number_of_child; index++)
1060  {
1061  Name child_name = manipulator->getComponentChildName(my_name).at(index);
1062  forwardSolverUsingChainRule(manipulator, child_name);
1063  }
1064 }
1065 
1066 bool SolverUsingCRAndGeometry::inverseSolverUsingGeometry(Manipulator *manipulator, Name tool_name, Pose target_pose, std::vector<JointValue> *goal_joint_value)
1067 {
1068  Manipulator _manipulator = *manipulator;
1069  JointValue target_angle[6];
1070  std::vector<JointValue> target_angle_vector;
1071 
1073  // Compute Joint 1 Angle
1074  Eigen::VectorXd position = Eigen::VectorXd::Zero(3);
1075  Eigen::MatrixXd orientation = Eigen::MatrixXd::Zero(3,3);
1076  position = target_pose.kinematic.position;
1077  orientation = target_pose.kinematic.orientation;
1078  double d6 = 0.123;
1079 
1080  if (with_gripper_)
1081  {
1082  auto tool_length = _manipulator.getComponentRelativePositionFromParent(tool_name);
1083  d6 += tool_length(0);
1084  }
1085  Eigen::Vector3d position_2 = Eigen::VectorXd::Zero(3);
1086  position_2 << orientation(0,0), orientation(1,0), orientation(2,0);
1087  Eigen::Vector3d position_3 = Eigen::VectorXd::Zero(3);
1088  position_3 = position - d6*position_2;
1089  if (position_3(0) == 0) position_3(0) = 0.001;
1090  target_angle[0].position = atan(position_3(1) / position_3(0));
1091 
1092  // Compute Joint 3 Angle
1093  Eigen::VectorXd position3 = Eigen::VectorXd::Zero(3);
1094  Eigen::VectorXd position3_2 = Eigen::VectorXd::Zero(3);
1095  position3 << 0.0, 0.0, 0.126;
1096  position3_2 << 0.0, 0.0, 0.033;
1097  Eigen::MatrixXd orientation3 = math::convertRPYToRotationMatrix(0,0,target_angle[0].position);
1098  Eigen::VectorXd position3_3 = Eigen::VectorXd::Zero(3);
1099  position3_3 = position3 + orientation3 * position3_2;
1100  Eigen::VectorXd position3_4 = Eigen::VectorXd::Zero(3);
1101  position3_4 = position_3 - position3_3;
1102  double l1 = sqrt(0.264*0.264 + 0.030*0.030);
1103  double l2 = sqrt(0.030*0.030 + 0.258*0.258);
1104  double phi = acos((l1*l1 + l2*l2 - position3_4.norm()*position3_4.norm()) / (2*l1*l2));
1105  double alpha1 = atan2(0.030, 0.264);
1106  double alpha2 = atan2(0.258, 0.030);
1107  target_angle[2].position = PI - (phi-alpha1) - alpha2;
1108 
1109  // Compute Joint 2 Angle
1110  Eigen::VectorXd position2 = Eigen::VectorXd::Zero(3);
1111  Eigen::MatrixXd orientation2 = math::convertRPYToRotationMatrix(0,0,target_angle[0].position);
1112  position2 = orientation2.inverse() * position3_4;
1113  double beta1 = atan2(position2(2), position2(0));
1114  double beta2 = acos((l1*l1 + position3_4.norm()*position3_4.norm() - l2*l2) / (2*l1*position3_4.norm()));
1115  if (position3_4(2) > 0) target_angle[1].position = (PI/2-alpha1) - fabs(beta1) - beta2;
1116  else target_angle[1].position = (PI/2-alpha1) + fabs(beta1) - beta2;
1117 
1119  // Compute Joint 4, 5, 6 Angles
1120  Eigen::MatrixXd orientation_to_joint3 = math::convertRPYToRotationMatrix(0,0,target_angle[0].position)
1121  * math::convertRPYToRotationMatrix(0,target_angle[1].position,0)
1122  * math::convertRPYToRotationMatrix(0,target_angle[2].position,0);
1123  Eigen::MatrixXd orientation_def = orientation_to_joint3.transpose() * orientation;
1124 
1125  if(orientation_def(0,0) < 1.0)
1126  {
1127  if(orientation_def(0,0) > -1.0)
1128  {
1129  double joint4_angle_present = _manipulator.getJointPosition("joint4");
1130  double joint4_angle_temp_1 = atan2(orientation_def(1,0), -orientation_def(2,0));
1131  double joint4_angle_temp_2 = atan2(-orientation_def(1,0), orientation_def(2,0));
1132 
1133  if(fabs(joint4_angle_temp_1-joint4_angle_present) < fabs(joint4_angle_temp_2-joint4_angle_present))
1134  {
1135  // log::println("joint4_angle_temp_1", fabs(joint4_angle_present-joint4_angle_temp_1));
1136  target_angle[3].position = joint4_angle_temp_1;
1137  target_angle[4].position = acos(orientation_def(0,0));
1138  target_angle[5].position = atan2(orientation_def(0,1), orientation_def(0,2));
1139  }
1140  else
1141  {
1142  // log::println("joint4_angle_temp_2", fabs(joint4_angle_present-joint4_angle_temp_2));
1143  target_angle[3].position = joint4_angle_temp_2;
1144  target_angle[4].position = -acos(orientation_def(0,0));
1145  target_angle[5].position = atan2(-orientation_def(0,1), -orientation_def(0,2));
1146  }
1147  }
1148  else // R(0,0) = -1
1149  {
1150  target_angle[3].position = _manipulator.getJointPosition("joint4");
1151  target_angle[4].position = PI;
1152  target_angle[5].position = atan2(-orientation_def(1,2), orientation_def(1,1))+target_angle[3].position;
1153  }
1154  }
1155  else // R(0,0) = 1
1156  {
1157  target_angle[3].position = _manipulator.getJointPosition("joint4");
1158  target_angle[4].position = 0.0;
1159  target_angle[5].position = atan2(-orientation_def(1,2), orientation_def(1,1))-target_angle[3].position;
1160  }
1161 
1162  // log::println("------------------------------------");
1163  // log::println("End-effector Pose : ");
1164  // log::println("position1: ", target_angle[0].position);
1165  // log::println("position2: ", target_angle[1].position);
1166  // log::println("position3: ", target_angle[2].position);
1167  // log::println("position5: ", target_angle[4].position);
1168  // log::println("position4: ", target_angle[3].position);
1169  // log::println("position6: ", target_angle[5].position);
1170  // log::println("------------------------------------");
1171 
1172  if(std::isnan(target_angle[0].position) ||
1173  std::isnan(target_angle[1].position) ||
1174  std::isnan(target_angle[2].position) ||
1175  std::isnan(target_angle[3].position) ||
1176  std::isnan(target_angle[4].position) ||
1177  std::isnan(target_angle[5].position) )
1178  {
1179  log::error("Target angle value is NAN!!");
1180  return false;
1181  }
1182 
1183 
1184  if(std::isinf(target_angle[0].position) ||
1185  std::isinf(target_angle[1].position) ||
1186  std::isinf(target_angle[2].position) ||
1187  std::isinf(target_angle[3].position) ||
1188  std::isinf(target_angle[4].position) ||
1189  std::isinf(target_angle[5].position) )
1190  {
1191  log::error("Target angle value is INF!!");
1192  return false;
1193  }
1194 
1195  target_angle_vector.push_back(target_angle[0]);
1196  target_angle_vector.push_back(target_angle[1]);
1197  target_angle_vector.push_back(target_angle[2]);
1198  target_angle_vector.push_back(target_angle[3]);
1199  target_angle_vector.push_back(target_angle[4]);
1200  target_angle_vector.push_back(target_angle[5]);
1201 
1202  *goal_joint_value = target_angle_vector;
1203 
1204  return true;
1205 }
double getJointPosition(Name component_name)
bool param(const std::string &param_name, T &param_val, const T &default_val)
STRING Name
Pose getComponentPoseFromWorld(Name component_name)
void setAllActiveJointPosition(std::vector< double > joint_position_vector)
Eigen::Matrix3d getComponentRelativeOrientationFromParent(Name component_name)
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)
void error(STRING str)
Eigen::Matrix3d getWorldOrientation()
void warn(STRING str)
Eigen::Vector3d convertRotationMatrixToRPYVector(const Eigen::Matrix3d &rotation_matrix)
Eigen::Vector3d getComponentPositionFromWorld(Name component_name)
#define PI
Eigen::Matrix3d convertRPYToRotationMatrix(double roll, double pitch, double yaw)


open_manipulator_p_libs
Author(s): Ryan Shim , Yong-Ho Na , Hye-Jong KIM
autogenerated on Thu Oct 22 2020 03:16:37