DillCreator.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "DillCreator.hpp"
  2. #include <cmath>
  3. namespace btInverseDynamics
  4. {
  5. DillCreator::DillCreator(int level)
  6. : m_level(level),
  7. m_num_bodies(BT_ID_POW(2, level))
  8. {
  9. m_parent.resize(m_num_bodies);
  10. m_parent_r_parent_body_ref.resize(m_num_bodies);
  11. m_body_T_parent_ref.resize(m_num_bodies);
  12. m_body_axis_of_motion.resize(m_num_bodies);
  13. m_mass.resize(m_num_bodies);
  14. m_body_r_body_com.resize(m_num_bodies);
  15. m_body_I_body.resize(m_num_bodies);
  16. // generate names (for debugging)
  17. for (int i = 0; i < m_num_bodies; i++)
  18. {
  19. m_parent[i] = i - 1;
  20. // all z-axis (DH convention)
  21. m_body_axis_of_motion[i](0) = 0.0;
  22. m_body_axis_of_motion[i](1) = 0.0;
  23. m_body_axis_of_motion[i](2) = 1.0;
  24. }
  25. // recursively build data structures
  26. m_current_body = 0;
  27. const int parent = -1;
  28. const idScalar d_DH = 0.0;
  29. const idScalar a_DH = 0.0;
  30. const idScalar alpha_DH = 0.0;
  31. if (-1 == recurseDill(m_level, parent, d_DH, a_DH, alpha_DH))
  32. {
  33. bt_id_error_message("recurseDill failed\n");
  34. abort();
  35. }
  36. }
  37. DillCreator::~DillCreator() {}
  38. int DillCreator::getNumBodies(int* num_bodies) const
  39. {
  40. *num_bodies = m_num_bodies;
  41. return 0;
  42. }
  43. int DillCreator::getBody(const int body_index, int* parent_index, JointType* joint_type,
  44. vec3* parent_r_parent_body_ref, mat33* body_T_parent_ref,
  45. vec3* body_axis_of_motion, idScalar* mass, vec3* body_r_body_com,
  46. mat33* body_I_body, int* user_int, void** user_ptr) const
  47. {
  48. if (body_index < 0 || body_index >= m_num_bodies)
  49. {
  50. bt_id_error_message("invalid body index %d\n", body_index);
  51. return -1;
  52. }
  53. *parent_index = m_parent[body_index];
  54. *joint_type = REVOLUTE;
  55. *parent_r_parent_body_ref = m_parent_r_parent_body_ref[body_index];
  56. *body_T_parent_ref = m_body_T_parent_ref[body_index];
  57. *body_axis_of_motion = m_body_axis_of_motion[body_index];
  58. *mass = m_mass[body_index];
  59. *body_r_body_com = m_body_r_body_com[body_index];
  60. *body_I_body = m_body_I_body[body_index];
  61. *user_int = 0;
  62. *user_ptr = 0;
  63. return 0;
  64. }
  65. int DillCreator::recurseDill(const int level, const int parent, const idScalar d_DH_in,
  66. const idScalar a_DH_in, const idScalar alpha_DH_in)
  67. {
  68. if (level < 0)
  69. {
  70. bt_id_error_message("invalid level parameter (%d)\n", level);
  71. return -1;
  72. }
  73. if (m_current_body >= m_num_bodies || m_current_body < 0)
  74. {
  75. bt_id_error_message("invalid body parameter (%d, num_bodies: %d)\n", m_current_body,
  76. m_num_bodies);
  77. return -1;
  78. }
  79. idScalar size = BT_ID_MAX(level, 1);
  80. const int body = m_current_body;
  81. // length = 0.1 * size;
  82. // with = 2 * 0.01 * size;
  83. /// these parameters are from the paper ...
  84. /// TODO: add proper citation
  85. m_parent[body] = parent;
  86. m_mass[body] = 0.1 * BT_ID_POW(size, 3);
  87. m_body_r_body_com[body](0) = 0.05 * size;
  88. m_body_r_body_com[body](1) = 0;
  89. m_body_r_body_com[body](2) = 0;
  90. // initialization
  91. for (int i = 0; i < 3; i++)
  92. {
  93. m_parent_r_parent_body_ref[body](i) = 0;
  94. for (int j = 0; j < 3; j++)
  95. {
  96. m_body_I_body[body](i, j) = 0.0;
  97. m_body_T_parent_ref[body](i, j) = 0.0;
  98. }
  99. }
  100. const idScalar size_5 = std::pow(size, 5);
  101. m_body_I_body[body](0, 0) = size_5 / 0.2e6;
  102. m_body_I_body[body](1, 1) = size_5 * 403 / 1.2e6;
  103. m_body_I_body[body](2, 2) = m_body_I_body[body](1, 1);
  104. getVecMatFromDH(0, 0, a_DH_in, alpha_DH_in, &m_parent_r_parent_body_ref[body],
  105. &m_body_T_parent_ref[body]);
  106. // attach "level" Dill systems of levels 1...level
  107. for (int i = 1; i <= level; i++)
  108. {
  109. idScalar d_DH = 0.01 * size;
  110. if (i == level)
  111. {
  112. d_DH = 0.0;
  113. }
  114. const idScalar a_DH = i * 0.1;
  115. const idScalar alpha_DH = i * BT_ID_PI / 3.0;
  116. m_current_body++;
  117. recurseDill(i - 1, body, d_DH, a_DH, alpha_DH);
  118. }
  119. return 0; // ok!
  120. }
  121. } // namespace btInverseDynamics