- 3.0.2 optimal control module.
LinearOscillator.h
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 namespace example {
11 
12 
13 using namespace ct::core;
14 using namespace ct::optcon;
15 
16 using std::shared_ptr;
17 
18 const size_t state_dim = 2; // position, velocity
19 const size_t control_dim = 1; // force
20 
21 const double kStiffness = 0.1;
22 
23 
24 namespace tpl {
25 
26 template <typename SCALAR = double>
27 class LinearOscillator : public ControlledSystem<state_dim, control_dim, SCALAR>
28 {
29 public:
30  LinearOscillator() : ControlledSystem<state_dim, control_dim, SCALAR>(SYSTEM_TYPE::GENERAL) {}
32  const SCALAR& t,
34  StateVector<state_dim, SCALAR>& derivative) override
35  {
36  derivative(0) = state(1);
37  derivative(1) = control(0) - kStiffness * state(0); // mass is 1 kg
38  }
39 
40  LinearOscillator<SCALAR>* clone() const override { return new LinearOscillator<SCALAR>(); };
41 };
42 
43 
44 template <typename SCALAR = double>
45 class LinearOscillatorLinear : public LinearSystem<state_dim, control_dim, SCALAR>
46 {
47 public:
50 
51  state_matrix_t A_;
52  state_control_matrix_t B_;
53 
54  const state_matrix_t& getDerivativeState(const StateVector<state_dim, SCALAR>& x,
56  const SCALAR t = 0.0) override
57  {
58  A_ << 0, 1, -kStiffness, 0;
59  return A_;
60  }
61 
62  const state_control_matrix_t& getDerivativeControl(const StateVector<state_dim, SCALAR>& x,
64  const SCALAR t = 0.0) override
65  {
66  B_ << 0, 1;
67  return B_;
68  }
69 
71 };
72 
73 
74 template <typename SCALAR = double>
75 std::shared_ptr<CostFunctionQuadratic<state_dim, control_dim, SCALAR>> createCostFunctionLinearOscillator(
76  Eigen::Matrix<SCALAR, 2, 1>& x_final)
77 {
78  Eigen::Matrix<SCALAR, 2, 2> Q;
79  Q << 0, 0, 0, 1;
80 
81  Eigen::Matrix<SCALAR, 1, 1> R;
82  R << 100;
83 
86 
87  Eigen::Matrix<SCALAR, 2, 2> Q_final;
88  Q_final << 1000, 0, 0, 1000;
89 
90  std::shared_ptr<TermQuadratic<state_dim, control_dim>> termIntermediate(
91  new TermQuadratic<state_dim, control_dim>(Q, R, x_nominal, u_nominal));
92  std::shared_ptr<TermQuadratic<state_dim, control_dim>> termFinal(
93  new TermQuadratic<state_dim, control_dim>(Q_final, R, x_final, u_nominal));
94 
95  std::shared_ptr<CostFunctionAnalytical<state_dim, control_dim>> quadraticCostFunction(
97  quadraticCostFunction->addIntermediateTerm(termIntermediate);
98  quadraticCostFunction->addFinalTerm(termFinal);
99 
100  return quadraticCostFunction;
101 }
102 
103 } // namespace tpl
104 
107 
108 } //example
109 } //optcon
110 } //ct
A basic quadratic term of type .
Definition: TermQuadratic.hpp:22
state_matrix_t A_
Definition: LinearOscillator.h:51
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
core::StateMatrix< state_dim, SCALAR > state_matrix_t
Definition: LinearOscillator.h:48
state_control_matrix_t B_
Definition: LinearOscillator.h:52
const size_t state_dim
Definition: ConstraintComparison.h:20
LinearOscillator()
Definition: LinearOscillator.h:30
core::StateControlMatrix< state_dim, control_dim, SCALAR > state_control_matrix_t
Definition: LinearOscillator.h:49
A cost function which contains only terms that have analytical derivatives.
Definition: CostFunctionAnalytical.hpp:31
clear all close all load ct GNMSLog0 mat reformat t
Definition: gnmsPlot.m:6
Definition: LinearOscillator.h:45
LinearOscillatorLinear< SCALAR > * clone() const override
Definition: LinearOscillator.h:70
CppAD::AD< CppAD::cg::CG< double > > SCALAR
std::shared_ptr< CostFunctionQuadratic< state_dim, control_dim, SCALAR > > createCostFunctionLinearOscillator(Eigen::Matrix< SCALAR, 2, 1 > &x_final)
Definition: LinearOscillator.h:75
Definition: LinearOscillator.h:27
const double kStiffness
Definition: SymplecticTest.h:34
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
LinearOscillator< SCALAR > * clone() const override
Definition: LinearOscillator.h:40
GENERAL
tpl::LinearOscillator< double > LinearOscillator
Definition: LinearOscillator.h:105
const state_matrix_t & getDerivativeState(const StateVector< state_dim, SCALAR > &x, const ControlVector< control_dim, SCALAR > &u, const SCALAR t=0.0) override
Definition: LinearOscillator.h:54
tpl::LinearOscillatorLinear< double > LinearOscillatorLinear
Definition: LinearOscillator.h:106
void computeControlledDynamics(const StateVector< state_dim, SCALAR > &state, const SCALAR &t, const ControlVector< control_dim, SCALAR > &control, StateVector< state_dim, SCALAR > &derivative) override
Definition: LinearOscillator.h:31
const size_t control_dim
Definition: ConstraintComparison.h:21
Definition: exampleDir.h:9
const state_control_matrix_t & getDerivativeControl(const StateVector< state_dim, SCALAR > &x, const ControlVector< control_dim, SCALAR > &u, const SCALAR t=0.0) override
Definition: LinearOscillator.h:62