Crocoddyl
Loading...
Searching...
No Matches
floating-base.hpp
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.
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
17namespace crocoddyl {
18
34template <typename _Scalar>
36 : public ActuationModelAbstractTpl<_Scalar> {
37 public:
38 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
40
41 typedef _Scalar Scalar;
42 typedef MathBaseTpl<Scalar> MathBase;
45 typedef StateMultibodyTpl<Scalar> StateMultibody;
46 typedef typename MathBase::VectorXs VectorXs;
47 typedef typename MathBase::MatrixXs MatrixXs;
48
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 Base::u_lb_ = Scalar(-1.) * state->get_pinocchio()->effortLimit.tail(nu_);
65 Base::u_ub_ = state->get_pinocchio()->effortLimit.tail(nu_);
66 };
67 virtual ~ActuationModelFloatingBaseTpl() = default;
68
77 virtual void calc(const std::shared_ptr<Data>& data,
78 const Eigen::Ref<const VectorXs>& /*x*/,
79 const Eigen::Ref<const VectorXs>& u) override {
80 if (static_cast<std::size_t>(u.size()) != nu_) {
81 throw_pretty(
82 "Invalid argument: " << "u has wrong dimension (it should be " +
83 std::to_string(nu_) + ")");
84 }
85 data->tau.tail(nu_) = u;
86 };
87
95#ifndef NDEBUG
96 virtual void calcDiff(const std::shared_ptr<Data>& data,
97 const Eigen::Ref<const VectorXs>& /*x*/,
98 const Eigen::Ref<const VectorXs>& /*u*/) override {
99#else
100 virtual void calcDiff(const std::shared_ptr<Data>&,
101 const Eigen::Ref<const VectorXs>& /*x*/,
102 const Eigen::Ref<const VectorXs>& /*u*/) override {
103#endif
104 // The derivatives has constant values which were set in createData.
105 assert_pretty(data->dtau_dx.isZero(), "dtau_dx has wrong value");
106 assert_pretty(MatrixXs(data->dtau_du).isApprox(dtau_du_),
107 "dtau_du has wrong value");
108 };
109
110 virtual void commands(const std::shared_ptr<Data>& data,
111 const Eigen::Ref<const VectorXs>&,
112 const Eigen::Ref<const VectorXs>& tau) override {
113 if (static_cast<std::size_t>(tau.size()) != state_->get_nv()) {
114 throw_pretty(
115 "Invalid argument: " << "tau has wrong dimension (it should be " +
116 std::to_string(state_->get_nv()) + ")");
117 }
118 data->u = tau.tail(nu_);
119 }
120
121#ifndef NDEBUG
122 virtual void torqueTransform(const std::shared_ptr<Data>& data,
123 const Eigen::Ref<const VectorXs>&,
124 const Eigen::Ref<const VectorXs>&) override {
125#else
126 virtual void torqueTransform(const std::shared_ptr<Data>&,
127 const Eigen::Ref<const VectorXs>&,
128 const Eigen::Ref<const VectorXs>&) override {
129#endif
130 // The torque transform has constant values which were set in createData.
131 assert_pretty(MatrixXs(data->Mtau).isApprox(Mtau_), "Mtau has wrong value");
132 }
133
139 virtual std::shared_ptr<Data> createData() override {
140 typedef StateMultibodyTpl<Scalar> StateMultibody;
141 std::shared_ptr<StateMultibody> state =
142 std::static_pointer_cast<StateMultibody>(state_);
143 std::shared_ptr<Data> data =
144 std::allocate_shared<Data>(Eigen::aligned_allocator<Data>(), this);
145 const std::size_t root_joint_id =
146 state->get_pinocchio()->existJointName("root_joint")
147 ? state->get_pinocchio()->getJointId("root_joint")
148 : 0;
149 const std::size_t nfb = state->get_pinocchio()->joints[root_joint_id].nv();
150 data->dtau_du.diagonal(-nfb).setOnes();
151 data->Mtau.diagonal(nfb).setOnes();
152 for (std::size_t i = 0; i < nfb; ++i) {
153 data->tau_set[i] = false;
154 }
155#ifndef NDEBUG
156 dtau_du_ = data->dtau_du;
157 Mtau_ = data->Mtau;
158#endif
159 return data;
160 };
161
162 template <typename NewScalar>
165 typedef StateMultibodyTpl<NewScalar> StateType;
166 ReturnType ret(std::static_pointer_cast<StateType>(
167 state_->template cast<NewScalar>()));
168 return ret;
169 }
170
176 virtual void print(std::ostream& os) const override {
177 os << "ActuationModelFloatingBase {nu=" << nu_
178 << ", nv=" << state_->get_nv() << "}";
179 }
180
181 protected:
182 using Base::nu_;
183 using Base::state_;
184
185#ifndef NDEBUG
186 private:
187 MatrixXs dtau_du_;
188 MatrixXs Mtau_;
189#endif
190};
191
192} // namespace crocoddyl
193
194CROCODDYL_DECLARE_EXTERN_TEMPLATE_CLASS(
195 crocoddyl::ActuationModelFloatingBaseTpl)
196
197#endif // CROCODDYL_MULTIBODY_ACTUATIONS_FLOATING_BASE_HPP_
ActuationModelAbstractTpl(std::shared_ptr< StateAbstract > state, const std::size_t nu)
Initialize the actuation model.
Floating-base actuation model.
std::shared_ptr< StateAbstract > state_
virtual void calc(const std::shared_ptr< Data > &data, const Eigen::Ref< const VectorXs > &, const Eigen::Ref< const VectorXs > &u) override
Compute the floating-base actuation signal from the joint-torque input .
virtual std::shared_ptr< Data > createData() override
Create the floating-base actuation data.
virtual void commands(const std::shared_ptr< Data > &data, const Eigen::Ref< const VectorXs > &, const Eigen::Ref< const VectorXs > &tau) override
Compute the joint torque input from the generalized torques.
virtual void torqueTransform(const std::shared_ptr< Data > &data, const Eigen::Ref< const VectorXs > &, const Eigen::Ref< const VectorXs > &) override
Compute the torque transform from generalized torques to joint torque inputs.
ActuationModelFloatingBaseTpl(std::shared_ptr< StateMultibody > state)
Initialize the floating-base actuation model.
virtual void calcDiff(const std::shared_ptr< Data > &data, const Eigen::Ref< const VectorXs > &, const Eigen::Ref< const VectorXs > &) override
Compute the Jacobians of the floating-base actuation function.
virtual void print(std::ostream &os) const override
Print relevant information of the joint-effort residual.
State multibody representation.
Definition multibody.hpp:34