blob: c4a235184f2caa99a4291e30a821fc3b8e6e1825 [file] [log] [blame]
Austin Schuh7400da02018-01-28 19:54:58 -08001/**********************************************************************************************************************
2This file is part of the Control Toolbox (https://adrlab.bitbucket.io/ct), copyright by ETH Zurich, Google Inc.
3Authors: Michael Neunert, Markus Giftthaler, Markus Stäuble, Diego Pardo, Farbod Farshidian
4Licensed 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
15namespace ct {
16namespace models {
17namespace HyQ {
18
19// a floating base forward-dynamic system templated on scalar type
20typedef ct::rbd::FloatingBaseFDSystem<ct::rbd::HyQ::tpl::Dynamics<double>, false> HyQSystem;
21
22template <typename SCALAR>
23using ContactModel = ct::rbd::EEContactModel<ct::rbd::HyQ::tpl::Kinematics<SCALAR>>;
24
25// extract dimensions
26const size_t state_dim = HyQSystem::STATE_DIM;
27const size_t control_dim = HyQSystem::CONTROL_DIM;
28
29// shortcut for number of joints
30const size_t njoints = HyQSystem::Kinematics::NJOINTS;
31const size_t nEE = HyQSystem::Kinematics::NUM_EE;
32
33template <typename SCALAR>
34Eigen::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
58template <typename SCALAR>
59Eigen::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
78template <typename SCALAR>
79Eigen::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_ */