GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: include/crocoddyl/core/controls/poly-two-rk.hpp Lines: 5 6 83.3 %
Date: 2024-02-13 11:12:33 Branches: 2 4 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_CONTROLS_POLY_TWO_RK_HPP_
10
#define CROCODDYL_CORE_CONTROLS_POLY_TWO_RK_HPP_
11
12
#include "crocoddyl/core/control-base.hpp"
13
#include "crocoddyl/core/fwd.hpp"
14
#include "crocoddyl/core/integrator/rk.hpp"
15
#include "crocoddyl/core/utils/exception.hpp"
16
17
namespace crocoddyl {
18
19
/**
20
 * @brief A polynomial function of time of degree two, that is a quadratic
21
 * function
22
 *
23
 * The size of the parameters \f$\mathbf{u}\f$ is 3 times the size of the
24
 * control input \f$\mathbf{w}\f$. It defines a polynomial of degree two,
25
 * customized for the RK4 and RK4 integrators (even though it can be used with
26
 * whatever integration scheme). The first third of \f$\mathbf{u}\f$ represents
27
 * the value of \f$\mathbf{w}\f$ at time 0. The second third of \f$\mathbf{u}\f$
28
 * represents the value of \f$\mathbf{w}\f$ at time 0.5 or 1/3 for RK4 and RK3
29
 * parametrization, respectively. The last third of \f$\mathbf{u}\f$ represents
30
 * the value of \f$\mathbf{w}\f$ at time 1 or 2/3 for the RK4 and RK3
31
 * parametrization, respectively. This parametrization is suitable to be used
32
 * with the RK-4 or RK-3 integration schemes, because they require the value of
33
 * \f$\mathbf{w}\f$ exactly at 0, 0.5, 1 (for RK4) or 0, 1/3, 2/3 (for RK3).
34
 *
35
 * The main computations are carried out in `calc`, `multiplyByJacobian` and
36
 * `multiplyJacobianTransposeBy`, where the former computes control input
37
 * \f$\mathbf{w}\in\mathbb{R}^{nw}\f$ from a set of control parameters
38
 * \f$\mathbf{u}\in\mathbb{R}^{nu}\f$ where `nw` and `nu` represent the
39
 * dimension of the control inputs and parameters, respectively, and the latter
40
 * defines useful operations across the Jacobian of the control-parametrization
41
 * model. Finally, `params` allows us to obtain the control parameters from a
42
 * the control input, i.e., it is the inverse of `calc`. Note that
43
 * `multiplyByJacobian` and `multiplyJacobianTransposeBy` requires to run `calc`
44
 * first.
45
 *
46
 * \sa `ControlParametrizationAbstractTpl`, `calc()`, `calcDiff()`,
47
 * `createData()`, `params`, `multiplyByJacobian`, `multiplyJacobianTransposeBy`
48
 */
49
template <typename _Scalar>
50
class ControlParametrizationModelPolyTwoRKTpl
51
    : public ControlParametrizationModelAbstractTpl<_Scalar> {
52
 public:
53
  typedef _Scalar Scalar;
54
  typedef MathBaseTpl<Scalar> MathBase;
55
  typedef ControlParametrizationDataAbstractTpl<Scalar>
56
      ControlParametrizationDataAbstract;
57
  typedef ControlParametrizationModelAbstractTpl<Scalar> Base;
58
  typedef ControlParametrizationDataPolyTwoRKTpl<Scalar> Data;
59
  typedef typename MathBase::VectorXs VectorXs;
60
  typedef typename MathBase::MatrixXs MatrixXs;
61
62
  /**
63
   * @brief Initialize the poly-two RK control parametrization
64
   *
65
   * @param[in] nw      Dimension of control vector
66
   * @param[in] rktype  Type of RK parametrization
67
   */
68
  explicit ControlParametrizationModelPolyTwoRKTpl(const std::size_t nw,
69
                                                   const RKType rktype);
70
  virtual ~ControlParametrizationModelPolyTwoRKTpl();
71
72
  /**
73
   * @brief Get the value of the control at the specified time
74
   *
75
   * @param[in]  data   Poly-two-RK data
76
   * @param[in]  t      Time in [0,1]
77
   * @param[in]  u      Control parameters
78
   */
79
  virtual void calc(
80
      const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
81
      const Scalar t, const Eigen::Ref<const VectorXs>& u) const;
82
83
  /**
84
   * @brief Get the value of the Jacobian of the control with respect to the
85
   * parameters
86
   *
87
   * It assumes that `calc()` has been run first
88
   *
89
   * @param[in]  data   Poly-two-RK data
90
   * @param[in]  t      Time in [0,1]
91
   * @param[in]  u      Control parameters
92
   */
93
  virtual void calcDiff(
94
      const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
95
      const Scalar t, const Eigen::Ref<const VectorXs>& u) const;
96
97
  /**
98
   * @brief Create the control-parametrization data
99
   *
100
   * @return the control-parametrization data
101
   */
102
  virtual boost::shared_ptr<ControlParametrizationDataAbstract> createData();
103
104
  /**
105
   * @brief Get a value of the control parameters u such that the control at the
106
   * specified time t is equal to the specified value w
107
   *
108
   * @param[in]  data   Poly-two-RK data
109
   * @param[in]  t      Time in [0,1]
110
   * @param[in]  w      Control values
111
   */
112
  virtual void params(
113
      const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
114
      const Scalar t, const Eigen::Ref<const VectorXs>& w) const;
115
116
  /**
117
   * @brief Map the specified bounds from the control space to the parameter
118
   * space
119
   *
120
   * @param[in]  w_lb   Control lower bound
121
   * @param[in]  w_ub   Control lower bound
122
   * @param[out] u_lb   Control parameters lower bound
123
   * @param[out] u_ub   Control parameters upper bound
124
   */
125
  virtual void convertBounds(const Eigen::Ref<const VectorXs>& w_lb,
126
                             const Eigen::Ref<const VectorXs>& w_ub,
127
                             Eigen::Ref<VectorXs> u_lb,
128
                             Eigen::Ref<VectorXs> u_ub) const;
129
130
  /**
131
   * @brief Compute the product between a specified matrix and the Jacobian of
132
   * the control (with respect to the parameters)
133
   *
134
   * It assumes that `calc()` has been run first
135
   *
136
   * @param[in]  data   Poly-two-RK data
137
   * @param[in]  A      A matrix to multiply times the Jacobian
138
   * @param[out] out    Product between the matrix A and the Jacobian of the
139
   * control with respect to the parameters
140
   * @param[in] op      Assignment operator which sets, adds, or removes the
141
   * given results
142
   */
143
  virtual void multiplyByJacobian(
144
      const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
145
      const Eigen::Ref<const MatrixXs>& A, Eigen::Ref<MatrixXs> out,
146
      const AssignmentOp = setto) const;
147
148
  /**
149
   * @brief Compute the product between the transposed Jacobian of the control
150
   * (with respect to the parameters) and a specified matrix
151
   *
152
   * It assumes that `calc()` has been run first
153
   *
154
   * @param[in]  data   Poly-two-RK data
155
   * @param[in]  A      A matrix to multiply times the Jacobian
156
   * @param[out] out    Product between the transposed Jacobian of the control
157
   * with respect to the parameters and the matrix A
158
   * @param[in] op      Assignment operator which sets, adds, or removes the
159
   * given results
160
   */
161
  virtual void multiplyJacobianTransposeBy(
162
      const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
163
      const Eigen::Ref<const MatrixXs>& A, Eigen::Ref<MatrixXs> out,
164
      const AssignmentOp = setto) const;
165
166
 protected:
167
  using Base::nu_;
168
  using Base::nw_;
169
170
 private:
171
  RKType rktype_;
172
};
173
174
template <typename _Scalar>
175
struct ControlParametrizationDataPolyTwoRKTpl
176
    : public ControlParametrizationDataAbstractTpl<_Scalar> {
177
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
178
179
  typedef _Scalar Scalar;
180
  typedef MathBaseTpl<Scalar> MathBase;
181
  typedef ControlParametrizationDataAbstractTpl<Scalar> Base;
182
  typedef typename MathBase::Vector3s Vector3s;
183
184
  template <template <typename Scalar> class Model>
185
14930
  explicit ControlParametrizationDataPolyTwoRKTpl(Model<Scalar>* const model)
186
14930
      : Base(model), tmp_t2(0.) {
187
14930
    c.setZero();
188
14930
  }
189
190
29864
  virtual ~ControlParametrizationDataPolyTwoRKTpl() {}
191
192
  Vector3s c;     //!< Polynomial coefficients of the second-order control model
193
                  //!< that depends on time
194
  Scalar tmp_t2;  //!< Temporary variable to store the square of the time
195
};
196
197
}  // namespace crocoddyl
198
199
/* --- Details -------------------------------------------------------------- */
200
/* --- Details -------------------------------------------------------------- */
201
/* --- Details -------------------------------------------------------------- */
202
#include "crocoddyl/core/controls/poly-two-rk.hxx"
203
204
#endif  // CROCODDYL_CORE_CONTROLS_POLY_TWO_RK_HPP_