Directory: | ./ |
---|---|
File: | include/crocoddyl/core/numdiff/actuation.hpp |
Date: | 2025-03-26 19:23:43 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 15 | 16 | 93.8% |
Branches: | 23 | 42 | 54.8% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | ||
2 | // BSD 3-Clause License | ||
3 | // | ||
4 | // Copyright (C) 2019-2025, University of Edinburgh, LAAS-CNRS | ||
5 | // Heriot-Watt University | ||
6 | // Copyright note valid unless otherwise stated in individual files. | ||
7 | // All rights reserved. | ||
8 | /////////////////////////////////////////////////////////////////////////////// | ||
9 | |||
10 | #ifndef CROCODDYL_CORE_NUMDIFF_ACTUATION_HPP_ | ||
11 | #define CROCODDYL_CORE_NUMDIFF_ACTUATION_HPP_ | ||
12 | |||
13 | #include <iostream> | ||
14 | #include <vector> | ||
15 | |||
16 | #include "crocoddyl/core/actuation-base.hpp" | ||
17 | #include "crocoddyl/core/fwd.hpp" | ||
18 | |||
19 | namespace crocoddyl { | ||
20 | |||
21 | /** | ||
22 | * @brief This class computes the numerical differentiation of an actuation | ||
23 | * model. | ||
24 | * | ||
25 | * It computes the Jacobian of the residual model via numerical differentiation, | ||
26 | * i.e., \f$\frac{\partial\boldsymbol{\tau}}{\partial\mathbf{x}}\f$ and | ||
27 | * \f$\frac{\partial\boldsymbol{\tau}}{\partial\mathbf{u}}\f$ which denote the | ||
28 | * Jacobians of the actuation function | ||
29 | * \f$\boldsymbol{\tau}(\mathbf{x},\mathbf{u})\f$. | ||
30 | * | ||
31 | * \sa `ActuationModelAbstractTpl()`, `calcDiff()` | ||
32 | */ | ||
33 | template <typename _Scalar> | ||
34 | class ActuationModelNumDiffTpl : public ActuationModelAbstractTpl<_Scalar> { | ||
35 | public: | ||
36 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW | ||
37 | ✗ | CROCODDYL_DERIVED_CAST(ActuationModelBase, ActuationModelNumDiffTpl) | |
38 | |||
39 | typedef _Scalar Scalar; | ||
40 | typedef MathBaseTpl<Scalar> MathBase; | ||
41 | typedef ActuationModelAbstractTpl<Scalar> Base; | ||
42 | typedef ActuationDataNumDiffTpl<Scalar> Data; | ||
43 | typedef ActuationDataAbstractTpl<Scalar> ActuationDataAbstract; | ||
44 | typedef typename MathBase::VectorXs VectorXs; | ||
45 | typedef typename MathBase::MatrixXs MatrixXs; | ||
46 | |||
47 | /** | ||
48 | * @brief Initialize the numdiff residual model | ||
49 | * | ||
50 | * @param model Actuation model that we want to apply the numerical | ||
51 | * differentiation | ||
52 | */ | ||
53 | explicit ActuationModelNumDiffTpl(std::shared_ptr<Base> model); | ||
54 | |||
55 | /** | ||
56 | * @brief Destroy the numdiff actuation model | ||
57 | */ | ||
58 | 144 | virtual ~ActuationModelNumDiffTpl() = default; | |
59 | |||
60 | /** | ||
61 | * @brief @copydoc Base::calc() | ||
62 | */ | ||
63 | virtual void calc(const std::shared_ptr<ActuationDataAbstract>& data, | ||
64 | const Eigen::Ref<const VectorXs>& x, | ||
65 | const Eigen::Ref<const VectorXs>& u) override; | ||
66 | |||
67 | /** | ||
68 | * @brief @copydoc Base::calc(const std::shared_ptr<ActuationDataAbstract>& | ||
69 | * data, const Eigen::Ref<const VectorXs>& x) | ||
70 | */ | ||
71 | virtual void calc(const std::shared_ptr<ActuationDataAbstract>& data, | ||
72 | const Eigen::Ref<const VectorXs>& x); | ||
73 | |||
74 | /** | ||
75 | * @brief @copydoc Base::calcDiff() | ||
76 | */ | ||
77 | virtual void calcDiff(const std::shared_ptr<ActuationDataAbstract>& data, | ||
78 | const Eigen::Ref<const VectorXs>& x, | ||
79 | const Eigen::Ref<const VectorXs>& u) override; | ||
80 | |||
81 | /** | ||
82 | * @brief @copydoc Base::calcDiff(const | ||
83 | * std::shared_ptr<ActuationDataAbstract>& data, const Eigen::Ref<const | ||
84 | * VectorXs>& x) | ||
85 | */ | ||
86 | virtual void calcDiff(const std::shared_ptr<ActuationDataAbstract>& data, | ||
87 | const Eigen::Ref<const VectorXs>& x); | ||
88 | |||
89 | /** | ||
90 | * @brief @copydoc Base::commands() | ||
91 | */ | ||
92 | virtual void commands(const std::shared_ptr<ActuationDataAbstract>& data, | ||
93 | const Eigen::Ref<const VectorXs>& x, | ||
94 | const Eigen::Ref<const VectorXs>& tau) override; | ||
95 | |||
96 | /** | ||
97 | * @brief @copydoc Base::torqueTransform() | ||
98 | */ | ||
99 | virtual void torqueTransform( | ||
100 | const std::shared_ptr<ActuationDataAbstract>& data, | ||
101 | const Eigen::Ref<const VectorXs>& x, | ||
102 | const Eigen::Ref<const VectorXs>& u) override; | ||
103 | |||
104 | /** | ||
105 | * @brief @copydoc Base::createData() | ||
106 | */ | ||
107 | virtual std::shared_ptr<ActuationDataAbstract> createData() override; | ||
108 | |||
109 | template <typename NewScalar> | ||
110 | ActuationModelNumDiffTpl<NewScalar> cast() const; | ||
111 | |||
112 | /** | ||
113 | * @brief Return the original actuation model | ||
114 | */ | ||
115 | const std::shared_ptr<Base>& get_model() const; | ||
116 | |||
117 | /** | ||
118 | * @brief Return the disturbance constant used by the numerical | ||
119 | * differentiation routine | ||
120 | */ | ||
121 | const Scalar get_disturbance() const; | ||
122 | |||
123 | /** | ||
124 | * @brief Modify the disturbance constant used by the numerical | ||
125 | * differentiation routine | ||
126 | */ | ||
127 | void set_disturbance(const Scalar disturbance); | ||
128 | |||
129 | private: | ||
130 | std::shared_ptr<Base> model_; //!< Actuation model hat we want to apply the | ||
131 | //!< numerical differentiation | ||
132 | Scalar e_jac_; //!< Constant used for computing disturbances in Jacobian | ||
133 | //!< calculation | ||
134 | |||
135 | protected: | ||
136 | using Base::nu_; | ||
137 | }; | ||
138 | |||
139 | template <typename _Scalar> | ||
140 | struct ActuationDataNumDiffTpl : public ActuationDataAbstractTpl<_Scalar> { | ||
141 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW | ||
142 | |||
143 | typedef _Scalar Scalar; | ||
144 | typedef MathBaseTpl<Scalar> MathBase; | ||
145 | typedef typename MathBase::VectorXs VectorXs; | ||
146 | typedef ActuationDataAbstractTpl<Scalar> Base; | ||
147 | |||
148 | /** | ||
149 | * @brief Initialize the numdiff actuation data | ||
150 | * | ||
151 | * @tparam Model is the type of the `ActuationModelAbstractTpl`. | ||
152 | * @param model is the object to compute the numerical differentiation from. | ||
153 | */ | ||
154 | template <template <typename Scalar> class Model> | ||
155 | 144 | explicit ActuationDataNumDiffTpl(Model<Scalar>* const model) | |
156 | : Base(model), | ||
157 |
3/6✓ Branch 2 taken 72 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 72 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 72 times.
✗ Branch 10 not taken.
|
144 | dx(model->get_model()->get_state()->get_ndx()), |
158 |
2/4✓ Branch 3 taken 72 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 72 times.
✗ Branch 7 not taken.
|
144 | du(model->get_model()->get_nu()), |
159 |
3/6✓ Branch 5 taken 72 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 72 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 72 times.
✗ Branch 13 not taken.
|
288 | xp(model->get_model()->get_state()->get_nx()) { |
160 |
1/2✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
|
144 | dx.setZero(); |
161 |
1/2✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
|
144 | du.setZero(); |
162 |
1/2✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
|
144 | xp.setZero(); |
163 |
2/4✓ Branch 3 taken 72 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 72 times.
✗ Branch 8 not taken.
|
144 | const std::size_t ndx = model->get_model()->get_state()->get_ndx(); |
164 |
1/2✓ Branch 3 taken 72 times.
✗ Branch 4 not taken.
|
144 | const std::size_t nu = model->get_model()->get_nu(); |
165 |
1/2✓ Branch 3 taken 72 times.
✗ Branch 4 not taken.
|
144 | data_0 = model->get_model()->createData(); |
166 |
2/2✓ Branch 0 taken 2864 times.
✓ Branch 1 taken 72 times.
|
5872 | for (std::size_t i = 0; i < ndx; ++i) { |
167 |
2/4✓ Branch 3 taken 2864 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 2864 times.
✗ Branch 7 not taken.
|
5728 | data_x.push_back(model->get_model()->createData()); |
168 | } | ||
169 |
2/2✓ Branch 0 taken 1344 times.
✓ Branch 1 taken 72 times.
|
2832 | for (std::size_t i = 0; i < nu; ++i) { |
170 |
2/4✓ Branch 3 taken 1344 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1344 times.
✗ Branch 7 not taken.
|
2688 | data_u.push_back(model->get_model()->createData()); |
171 | } | ||
172 | } | ||
173 | |||
174 | Scalar x_norm; //!< Norm of the state vector | ||
175 | Scalar | ||
176 | xh_jac; //!< Disturbance value used for computing \f$ \ell_\mathbf{x} \f$ | ||
177 | Scalar | ||
178 | uh_jac; //!< Disturbance value used for computing \f$ \ell_\mathbf{u} \f$ | ||
179 | VectorXs dx; //!< State disturbance | ||
180 | VectorXs du; //!< Control disturbance | ||
181 | VectorXs xp; //!< The integrated state from the disturbance on one DoF "\f$ | ||
182 | //!< \int x dx_i \f$" | ||
183 | std::shared_ptr<Base> data_0; //!< The data that contains the final results | ||
184 | std::vector<std::shared_ptr<Base> > | ||
185 | data_x; //!< The temporary data associated with the state variation | ||
186 | std::vector<std::shared_ptr<Base> > | ||
187 | data_u; //!< The temporary data associated with the control variation | ||
188 | |||
189 | using Base::dtau_du; | ||
190 | using Base::dtau_dx; | ||
191 | using Base::tau; | ||
192 | }; | ||
193 | |||
194 | } // namespace crocoddyl | ||
195 | |||
196 | /* --- Details -------------------------------------------------------------- */ | ||
197 | /* --- Details -------------------------------------------------------------- */ | ||
198 | /* --- Details -------------------------------------------------------------- */ | ||
199 | #include "crocoddyl/core/numdiff/actuation.hxx" | ||
200 | |||
201 | #endif // CROCODDYL_CORE_NUMDIFF_ACTUATION_HPP_ | ||
202 |