DillCreator.cpp 4.0 KB

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