2017-08-01 14:30:58 +02:00
|
|
|
#include "MultiBodyTree.hpp"
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
#include <limits>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "IDMath.hpp"
|
|
|
|
#include "details/MultiBodyTreeImpl.hpp"
|
|
|
|
#include "details/MultiBodyTreeInitCache.hpp"
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
namespace btInverseDynamics
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
MultiBodyTree::MultiBodyTree()
|
|
|
|
: m_is_finalized(false),
|
|
|
|
m_mass_parameters_are_valid(true),
|
|
|
|
m_accept_invalid_mass_parameters(false),
|
|
|
|
m_impl(0x0),
|
2019-01-03 14:26:51 +01:00
|
|
|
m_init_cache(0x0)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
m_init_cache = new InitCache();
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
MultiBodyTree::~MultiBodyTree()
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
delete m_impl;
|
|
|
|
delete m_init_cache;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void MultiBodyTree::setAcceptInvalidMassParameters(bool flag)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
m_accept_invalid_mass_parameters = flag;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
bool MultiBodyTree::getAcceptInvalidMassProperties() const
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_accept_invalid_mass_parameters;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getBodyOrigin(const int body_index, vec3 *world_origin) const
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->getBodyOrigin(body_index, world_origin);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getBodyCoM(const int body_index, vec3 *world_com) const
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->getBodyCoM(body_index, world_com);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getBodyTransform(const int body_index, mat33 *world_T_body) const
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->getBodyTransform(body_index, world_T_body);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getBodyAngularVelocity(const int body_index, vec3 *world_omega) const
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->getBodyAngularVelocity(body_index, world_omega);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getBodyLinearVelocity(const int body_index, vec3 *world_velocity) const
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->getBodyLinearVelocity(body_index, world_velocity);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getBodyLinearVelocityCoM(const int body_index, vec3 *world_velocity) const
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->getBodyLinearVelocityCoM(body_index, world_velocity);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getBodyAngularAcceleration(const int body_index, vec3 *world_dot_omega) const
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->getBodyAngularAcceleration(body_index, world_dot_omega);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getBodyLinearAcceleration(const int body_index, vec3 *world_acceleration) const
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->getBodyLinearAcceleration(body_index, world_acceleration);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getParentRParentBodyRef(const int body_index, vec3 *r) const
|
|
|
|
{
|
|
|
|
return m_impl->getParentRParentBodyRef(body_index, r);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getBodyTParentRef(const int body_index, mat33 *T) const
|
|
|
|
{
|
|
|
|
return m_impl->getBodyTParentRef(body_index, T);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getBodyAxisOfMotion(const int body_index, vec3 *axis) const
|
|
|
|
{
|
|
|
|
return m_impl->getBodyAxisOfMotion(body_index, axis);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MultiBodyTree::printTree() { m_impl->printTree(); }
|
|
|
|
void MultiBodyTree::printTreeData() { m_impl->printTreeData(); }
|
|
|
|
|
|
|
|
int MultiBodyTree::numBodies() const { return m_impl->m_num_bodies; }
|
|
|
|
|
|
|
|
int MultiBodyTree::numDoFs() const { return m_impl->m_num_dofs; }
|
|
|
|
|
|
|
|
int MultiBodyTree::calculateInverseDynamics(const vecx &q, const vecx &u, const vecx &dot_u,
|
2019-01-03 14:26:51 +01:00
|
|
|
vecx *joint_forces)
|
|
|
|
{
|
|
|
|
if (false == m_is_finalized)
|
|
|
|
{
|
2018-09-07 16:11:04 +02:00
|
|
|
bt_id_error_message("system has not been initialized\n");
|
2017-08-01 14:30:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
if (-1 == m_impl->calculateInverseDynamics(q, u, dot_u, joint_forces))
|
|
|
|
{
|
2018-09-07 16:11:04 +02:00
|
|
|
bt_id_error_message("error in inverse dynamics calculation\n");
|
2017-08-01 14:30:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MultiBodyTree::calculateMassMatrix(const vecx &q, const bool update_kinematics,
|
|
|
|
const bool initialize_matrix,
|
2019-01-03 14:26:51 +01:00
|
|
|
const bool set_lower_triangular_matrix, matxx *mass_matrix)
|
|
|
|
{
|
|
|
|
if (false == m_is_finalized)
|
|
|
|
{
|
2018-09-07 16:11:04 +02:00
|
|
|
bt_id_error_message("system has not been initialized\n");
|
2017-08-01 14:30:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (-1 ==
|
|
|
|
m_impl->calculateMassMatrix(q, update_kinematics, initialize_matrix,
|
2019-01-03 14:26:51 +01:00
|
|
|
set_lower_triangular_matrix, mass_matrix))
|
|
|
|
{
|
2018-09-07 16:11:04 +02:00
|
|
|
bt_id_error_message("error in mass matrix calculation\n");
|
2017-08-01 14:30:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::calculateMassMatrix(const vecx &q, matxx *mass_matrix)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return calculateMassMatrix(q, true, true, true, mass_matrix);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::calculateKinematics(const vecx &q, const vecx &u, const vecx &dot_u)
|
|
|
|
{
|
|
|
|
vec3 world_gravity(m_impl->m_world_gravity);
|
|
|
|
// temporarily set gravity to zero, to ensure we get the actual accelerations
|
|
|
|
setZero(m_impl->m_world_gravity);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (false == m_is_finalized)
|
|
|
|
{
|
|
|
|
bt_id_error_message("system has not been initialized\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (-1 == m_impl->calculateKinematics(q, u, dot_u,
|
|
|
|
MultiBodyTree::MultiBodyImpl::POSITION_VELOCITY_ACCELERATION))
|
|
|
|
{
|
|
|
|
bt_id_error_message("error in kinematics calculation\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
m_impl->m_world_gravity = world_gravity;
|
|
|
|
return 0;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::calculatePositionKinematics(const vecx &q)
|
|
|
|
{
|
|
|
|
if (false == m_is_finalized)
|
|
|
|
{
|
2018-09-07 16:11:04 +02:00
|
|
|
bt_id_error_message("system has not been initialized\n");
|
2017-08-01 14:30:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (-1 == m_impl->calculateKinematics(q, q, q,
|
2019-01-03 14:26:51 +01:00
|
|
|
MultiBodyTree::MultiBodyImpl::POSITION_VELOCITY))
|
|
|
|
{
|
2018-09-07 16:11:04 +02:00
|
|
|
bt_id_error_message("error in kinematics calculation\n");
|
2017-08-01 14:30:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::calculatePositionAndVelocityKinematics(const vecx &q, const vecx &u)
|
|
|
|
{
|
|
|
|
if (false == m_is_finalized)
|
|
|
|
{
|
2018-09-07 16:11:04 +02:00
|
|
|
bt_id_error_message("system has not been initialized\n");
|
2017-08-01 14:30:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (-1 == m_impl->calculateKinematics(q, u, u,
|
2019-01-03 14:26:51 +01:00
|
|
|
MultiBodyTree::MultiBodyImpl::POSITION_VELOCITY))
|
|
|
|
{
|
2018-09-07 16:11:04 +02:00
|
|
|
bt_id_error_message("error in kinematics calculation\n");
|
2017-08-01 14:30:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if (defined BT_ID_HAVE_MAT3X) && (defined BT_ID_WITH_JACOBIANS)
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::calculateJacobians(const vecx &q, const vecx &u)
|
|
|
|
{
|
|
|
|
if (false == m_is_finalized)
|
|
|
|
{
|
|
|
|
bt_id_error_message("system has not been initialized\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (-1 == m_impl->calculateJacobians(q, u,
|
|
|
|
MultiBodyTree::MultiBodyImpl::POSITION_VELOCITY))
|
|
|
|
{
|
|
|
|
bt_id_error_message("error in jacobian calculation\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::calculateJacobians(const vecx &q)
|
|
|
|
{
|
|
|
|
if (false == m_is_finalized)
|
|
|
|
{
|
|
|
|
bt_id_error_message("system has not been initialized\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (-1 == m_impl->calculateJacobians(q, q,
|
|
|
|
MultiBodyTree::MultiBodyImpl::POSITION_ONLY))
|
|
|
|
{
|
|
|
|
bt_id_error_message("error in jacobian calculation\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getBodyDotJacobianTransU(const int body_index, vec3 *world_dot_jac_trans_u) const
|
|
|
|
{
|
|
|
|
return m_impl->getBodyDotJacobianTransU(body_index, world_dot_jac_trans_u);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getBodyDotJacobianRotU(const int body_index, vec3 *world_dot_jac_rot_u) const
|
|
|
|
{
|
|
|
|
return m_impl->getBodyDotJacobianRotU(body_index, world_dot_jac_rot_u);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getBodyJacobianTrans(const int body_index, mat3x *world_jac_trans) const
|
|
|
|
{
|
|
|
|
return m_impl->getBodyJacobianTrans(body_index, world_jac_trans);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getBodyJacobianRot(const int body_index, mat3x *world_jac_rot) const
|
|
|
|
{
|
|
|
|
return m_impl->getBodyJacobianRot(body_index, world_jac_rot);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int MultiBodyTree::addBody(int body_index, int parent_index, JointType joint_type,
|
|
|
|
const vec3 &parent_r_parent_body_ref, const mat33 &body_T_parent_ref,
|
|
|
|
const vec3 &body_axis_of_motion_, idScalar mass,
|
|
|
|
const vec3 &body_r_body_com, const mat33 &body_I_body,
|
2019-01-03 14:26:51 +01:00
|
|
|
const int user_int, void *user_ptr)
|
|
|
|
{
|
|
|
|
if (body_index < 0)
|
|
|
|
{
|
2018-09-07 16:11:04 +02:00
|
|
|
bt_id_error_message("body index must be positive (got %d)\n", body_index);
|
2017-08-01 14:30:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
vec3 body_axis_of_motion(body_axis_of_motion_);
|
2019-01-03 14:26:51 +01:00
|
|
|
switch (joint_type)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
case REVOLUTE:
|
|
|
|
case PRISMATIC:
|
|
|
|
// check if axis is unit vector
|
2019-01-03 14:26:51 +01:00
|
|
|
if (!isUnitVector(body_axis_of_motion))
|
|
|
|
{
|
2018-09-07 16:11:04 +02:00
|
|
|
bt_id_warning_message(
|
2017-08-01 14:30:58 +02:00
|
|
|
"axis of motion not a unit axis ([%f %f %f]), will use normalized vector\n",
|
|
|
|
body_axis_of_motion(0), body_axis_of_motion(1), body_axis_of_motion(2));
|
|
|
|
idScalar length = BT_ID_SQRT(BT_ID_POW(body_axis_of_motion(0), 2) +
|
2019-01-03 14:26:51 +01:00
|
|
|
BT_ID_POW(body_axis_of_motion(1), 2) +
|
|
|
|
BT_ID_POW(body_axis_of_motion(2), 2));
|
|
|
|
if (length < BT_ID_SQRT(std::numeric_limits<idScalar>::min()))
|
|
|
|
{
|
2018-09-07 16:11:04 +02:00
|
|
|
bt_id_error_message("axis of motion vector too short (%e)\n", length);
|
2017-08-01 14:30:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
body_axis_of_motion = (1.0 / length) * body_axis_of_motion;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FIXED:
|
|
|
|
break;
|
|
|
|
case FLOATING:
|
|
|
|
break;
|
2019-01-03 14:26:51 +01:00
|
|
|
case SPHERICAL:
|
|
|
|
break;
|
2017-08-01 14:30:58 +02:00
|
|
|
default:
|
2018-09-07 16:11:04 +02:00
|
|
|
bt_id_error_message("unknown joint type %d\n", joint_type);
|
2017-08-01 14:30:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sanity check for mass properties. Zero mass is OK.
|
2019-01-03 14:26:51 +01:00
|
|
|
if (mass < 0)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
m_mass_parameters_are_valid = false;
|
2018-09-07 16:11:04 +02:00
|
|
|
bt_id_error_message("Body %d has invalid mass %e\n", body_index, mass);
|
2019-01-03 14:26:51 +01:00
|
|
|
if (!m_accept_invalid_mass_parameters)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (!isValidInertiaMatrix(body_I_body, body_index, FIXED == joint_type))
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
m_mass_parameters_are_valid = false;
|
|
|
|
// error message printed in function call
|
2019-01-03 14:26:51 +01:00
|
|
|
if (!m_accept_invalid_mass_parameters)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (!isValidTransformMatrix(body_T_parent_ref))
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_init_cache->addBody(body_index, parent_index, joint_type, parent_r_parent_body_ref,
|
|
|
|
body_T_parent_ref, body_axis_of_motion, mass, body_r_body_com,
|
|
|
|
body_I_body, user_int, user_ptr);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getParentIndex(const int body_index, int *parent_index) const
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->getParentIndex(body_index, parent_index);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getUserInt(const int body_index, int *user_int) const
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->getUserInt(body_index, user_int);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getUserPtr(const int body_index, void **user_ptr) const
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->getUserPtr(body_index, user_ptr);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::setUserInt(const int body_index, const int user_int)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->setUserInt(body_index, user_int);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::setUserPtr(const int body_index, void *const user_ptr)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->setUserPtr(body_index, user_ptr);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::finalize()
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
const int &num_bodies = m_init_cache->numBodies();
|
|
|
|
const int &num_dofs = m_init_cache->numDoFs();
|
|
|
|
|
2019-06-11 13:18:05 +02:00
|
|
|
if (num_dofs < 0)
|
2019-01-03 14:26:51 +01:00
|
|
|
{
|
|
|
|
bt_id_error_message("Need num_dofs>=1, but num_dofs= %d\n", num_dofs);
|
|
|
|
//return -1;
|
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
// 1 allocate internal MultiBody structure
|
|
|
|
m_impl = new MultiBodyImpl(num_bodies, num_dofs);
|
|
|
|
|
|
|
|
// 2 build new index set assuring index(parent) < index(child)
|
2019-01-03 14:26:51 +01:00
|
|
|
if (-1 == m_init_cache->buildIndexSets())
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
m_init_cache->getParentIndexArray(&m_impl->m_parent_index);
|
|
|
|
|
|
|
|
// 3 setup internal kinematic and dynamic data
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int index = 0; index < num_bodies; index++)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
InertiaData inertia;
|
|
|
|
JointData joint;
|
2019-01-03 14:26:51 +01:00
|
|
|
if (-1 == m_init_cache->getInertiaData(index, &inertia))
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
if (-1 == m_init_cache->getJointData(index, &joint))
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
RigidBody &rigid_body = m_impl->m_body_list[index];
|
|
|
|
|
|
|
|
rigid_body.m_mass = inertia.m_mass;
|
|
|
|
rigid_body.m_body_mass_com = inertia.m_mass * inertia.m_body_pos_body_com;
|
|
|
|
rigid_body.m_body_I_body = inertia.m_body_I_body;
|
|
|
|
rigid_body.m_joint_type = joint.m_type;
|
|
|
|
rigid_body.m_parent_pos_parent_body_ref = joint.m_parent_pos_parent_child_ref;
|
|
|
|
rigid_body.m_body_T_parent_ref = joint.m_child_T_parent_ref;
|
|
|
|
rigid_body.m_parent_pos_parent_body_ref = joint.m_parent_pos_parent_child_ref;
|
|
|
|
rigid_body.m_joint_type = joint.m_type;
|
|
|
|
|
2018-09-07 16:11:04 +02:00
|
|
|
int user_int;
|
2019-01-03 14:26:51 +01:00
|
|
|
if (-1 == m_init_cache->getUserInt(index, &user_int))
|
|
|
|
{
|
2018-09-07 16:11:04 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
if (-1 == m_impl->setUserInt(index, user_int))
|
|
|
|
{
|
2018-09-07 16:11:04 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void *user_ptr;
|
|
|
|
if (-1 == m_init_cache->getUserPtr(index, &user_ptr))
|
|
|
|
{
|
2018-09-07 16:11:04 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
if (-1 == m_impl->setUserPtr(index, user_ptr))
|
|
|
|
{
|
2018-09-07 16:11:04 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
// Set joint Jacobians. Note that the dimension is always 3x1 here to avoid variable sized
|
|
|
|
// matrices.
|
2019-01-03 14:26:51 +01:00
|
|
|
switch (rigid_body.m_joint_type)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
case REVOLUTE:
|
|
|
|
rigid_body.m_Jac_JR(0) = joint.m_child_axis_of_motion(0);
|
|
|
|
rigid_body.m_Jac_JR(1) = joint.m_child_axis_of_motion(1);
|
|
|
|
rigid_body.m_Jac_JR(2) = joint.m_child_axis_of_motion(2);
|
|
|
|
rigid_body.m_Jac_JT(0) = 0.0;
|
|
|
|
rigid_body.m_Jac_JT(1) = 0.0;
|
|
|
|
rigid_body.m_Jac_JT(2) = 0.0;
|
|
|
|
break;
|
|
|
|
case PRISMATIC:
|
|
|
|
rigid_body.m_Jac_JR(0) = 0.0;
|
|
|
|
rigid_body.m_Jac_JR(1) = 0.0;
|
|
|
|
rigid_body.m_Jac_JR(2) = 0.0;
|
|
|
|
rigid_body.m_Jac_JT(0) = joint.m_child_axis_of_motion(0);
|
|
|
|
rigid_body.m_Jac_JT(1) = joint.m_child_axis_of_motion(1);
|
|
|
|
rigid_body.m_Jac_JT(2) = joint.m_child_axis_of_motion(2);
|
|
|
|
break;
|
|
|
|
case FIXED:
|
|
|
|
// NOTE/TODO: dimension really should be zero ..
|
|
|
|
rigid_body.m_Jac_JR(0) = 0.0;
|
|
|
|
rigid_body.m_Jac_JR(1) = 0.0;
|
|
|
|
rigid_body.m_Jac_JR(2) = 0.0;
|
|
|
|
rigid_body.m_Jac_JT(0) = 0.0;
|
|
|
|
rigid_body.m_Jac_JT(1) = 0.0;
|
|
|
|
rigid_body.m_Jac_JT(2) = 0.0;
|
|
|
|
break;
|
2019-01-03 14:26:51 +01:00
|
|
|
case SPHERICAL:
|
|
|
|
// NOTE/TODO: this is not really correct.
|
|
|
|
// the Jacobians should be 3x3 matrices here !
|
|
|
|
rigid_body.m_Jac_JR(0) = 0.0;
|
|
|
|
rigid_body.m_Jac_JR(1) = 0.0;
|
|
|
|
rigid_body.m_Jac_JR(2) = 0.0;
|
|
|
|
rigid_body.m_Jac_JT(0) = 0.0;
|
|
|
|
rigid_body.m_Jac_JT(1) = 0.0;
|
|
|
|
rigid_body.m_Jac_JT(2) = 0.0;
|
|
|
|
break;
|
2017-08-01 14:30:58 +02:00
|
|
|
case FLOATING:
|
|
|
|
// NOTE/TODO: this is not really correct.
|
|
|
|
// the Jacobians should be 3x3 matrices here !
|
|
|
|
rigid_body.m_Jac_JR(0) = 0.0;
|
|
|
|
rigid_body.m_Jac_JR(1) = 0.0;
|
|
|
|
rigid_body.m_Jac_JR(2) = 0.0;
|
|
|
|
rigid_body.m_Jac_JT(0) = 0.0;
|
|
|
|
rigid_body.m_Jac_JT(1) = 0.0;
|
|
|
|
rigid_body.m_Jac_JT(2) = 0.0;
|
|
|
|
break;
|
|
|
|
default:
|
2018-09-07 16:11:04 +02:00
|
|
|
bt_id_error_message("unsupported joint type %d\n", rigid_body.m_joint_type);
|
2017-08-01 14:30:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4 assign degree of freedom indices & build per-joint-type index arrays
|
2019-01-03 14:26:51 +01:00
|
|
|
if (-1 == m_impl->generateIndexSets())
|
|
|
|
{
|
2018-09-07 16:11:04 +02:00
|
|
|
bt_id_error_message("generating index sets\n");
|
2017-08-01 14:30:58 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5 do some pre-computations ..
|
|
|
|
m_impl->calculateStaticData();
|
|
|
|
|
|
|
|
// 6. make sure all user forces are set to zero, as this might not happen
|
|
|
|
// in the vector ctors.
|
|
|
|
m_impl->clearAllUserForcesAndMoments();
|
|
|
|
|
|
|
|
m_is_finalized = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::setGravityInWorldFrame(const vec3 &gravity)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->setGravityInWorldFrame(gravity);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getJointType(const int body_index, JointType *joint_type) const
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->getJointType(body_index, joint_type);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getJointTypeStr(const int body_index, const char **joint_type) const
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->getJointTypeStr(body_index, joint_type);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getDoFOffset(const int body_index, int *q_offset) const
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->getDoFOffset(body_index, q_offset);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::setBodyMass(const int body_index, idScalar mass)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->setBodyMass(body_index, mass);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::setBodyFirstMassMoment(const int body_index, const vec3 &first_mass_moment)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->setBodyFirstMassMoment(body_index, first_mass_moment);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::setBodySecondMassMoment(const int body_index, const mat33 &second_mass_moment)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->setBodySecondMassMoment(body_index, second_mass_moment);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getBodyMass(const int body_index, idScalar *mass) const
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->getBodyMass(body_index, mass);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getBodyFirstMassMoment(const int body_index, vec3 *first_mass_moment) const
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->getBodyFirstMassMoment(body_index, first_mass_moment);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::getBodySecondMassMoment(const int body_index, mat33 *second_mass_moment) const
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->getBodySecondMassMoment(body_index, second_mass_moment);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiBodyTree::clearAllUserForcesAndMoments() { m_impl->clearAllUserForcesAndMoments(); }
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::addUserForce(const int body_index, const vec3 &body_force)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->addUserForce(body_index, body_force);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int MultiBodyTree::addUserMoment(const int body_index, const vec3 &body_moment)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
return m_impl->addUserMoment(body_index, body_moment);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
} // namespace btInverseDynamics
|