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