Austin Schuh | 7400da0 | 2018-01-28 19:54:58 -0800 | [diff] [blame^] | 1 | /********************************************************************************************************************** |
| 2 | This file is part of the Control Toolbox (https://adrlab.bitbucket.io/ct), copyright by ETH Zurich, Google Inc. |
| 3 | Authors: Michael Neunert, Markus Giftthaler, Markus Stäuble, Diego Pardo, Farbod Farshidian |
| 4 | Licensed under Apache2 license (see LICENSE file in main directory) |
| 5 | **********************************************************************************************************************/ |
| 6 | |
| 7 | |
| 8 | #ifndef SRC_HYQ_CODEGEN_HELPERFUNCTIONS_H_ |
| 9 | #define SRC_HYQ_CODEGEN_HELPERFUNCTIONS_H_ |
| 10 | |
| 11 | #include <ct/core/core.h> |
| 12 | #include <ct/rbd/rbd.h> |
| 13 | #include <ct/models/HyQ/HyQ.h> |
| 14 | |
| 15 | namespace ct { |
| 16 | namespace models { |
| 17 | namespace HyQ { |
| 18 | |
| 19 | // a floating base forward-dynamic system templated on scalar type |
| 20 | typedef ct::rbd::FloatingBaseFDSystem<ct::rbd::HyQ::tpl::Dynamics<double>, false> HyQSystem; |
| 21 | |
| 22 | template <typename SCALAR> |
| 23 | using ContactModel = ct::rbd::EEContactModel<ct::rbd::HyQ::tpl::Kinematics<SCALAR>>; |
| 24 | |
| 25 | // extract dimensions |
| 26 | const size_t state_dim = HyQSystem::STATE_DIM; |
| 27 | const size_t control_dim = HyQSystem::CONTROL_DIM; |
| 28 | |
| 29 | // shortcut for number of joints |
| 30 | const size_t njoints = HyQSystem::Kinematics::NJOINTS; |
| 31 | const size_t nEE = HyQSystem::Kinematics::NUM_EE; |
| 32 | |
| 33 | template <typename SCALAR> |
| 34 | Eigen::Matrix<SCALAR, control_dim + 6, 1> hyqInverseDynamics(const Eigen::Matrix<SCALAR, state_dim + 18, 1>& x) |
| 35 | { |
| 36 | ct::rbd::HyQ::tpl::Dynamics<SCALAR> hyqDynamics; |
| 37 | ct::rbd::tpl::RBDState<njoints, SCALAR> hyqState; |
| 38 | |
| 39 | // we assume x contains: q, q_dot, q_ddot |
| 40 | hyqState.fromStateVectorEulerXyz(x.template topRows<state_dim>()); |
| 41 | |
| 42 | ct::rbd::tpl::RigidBodyAcceleration<SCALAR> base_a(x.template segment<6>(state_dim)); |
| 43 | ct::rbd::tpl::JointAcceleration<njoints, SCALAR> qdd(x.template bottomRows<njoints>()); |
| 44 | |
| 45 | typename ct::rbd::HyQ::tpl::Dynamics<SCALAR>::ExtLinkForces_t fext(Eigen::Matrix<SCALAR, 6, 1>::Zero()); //zero |
| 46 | |
| 47 | // output |
| 48 | ct::core::ControlVector<control_dim, SCALAR> u; |
| 49 | typename ct::rbd::HyQ::tpl::Dynamics<SCALAR>::ForceVector_t base_w; |
| 50 | |
| 51 | hyqDynamics.FloatingBaseFullyActuatedID(hyqState, base_a, qdd, fext, base_w, u); |
| 52 | |
| 53 | Eigen::Matrix<SCALAR, control_dim + 6, 1> y; |
| 54 | y << base_w, u; |
| 55 | return y; |
| 56 | } |
| 57 | |
| 58 | template <typename SCALAR> |
| 59 | Eigen::Matrix<SCALAR, nEE * 6, 1> hyqForwardKinematics(const Eigen::Matrix<SCALAR, state_dim, 1>& x) |
| 60 | { |
| 61 | ct::rbd::HyQ::tpl::Kinematics<SCALAR> hyqKinematics; |
| 62 | ct::rbd::tpl::RBDState<njoints, SCALAR> hyqState; |
| 63 | hyqState.fromStateVectorEulerXyz(x.template topRows<state_dim>()); |
| 64 | |
| 65 | // output vector: positions and velocities for all endeffectors |
| 66 | Eigen::Matrix<SCALAR, nEE * 6, 1> y; |
| 67 | |
| 68 | for (size_t i = 0; i < nEE; i++) |
| 69 | { |
| 70 | y.template segment<3>(i * 6) = |
| 71 | hyqKinematics.getEEPositionInWorld(i, hyqState.basePose(), hyqState.jointPositions()).toImplementation(); |
| 72 | y.template segment<3>(i * 6 + 3) = hyqKinematics.getEEVelocityInWorld(i, hyqState).toImplementation(); |
| 73 | } |
| 74 | |
| 75 | return y; |
| 76 | } |
| 77 | |
| 78 | template <typename SCALAR> |
| 79 | Eigen::Matrix<SCALAR, state_dim, 1> hyqContactModelForwardDynamics( |
| 80 | const Eigen::Matrix<SCALAR, state_dim + control_dim + 1, 1>& x) // state, input, time |
| 81 | { |
| 82 | ct::rbd::FloatingBaseFDSystem<ct::rbd::HyQ::tpl::Dynamics<SCALAR>, false> system; |
| 83 | std::shared_ptr<ContactModel<SCALAR>> contactModel = |
| 84 | std::shared_ptr<ContactModel<SCALAR>>(new ContactModel<SCALAR>()); |
| 85 | contactModel->k() = SCALAR(5000); |
| 86 | contactModel->d() = SCALAR(1000); |
| 87 | contactModel->alpha() = SCALAR(100); |
| 88 | contactModel->alpha_n() = SCALAR(100); |
| 89 | contactModel->zOffset() = SCALAR(-0.02); |
| 90 | contactModel->smoothing() = static_cast<typename ContactModel<SCALAR>::VELOCITY_SMOOTHING>( |
| 91 | ContactModel<SCALAR>::VELOCITY_SMOOTHING::SIGMOID); |
| 92 | |
| 93 | system.setContactModel(contactModel); |
| 94 | ct::core::StateVector<state_dim, SCALAR> y; |
| 95 | |
| 96 | system.computeControlledDynamics(x.segment(0, state_dim), SCALAR(0.0), x.segment(state_dim, control_dim), y); |
| 97 | |
| 98 | return y; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | |
| 105 | #endif /* SRC_HYQ_CODEGEN_HELPERFUNCTIONS_H_ */ |