treejnttojacsolver.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /** \file itasc/kdl/treejnttojacsolver.cpp
  2. * \ingroup itasc
  3. */
  4. /*
  5. * TreeJntToJacSolver.cpp
  6. *
  7. * Created on: Nov 27, 2008
  8. * Author: rubensmits
  9. */
  10. #include "treejnttojacsolver.hpp"
  11. #include <iostream>
  12. namespace KDL {
  13. TreeJntToJacSolver::TreeJntToJacSolver(const Tree& tree_in) :
  14. tree(tree_in) {
  15. }
  16. TreeJntToJacSolver::~TreeJntToJacSolver() {
  17. }
  18. int TreeJntToJacSolver::JntToJac(const JntArray& q_in, Jacobian& jac,
  19. const std::string& segmentname) {
  20. //First we check all the sizes:
  21. if (q_in.rows() != tree.getNrOfJoints() || jac.columns()
  22. != tree.getNrOfJoints())
  23. return -1;
  24. //Lets search the tree-element
  25. SegmentMap::value_type const* it = tree.getSegmentPtr(segmentname);
  26. //If segmentname is not inside the tree, back out:
  27. if (!it)
  28. return -2;
  29. //Let's make the jacobian zero:
  30. SetToZero(jac);
  31. SegmentMap::value_type const* root = tree.getSegmentPtr("root");
  32. Frame T_total = Frame::Identity();
  33. Frame T_local, T_joint;
  34. Twist t_local;
  35. //Lets recursively iterate until we are in the root segment
  36. while (it != root) {
  37. //get the corresponding q_nr for this TreeElement:
  38. unsigned int q_nr = it->second.q_nr;
  39. //get the pose of the joint.
  40. T_joint = it->second.segment.getJoint().pose(((JntArray&)q_in)(q_nr));
  41. // combine with the tip to have the tip pose
  42. T_local = T_joint*it->second.segment.getFrameToTip();
  43. //calculate new T_end:
  44. T_total = T_local * T_total;
  45. //get the twist of the segment:
  46. int ndof = it->second.segment.getJoint().getNDof();
  47. for (int dof=0; dof<ndof; dof++) {
  48. // combine joint rotation with tip position to get a reference frame for the joint
  49. T_joint.p = T_local.p;
  50. // in which the twist can be computed (needed for NDof joint)
  51. t_local = it->second.segment.twist(T_joint, 1.0, dof);
  52. //transform the endpoint of the local twist to the global endpoint:
  53. t_local = t_local.RefPoint(T_total.p - T_local.p);
  54. //transform the base of the twist to the endpoint
  55. t_local = T_total.M.Inverse(t_local);
  56. //store the twist in the jacobian:
  57. jac.twists[q_nr+dof] = t_local;
  58. }
  59. //goto the parent
  60. it = it->second.parent;
  61. }//endwhile
  62. //Change the base of the complete jacobian from the endpoint to the base
  63. changeBase(jac, T_total.M, jac);
  64. return 0;
  65. }//end JntToJac
  66. }//end namespace