GCC Code Coverage Report


Directory: ./
File: include/crocoddyl/core/control-base.hpp
Date: 2025-02-24 23:41:29
Exec Total Coverage
Lines: 8 8 100.0%
Branches: 6 12 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_CONTROL_BASE_HPP_
11 #define CROCODDYL_CORE_CONTROL_BASE_HPP_
12
13 #include <memory>
14
15 #include "crocoddyl/core/fwd.hpp"
16 #include "crocoddyl/core/mathbase.hpp"
17 #include "crocoddyl/core/utils/exception.hpp"
18
19 namespace crocoddyl {
20
21 /**
22 * @brief Abstract class for the control trajectory parametrization
23 *
24 * The control trajectory is a function of the (normalized) time.
25 * Normalized time is between 0 and 1, where 0 represents the beginning of the
26 * time step, and 1 represents its end. The trajectory depends on the control
27 * parameters u, whose size may be larger than the size of the control inputs w.
28 *
29 * The main computations are carried out in `calc`, `multiplyByJacobian` and
30 * `multiplyJacobianTransposeBy`, where the former computes control input
31 * \f$\mathbf{w}\in\mathbb{R}^{nw}\f$ from a set of control parameters
32 * \f$\mathbf{u}\in\mathbb{R}^{nu}\f$ where `nw` and `nu` represent the
33 * dimension of the control inputs and parameters, respectively, and the latter
34 * defines useful operations across the Jacobian of the control-parametrization
35 * model. Finally, `params` allows us to obtain the control parameters from a
36 * the control input, i.e., it is the dual of `calc`. Note that
37 * `multiplyByJacobian` and `multiplyJacobianTransposeBy` requires to run `calc`
38 * first.
39 *
40 * \sa `calc()`, `calcDiff()`, `createData()`, `params`, `multiplyByJacobian`,
41 * `multiplyJacobianTransposeBy`
42 */
43 template <typename _Scalar>
44 class ControlParametrizationModelAbstractTpl {
45 public:
46 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
47
48 typedef _Scalar Scalar;
49 typedef MathBaseTpl<Scalar> MathBase;
50 typedef ControlParametrizationDataAbstractTpl<Scalar>
51 ControlParametrizationDataAbstract;
52 typedef typename MathBase::VectorXs VectorXs;
53 typedef typename MathBase::MatrixXs MatrixXs;
54
55 /**
56 * @brief Initialize the control dimensions
57 *
58 * @param[in] nw Dimension of control inputs
59 * @param[in] nu Dimension of control parameters
60 */
61 ControlParametrizationModelAbstractTpl(const std::size_t nw,
62 const std::size_t nu);
63 virtual ~ControlParametrizationModelAbstractTpl();
64
65 /**
66 * @brief Get the value of the control at the specified time
67 *
68 * @param[in] data Data structure containing the control vector to write
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 = 0;
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 = 0;
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
97 /**
98 * @brief Update the control parameters u for a specified time t given the
99 * control input w
100 *
101 * @param[in] data Control-parametrization data
102 * @param[in] t Time in [0,1]
103 * @param[in] w Control inputs
104 */
105 virtual void params(
106 const std::shared_ptr<ControlParametrizationDataAbstract>& data,
107 const Scalar t, const Eigen::Ref<const VectorXs>& w) const = 0;
108
109 /**
110 * @brief Convert the bounds on the control inputs w to bounds on the control
111 * parameters u
112 *
113 * @param[in] w_lb Control lower bound
114 * @param[in] w_ub Control lower bound
115 * @param[out] u_lb Control parameters lower bound
116 * @param[out] u_ub Control parameters upper bound
117 */
118 virtual void convertBounds(const Eigen::Ref<const VectorXs>& w_lb,
119 const Eigen::Ref<const VectorXs>& w_ub,
120 Eigen::Ref<VectorXs> u_lb,
121 Eigen::Ref<VectorXs> u_ub) const = 0;
122
123 /**
124 * @brief Compute the product between the given matrix A and the derivative of
125 * the control input with respect to the control parameters (i.e., A*dw_du).
126 *
127 * It assumes that `calc()` has been run first
128 *
129 * @param[in] data Control-parametrization data
130 * @param[in] A A matrix to multiply times the Jacobian
131 * @param[out] out Product between the matrix A and the Jacobian of the
132 * control with respect to the parameters
133 * @param[in] op Assignment operator which sets, adds, or removes the
134 * given results
135 */
136 virtual void multiplyByJacobian(
137 const std::shared_ptr<ControlParametrizationDataAbstract>& data,
138 const Eigen::Ref<const MatrixXs>& A, Eigen::Ref<MatrixXs> out,
139 const AssignmentOp op = setto) const = 0;
140
141 virtual MatrixXs multiplyByJacobian_J(
142 const std::shared_ptr<ControlParametrizationDataAbstract>& data,
143 const Eigen::Ref<const MatrixXs>& A, const AssignmentOp op = setto) const;
144
145 /**
146 * @brief Compute the product between the transpose of the derivative of the
147 * control input with respect to the control parameters and a given matrix A
148 * (i.e., dw_du^T*A)
149 *
150 * It assumes that `calc()` has been run first
151 *
152 * @param[in] data Control-parametrization data
153 * @param[in] A A matrix to multiply times the Jacobian
154 * @param[out] out Product between the transposed Jacobian of the control
155 * with respect to the parameters and the matrix A
156 * @param[in] op Assignment operator which sets, adds, or removes the
157 * given results
158 */
159 virtual void multiplyJacobianTransposeBy(
160 const std::shared_ptr<ControlParametrizationDataAbstract>& data,
161 const Eigen::Ref<const MatrixXs>& A, Eigen::Ref<MatrixXs> out,
162 const AssignmentOp op = setto) const = 0;
163
164 virtual MatrixXs multiplyJacobianTransposeBy_J(
165 const std::shared_ptr<ControlParametrizationDataAbstract>& data,
166 const Eigen::Ref<const MatrixXs>& A, const AssignmentOp op = setto) const;
167
168 /**
169 * @brief Checks that a specific data belongs to this model
170 */
171 virtual bool checkData(
172 const std::shared_ptr<ControlParametrizationDataAbstract>& data);
173
174 /**
175 * @brief Return the dimension of the control inputs
176 */
177 std::size_t get_nw() const;
178
179 /**
180 * @brief Return the dimension of control parameters
181 */
182 std::size_t get_nu() const;
183
184 protected:
185 std::size_t nw_; //!< Control dimension
186 std::size_t nu_; //!< Control parameters dimension
187 };
188
189 template <typename _Scalar>
190 struct ControlParametrizationDataAbstractTpl {
191 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
192
193 typedef _Scalar Scalar;
194 typedef MathBaseTpl<Scalar> MathBase;
195 typedef typename MathBase::VectorXs VectorXs;
196 typedef typename MathBase::MatrixXs MatrixXs;
197
198 template <template <typename Scalar> class Model>
199 210716 explicit ControlParametrizationDataAbstractTpl(Model<Scalar>* const model)
200
1/2
✓ Branch 1 taken 105358 times.
✗ Branch 2 not taken.
210716 : w(model->get_nw()),
201
1/2
✓ Branch 2 taken 105358 times.
✗ Branch 3 not taken.
210716 u(model->get_nu()),
202
1/2
✓ Branch 4 taken 105358 times.
✗ Branch 5 not taken.
421432 dw_du(model->get_nw(), model->get_nu()) {
203
1/2
✓ Branch 1 taken 105358 times.
✗ Branch 2 not taken.
210716 w.setZero();
204
1/2
✓ Branch 1 taken 105358 times.
✗ Branch 2 not taken.
210716 u.setZero();
205
1/2
✓ Branch 1 taken 105358 times.
✗ Branch 2 not taken.
210716 dw_du.setZero();
206 }
207 106270 virtual ~ControlParametrizationDataAbstractTpl() {}
208
209 VectorXs w; //!< value of the differential control
210 VectorXs u; //!< value of the control parameters
211 MatrixXs dw_du; //!< Jacobian of the differential control with respect to the
212 //!< parameters
213 };
214
215 } // namespace crocoddyl
216
217 /* --- Details -------------------------------------------------------------- */
218 /* --- Details -------------------------------------------------------------- */
219 /* --- Details -------------------------------------------------------------- */
220 #include "crocoddyl/core/control-base.hxx"
221
222 #endif // CROCODDYL_CORE_CONTROL_BASE_HPP_
223