- 3.0.2 optimal control module.
LinkedMasses.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 
12 #pragma once
13 
14 using namespace ct::core;
15 
16 
17 void dmcopy(int row, int col, double* A, int lda, double* B, int ldb)
18 {
19  int i, j;
20  for (j = 0; j < col; j++)
21  {
22  for (i = 0; i < row; i++)
23  {
24  B[i + j * ldb] = A[i + j * lda];
25  }
26  }
27 }
28 
29 class LinkedMasses : public LinearSystem<8, 3>
30 {
31 public:
32  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
33 
34  static const int state_dim = 8;
35  static const int control_dim = 3;
36  static const int pp = state_dim / 2; // number of masses
37 
39  {
40  A_.setZero();
41  B_.setZero();
42 
43 
44  Eigen::Matrix<double, pp, pp> TEigen;
45  TEigen.setZero();
46 
47  double* T = TEigen.data();
48  int ii;
49  for (ii = 0; ii < pp; ii++)
50  T[ii * (pp + 1)] = -2;
51  for (ii = 0; ii < pp - 1; ii++)
52  T[ii * (pp + 1) + 1] = 1;
53  for (ii = 1; ii < pp; ii++)
54  T[ii * (pp + 1) - 1] = 1;
55 
56  Eigen::Matrix<double, pp, pp> ZEigen;
57  ZEigen.setZero();
58  double* Z = ZEigen.data();
59 
60  Eigen::Matrix<double, pp, pp> IEigen;
61  IEigen.setIdentity();
62  double* I = IEigen.data();
63 
64  double* Ac = A_.data();
65  dmcopy(pp, pp, Z, pp, Ac, state_dim);
66  dmcopy(pp, pp, T, pp, Ac + pp, state_dim);
67  dmcopy(pp, pp, I, pp, Ac + pp * state_dim, state_dim);
68  dmcopy(pp, pp, Z, pp, Ac + pp * (state_dim + 1), state_dim);
69 
70  Eigen::Matrix<double, control_dim, control_dim> InuEigen;
71  InuEigen.setIdentity();
72  double* Inu = InuEigen.data();
73 
74  double* Bc = B_.data();
75  dmcopy(control_dim, control_dim, Inu, control_dim, Bc + pp, state_dim);
76  }
77 
78 
81  const double t = 0.0) override
82  {
83  return A_;
84  }
85 
88  const double t = 0.0) override
89  {
90  return B_;
91  }
92 
93  LinkedMasses* clone() const override { return new LinkedMasses(); };
94 private:
95  state_matrix_t A_;
97 };
98 
99 
100 class LinkedMasses2 : public ControlledSystem<8, 3>
101 {
102 public:
103  static const int state_dim = 8;
104  static const int control_dim = 3;
105 
106  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
107 
109  {
110  A_.setZero();
111  B_.setZero();
112  b_.setZero();
113 
114  b_ << 0.145798, 0.150018, 0.150018, 0.145798, 0.245798, 0.200018, 0.200018, 0.245798;
115 
116  static const int pp = state_dim / 2; // number of masses
117 
118  Eigen::Matrix<double, pp, pp> TEigen;
119  TEigen.setZero();
120 
121  double* T = TEigen.data();
122  int ii;
123  for (ii = 0; ii < pp; ii++)
124  T[ii * (pp + 1)] = -2;
125  for (ii = 0; ii < pp - 1; ii++)
126  T[ii * (pp + 1) + 1] = 1;
127  for (ii = 1; ii < pp; ii++)
128  T[ii * (pp + 1) - 1] = 1;
129 
130  Eigen::Matrix<double, pp, pp> ZEigen;
131  ZEigen.setZero();
132  double* Z = ZEigen.data();
133 
134  Eigen::Matrix<double, pp, pp> IEigen;
135  IEigen.setIdentity();
136  double* I = IEigen.data();
137 
138  double* Ac = A_.data();
139  dmcopy(pp, pp, Z, pp, Ac, state_dim);
140  dmcopy(pp, pp, T, pp, Ac + pp, state_dim);
141  dmcopy(pp, pp, I, pp, Ac + pp * state_dim, state_dim);
142  dmcopy(pp, pp, Z, pp, Ac + pp * (state_dim + 1), state_dim);
143 
144  Eigen::Matrix<double, control_dim, control_dim> InuEigen;
145  InuEigen.setIdentity();
146  double* Inu = InuEigen.data();
147 
148  double* Bc = B_.data();
149  dmcopy(control_dim, control_dim, Inu, control_dim, Bc + pp, state_dim);
150  }
151 
152  LinkedMasses2* clone() const override { return new LinkedMasses2(); };
154  const double& t,
156  ct::core::StateVector<state_dim>& derivative) override
157  {
158  derivative = A_ * state + B_ * control + b_;
159  }
160 
161 private:
165 };
EIGEN_MAKE_ALIGNED_OPERATOR_NEW LinkedMasses2()
Definition: LinkedMasses.h:108
LinkedMasses()
Definition: LinkedMasses.h:38
void dmcopy(int row, int col, double *A, int lda, double *B, int ldb)
Definition: LinkedMasses.h:17
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
void computeControlledDynamics(const ct::core::StateVector< state_dim > &state, const double &t, const ct::core::ControlVector< control_dim > &control, ct::core::StateVector< state_dim > &derivative) override
Definition: LinkedMasses.h:153
const size_t state_dim
Definition: ConstraintExampleOutput.cpp:17
clear all close all load ct GNMSLog0 mat reformat t
Definition: gnmsPlot.m:6
for i
Definition: mpc_unittest_plotting.m:14
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
const state_matrix_t & getDerivativeState(const StateVector< state_dim > &x, const ControlVector< control_dim > &u, const double t=0.0) override
Definition: LinkedMasses.h:79
Definition: LinkedMasses.h:29
LinkedMasses2 * clone() const override
Definition: LinkedMasses.h:152
const state_control_matrix_t & getDerivativeControl(const StateVector< state_dim > &x, const ControlVector< control_dim > &u, const double t=0.0) override
Definition: LinkedMasses.h:86
LinkedMasses * clone() const override
Definition: LinkedMasses.h:93
Definition: LinkedMasses.h:100
Z
const size_t control_dim
Definition: ConstraintExampleOutput.cpp:18