| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /////////////////////////////////////////////////////////////////////////////// | ||
| 2 | // BSD 3-Clause License | ||
| 3 | // | ||
| 4 | // Copyright (C) 2019-2025, LAAS-CNRS, University of Edinburgh, | ||
| 5 | // Heriot-Watt University | ||
| 6 | // Copyright note valid unless otherwise stated in individual files. | ||
| 7 | // All rights reserved. | ||
| 8 | /////////////////////////////////////////////////////////////////////////////// | ||
| 9 | |||
| 10 | #ifndef CROCODDYL_MULTIBODY_ACTUATIONS_FLOATING_BASE_HPP_ | ||
| 11 | #define CROCODDYL_MULTIBODY_ACTUATIONS_FLOATING_BASE_HPP_ | ||
| 12 | |||
| 13 | #include "crocoddyl/core/actuation-base.hpp" | ||
| 14 | #include "crocoddyl/multibody/fwd.hpp" | ||
| 15 | #include "crocoddyl/multibody/states/multibody.hpp" | ||
| 16 | |||
| 17 | namespace crocoddyl { | ||
| 18 | |||
| 19 | /** | ||
| 20 | * @brief Floating-base actuation model | ||
| 21 | * | ||
| 22 | * It considers the first joint, defined in the Pinocchio model, as the | ||
| 23 | * floating-base joints. Then, this joint (that might have various DoFs) is | ||
| 24 | * unactuated. | ||
| 25 | * | ||
| 26 | * The main computations are carrying out in `calc`, and `calcDiff`, where the | ||
| 27 | * former computes actuation signal \f$\mathbf{a}\f$ from a given joint-torque | ||
| 28 | * input \f$\mathbf{u}\f$ and state point \f$\mathbf{x}\f$, and the latter | ||
| 29 | * computes the Jacobians of the actuation-mapping function. Note that | ||
| 30 | * `calcDiff` requires to run `calc` first. | ||
| 31 | * | ||
| 32 | * \sa `ActuationModelAbstractTpl`, `calc()`, `calcDiff()`, `createData()` | ||
| 33 | */ | ||
| 34 | template <typename _Scalar> | ||
| 35 | class ActuationModelFloatingBaseTpl | ||
| 36 | : public ActuationModelAbstractTpl<_Scalar> { | ||
| 37 | public: | ||
| 38 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW | ||
| 39 | ✗ | CROCODDYL_DERIVED_CAST(ActuationModelBase, ActuationModelFloatingBaseTpl) | |
| 40 | |||
| 41 | typedef _Scalar Scalar; | ||
| 42 | typedef MathBaseTpl<Scalar> MathBase; | ||
| 43 | typedef ActuationModelAbstractTpl<Scalar> Base; | ||
| 44 | typedef ActuationDataAbstractTpl<Scalar> Data; | ||
| 45 | typedef StateMultibodyTpl<Scalar> StateMultibody; | ||
| 46 | typedef typename MathBase::VectorXs VectorXs; | ||
| 47 | typedef typename MathBase::MatrixXs MatrixXs; | ||
| 48 | |||
| 49 | /** | ||
| 50 | * @brief Initialize the floating-base actuation model | ||
| 51 | * | ||
| 52 | * @param[in] state State of a multibody system | ||
| 53 | * @param[in] nu Dimension of joint-torque vector | ||
| 54 | */ | ||
| 55 | ✗ | explicit ActuationModelFloatingBaseTpl(std::shared_ptr<StateMultibody> state) | |
| 56 | : Base(state, | ||
| 57 | ✗ | state->get_nv() - | |
| 58 | ✗ | state->get_pinocchio() | |
| 59 | ✗ | ->joints[( | |
| 60 | ✗ | state->get_pinocchio()->existJointName("root_joint") | |
| 61 | ✗ | ? state->get_pinocchio()->getJointId("root_joint") | |
| 62 | : 0)] | ||
| 63 | ✗ | .nv()) {}; | |
| 64 | ✗ | virtual ~ActuationModelFloatingBaseTpl() = default; | |
| 65 | |||
| 66 | /** | ||
| 67 | * @brief Compute the floating-base actuation signal from the joint-torque | ||
| 68 | * input \f$\mathbf{u}\in\mathbb{R}^{nu}\f$ | ||
| 69 | * | ||
| 70 | * @param[in] data Actuation data | ||
| 71 | * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$ | ||
| 72 | * @param[in] u Joint-torque input \f$\mathbf{u}\in\mathbb{R}^{nu}\f$ | ||
| 73 | */ | ||
| 74 | ✗ | virtual void calc(const std::shared_ptr<Data>& data, | |
| 75 | const Eigen::Ref<const VectorXs>& /*x*/, | ||
| 76 | const Eigen::Ref<const VectorXs>& u) override { | ||
| 77 | ✗ | if (static_cast<std::size_t>(u.size()) != nu_) { | |
| 78 | ✗ | throw_pretty( | |
| 79 | "Invalid argument: " << "u has wrong dimension (it should be " + | ||
| 80 | std::to_string(nu_) + ")"); | ||
| 81 | } | ||
| 82 | ✗ | data->tau.tail(nu_) = u; | |
| 83 | ✗ | }; | |
| 84 | |||
| 85 | /** | ||
| 86 | * @brief Compute the Jacobians of the floating-base actuation function | ||
| 87 | * | ||
| 88 | * @param[in] data Actuation data | ||
| 89 | * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$ | ||
| 90 | * @param[in] u Joint-torque input \f$\mathbf{u}\in\mathbb{R}^{nu}\f$ | ||
| 91 | */ | ||
| 92 | #ifndef NDEBUG | ||
| 93 | ✗ | virtual void calcDiff(const std::shared_ptr<Data>& data, | |
| 94 | const Eigen::Ref<const VectorXs>& /*x*/, | ||
| 95 | const Eigen::Ref<const VectorXs>& /*u*/) override { | ||
| 96 | #else | ||
| 97 | virtual void calcDiff(const std::shared_ptr<Data>&, | ||
| 98 | const Eigen::Ref<const VectorXs>& /*x*/, | ||
| 99 | const Eigen::Ref<const VectorXs>& /*u*/) override { | ||
| 100 | #endif | ||
| 101 | // The derivatives has constant values which were set in createData. | ||
| 102 | ✗ | assert_pretty(data->dtau_dx.isZero(), "dtau_dx has wrong value"); | |
| 103 | ✗ | assert_pretty(MatrixXs(data->dtau_du).isApprox(dtau_du_), | |
| 104 | "dtau_du has wrong value"); | ||
| 105 | ✗ | }; | |
| 106 | |||
| 107 | ✗ | virtual void commands(const std::shared_ptr<Data>& data, | |
| 108 | const Eigen::Ref<const VectorXs>&, | ||
| 109 | const Eigen::Ref<const VectorXs>& tau) override { | ||
| 110 | ✗ | if (static_cast<std::size_t>(tau.size()) != state_->get_nv()) { | |
| 111 | ✗ | throw_pretty( | |
| 112 | "Invalid argument: " << "tau has wrong dimension (it should be " + | ||
| 113 | std::to_string(state_->get_nv()) + ")"); | ||
| 114 | } | ||
| 115 | ✗ | data->u = tau.tail(nu_); | |
| 116 | ✗ | } | |
| 117 | |||
| 118 | #ifndef NDEBUG | ||
| 119 | ✗ | virtual void torqueTransform(const std::shared_ptr<Data>& data, | |
| 120 | const Eigen::Ref<const VectorXs>&, | ||
| 121 | const Eigen::Ref<const VectorXs>&) override { | ||
| 122 | #else | ||
| 123 | virtual void torqueTransform(const std::shared_ptr<Data>&, | ||
| 124 | const Eigen::Ref<const VectorXs>&, | ||
| 125 | const Eigen::Ref<const VectorXs>&) override { | ||
| 126 | #endif | ||
| 127 | // The torque transform has constant values which were set in createData. | ||
| 128 | ✗ | assert_pretty(MatrixXs(data->Mtau).isApprox(Mtau_), "Mtau has wrong value"); | |
| 129 | ✗ | } | |
| 130 | |||
| 131 | /** | ||
| 132 | * @brief Create the floating-base actuation data | ||
| 133 | * | ||
| 134 | * @return the actuation data | ||
| 135 | */ | ||
| 136 | ✗ | virtual std::shared_ptr<Data> createData() override { | |
| 137 | typedef StateMultibodyTpl<Scalar> StateMultibody; | ||
| 138 | ✗ | std::shared_ptr<StateMultibody> state = | |
| 139 | ✗ | std::static_pointer_cast<StateMultibody>(state_); | |
| 140 | ✗ | std::shared_ptr<Data> data = | |
| 141 | ✗ | std::allocate_shared<Data>(Eigen::aligned_allocator<Data>(), this); | |
| 142 | ✗ | const std::size_t root_joint_id = | |
| 143 | ✗ | state->get_pinocchio()->existJointName("root_joint") | |
| 144 | ✗ | ? state->get_pinocchio()->getJointId("root_joint") | |
| 145 | : 0; | ||
| 146 | ✗ | const std::size_t nfb = state->get_pinocchio()->joints[root_joint_id].nv(); | |
| 147 | ✗ | data->dtau_du.diagonal(-nfb).setOnes(); | |
| 148 | ✗ | data->Mtau.diagonal(nfb).setOnes(); | |
| 149 | ✗ | for (std::size_t i = 0; i < nfb; ++i) { | |
| 150 | ✗ | data->tau_set[i] = false; | |
| 151 | } | ||
| 152 | #ifndef NDEBUG | ||
| 153 | ✗ | dtau_du_ = data->dtau_du; | |
| 154 | ✗ | Mtau_ = data->Mtau; | |
| 155 | #endif | ||
| 156 | ✗ | return data; | |
| 157 | ✗ | }; | |
| 158 | |||
| 159 | template <typename NewScalar> | ||
| 160 | ✗ | ActuationModelFloatingBaseTpl<NewScalar> cast() const { | |
| 161 | typedef ActuationModelFloatingBaseTpl<NewScalar> ReturnType; | ||
| 162 | typedef StateMultibodyTpl<NewScalar> StateType; | ||
| 163 | ✗ | ReturnType ret(std::static_pointer_cast<StateType>( | |
| 164 | ✗ | state_->template cast<NewScalar>())); | |
| 165 | ✗ | return ret; | |
| 166 | } | ||
| 167 | |||
| 168 | /** | ||
| 169 | * @brief Print relevant information of the joint-effort residual | ||
| 170 | * | ||
| 171 | * @param[out] os Output stream object | ||
| 172 | */ | ||
| 173 | ✗ | virtual void print(std::ostream& os) const override { | |
| 174 | ✗ | os << "ActuationModelFloatingBase {nu=" << nu_ | |
| 175 | ✗ | << ", nv=" << state_->get_nv() << "}"; | |
| 176 | ✗ | } | |
| 177 | |||
| 178 | protected: | ||
| 179 | using Base::nu_; | ||
| 180 | using Base::state_; | ||
| 181 | |||
| 182 | #ifndef NDEBUG | ||
| 183 | private: | ||
| 184 | MatrixXs dtau_du_; | ||
| 185 | MatrixXs Mtau_; | ||
| 186 | #endif | ||
| 187 | }; | ||
| 188 | |||
| 189 | } // namespace crocoddyl | ||
| 190 | |||
| 191 | CROCODDYL_DECLARE_EXTERN_TEMPLATE_CLASS( | ||
| 192 | crocoddyl::ActuationModelFloatingBaseTpl) | ||
| 193 | |||
| 194 | #endif // CROCODDYL_MULTIBODY_ACTUATIONS_FLOATING_BASE_HPP_ | ||
| 195 |