- 3.0.2 optimal control module.
TermQuadTracking.hpp
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 #pragma once
7 
8 #include "TermBase.hpp"
9 
10 namespace ct {
11 namespace optcon {
12 
19 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL = double, typename SCALAR = SCALAR_EVAL>
20 class TermQuadTracking : public TermBase<STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR>
21 {
22 public:
23  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
24 
25  typedef Eigen::Matrix<SCALAR_EVAL, STATE_DIM, STATE_DIM> state_matrix_t;
26  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM> control_matrix_t;
27  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, STATE_DIM> control_state_matrix_t;
28  typedef Eigen::Matrix<SCALAR_EVAL, STATE_DIM, STATE_DIM> state_matrix_double_t;
29  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM> control_matrix_double_t;
30  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, STATE_DIM> control_state_matrix_double_t;
31 
33 
34  TermQuadTracking(const state_matrix_t& Q,
35  const control_matrix_t& R,
36  const core::InterpolationType& stateSplineType,
37  const core::InterpolationType& controlSplineType,
38  const bool trackControlTrajectory = false);
39 
41 
42  virtual ~TermQuadTracking();
43 
45 
46  void setWeights(const state_matrix_double_t& Q, const control_matrix_double_t& R);
47 
50 
51  virtual SCALAR evaluate(const Eigen::Matrix<SCALAR, STATE_DIM, 1>& x,
52  const Eigen::Matrix<SCALAR, CONTROL_DIM, 1>& u,
53  const SCALAR& t) override;
54 
57  const SCALAR_EVAL& t) override;
58 
61  const SCALAR_EVAL& t) override;
62 
65  const SCALAR_EVAL& t) override;
66 
69  const SCALAR_EVAL& t) override;
70 
73  const SCALAR_EVAL& t) override;
74 
75  virtual void loadConfigFile(const std::string& filename,
76  const std::string& termName,
77  bool verbose = false) override;
78 
79 protected:
80  template <typename SC>
81  SC evalLocal(const Eigen::Matrix<SC, STATE_DIM, 1>& x, const Eigen::Matrix<SC, CONTROL_DIM, 1>& u, const SC& t);
82 
83  state_matrix_t Q_;
84  control_matrix_t R_;
85 
86  // the reference trajectories to be tracked
89 
90  // Option whether the control trajectory deviation shall be penalized or not
92 };
93 
94 
95 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
96 template <typename SC>
98  const Eigen::Matrix<SC, CONTROL_DIM, 1>& u,
99  const SC& t)
100 {
101  Eigen::Matrix<SC, STATE_DIM, 1> xDiff = x - x_traj_ref_.eval((SCALAR_EVAL)t).template cast<SC>();
102 
103  Eigen::Matrix<SC, CONTROL_DIM, 1> uDiff;
104 
106  uDiff = u - u_traj_ref_.eval((SCALAR_EVAL)t).template cast<SC>();
107  else
108  uDiff = u;
109 
110  return (xDiff.transpose() * Q_.template cast<SC>() * xDiff + uDiff.transpose() * R_.template cast<SC>() * uDiff)(
111  0, 0);
112 }
113 
114 } // namespace optcon
115 } // namespace ct
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, STATE_DIM > control_state_matrix_t
Definition: TermQuadTracking.hpp:27
TermQuadTracking< STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR > * clone() const override
Deep-copy term.
Definition: TermQuadTracking-impl.hpp:53
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef Eigen::Matrix< SCALAR_EVAL, STATE_DIM, STATE_DIM > state_matrix_t
Definition: TermQuadTracking.hpp:25
An interface for a term, supporting both analytical and auto-diff terms.
Definition: TermBase.hpp:30
A quadratic tracking term of type .
Definition: TermQuadTracking.hpp:20
void setWeights(const state_matrix_double_t &Q, const control_matrix_double_t &R)
Definition: TermQuadTracking-impl.hpp:60
core::StateVector< STATE_DIM, SCALAR_EVAL > stateDerivative(const core::StateVector< STATE_DIM, SCALAR_EVAL > &x, const core::ControlVector< CONTROL_DIM, SCALAR_EVAL > &u, const SCALAR_EVAL &t) override
compute derivative of this cost term w.r.t. the state
Definition: TermQuadTracking-impl.hpp:88
control_state_matrix_t stateControlDerivative(const core::StateVector< STATE_DIM, SCALAR_EVAL > &x, const core::ControlVector< CONTROL_DIM, SCALAR_EVAL > &u, const SCALAR_EVAL &t) override
compute the cross-term derivative (state-control) of this cost function term
Definition: TermQuadTracking-impl.hpp:137
state_matrix_t stateSecondDerivative(const core::StateVector< STATE_DIM, SCALAR_EVAL > &x, const core::ControlVector< CONTROL_DIM, SCALAR_EVAL > &u, const SCALAR_EVAL &t) override
compute second order derivative of this cost term w.r.t. the state
Definition: TermQuadTracking-impl.hpp:100
clear all close all load ct GNMSLog0 mat reformat t
Definition: gnmsPlot.m:6
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM > control_matrix_double_t
Definition: TermQuadTracking.hpp:29
CppAD::AD< CppAD::cg::CG< double > > SCALAR
virtual SCALAR evaluate(const Eigen::Matrix< SCALAR, STATE_DIM, 1 > &x, const Eigen::Matrix< SCALAR, CONTROL_DIM, 1 > &u, const SCALAR &t) override
Evaluates the term at x, u, t.
Definition: TermQuadTracking-impl.hpp:78
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
Eigen::Matrix< SCALAR_EVAL, STATE_DIM, STATE_DIM > state_matrix_double_t
Definition: TermQuadTracking.hpp:28
void setStateAndControlReference(const core::StateTrajectory< STATE_DIM > &xTraj, const core::ControlTrajectory< CONTROL_DIM > &uTraj)
Definition: TermQuadTracking-impl.hpp:68
SC evalLocal(const Eigen::Matrix< SC, STATE_DIM, 1 > &x, const Eigen::Matrix< SC, CONTROL_DIM, 1 > &u, const SC &t)
Definition: TermQuadTracking.hpp:97
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, STATE_DIM > control_state_matrix_double_t
Definition: TermQuadTracking.hpp:30
ct::core::ControlTrajectory< CONTROL_DIM, SCALAR_EVAL > u_traj_ref_
Definition: TermQuadTracking.hpp:88
control_matrix_t R_
Definition: TermQuadTracking.hpp:84
control_matrix_t controlSecondDerivative(const core::StateVector< STATE_DIM, SCALAR_EVAL > &x, const core::ControlVector< CONTROL_DIM, SCALAR_EVAL > &u, const SCALAR_EVAL &t) override
compute second order derivative of this cost term w.r.t. the control input
Definition: TermQuadTracking-impl.hpp:127
virtual void loadConfigFile(const std::string &filename, const std::string &termName, bool verbose=false) override
load this term from a configuration file
Definition: TermQuadTracking-impl.hpp:146
const bool verbose
Definition: ConstraintComparison.h:18
core::ControlVector< CONTROL_DIM, SCALAR_EVAL > controlDerivative(const core::StateVector< STATE_DIM, SCALAR_EVAL > &x, const core::ControlVector< CONTROL_DIM, SCALAR_EVAL > &u, const SCALAR_EVAL &t) override
compute derivative of this cost term w.r.t. the control input
Definition: TermQuadTracking-impl.hpp:110
virtual ~TermQuadTracking()
Definition: TermQuadTracking-impl.hpp:47
TermQuadTracking()
Definition: TermQuadTracking-impl.hpp:26
ct::core::StateTrajectory< STATE_DIM, SCALAR_EVAL > x_traj_ref_
Definition: TermQuadTracking.hpp:87
state_matrix_t Q_
Definition: TermQuadTracking.hpp:83
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM > control_matrix_t
Definition: TermQuadTracking.hpp:26
bool trackControlTrajectory_
Definition: TermQuadTracking.hpp:91