GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/inverse_pendulum/modelIP.cpp Lines: 0 65 0.0 %
Date: 2023-06-02 15:50:43 Branches: 0 74 0.0 %

Line Branch Exec Source
1
#include "ddp-actuator-solver/inverse_pendulum/modelIP.hh"
2
3
#include <math.h>
4
#include <sys/time.h>
5
6
#include <eigen3/unsupported/Eigen/MatrixFunctions>
7
#include <iostream>
8
9
/*
10
 * x0 -> actuator position
11
 * x1 -> actuator speed
12
 * x2 -> motor temperature
13
 * x3 -> external torque
14
 * x4 -> ambiant temperature
15
 */
16
17
ModelIP::ModelIP(double& mydt, bool noiseOnParameters) {
18
  stateNb = 5;
19
  commandNb = 1;
20
  dt = mydt;
21
22
  if (!noiseOnParameters) {
23
    J = 119e-7;
24
    K_M = 77.1e-3;
25
    f_VL = 0.429e-6;
26
    R_th = 2.8;
27
    tau_th = 15.7;
28
  } else {
29
    J = 119e-17;
30
    K_M = 77.1e-3;
31
    f_VL = 0.429e-6;
32
    R_th = 2.8;
33
    tau_th = 15.7;
34
  }
35
36
  Id.setIdentity();
37
38
  fu.setZero();
39
  fx.setZero();
40
41
  fxx[0].setZero();
42
  fxx[1].setZero();
43
  fxx[2].setZero();
44
  fxx[3].setZero();
45
  fxx[4].setZero();
46
47
  fxu[0].setZero();
48
  fxu[0].setZero();
49
50
  fuu[0].setZero();
51
  fux[0].setZero();
52
  fxu[0].setZero();
53
54
  QxxCont.setZero();
55
  QuuCont.setZero();
56
  QuxCont.setZero();
57
58
  lowerCommandBounds << -1.0;
59
  upperCommandBounds << 1.0;
60
}
61
62
ModelIP::stateVec_t ModelIP::computeDeriv(double&, const stateVec_t& X,
63
                                          const commandVec_t& U) {
64
  dX[0] = X[1];
65
  dX[1] = (K_M / J) * U[0] - (f_VL / J) * X[1] - (1.0 / J) * X[3];
66
  dX[2] = R_th * U[0] * U[0] - (X[2] - X[4]) / tau_th;
67
  dX[3] = 0.0;
68
  dX[4] = 0.0;
69
  // std::cout << dX.transpose() << std::endl;
70
  return dX;
71
}
72
73
ModelIP::stateVec_t ModelIP::computeNextState(double& dt, const stateVec_t& X,
74
                                              const commandVec_t& U) {
75
  k1 = computeDeriv(dt, X, U);
76
  k2 = computeDeriv(dt, X + (dt / 2) * k1, U);
77
  k3 = computeDeriv(dt, X + (dt / 2) * k2, U);
78
  k4 = computeDeriv(dt, X + dt * k3, U);
79
  x_next = X + (dt / 6) * (k1 + 2 * k2 + 2 * k3 + k4);
80
  return x_next;
81
}
82
83
void ModelIP::computeModelDeriv(double& dt, const stateVec_t& X,
84
                                const commandVec_t& U) {
85
  double dh = 1e-7;
86
  stateVec_t Xp, Xm;
87
  Xp = X;
88
  Xm = X;
89
  for (unsigned int i = 0; i < stateNb; i++) {
90
    Xp[i] += dh / 2;
91
    Xm[i] -= dh / 2;
92
    fx.col(i) =
93
        (computeNextState(dt, Xp, U) - computeNextState(dt, Xm, U)) / dh;
94
    Xp = X;
95
    Xm = X;
96
  }
97
}
98
99
ModelIP::stateMat_t ModelIP::computeTensorContxx(const stateVec_t&) {
100
  return QxxCont;
101
}
102
103
ModelIP::commandMat_t ModelIP::computeTensorContuu(const stateVec_t&) {
104
  return QuuCont;
105
}
106
107
ModelIP::commandR_stateC_t ModelIP::computeTensorContux(const stateVec_t&) {
108
  return QuxCont;
109
}