MultiBodyTreeImpl.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // The structs and classes defined here provide a basic inverse fynamics implementation used
  2. // by MultiBodyTree
  3. // User interaction should be through MultiBodyTree
  4. #ifndef MULTI_BODY_REFERENCE_IMPL_HPP_
  5. #define MULTI_BODY_REFERENCE_IMPL_HPP_
  6. #include "../IDConfig.hpp"
  7. #include "../MultiBodyTree.hpp"
  8. namespace btInverseDynamics
  9. {
  10. /// Structure for for rigid body mass properties, connectivity and kinematic state
  11. /// all vectors and matrices are in body-fixed frame, if not indicated otherwise.
  12. /// The body-fixed frame is located in the joint connecting the body to its parent.
  13. struct RigidBody
  14. {
  15. ID_DECLARE_ALIGNED_ALLOCATOR();
  16. // 1 Inertial properties
  17. /// Mass
  18. idScalar m_mass;
  19. /// Mass times center of gravity in body-fixed frame
  20. vec3 m_body_mass_com;
  21. /// Moment of inertia w.r.t. body-fixed frame
  22. mat33 m_body_I_body;
  23. // 2 dynamic properties
  24. /// Left-hand side of the body equation of motion, translational part
  25. vec3 m_eom_lhs_translational;
  26. /// Left-hand side of the body equation of motion, rotational part
  27. vec3 m_eom_lhs_rotational;
  28. /// Force acting at the joint when the body is cut from its parent;
  29. /// includes impressed joint force in J_JT direction,
  30. /// as well as constraint force,
  31. /// in body-fixed frame
  32. vec3 m_force_at_joint;
  33. /// Moment acting at the joint when the body is cut from its parent;
  34. /// includes impressed joint moment in J_JR direction, and constraint moment
  35. /// in body-fixed frame
  36. vec3 m_moment_at_joint;
  37. /// external (user provided) force acting at the body-fixed frame's origin, written in that
  38. /// frame
  39. vec3 m_body_force_user;
  40. /// external (user provided) moment acting at the body-fixed frame's origin, written in that
  41. /// frame
  42. vec3 m_body_moment_user;
  43. // 3 absolute kinematic properties
  44. /// Position of body-fixed frame relative to world frame
  45. /// this is currently only for debugging purposes
  46. vec3 m_body_pos;
  47. /// Absolute velocity of body-fixed frame
  48. vec3 m_body_vel;
  49. /// Absolute acceleration of body-fixed frame
  50. /// NOTE: if gravitational acceleration is not zero, this is the accelation PLUS gravitational
  51. /// acceleration!
  52. vec3 m_body_acc;
  53. /// Absolute angular velocity
  54. vec3 m_body_ang_vel;
  55. /// Absolute angular acceleration
  56. /// NOTE: if gravitational acceleration is not zero, this is the accelation PLUS gravitational
  57. /// acceleration!
  58. vec3 m_body_ang_acc;
  59. // 4 relative kinematic properties.
  60. // these are in the parent body frame
  61. /// Transform from world to body-fixed frame;
  62. /// this is currently only for debugging purposes
  63. mat33 m_body_T_world;
  64. /// Transform from parent to body-fixed frame
  65. mat33 m_body_T_parent;
  66. /// Vector from parent to child frame in parent frame
  67. vec3 m_parent_pos_parent_body;
  68. /// Relative angular velocity
  69. vec3 m_body_ang_vel_rel;
  70. /// Relative linear velocity
  71. vec3 m_parent_vel_rel;
  72. /// Relative angular acceleration
  73. vec3 m_body_ang_acc_rel;
  74. /// Relative linear acceleration
  75. vec3 m_parent_acc_rel;
  76. // 5 Data describing the joint type and geometry
  77. /// Type of joint
  78. JointType m_joint_type;
  79. /// Position of joint frame (body-fixed frame at q=0) relative to the parent frame
  80. /// Components are in body-fixed frame of the parent
  81. vec3 m_parent_pos_parent_body_ref;
  82. /// Orientation of joint frame (body-fixed frame at q=0) relative to the parent frame
  83. mat33 m_body_T_parent_ref;
  84. /// Joint rotational Jacobian, ie, the partial derivative of the body-fixed frames absolute
  85. /// angular velocity w.r.t. the generalized velocity of this body's relative degree of freedom.
  86. /// For revolute joints this is the joint axis, for prismatic joints it is a null matrix.
  87. /// (NOTE: dimensions will have to be dynamic for additional joint types!)
  88. vec3 m_Jac_JR;
  89. /// Joint translational Jacobian, ie, the partial derivative of the body-fixed frames absolute
  90. /// linear velocity w.r.t. the generalized velocity of this body's relative degree of freedom.
  91. /// For prismatic joints this is the joint axis, for revolute joints it is a null matrix.
  92. /// (NOTE: dimensions might have to be dynamic for additional joint types!)
  93. vec3 m_Jac_JT;
  94. /// m_Jac_JT in the parent frame, it, m_body_T_parent_ref.transpose()*m_Jac_JT
  95. vec3 m_parent_Jac_JT;
  96. /// Start of index range for the position degree(s) of freedom describing this body's motion
  97. /// relative to
  98. /// its parent. The indices are wrt the multibody system's q-vector of generalized coordinates.
  99. int m_q_index;
  100. // 6 Scratch data for mass matrix computation using "composite rigid body algorithm"
  101. /// mass of the subtree rooted in this body
  102. idScalar m_subtree_mass;
  103. /// center of mass * mass for subtree rooted in this body, in body-fixed frame
  104. vec3 m_body_subtree_mass_com;
  105. /// moment of inertia of subtree rooted in this body, w.r.t. body origin, in body-fixed frame
  106. mat33 m_body_subtree_I_body;
  107. #if (defined BT_ID_HAVE_MAT3X) && (defined BT_ID_WITH_JACOBIANS)
  108. /// translational jacobian in body-fixed frame d(m_body_vel)/du
  109. mat3x m_body_Jac_T;
  110. /// rotationsl jacobian in body-fixed frame d(m_body_ang_vel)/du
  111. mat3x m_body_Jac_R;
  112. /// components of linear acceleration depending on u
  113. /// (same as is d(m_Jac_T)/dt*u)
  114. vec3 m_body_dot_Jac_T_u;
  115. /// components of angular acceleration depending on u
  116. /// (same as is d(m_Jac_T)/dt*u)
  117. vec3 m_body_dot_Jac_R_u;
  118. #endif
  119. };
  120. /// The MBS implements a tree structured multibody system
  121. class MultiBodyTree::MultiBodyImpl
  122. {
  123. friend class MultiBodyTree;
  124. public:
  125. ID_DECLARE_ALIGNED_ALLOCATOR();
  126. enum KinUpdateType
  127. {
  128. POSITION_ONLY,
  129. POSITION_VELOCITY,
  130. POSITION_VELOCITY_ACCELERATION
  131. };
  132. /// constructor
  133. /// @param num_bodies the number of bodies in the system
  134. /// @param num_dofs number of degrees of freedom in the system
  135. MultiBodyImpl(int num_bodies_, int num_dofs_);
  136. /// \copydoc MultiBodyTree::calculateInverseDynamics
  137. int calculateInverseDynamics(const vecx& q, const vecx& u, const vecx& dot_u,
  138. vecx* joint_forces);
  139. ///\copydoc MultiBodyTree::calculateMassMatrix
  140. int calculateMassMatrix(const vecx& q, const bool update_kinematics,
  141. const bool initialize_matrix, const bool set_lower_triangular_matrix,
  142. matxx* mass_matrix);
  143. /// calculate kinematics (vector quantities)
  144. /// Depending on type, update positions only, positions & velocities, or positions, velocities
  145. /// and accelerations.
  146. int calculateKinematics(const vecx& q, const vecx& u, const vecx& dot_u, const KinUpdateType type);
  147. #if (defined BT_ID_HAVE_MAT3X) && (defined BT_ID_WITH_JACOBIANS)
  148. /// calculate jacobians and (if type == POSITION_VELOCITY), also velocity-dependent accelration terms.
  149. int calculateJacobians(const vecx& q, const vecx& u, const KinUpdateType type);
  150. /// \copydoc MultiBodyTree::getBodyDotJacobianTransU
  151. int getBodyDotJacobianTransU(const int body_index, vec3* world_dot_jac_trans_u) const;
  152. /// \copydoc MultiBodyTree::getBodyDotJacobianRotU
  153. int getBodyDotJacobianRotU(const int body_index, vec3* world_dot_jac_rot_u) const;
  154. /// \copydoc MultiBodyTree::getBodyJacobianTrans
  155. int getBodyJacobianTrans(const int body_index, mat3x* world_jac_trans) const;
  156. /// \copydoc MultiBodyTree::getBodyJacobianRot
  157. int getBodyJacobianRot(const int body_index, mat3x* world_jac_rot) const;
  158. /// Add relative Jacobian component from motion relative to parent body
  159. /// @param body the body to add the Jacobian component for
  160. void addRelativeJacobianComponent(RigidBody& body);
  161. #endif
  162. /// generate additional index sets from the parent_index array
  163. /// @return -1 on error, 0 on success
  164. int generateIndexSets();
  165. /// set gravity acceleration in world frame
  166. /// @param gravity gravity vector in the world frame
  167. /// @return 0 on success, -1 on error
  168. int setGravityInWorldFrame(const vec3& gravity);
  169. /// pretty print tree
  170. void printTree();
  171. /// print tree data
  172. void printTreeData();
  173. /// initialize fixed data
  174. void calculateStaticData();
  175. /// \copydoc MultiBodyTree::getBodyFrame
  176. int getBodyFrame(const int index, vec3* world_origin, mat33* body_T_world) const;
  177. /// \copydoc MultiBodyTree::getParentIndex
  178. int getParentIndex(const int body_index, int* m_parent_index);
  179. /// \copydoc MultiBodyTree::getJointType
  180. int getJointType(const int body_index, JointType* joint_type) const;
  181. /// \copydoc MultiBodyTree::getJointTypeStr
  182. int getJointTypeStr(const int body_index, const char** joint_type) const;
  183. /// \copydoc MultiBodyTree::getParentRParentBodyRef
  184. int getParentRParentBodyRef(const int body_index, vec3* r) const;
  185. /// \copydoc MultiBodyTree::getBodyTParentRef
  186. int getBodyTParentRef(const int body_index, mat33* T) const;
  187. /// \copydoc MultiBodyTree::getBodyAxisOfMotion
  188. int getBodyAxisOfMotion(const int body_index, vec3* axis) const;
  189. /// \copydoc MultiBodyTree:getDoFOffset
  190. int getDoFOffset(const int body_index, int* q_index) const;
  191. /// \copydoc MultiBodyTree::getBodyOrigin
  192. int getBodyOrigin(const int body_index, vec3* world_origin) const;
  193. /// \copydoc MultiBodyTree::getBodyCoM
  194. int getBodyCoM(const int body_index, vec3* world_com) const;
  195. /// \copydoc MultiBodyTree::getBodyTransform
  196. int getBodyTransform(const int body_index, mat33* world_T_body) const;
  197. /// \copydoc MultiBodyTree::getBodyAngularVelocity
  198. int getBodyAngularVelocity(const int body_index, vec3* world_omega) const;
  199. /// \copydoc MultiBodyTree::getBodyLinearVelocity
  200. int getBodyLinearVelocity(const int body_index, vec3* world_velocity) const;
  201. /// \copydoc MultiBodyTree::getBodyLinearVelocityCoM
  202. int getBodyLinearVelocityCoM(const int body_index, vec3* world_velocity) const;
  203. /// \copydoc MultiBodyTree::getBodyAngularAcceleration
  204. int getBodyAngularAcceleration(const int body_index, vec3* world_dot_omega) const;
  205. /// \copydoc MultiBodyTree::getBodyLinearAcceleration
  206. int getBodyLinearAcceleration(const int body_index, vec3* world_acceleration) const;
  207. /// \copydoc MultiBodyTree::getUserInt
  208. int getUserInt(const int body_index, int* user_int) const;
  209. /// \copydoc MultiBodyTree::getUserPtr
  210. int getUserPtr(const int body_index, void** user_ptr) const;
  211. /// \copydoc MultiBodyTree::setUserInt
  212. int setUserInt(const int body_index, const int user_int);
  213. /// \copydoc MultiBodyTree::setUserPtr
  214. int setUserPtr(const int body_index, void* const user_ptr);
  215. ///\copydoc MultiBodytTree::setBodyMass
  216. int setBodyMass(const int body_index, const idScalar mass);
  217. ///\copydoc MultiBodytTree::setBodyFirstMassMoment
  218. int setBodyFirstMassMoment(const int body_index, const vec3& first_mass_moment);
  219. ///\copydoc MultiBodytTree::setBodySecondMassMoment
  220. int setBodySecondMassMoment(const int body_index, const mat33& second_mass_moment);
  221. ///\copydoc MultiBodytTree::getBodyMass
  222. int getBodyMass(const int body_index, idScalar* mass) const;
  223. ///\copydoc MultiBodytTree::getBodyFirstMassMoment
  224. int getBodyFirstMassMoment(const int body_index, vec3* first_mass_moment) const;
  225. ///\copydoc MultiBodytTree::getBodySecondMassMoment
  226. int getBodySecondMassMoment(const int body_index, mat33* second_mass_moment) const;
  227. /// \copydoc MultiBodyTree::clearAllUserForcesAndMoments
  228. void clearAllUserForcesAndMoments();
  229. /// \copydoc MultiBodyTree::addUserForce
  230. int addUserForce(const int body_index, const vec3& body_force);
  231. /// \copydoc MultiBodyTree::addUserMoment
  232. int addUserMoment(const int body_index, const vec3& body_moment);
  233. private:
  234. // debug function. print tree structure to stdout
  235. void printTree(int index, int indentation);
  236. // get string representation of JointType (for debugging)
  237. const char* jointTypeToString(const JointType& type) const;
  238. // get number of degrees of freedom from joint type
  239. int bodyNumDoFs(const JointType& type) const;
  240. // number of bodies in the system
  241. int m_num_bodies;
  242. // number of degrees of freedom
  243. int m_num_dofs;
  244. // Gravitational acceleration (in world frame)
  245. vec3 m_world_gravity;
  246. // vector of bodies in the system
  247. // body 0 is used as an environment body and is allways fixed.
  248. // The bodies are ordered such that a parent body always has an index
  249. // smaller than its child.
  250. idArray<RigidBody>::type m_body_list;
  251. // Parent_index[i] is the index for i's parent body in body_list.
  252. // This fully describes the tree.
  253. idArray<int>::type m_parent_index;
  254. // child_indices[i] contains a vector of indices of
  255. // all children of the i-th body
  256. idArray<idArray<int>::type>::type m_child_indices;
  257. // Indices of rotary joints
  258. idArray<int>::type m_body_revolute_list;
  259. // Indices of prismatic joints
  260. idArray<int>::type m_body_prismatic_list;
  261. // Indices of floating joints
  262. idArray<int>::type m_body_floating_list;
  263. // Indices of spherical joints
  264. idArray<int>::type m_body_spherical_list;
  265. // a user-provided integer
  266. idArray<int>::type m_user_int;
  267. // a user-provided pointer
  268. idArray<void*>::type m_user_ptr;
  269. #if (defined BT_ID_HAVE_MAT3X) && (defined BT_ID_WITH_JACOBIANS)
  270. mat3x m_m3x;
  271. #endif
  272. };
  273. } // namespace btInverseDynamics
  274. #endif