- 3.0.2 optimal control module.
TermQuadTracking-impl.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 namespace ct {
9 namespace optcon {
10 
11 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
13  const control_matrix_t& R,
14  const core::InterpolationType& stateSplineType,
15  const core::InterpolationType& controlSplineType,
16  const bool trackControlTrajectory)
17  : Q_(Q),
18  R_(R),
19  x_traj_ref_(stateSplineType),
20  u_traj_ref_(controlSplineType),
21  trackControlTrajectory_(trackControlTrajectory)
22 {
23 }
24 
25 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
27 {
28  // default values
29  Q_.setIdentity();
30  R_.setIdentity();
32 }
33 
34 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
38  Q_(arg.Q_),
39  R_(arg.R_),
43 {
44 }
45 
46 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
48 {
49 }
50 
51 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
54 {
55  return new TermQuadTracking(*this);
56 }
57 
58 
59 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
61  const control_matrix_double_t& R)
62 {
63  Q_ = Q.template cast<SCALAR_EVAL>();
64  R_ = R.template cast<SCALAR_EVAL>();
65 }
66 
67 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
71 {
72  x_traj_ref_ = xTraj;
73  u_traj_ref_ = uTraj;
74 }
75 
76 
77 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
79  const Eigen::Matrix<SCALAR, STATE_DIM, 1>& x,
80  const Eigen::Matrix<SCALAR, CONTROL_DIM, 1>& u,
81  const SCALAR& t)
82 {
83  return evalLocal<SCALAR>(x, u, t);
84 }
85 
86 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
91  const SCALAR_EVAL& t)
92 {
93  Eigen::Matrix<SCALAR_EVAL, STATE_DIM, 1> xDiff = x - x_traj_ref_.eval(t);
94 
95  return xDiff.transpose() * Q_.transpose() + xDiff.transpose() * Q_;
96 }
97 
98 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
103  const SCALAR_EVAL& t)
104 {
105  return Q_ + Q_.transpose();
106 }
107 
108 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
113  const SCALAR_EVAL& t)
114 {
115  Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, 1> uDiff;
116 
118  uDiff = u - u_traj_ref_.eval(t);
119  else
120  uDiff = u;
121 
122  return uDiff.transpose() * R_.transpose() + uDiff.transpose() * R_;
123 }
124 
125 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
130  const SCALAR_EVAL& t)
131 {
132  return R_ + R_.transpose();
133 }
134 
135 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
140  const SCALAR_EVAL& t)
141 {
142  return control_state_matrix_t::Zero();
143 }
144 
145 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
147  const std::string& termName,
148  bool verbose)
149 {
150  loadMatrixCF(filename, "Q", Q_, termName);
151  loadMatrixCF(filename, "R", R_, termName);
152  if (verbose)
153  {
154  std::cout << "Read Q as Q = \n" << Q_ << std::endl;
155  std::cout << "Read R as R = \n" << R_ << std::endl;
156  }
157 }
158 } // namespace optcon
159 } // namespace ct
Eigen::Matrix< SCALAR_EVAL, STATE_DIM, STATE_DIM > state_matrix_double_t
Definition: TermBase.hpp:42
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
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
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
void setStateAndControlReference(const core::StateTrajectory< STATE_DIM > &xTraj, const core::ControlTrajectory< CONTROL_DIM > &uTraj)
Definition: TermQuadTracking-impl.hpp:68
ct::core::ControlTrajectory< CONTROL_DIM, SCALAR_EVAL > u_traj_ref_
Definition: TermQuadTracking.hpp:88
control_matrix_t R_
Definition: TermQuadTracking.hpp:84
Eigen::Matrix< SCALAR_EVAL, STATE_DIM, STATE_DIM > state_matrix_t
Definition: TermBase.hpp:39
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM > control_matrix_double_t
Definition: TermBase.hpp:43
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
void loadMatrixCF(const std::string &filename, const std::string &matrixName, Eigen::Matrix< SCALAR, ROW, COL > &matrix, const std::string &termName="")
Definition: utilities.hpp:46
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
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM > control_matrix_t
Definition: TermBase.hpp:40