Crocoddyl
floating-base.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2022, 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/core/utils/exception.hpp"
15 #include "crocoddyl/multibody/fwd.hpp"
16 #include "crocoddyl/multibody/states/multibody.hpp"
17 
18 namespace crocoddyl {
19 
35 template <typename _Scalar>
37  : public ActuationModelAbstractTpl<_Scalar> {
38  public:
39  typedef _Scalar Scalar;
44  typedef typename MathBase::VectorXs VectorXs;
45  typedef typename MathBase::MatrixXs MatrixXs;
46 
53  explicit ActuationModelFloatingBaseTpl(std::shared_ptr<StateMultibody> state)
54  : Base(state,
55  state->get_nv() -
56  state->get_pinocchio()
57  ->joints[(
58  state->get_pinocchio()->existJointName("root_joint")
59  ? state->get_pinocchio()->getJointId("root_joint")
60  : 0)]
61  .nv()) {};
62  virtual ~ActuationModelFloatingBaseTpl() {};
63 
72  virtual void calc(const std::shared_ptr<Data>& data,
73  const Eigen::Ref<const VectorXs>& /*x*/,
74  const Eigen::Ref<const VectorXs>& u) {
75  if (static_cast<std::size_t>(u.size()) != nu_) {
76  throw_pretty(
77  "Invalid argument: " << "u has wrong dimension (it should be " +
78  std::to_string(nu_) + ")");
79  }
80  data->tau.tail(nu_) = u;
81  };
82 
90 #ifndef NDEBUG
91  virtual void calcDiff(const std::shared_ptr<Data>& data,
92  const Eigen::Ref<const VectorXs>& /*x*/,
93  const Eigen::Ref<const VectorXs>& /*u*/) {
94 #else
95  virtual void calcDiff(const std::shared_ptr<Data>&,
96  const Eigen::Ref<const VectorXs>& /*x*/,
97  const Eigen::Ref<const VectorXs>& /*u*/) {
98 #endif
99  // The derivatives has constant values which were set in createData.
100  assert_pretty(data->dtau_dx.isZero(), "dtau_dx has wrong value");
101  assert_pretty(MatrixXs(data->dtau_du).isApprox(dtau_du_),
102  "dtau_du has wrong value");
103  };
104 
105  virtual void commands(const std::shared_ptr<Data>& data,
106  const Eigen::Ref<const VectorXs>&,
107  const Eigen::Ref<const VectorXs>& tau) {
108  if (static_cast<std::size_t>(tau.size()) != state_->get_nv()) {
109  throw_pretty(
110  "Invalid argument: " << "tau has wrong dimension (it should be " +
111  std::to_string(state_->get_nv()) + ")");
112  }
113  data->u = tau.tail(nu_);
114  }
115 
116 #ifndef NDEBUG
117  virtual void torqueTransform(const std::shared_ptr<Data>& data,
118  const Eigen::Ref<const VectorXs>&,
119  const Eigen::Ref<const VectorXs>&) {
120 #else
121  virtual void torqueTransform(const std::shared_ptr<Data>&,
122  const Eigen::Ref<const VectorXs>&,
123  const Eigen::Ref<const VectorXs>&) {
124 #endif
125  // The torque transform has constant values which were set in createData.
126  assert_pretty(MatrixXs(data->Mtau).isApprox(Mtau_), "Mtau has wrong value");
127  }
128 
134  virtual std::shared_ptr<Data> createData() {
136  std::shared_ptr<StateMultibody> state =
137  std::static_pointer_cast<StateMultibody>(state_);
138  std::shared_ptr<Data> data =
139  std::allocate_shared<Data>(Eigen::aligned_allocator<Data>(), this);
140  const std::size_t root_joint_id =
141  state->get_pinocchio()->existJointName("root_joint")
142  ? state->get_pinocchio()->getJointId("root_joint")
143  : 0;
144  const std::size_t nfb = state->get_pinocchio()->joints[root_joint_id].nv();
145  data->dtau_du.diagonal(-nfb).setOnes();
146  data->Mtau.diagonal(nfb).setOnes();
147  for (std::size_t i = 0; i < nfb; ++i) {
148  data->tau_set[i] = false;
149  }
150 #ifndef NDEBUG
151  dtau_du_ = data->dtau_du;
152  Mtau_ = data->Mtau;
153 #endif
154  return data;
155  };
156 
157  protected:
158  using Base::nu_;
159  using Base::state_;
160 
161 #ifndef NDEBUG
162  private:
163  MatrixXs dtau_du_;
164  MatrixXs Mtau_;
165 #endif
166 };
167 
168 } // namespace crocoddyl
169 
170 #endif // CROCODDYL_MULTIBODY_ACTUATIONS_FLOATING_BASE_HPP_
Abstract class for the actuation-mapping model.
std::shared_ptr< StateAbstract > state_
Model of the state.
std::size_t nu_
Dimension of joint torque inputs.
Floating-base actuation model.
virtual std::shared_ptr< Data > createData()
Create the floating-base actuation data.
std::shared_ptr< StateAbstract > state_
Model of the state.
virtual void commands(const std::shared_ptr< Data > &data, const Eigen::Ref< const VectorXs > &, const Eigen::Ref< const VectorXs > &tau)
Compute the joint torque input from the generalized torques.
virtual void calcDiff(const std::shared_ptr< Data > &data, const Eigen::Ref< const VectorXs > &, const Eigen::Ref< const VectorXs > &)
Compute the Jacobians of the floating-base actuation function.
virtual void calc(const std::shared_ptr< Data > &data, const Eigen::Ref< const VectorXs > &, const Eigen::Ref< const VectorXs > &u)
Compute the floating-base actuation signal from the joint-torque input .
ActuationModelFloatingBaseTpl(std::shared_ptr< StateMultibody > state)
Initialize the floating-base actuation model.
std::size_t nu_
Dimension of joint torque inputs.
virtual void torqueTransform(const std::shared_ptr< Data > &data, const Eigen::Ref< const VectorXs > &, const Eigen::Ref< const VectorXs > &)
Compute the torque transform from generalized torques to joint torque inputs.
State multibody representation.
Definition: multibody.hpp:35