GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: include/crocoddyl/core/control-base.hpp Lines: 7 8 87.5 %
Date: 2024-02-13 11:12:33 Branches: 6 12 50.0 %

Line Branch Exec Source
1
///////////////////////////////////////////////////////////////////////////////
2
// BSD 3-Clause License
3
//
4
// Copyright (C) 2021, University of Edinburgh, University of Trento
5
// Copyright note valid unless otherwise stated in individual files.
6
// All rights reserved.
7
///////////////////////////////////////////////////////////////////////////////
8
9
#ifndef CROCODDYL_CORE_CONTROL_BASE_HPP_
10
#define CROCODDYL_CORE_CONTROL_BASE_HPP_
11
12
#include <boost/shared_ptr.hpp>
13
14
#include "crocoddyl/core/fwd.hpp"
15
#include "crocoddyl/core/mathbase.hpp"
16
#include "crocoddyl/core/utils/exception.hpp"
17
18
namespace crocoddyl {
19
20
/**
21
 * @brief Abstract class for the control trajectory parametrization
22
 *
23
 * The control trajectory is a function of the (normalized) time.
24
 * Normalized time is between 0 and 1, where 0 represents the beginning of the
25
 * time step, and 1 represents its end. The trajectory depends on the control
26
 * parameters u, whose size may be larger than the size of the control inputs w.
27
 *
28
 * The main computations are carried out in `calc`, `multiplyByJacobian` and
29
 * `multiplyJacobianTransposeBy`, where the former computes control input
30
 * \f$\mathbf{w}\in\mathbb{R}^{nw}\f$ from a set of control parameters
31
 * \f$\mathbf{u}\in\mathbb{R}^{nu}\f$ where `nw` and `nu` represent the
32
 * dimension of the control inputs and parameters, respectively, and the latter
33
 * defines useful operations across the Jacobian of the control-parametrization
34
 * model. Finally, `params` allows us to obtain the control parameters from a
35
 * the control input, i.e., it is the dual of `calc`. Note that
36
 * `multiplyByJacobian` and `multiplyJacobianTransposeBy` requires to run `calc`
37
 * first.
38
 *
39
 * \sa `calc()`, `calcDiff()`, `createData()`, `params`, `multiplyByJacobian`,
40
 * `multiplyJacobianTransposeBy`
41
 */
42
template <typename _Scalar>
43
class ControlParametrizationModelAbstractTpl {
44
 public:
45
1086
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
46
47
  typedef _Scalar Scalar;
48
  typedef MathBaseTpl<Scalar> MathBase;
49
  typedef ControlParametrizationDataAbstractTpl<Scalar>
50
      ControlParametrizationDataAbstract;
51
  typedef typename MathBase::VectorXs VectorXs;
52
  typedef typename MathBase::MatrixXs MatrixXs;
53
54
  /**
55
   * @brief Initialize the control dimensions
56
   *
57
   * @param[in] nw   Dimension of control inputs
58
   * @param[in] nu   Dimension of control parameters
59
   */
60
  ControlParametrizationModelAbstractTpl(const std::size_t nw,
61
                                         const std::size_t nu);
62
  virtual ~ControlParametrizationModelAbstractTpl();
63
64
  /**
65
   * @brief Get the value of the control at the specified time
66
   *
67
   * @param[in]  data   Data structure containing the control vector to write
68
   * @param[in]  t      Time in [0,1]
69
   * @param[in]  u      Control parameters
70
   */
71
  virtual void calc(
72
      const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
73
      const Scalar t, const Eigen::Ref<const VectorXs>& u) const = 0;
74
75
  /**
76
   * @brief Get the value of the Jacobian of the control with respect to the
77
   * parameters
78
   *
79
   * It assumes that `calc()` has been run first
80
   *
81
   * @param[in]  data   Control-parametrization data
82
   * @param[in]  t      Time in [0,1]
83
   * @param[in]  u      Control parameters
84
   */
85
  virtual void calcDiff(
86
      const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
87
      const Scalar t, const Eigen::Ref<const VectorXs>& u) const = 0;
88
89
  /**
90
   * @brief Create the control-parametrization data
91
   *
92
   * @return the control-parametrization data
93
   */
94
  virtual boost::shared_ptr<ControlParametrizationDataAbstract> createData();
95
96
  /**
97
   * @brief Update the control parameters u for a specified time t given the
98
   * control input w
99
   *
100
   * @param[in]  data   Control-parametrization data
101
   * @param[in]  t      Time in [0,1]
102
   * @param[in]  w      Control inputs
103
   */
104
  virtual void params(
105
      const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
106
      const Scalar t, const Eigen::Ref<const VectorXs>& w) const = 0;
107
108
  /**
109
   * @brief Convert the bounds on the control inputs w to bounds on the control
110
   * parameters u
111
   *
112
   * @param[in]  w_lb   Control lower bound
113
   * @param[in]  w_ub   Control lower bound
114
   * @param[out] u_lb   Control parameters lower bound
115
   * @param[out] u_ub   Control parameters upper bound
116
   */
117
  virtual void convertBounds(const Eigen::Ref<const VectorXs>& w_lb,
118
                             const Eigen::Ref<const VectorXs>& w_ub,
119
                             Eigen::Ref<VectorXs> u_lb,
120
                             Eigen::Ref<VectorXs> u_ub) const = 0;
121
122
  /**
123
   * @brief Compute the product between the given matrix A and the derivative of
124
   * the control input with respect to the control parameters (i.e., A*dw_du).
125
   *
126
   * It assumes that `calc()` has been run first
127
   *
128
   * @param[in]  data   Control-parametrization data
129
   * @param[in]  A      A matrix to multiply times the Jacobian
130
   * @param[out] out    Product between the matrix A and the Jacobian of the
131
   * control with respect to the parameters
132
   * @param[in] op      Assignment operator which sets, adds, or removes the
133
   * given results
134
   */
135
  virtual void multiplyByJacobian(
136
      const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
137
      const Eigen::Ref<const MatrixXs>& A, Eigen::Ref<MatrixXs> out,
138
      const AssignmentOp = setto) const = 0;
139
140
  virtual MatrixXs multiplyByJacobian_J(
141
      const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
142
      const Eigen::Ref<const MatrixXs>& A, const AssignmentOp = setto) const;
143
144
  /**
145
   * @brief Compute the product between the transpose of the derivative of the
146
   * control input with respect to the control parameters and a given matrix A
147
   * (i.e., dw_du^T*A)
148
   *
149
   * It assumes that `calc()` has been run first
150
   *
151
   * @param[in]  data   Control-parametrization data
152
   * @param[in]  A      A matrix to multiply times the Jacobian
153
   * @param[out] out    Product between the transposed Jacobian of the control
154
   * with respect to the parameters and the matrix A
155
   * @param[in] op      Assignment operator which sets, adds, or removes the
156
   * given results
157
   */
158
  virtual void multiplyJacobianTransposeBy(
159
      const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
160
      const Eigen::Ref<const MatrixXs>& A, Eigen::Ref<MatrixXs> out,
161
      const AssignmentOp = setto) const = 0;
162
163
  virtual MatrixXs multiplyJacobianTransposeBy_J(
164
      const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
165
      const Eigen::Ref<const MatrixXs>& A, const AssignmentOp = setto) const;
166
167
  /**
168
   * @brief Checks that a specific data belongs to this model
169
   */
170
  virtual bool checkData(
171
      const boost::shared_ptr<ControlParametrizationDataAbstract>& data);
172
173
  /**
174
   * @brief Return the dimension of the control inputs
175
   */
176
  std::size_t get_nw() const;
177
178
  /**
179
   * @brief Return the dimension of control parameters
180
   */
181
  std::size_t get_nu() const;
182
183
 protected:
184
  std::size_t nw_;  //!< Control dimension
185
  std::size_t nu_;  //!< Control parameters dimension
186
};
187
188
template <typename _Scalar>
189
struct ControlParametrizationDataAbstractTpl {
190
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
191
192
  typedef _Scalar Scalar;
193
  typedef MathBaseTpl<Scalar> MathBase;
194
  typedef typename MathBase::VectorXs VectorXs;
195
  typedef typename MathBase::MatrixXs MatrixXs;
196
197
  template <template <typename Scalar> class Model>
198
196896
  explicit ControlParametrizationDataAbstractTpl(Model<Scalar>* const model)
199
      : w(model->get_nw()),
200
        u(model->get_nu()),
201

196896
        dw_du(model->get_nw(), model->get_nu()) {
202
196896
    w.setZero();
203
196896
    u.setZero();
204
196896
    dw_du.setZero();
205
  }
206
99088
  virtual ~ControlParametrizationDataAbstractTpl() {}
207
208
  VectorXs w;      //!< value of the differential control
209
  VectorXs u;      //!< value of the control parameters
210
  MatrixXs dw_du;  //!< Jacobian of the differential control with respect to the
211
                   //!< parameters
212
};
213
214
}  // namespace crocoddyl
215
216
/* --- Details -------------------------------------------------------------- */
217
/* --- Details -------------------------------------------------------------- */
218
/* --- Details -------------------------------------------------------------- */
219
#include "crocoddyl/core/control-base.hxx"
220
221
#endif  // CROCODDYL_CORE_CONTROL_BASE_HPP_