Directory: | ./ |
---|---|
File: | include/crocoddyl/core/controls/poly-one.hpp |
Date: | 2025-03-26 19:23:43 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 7 | 7 | 100.0% |
Branches: | 2 | 4 | 50.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | ||
2 | // BSD 3-Clause License | ||
3 | // | ||
4 | // Copyright (C) 2021-2025, University of Edinburgh, University of Trento, | ||
5 | // Heriot-Watt University | ||
6 | // Copyright note valid unless otherwise stated in individual files. | ||
7 | // All rights reserved. | ||
8 | /////////////////////////////////////////////////////////////////////////////// | ||
9 | |||
10 | #ifndef CROCODDYL_CORE_CONTROLS_POLY_ONE_HPP_ | ||
11 | #define CROCODDYL_CORE_CONTROLS_POLY_ONE_HPP_ | ||
12 | |||
13 | #include "crocoddyl/core/control-base.hpp" | ||
14 | #include "crocoddyl/core/fwd.hpp" | ||
15 | |||
16 | namespace crocoddyl { | ||
17 | |||
18 | /** | ||
19 | * @brief A polynomial function of time of degree one, that is a linear function | ||
20 | * | ||
21 | * The size of the parameters \f$\mathbf{u}\f$ is twice the size of the control | ||
22 | * input \f$\mathbf{w}\f$. The first half of \f$\mathbf{u}\f$ represents the | ||
23 | * value of w at time 0. The second half of \f$\mathbf{u}\f$ represents the | ||
24 | * value of \f$\mathbf{w}\f$ at time 0.5. | ||
25 | * | ||
26 | * The main computations are carried out in `calc`, `multiplyByJacobian` and | ||
27 | * `multiplyJacobianTransposeBy`, where the former computes control input | ||
28 | * \f$\mathbf{w}\in\mathbb{R}^{nw}\f$ from a set of control parameters | ||
29 | * \f$\mathbf{u}\in\mathbb{R}^{nu}\f$ where `nw` and `nu` represent the | ||
30 | * dimension of the control inputs and parameters, respectively, and the latter | ||
31 | * defines useful operations across the Jacobian of the control-parametrization | ||
32 | * model. Finally, `params` allows us to obtain the control parameters from the | ||
33 | * control input, i.e., it is the inverse of `calc`. Note that | ||
34 | * `multiplyByJacobian` and `multiplyJacobianTransposeBy` requires to run `calc` | ||
35 | * first. | ||
36 | * | ||
37 | * \sa `ControlParametrizationAbstractTpl`, `calc()`, `calcDiff()`, | ||
38 | * `createData()`, `params`, `multiplyByJacobian`, `multiplyJacobianTransposeBy` | ||
39 | */ | ||
40 | template <typename _Scalar> | ||
41 | class ControlParametrizationModelPolyOneTpl | ||
42 | : public ControlParametrizationModelAbstractTpl<_Scalar> { | ||
43 | public: | ||
44 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW | ||
45 | 12 | CROCODDYL_DERIVED_CAST(ControlParametrizationModelBase, | |
46 | ControlParametrizationModelPolyOneTpl) | ||
47 | |||
48 | typedef _Scalar Scalar; | ||
49 | typedef MathBaseTpl<Scalar> MathBase; | ||
50 | typedef ControlParametrizationDataAbstractTpl<Scalar> | ||
51 | ControlParametrizationDataAbstract; | ||
52 | typedef ControlParametrizationModelAbstractTpl<Scalar> Base; | ||
53 | typedef ControlParametrizationDataPolyOneTpl<Scalar> Data; | ||
54 | typedef typename MathBase::VectorXs VectorXs; | ||
55 | typedef typename MathBase::MatrixXs MatrixXs; | ||
56 | |||
57 | /** | ||
58 | * @brief Initialize the poly-one control parametrization | ||
59 | * | ||
60 | * @param[in] nw Dimension of control vector | ||
61 | */ | ||
62 | explicit ControlParametrizationModelPolyOneTpl(const std::size_t nw); | ||
63 | 530 | virtual ~ControlParametrizationModelPolyOneTpl() = default; | |
64 | |||
65 | /** | ||
66 | * @brief Get the value of the control at the specified time | ||
67 | * | ||
68 | * @param[in] data Control-parametrization data | ||
69 | * @param[in] t Time in [0,1] | ||
70 | * @param[in] u Control parameters | ||
71 | */ | ||
72 | virtual void calc( | ||
73 | const std::shared_ptr<ControlParametrizationDataAbstract>& data, | ||
74 | const Scalar t, const Eigen::Ref<const VectorXs>& u) const override; | ||
75 | |||
76 | /** | ||
77 | * @brief Get the value of the Jacobian of the control with respect to the | ||
78 | * parameters | ||
79 | * | ||
80 | * It assumes that `calc()` has been run first | ||
81 | * | ||
82 | * @param[in] data Control-parametrization data | ||
83 | * @param[in] t Time in [0,1] | ||
84 | * @param[in] u Control parameters | ||
85 | */ | ||
86 | virtual void calcDiff( | ||
87 | const std::shared_ptr<ControlParametrizationDataAbstract>& data, | ||
88 | const Scalar t, const Eigen::Ref<const VectorXs>& u) const override; | ||
89 | |||
90 | /** | ||
91 | * @brief Create the control-parametrization data | ||
92 | * | ||
93 | * @return the control-parametrization data | ||
94 | */ | ||
95 | virtual std::shared_ptr<ControlParametrizationDataAbstract> createData() | ||
96 | override; | ||
97 | |||
98 | /** | ||
99 | * @brief Get a value of the control parameters such that the control at the | ||
100 | * specified time t is equal to the specified value w | ||
101 | * | ||
102 | * @param[in] data Control-parametrization data | ||
103 | * @param[in] t Time in [0,1] | ||
104 | * @param[in] w Control values | ||
105 | */ | ||
106 | virtual void params( | ||
107 | const std::shared_ptr<ControlParametrizationDataAbstract>& data, | ||
108 | const Scalar t, const Eigen::Ref<const VectorXs>& w) const override; | ||
109 | |||
110 | /** | ||
111 | * @brief Map the specified bounds from the control space to the parameter | ||
112 | * space | ||
113 | * | ||
114 | * @param[in] w_lb Control lower bound | ||
115 | * @param[in] w_ub Control lower bound | ||
116 | * @param[out] u_lb Control parameters lower bound | ||
117 | * @param[out] u_ub Control parameters upper bound | ||
118 | */ | ||
119 | virtual void convertBounds(const Eigen::Ref<const VectorXs>& w_lb, | ||
120 | const Eigen::Ref<const VectorXs>& w_ub, | ||
121 | Eigen::Ref<VectorXs> u_lb, | ||
122 | Eigen::Ref<VectorXs> u_ub) const override; | ||
123 | |||
124 | /** | ||
125 | * @brief Compute the product between a specified matrix and the Jacobian of | ||
126 | * the control (with respect to the parameters) | ||
127 | * | ||
128 | * It assumes that `calc()` has been run first | ||
129 | * | ||
130 | * @param[in] data Control-parametrization data | ||
131 | * @param[in] A A matrix to multiply times the Jacobian | ||
132 | * @param[out] out Product between the matrix A and the Jacobian of the | ||
133 | * control with respect to the parameters | ||
134 | * @param[in] op Assignment operator which sets, adds, or removes the | ||
135 | * given results | ||
136 | */ | ||
137 | virtual void multiplyByJacobian( | ||
138 | const std::shared_ptr<ControlParametrizationDataAbstract>& data, | ||
139 | const Eigen::Ref<const MatrixXs>& A, Eigen::Ref<MatrixXs> out, | ||
140 | const AssignmentOp = setto) const override; | ||
141 | |||
142 | /** | ||
143 | * @brief Compute the product between the transposed Jacobian of the control | ||
144 | * (with respect to the parameters) and a specified matrix | ||
145 | * | ||
146 | * It assumes that `calc()` has been run first | ||
147 | * | ||
148 | * @param[in] data Control-parametrization data | ||
149 | * @param[in] A A matrix to multiply times the Jacobian | ||
150 | * @param[out] out Product between the transposed Jacobian of the control | ||
151 | * with respect to the parameters and the matrix A | ||
152 | * @param[in] op Assignment operator which sets, adds, or removes the | ||
153 | * given results | ||
154 | */ | ||
155 | virtual void multiplyJacobianTransposeBy( | ||
156 | const std::shared_ptr<ControlParametrizationDataAbstract>& data, | ||
157 | const Eigen::Ref<const MatrixXs>& A, Eigen::Ref<MatrixXs> out, | ||
158 | const AssignmentOp = setto) const override; | ||
159 | |||
160 | template <typename NewScalar> | ||
161 | ControlParametrizationModelPolyOneTpl<NewScalar> cast() const; | ||
162 | |||
163 | virtual void print(std::ostream& os) const override; | ||
164 | |||
165 | protected: | ||
166 | using Base::nu_; | ||
167 | using Base::nw_; | ||
168 | }; | ||
169 | |||
170 | template <typename _Scalar> | ||
171 | struct ControlParametrizationDataPolyOneTpl | ||
172 | : public ControlParametrizationDataAbstractTpl<_Scalar> { | ||
173 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW | ||
174 | |||
175 | typedef _Scalar Scalar; | ||
176 | typedef MathBaseTpl<Scalar> MathBase; | ||
177 | typedef ControlParametrizationDataAbstractTpl<Scalar> Base; | ||
178 | typedef typename MathBase::Vector2s Vector2s; | ||
179 | |||
180 | template <template <typename Scalar> class Model> | ||
181 | 16787 | explicit ControlParametrizationDataPolyOneTpl(Model<Scalar>* const model) | |
182 |
1/2✓ Branch 2 taken 16785 times.
✗ Branch 3 not taken.
|
16787 | : Base(model) { |
183 |
1/2✓ Branch 1 taken 16785 times.
✗ Branch 2 not taken.
|
16787 | c.setZero(); |
184 | 16787 | } | |
185 | 33570 | virtual ~ControlParametrizationDataPolyOneTpl() = default; | |
186 | |||
187 | Vector2s c; //!< Coefficients of the linear control that depends on time | ||
188 | |||
189 | using Base::dw_du; | ||
190 | using Base::u; | ||
191 | using Base::w; | ||
192 | }; | ||
193 | |||
194 | } // namespace crocoddyl | ||
195 | |||
196 | /* --- Details -------------------------------------------------------------- */ | ||
197 | /* --- Details -------------------------------------------------------------- */ | ||
198 | /* --- Details -------------------------------------------------------------- */ | ||
199 | #include "crocoddyl/core/controls/poly-one.hxx" | ||
200 | |||
201 | CROCODDYL_DECLARE_EXTERN_TEMPLATE_CLASS( | ||
202 | crocoddyl::ControlParametrizationModelPolyOneTpl) | ||
203 | CROCODDYL_DECLARE_EXTERN_TEMPLATE_STRUCT( | ||
204 | crocoddyl::ControlParametrizationDataPolyOneTpl) | ||
205 | |||
206 | #endif // CROCODDYL_CORE_CONTROLS_POLY_ONE_HPP_ | ||
207 |