GCC Code Coverage Report


Directory: ./
File: include/crocoddyl/core/actuation-base.hpp
Date: 2025-05-13 10:30:51
Exec Total Coverage
Lines: 0 17 0.0%
Branches: 0 52 0.0%

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_CORE_ACTUATION_BASE_HPP_
11 #define CROCODDYL_CORE_ACTUATION_BASE_HPP_
12
13 #include "crocoddyl/core/fwd.hpp"
14 #include "crocoddyl/core/state-base.hpp"
15
16 namespace crocoddyl {
17
18 class ActuationModelBase {
19 public:
20 virtual ~ActuationModelBase() = default;
21
22 CROCODDYL_BASE_CAST(ActuationModelBase, ActuationModelAbstractTpl)
23 };
24
25 /**
26 * @brief Abstract class for the actuation-mapping model
27 *
28 * The generalized torques \f$\boldsymbol{\tau}\in\mathbb{R}^{nv}\f$ can by any
29 * nonlinear function of the joint-torque inputs
30 * \f$\mathbf{u}\in\mathbb{R}^{nu}\f$, and state point
31 * \f$\mathbf{x}\in\mathbb{R}^{nx}\f$, where `nv`, `nu`, and `ndx` are the
32 * number of joints, dimension of the joint torque input and state manifold,
33 * respectively. Additionally, the generalized torques are also named as the
34 * actuation signals of our system.
35 *
36 * The main computations are carried out in `calc()`, and `calcDiff()`, where
37 * the former computes actuation signal, and the latter computes the Jacobians
38 * of the actuation-mapping function, i.e.,
39 * \f$\frac{\partial\boldsymbol{\tau}}{\partial\mathbf{x}}\f$ and
40 * \f$\frac{\partial\boldsymbol{\tau}}{\partial\mathbf{u}}\f$. Note that
41 * `calcDiff()` requires to run `calc()` first.
42 *
43 * \sa `calc()`, `calcDiff()`, `createData()`
44 */
45 template <typename _Scalar>
46 class ActuationModelAbstractTpl : public ActuationModelBase {
47 public:
48 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
49
50 typedef _Scalar Scalar;
51 typedef MathBaseTpl<Scalar> MathBase;
52 typedef StateAbstractTpl<Scalar> StateAbstract;
53 typedef ActuationDataAbstractTpl<Scalar> ActuationDataAbstract;
54 typedef typename MathBase::VectorXs VectorXs;
55 typedef typename MathBase::MatrixXs MatrixXs;
56
57 /**
58 * @brief Initialize the actuation model
59 *
60 * @param[in] state State description
61 * @param[in] nu Dimension of joint-torque input
62 */
63 ActuationModelAbstractTpl(std::shared_ptr<StateAbstract> state,
64 const std::size_t nu);
65 virtual ~ActuationModelAbstractTpl() = default;
66
67 /**
68 * @brief Compute the actuation signal from the state point
69 * \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$ and joint torque inputs
70 * \f$\mathbf{u}\in\mathbb{R}^{nu}\f$
71 *
72 * @param[in] data Actuation data
73 * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
74 * @param[in] u Joint-torque input \f$\mathbf{u}\in\mathbb{R}^{nu}\f$
75 */
76 virtual void calc(const std::shared_ptr<ActuationDataAbstract>& data,
77 const Eigen::Ref<const VectorXs>& x,
78 const Eigen::Ref<const VectorXs>& u) = 0;
79
80 /**
81 * @brief Ignore the computation of the actuation signal
82 *
83 * It does not update the actuation signal as this function is used in the
84 * terminal nodes of an optimal control problem.
85 *
86 * @param[in] data Actuation data
87 * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
88 */
89 void calc(const std::shared_ptr<ActuationDataAbstract>& data,
90 const Eigen::Ref<const VectorXs>& x);
91
92 /**
93 * @brief Compute the Jacobians of the actuation function
94 *
95 * @param[in] data Actuation data
96 * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
97 * @param[in] u Joint-torque input \f$\mathbf{u}\in\mathbb{R}^{nu}\f$
98 */
99 virtual void calcDiff(const std::shared_ptr<ActuationDataAbstract>& data,
100 const Eigen::Ref<const VectorXs>& x,
101 const Eigen::Ref<const VectorXs>& u) = 0;
102
103 /**
104 * @brief Ignore the computation of the Jacobians of the actuation function
105 *
106 * It does not update the Jacobians of the actuation function as this function
107 * is used in the terminal nodes of an optimal control problem.
108 *
109 * @param[in] data Actuation data
110 * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
111 */
112 void calcDiff(const std::shared_ptr<ActuationDataAbstract>& data,
113 const Eigen::Ref<const VectorXs>& x);
114
115 /**
116 * @brief Compute the joint torque input from the generalized torques
117 *
118 * It stores the results in `ActuationDataAbstractTpl::u`.
119 *
120 * @param[in] data Actuation data
121 * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
122 * @param[in] tau Generalized torques \f$\mathbf{u}\in\mathbb{R}^{nv}\f$
123 */
124 virtual void commands(const std::shared_ptr<ActuationDataAbstract>& data,
125 const Eigen::Ref<const VectorXs>& x,
126 const Eigen::Ref<const VectorXs>& tau) = 0;
127
128 /**
129 * @brief Compute the torque transform from generalized torques to joint
130 * torque inputs
131 *
132 * It stores the results in `ActuationDataAbstractTpl::Mtau`.
133 *
134 * @param[in] data Actuation data
135 * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
136 * @param[in] tau Joint-torque inputs \f$\mathbf{u}\in\mathbb{R}^{nu}\f$
137 */
138 virtual void torqueTransform(
139 const std::shared_ptr<ActuationDataAbstract>& data,
140 const Eigen::Ref<const VectorXs>& x, const Eigen::Ref<const VectorXs>& u);
141 /**
142 * @brief Create the actuation data
143 *
144 * @return the actuation data
145 */
146 virtual std::shared_ptr<ActuationDataAbstract> createData();
147
148 /**
149 * @brief Return the dimension of the joint-torque input
150 */
151 std::size_t get_nu() const;
152
153 /**
154 * @brief Return the state
155 */
156 const std::shared_ptr<StateAbstract>& get_state() const;
157
158 /**
159 * @brief Print information on the actuation model
160 */
161 template <class Scalar>
162 friend std::ostream& operator<<(
163 std::ostream& os, const ActuationModelAbstractTpl<Scalar>& model);
164
165 /**
166 * @brief Print relevant information of the residual model
167 *
168 * @param[out] os Output stream object
169 */
170 virtual void print(std::ostream& os) const;
171
172 protected:
173 std::size_t nu_; //!< Dimension of joint torque inputs
174 std::shared_ptr<StateAbstract> state_; //!< Model of the state
175 ActuationModelAbstractTpl() : nu_(0), state_(nullptr) {};
176 };
177
178 template <typename _Scalar>
179 struct ActuationDataAbstractTpl {
180 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
181
182 typedef _Scalar Scalar;
183 typedef MathBaseTpl<Scalar> MathBase;
184 typedef typename MathBase::VectorXs VectorXs;
185 typedef typename MathBase::MatrixXs MatrixXs;
186
187 template <template <typename Scalar> class Model>
188 explicit ActuationDataAbstractTpl(Model<Scalar>* const model)
189 : tau(model->get_state()->get_nv()),
190 u(model->get_nu()),
191 dtau_dx(model->get_state()->get_nv(), model->get_state()->get_ndx()),
192 dtau_du(model->get_state()->get_nv(), model->get_nu()),
193 Mtau(model->get_nu(), model->get_state()->get_nv()),
194 tau_set(model->get_state()->get_nv(), true) {
195 tau.setZero();
196 u.setZero();
197 dtau_dx.setZero();
198 dtau_du.setZero();
199 Mtau.setZero();
200 }
201 virtual ~ActuationDataAbstractTpl() = default;
202
203 VectorXs tau; //!< Generalized torques
204 VectorXs u; //!< Joint torques
205 MatrixXs dtau_dx; //!< Partial derivatives of the actuation model w.r.t. the
206 //!< state point
207 MatrixXs dtau_du; //!< Partial derivatives of the actuation model w.r.t. the
208 //!< joint torque input
209 MatrixXs Mtau; //!< Torque transform from generalized torques to joint torque
210 //!< inputs
211 std::vector<bool> tau_set; //!< True for joints that are actuacted
212 };
213
214 } // namespace crocoddyl
215
216 /* --- Details -------------------------------------------------------------- */
217 /* --- Details -------------------------------------------------------------- */
218 /* --- Details -------------------------------------------------------------- */
219 #include "crocoddyl/core/actuation-base.hxx"
220
221 CROCODDYL_DECLARE_EXTERN_TEMPLATE_CLASS(crocoddyl::ActuationModelAbstractTpl)
222 CROCODDYL_DECLARE_EXTERN_TEMPLATE_STRUCT(crocoddyl::ActuationDataAbstractTpl)
223
224 #endif // CROCODDYL_CORE_ACTUATION_BASE_HPP_
225