Directory: | ./ |
---|---|
File: | include/crocoddyl/core/numdiff/actuation.hpp |
Date: | 2025-01-16 08:47:40 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 15 | 15 | 100.0% |
Branches: | 15 | 26 | 57.7% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | ||
2 | // BSD 3-Clause License | ||
3 | // | ||
4 | // Copyright (C) 2019-2024, 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 | typedef _Scalar Scalar; | ||
37 | typedef MathBaseTpl<Scalar> MathBase; | ||
38 | typedef ActuationModelAbstractTpl<Scalar> Base; | ||
39 | typedef ActuationDataNumDiffTpl<Scalar> Data; | ||
40 | typedef ActuationDataAbstractTpl<Scalar> ActuationDataAbstract; | ||
41 | typedef typename MathBase::VectorXs VectorXs; | ||
42 | typedef typename MathBase::MatrixXs MatrixXs; | ||
43 | |||
44 | /** | ||
45 | * @brief Initialize the numdiff residual model | ||
46 | * | ||
47 | * @param model Actuation model that we want to apply the numerical | ||
48 | * differentiation | ||
49 | */ | ||
50 | explicit ActuationModelNumDiffTpl(boost::shared_ptr<Base> model); | ||
51 | |||
52 | /** | ||
53 | * @brief Destroy the numdiff actuation model | ||
54 | */ | ||
55 | virtual ~ActuationModelNumDiffTpl(); | ||
56 | |||
57 | /** | ||
58 | * @brief @copydoc Base::calc() | ||
59 | */ | ||
60 | virtual void calc(const boost::shared_ptr<ActuationDataAbstract>& data, | ||
61 | const Eigen::Ref<const VectorXs>& x, | ||
62 | const Eigen::Ref<const VectorXs>& u); | ||
63 | |||
64 | /** | ||
65 | * @brief @copydoc Base::calc(const boost::shared_ptr<ActuationDataAbstract>& | ||
66 | * data, const Eigen::Ref<const VectorXs>& x) | ||
67 | */ | ||
68 | virtual void calc(const boost::shared_ptr<ActuationDataAbstract>& data, | ||
69 | const Eigen::Ref<const VectorXs>& x); | ||
70 | |||
71 | /** | ||
72 | * @brief @copydoc Base::calcDiff() | ||
73 | */ | ||
74 | virtual void calcDiff(const boost::shared_ptr<ActuationDataAbstract>& data, | ||
75 | const Eigen::Ref<const VectorXs>& x, | ||
76 | const Eigen::Ref<const VectorXs>& u); | ||
77 | |||
78 | /** | ||
79 | * @brief @copydoc Base::calcDiff(const | ||
80 | * boost::shared_ptr<ActuationDataAbstract>& data, const Eigen::Ref<const | ||
81 | * VectorXs>& x) | ||
82 | */ | ||
83 | virtual void calcDiff(const boost::shared_ptr<ActuationDataAbstract>& data, | ||
84 | const Eigen::Ref<const VectorXs>& x); | ||
85 | |||
86 | /** | ||
87 | * @brief @copydoc Base::commands() | ||
88 | */ | ||
89 | virtual void commands(const boost::shared_ptr<ActuationDataAbstract>& data, | ||
90 | const Eigen::Ref<const VectorXs>& x, | ||
91 | const Eigen::Ref<const VectorXs>& tau); | ||
92 | |||
93 | /** | ||
94 | * @brief @copydoc Base::torqueTransform() | ||
95 | */ | ||
96 | virtual void torqueTransform( | ||
97 | const boost::shared_ptr<ActuationDataAbstract>& data, | ||
98 | const Eigen::Ref<const VectorXs>& x, const Eigen::Ref<const VectorXs>& u); | ||
99 | |||
100 | /** | ||
101 | * @brief @copydoc Base::createData() | ||
102 | */ | ||
103 | virtual boost::shared_ptr<ActuationDataAbstract> createData(); | ||
104 | |||
105 | /** | ||
106 | * @brief Return the original actuation model | ||
107 | */ | ||
108 | const boost::shared_ptr<Base>& get_model() const; | ||
109 | |||
110 | /** | ||
111 | * @brief Return the disturbance constant used by the numerical | ||
112 | * differentiation routine | ||
113 | */ | ||
114 | const Scalar get_disturbance() const; | ||
115 | |||
116 | /** | ||
117 | * @brief Modify the disturbance constant used by the numerical | ||
118 | * differentiation routine | ||
119 | */ | ||
120 | void set_disturbance(const Scalar disturbance); | ||
121 | |||
122 | private: | ||
123 | boost::shared_ptr<Base> model_; //!< Actuation model hat we want to apply the | ||
124 | //!< numerical differentiation | ||
125 | Scalar e_jac_; //!< Constant used for computing disturbances in Jacobian | ||
126 | //!< calculation | ||
127 | |||
128 | protected: | ||
129 | using Base::nu_; | ||
130 | }; | ||
131 | |||
132 | template <typename _Scalar> | ||
133 | struct ActuationDataNumDiffTpl : public ActuationDataAbstractTpl<_Scalar> { | ||
134 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW | ||
135 | |||
136 | typedef _Scalar Scalar; | ||
137 | typedef MathBaseTpl<Scalar> MathBase; | ||
138 | typedef typename MathBase::VectorXs VectorXs; | ||
139 | typedef ActuationDataAbstractTpl<Scalar> Base; | ||
140 | |||
141 | /** | ||
142 | * @brief Initialize the numdiff actuation data | ||
143 | * | ||
144 | * @tparam Model is the type of the `ActuationModelAbstractTpl`. | ||
145 | * @param model is the object to compute the numerical differentiation from. | ||
146 | */ | ||
147 | template <template <typename Scalar> class Model> | ||
148 | 72 | explicit ActuationDataNumDiffTpl(Model<Scalar>* const model) | |
149 | : Base(model), | ||
150 |
1/2✓ Branch 5 taken 72 times.
✗ Branch 6 not taken.
|
72 | dx(model->get_model()->get_state()->get_ndx()), |
151 |
1/2✓ Branch 4 taken 72 times.
✗ Branch 5 not taken.
|
72 | du(model->get_model()->get_nu()), |
152 |
1/2✓ Branch 8 taken 72 times.
✗ Branch 9 not taken.
|
144 | xp(model->get_model()->get_state()->get_nx()) { |
153 |
1/2✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
|
72 | dx.setZero(); |
154 |
1/2✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
|
72 | du.setZero(); |
155 |
1/2✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
|
72 | xp.setZero(); |
156 | 72 | const std::size_t ndx = model->get_model()->get_state()->get_ndx(); | |
157 | 72 | const std::size_t nu = model->get_model()->get_nu(); | |
158 |
1/2✓ Branch 3 taken 72 times.
✗ Branch 4 not taken.
|
72 | data_0 = model->get_model()->createData(); |
159 |
2/2✓ Branch 0 taken 2864 times.
✓ Branch 1 taken 72 times.
|
2936 | for (std::size_t i = 0; i < ndx; ++i) { |
160 |
2/4✓ Branch 3 taken 2864 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 2864 times.
✗ Branch 7 not taken.
|
2864 | data_x.push_back(model->get_model()->createData()); |
161 | } | ||
162 |
2/2✓ Branch 0 taken 1344 times.
✓ Branch 1 taken 72 times.
|
1416 | for (std::size_t i = 0; i < nu; ++i) { |
163 |
2/4✓ Branch 3 taken 1344 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1344 times.
✗ Branch 7 not taken.
|
1344 | data_u.push_back(model->get_model()->createData()); |
164 | } | ||
165 | 72 | } | |
166 | |||
167 | Scalar x_norm; //!< Norm of the state vector | ||
168 | Scalar | ||
169 | xh_jac; //!< Disturbance value used for computing \f$ \ell_\mathbf{x} \f$ | ||
170 | Scalar | ||
171 | uh_jac; //!< Disturbance value used for computing \f$ \ell_\mathbf{u} \f$ | ||
172 | VectorXs dx; //!< State disturbance | ||
173 | VectorXs du; //!< Control disturbance | ||
174 | VectorXs xp; //!< The integrated state from the disturbance on one DoF "\f$ | ||
175 | //!< \int x dx_i \f$" | ||
176 | boost::shared_ptr<Base> data_0; //!< The data that contains the final results | ||
177 | std::vector<boost::shared_ptr<Base> > | ||
178 | data_x; //!< The temporary data associated with the state variation | ||
179 | std::vector<boost::shared_ptr<Base> > | ||
180 | data_u; //!< The temporary data associated with the control variation | ||
181 | |||
182 | using Base::dtau_du; | ||
183 | using Base::dtau_dx; | ||
184 | using Base::tau; | ||
185 | }; | ||
186 | |||
187 | } // namespace crocoddyl | ||
188 | |||
189 | /* --- Details -------------------------------------------------------------- */ | ||
190 | /* --- Details -------------------------------------------------------------- */ | ||
191 | /* --- Details -------------------------------------------------------------- */ | ||
192 | #include "crocoddyl/core/numdiff/actuation.hxx" | ||
193 | |||
194 | #endif // CROCODDYL_CORE_NUMDIFF_ACTUATION_HPP_ | ||
195 |