void AISTSimulatorItemImpl::addBody(AISTSimBody* simBody) { DyBody* body = static_cast<DyBody*>(simBody->body()); DyLink* rootLink = body->rootLink(); rootLink->v().setZero(); rootLink->dv().setZero(); rootLink->w().setZero(); rootLink->dw().setZero(); rootLink->vo().setZero(); rootLink->dvo().setZero(); for(int i=0; i < body->numLinks(); ++i){ Link* link = body->link(i); link->u() = 0.0; link->dq() = 0.0; link->ddq() = 0.0; } body->clearExternalForces(); body->calcForwardKinematics(true, true); int bodyIndex; if(dynamicsMode.is(AISTSimulatorItem::HG_DYNAMICS)){ ForwardDynamicsCBMPtr cbm = make_shared_aligned<ForwardDynamicsCBM>(body); cbm->setHighGainModeForAllJoints(); bodyIndex = world.addBody(body, cbm); } else { bodyIndex = world.addBody(body); } bodyIndexMap[body] = bodyIndex; }
void ForwardDynamicsABM::calcMotionWithEulerMethod() { calcABMLastHalf(); if(!sensorHelper.forceSensors().empty()){ updateForceSensors(); } DyLink* root = body->rootLink(); if(!root->isFixedJoint()){ root->dv() = root->dvo() - root->p().cross(root->dw()) + root->w().cross(root->vo() + root->w().cross(root->p())); Position T; SE3exp(T, root->T(), root->w(), root->vo(), timeStep); root->T() = T; root->vo() += root->dvo() * timeStep; root->w() += root->dw() * timeStep; } int n = body->numLinks(); for(int i=1; i < n; ++i){ DyLink* link = body->link(i); link->q() += link->dq() * timeStep; link->dq() += link->ddq() * timeStep; } }
/** \note v, dv, dw are not used in the forward dynamics, but are calculated for forward dynamics users. */ void ForwardDynamicsABM::calcABMPhase1(bool updateNonSpatialVariables) { const LinkTraverse& traverse = body->linkTraverse(); const int n = traverse.numLinks(); for(int i=0; i < n; ++i){ DyLink* link = static_cast<DyLink*>(traverse[i]); const DyLink* parent = link->parent(); if(parent){ switch(link->jointType()){ case Link::ROTATIONAL_JOINT: { const Vector3 arm = parent->R() * link->b(); link->R().noalias() = parent->R() * AngleAxisd(link->q(), link->a()); link->p().noalias() = arm + parent->p(); link->sw().noalias() = parent->R() * link->a(); link->sv().noalias() = link->p().cross(link->sw()); link->w().noalias() = link->dq() * link->sw() + parent->w(); if(updateNonSpatialVariables){ link->dw().noalias() = parent->dw() + link->dq() * parent->w().cross(link->sw()) + (link->ddq() * link->sw()); link->dv().noalias() = parent->dv() + parent->w().cross(parent->w().cross(arm)) + parent->dw().cross(arm); } break; } case Link::SLIDE_JOINT: link->p().noalias() = parent->R() * (link->b() + link->q() * link->d()) + parent->p(); link->R() = parent->R(); link->sw().setZero(); link->sv().noalias() = parent->R() * link->d(); link->w() = parent->w(); if(updateNonSpatialVariables){ link->dw() = parent->dw(); const Vector3 arm = parent->R() * link->b(); link->dv().noalias() = parent->dv() + parent->w().cross(parent->w().cross(arm)) + parent->dw().cross(arm) + 2.0 * link->dq() * parent->w().cross(link->sv()) + link->ddq() * link->sv(); } break; case Link::FIXED_JOINT: default: link->p().noalias() = parent->R() * link->b() + parent->p(); link->R() = parent->R(); link->w() = parent->w(); link->vo() = parent->vo(); link->sw().setZero(); link->sv().setZero(); link->cv().setZero(); link->cw().setZero(); if(updateNonSpatialVariables){ link->dw() = parent->dw(); const Vector3 arm = parent->R() * link->b(); link->dv().noalias() = parent->dv() + parent->w().cross(parent->w().cross(arm)) + parent->dw().cross(arm); } goto COMMON_CALCS_FOR_ALL_JOINT_TYPES; } // Common for ROTATE and SLIDE link->vo().noalias() = link->dq() * link->sv() + parent->vo(); const Vector3 dsv = parent->w().cross(link->sv()) + parent->vo().cross(link->sw()); const Vector3 dsw = parent->w().cross(link->sw()); link->cv() = link->dq() * dsv; link->cw() = link->dq() * dsw; } COMMON_CALCS_FOR_ALL_JOINT_TYPES: if(updateNonSpatialVariables){ link->v().noalias() = link->vo() + link->w().cross(link->p()); } link->wc().noalias() = link->R() * link->c() + link->p(); // compute I^s (Eq.(6.24) of Kajita's textbook)) const Matrix3 Iw = link->R() * link->I() * link->R().transpose(); const double m = link->m(); const Matrix3 c_hat = hat(link->wc()); link->Iww().noalias() = m * c_hat * c_hat.transpose() + Iw; link->Ivv() << m, 0.0, 0.0, 0.0, m, 0.0, 0.0, 0.0, m; link->Iwv() = m * c_hat; // compute P and L (Eq.(6.25) of Kajita's textbook) const Vector3 P = m * (link->vo() + link->w().cross(link->wc())); const Vector3 L = link->Iww() * link->w() + m * link->wc().cross(link->vo()); link->pf().noalias() = link->w().cross(P); link->ptau().noalias() = link->vo().cross(P) + link->w().cross(L); const Vector3 fg = m * g; const Vector3 tg = link->wc().cross(fg); link->pf() -= fg; link->ptau() -= tg; } }
void ForwardDynamicsABM::calcMotionWithRungeKuttaMethod() { DyLink* root = body->rootLink(); if(!root->isFixedJoint()){ T0 = root->T(); vo0 = root->vo(); w0 = root->w(); } vo.setZero(); w.setZero(); dvo.setZero(); dw.setZero(); int n = body->numLinks(); for(int i=1; i < n; ++i){ DyLink* link = body->link(i); q0[i] = link->q(); dq0[i] = link->dq(); dq[i] = 0.0; ddq[i] = 0.0; } calcABMLastHalf(); if(!sensorHelper.forceSensors().empty()){ updateForceSensors(); } integrateRungeKuttaOneStep(1.0 / 6.0, timeStep / 2.0); calcABMPhase1(false); calcABMPhase2(); calcABMPhase3(); integrateRungeKuttaOneStep(2.0 / 6.0, timeStep / 2.0); calcABMPhase1(false); calcABMPhase2(); calcABMPhase3(); integrateRungeKuttaOneStep(2.0 / 6.0, timeStep); calcABMPhase1(false); calcABMPhase2(); calcABMPhase3(); if(!root->isFixedJoint()){ root->dvo() = dvo + root->dvo() / 6.0; root->dw() = dw + root->dw() / 6.0; root->dv() = root->dvo() - T0.translation().cross(root->dw()) + w0.cross(vo0 + w0.cross(T0.translation())); root->vo() = vo0 + root->dvo() * timeStep; root->w() = w0 + root->dw() * timeStep; SE3exp(root->T(), T0, w0, vo0, timeStep); } for(int i=1; i < n; ++i){ DyLink* link = body->link(i); link->q() = q0[i] + ( dq[i] + link->dq() / 6.0) * timeStep; link->dq() = dq0[i] + (ddq[i] + link->ddq() / 6.0) * timeStep; } }