Cheetah Software  1.0
LinearKFPositionVelocityEstimator< T > Class Template Reference

#include <PositionVelocityEstimator.h>

+ Inheritance diagram for LinearKFPositionVelocityEstimator< T >:
+ Collaboration diagram for LinearKFPositionVelocityEstimator< T >:

Public Member Functions

EIGEN_MAKE_ALIGNED_OPERATOR_NEW LinearKFPositionVelocityEstimator ()
 
virtual void run ()
 
virtual void setup ()
 
- Public Member Functions inherited from GenericEstimator< T >
void setData (StateEstimatorData< T > data)
 
virtual ~GenericEstimator ()=default
 

Private Attributes

Eigen::Matrix< T, 18, 1 > _xhat
 
Eigen::Matrix< T, 12, 1 > _ps
 
Eigen::Matrix< T, 12, 1 > _vs
 
Eigen::Matrix< T, 18, 18 > _A
 
Eigen::Matrix< T, 18, 18 > _Q0
 
Eigen::Matrix< T, 18, 18 > _P
 
Eigen::Matrix< T, 28, 28 > _R0
 
Eigen::Matrix< T, 18, 3 > _B
 
Eigen::Matrix< T, 28, 18 > _C
 

Additional Inherited Members

- Public Attributes inherited from GenericEstimator< T >
StateEstimatorData< T > _stateEstimatorData
 

Detailed Description

template<typename T>
class LinearKFPositionVelocityEstimator< T >

Definition at line 16 of file PositionVelocityEstimator.h.

Constructor & Destructor Documentation

Definition at line 51 of file PositionVelocityEstimator.cpp.

51 {}

Member Function Documentation

template<typename T >
void LinearKFPositionVelocityEstimator< T >::run ( )
virtual

Implements GenericEstimator< T >.

Definition at line 54 of file PositionVelocityEstimator.cpp.

References f(), and Quadruped< T >::getHipLocation().

54  {
55  T process_noise_pimu =
56  this->_stateEstimatorData.parameters->imu_process_noise_position;
57  T process_noise_vimu =
58  this->_stateEstimatorData.parameters->imu_process_noise_velocity;
59  T process_noise_pfoot =
60  this->_stateEstimatorData.parameters->foot_process_noise_position;
61  T sensor_noise_pimu_rel_foot =
62  this->_stateEstimatorData.parameters->foot_sensor_noise_position;
63  T sensor_noise_vimu_rel_foot =
64  this->_stateEstimatorData.parameters->foot_sensor_noise_velocity;
65  T sensor_noise_zfoot =
66  this->_stateEstimatorData.parameters->foot_height_sensor_noise;
67 
68  Eigen::Matrix<T, 18, 18> Q = Eigen::Matrix<T, 18, 18>::Identity();
69  Q.block(0, 0, 3, 3) = _Q0.block(0, 0, 3, 3) * process_noise_pimu;
70  Q.block(3, 3, 3, 3) = _Q0.block(3, 3, 3, 3) * process_noise_vimu;
71  Q.block(6, 6, 12, 12) = _Q0.block(6, 6, 12, 12) * process_noise_pfoot;
72 
73  Eigen::Matrix<T, 28, 28> R = Eigen::Matrix<T, 28, 28>::Identity();
74  R.block(0, 0, 12, 12) = _R0.block(0, 0, 12, 12) * sensor_noise_pimu_rel_foot;
75  R.block(12, 12, 12, 12) =
76  _R0.block(12, 12, 12, 12) * sensor_noise_vimu_rel_foot;
77  R.block(24, 24, 4, 4) = _R0.block(24, 24, 4, 4) * sensor_noise_zfoot;
78 
79  int qindex = 0;
80  int rindex1 = 0;
81  int rindex2 = 0;
82  int rindex3 = 0;
83 
84  Vec3<T> g(0, 0, T(-9.81));
85  Mat3<T> Rbod = this->_stateEstimatorData.result->rBody.transpose();
86  Vec3<T> a = this->_stateEstimatorData.result->aWorld +
87  g; // in old code, Rbod * se_acc + g
88  // std::cout << "A WORLD\n" << a << "\n";
89  Vec4<T> pzs = Vec4<T>::Zero();
90  Vec4<T> trusts = Vec4<T>::Zero();
91  Vec3<T> p0, v0;
92  p0 << _xhat[0], _xhat[1], _xhat[2];
93  v0 << _xhat[3], _xhat[4], _xhat[5];
94 
95  for (int i = 0; i < 4; i++) {
96  int i1 = 3 * i;
97  Quadruped<T>& quadruped =
98  *(this->_stateEstimatorData.legControllerData->quadruped);
99  Vec3<T> ph = quadruped.getHipLocation(i); // hip positions relative to CoM
100  Vec3<T> p_rel = ph + this->_stateEstimatorData.legControllerData[i]
101  .p; // hw_i->leg_controller->leg_datas[i].p; //
102  Vec3<T> dp_rel = this->_stateEstimatorData.legControllerData[i]
103  .v; // hw_i->leg_controller->leg_datas[i].v;
104  Vec3<T> p_f = Rbod * p_rel;
105  Vec3<T> dp_f =
106  Rbod *
107  (this->_stateEstimatorData.result->omegaBody.cross(p_rel) + dp_rel);
108 
109  qindex = 6 + i1;
110  rindex1 = i1;
111  rindex2 = 12 + i1;
112  rindex3 = 24 + i;
113 
114  T trust = T(1);
115  T phase = fmin(this->_stateEstimatorData.result->contactEstimate(i), T(1));
116  T trust_window = T(0.2);
117 
118  if (phase < trust_window) {
119  trust = phase / trust_window;
120  } else if (phase > (T(1) - trust_window)) {
121  trust = (T(1) - phase) / trust_window;
122  }
123 
124  // printf("Trust %d: %.3f\n", i, trust);
125  Q.block(qindex, qindex, 3, 3) =
126  (T(1) + (T(1) - trust) * T(100)) * Q.block(qindex, qindex, 3, 3);
127  R.block(rindex1, rindex1, 3, 3) = 1 * R.block(rindex1, rindex1, 3, 3);
128  R.block(rindex2, rindex2, 3, 3) =
129  (T(1) + (T(1) - trust) * 100.0f) * R.block(rindex2, rindex2, 3, 3);
130  R(rindex3, rindex3) =
131  (T(1) + (T(1) - trust) * T(100)) * R(rindex3, rindex3);
132 
133  trusts(i) = trust;
134 
135  _ps.segment(i1, 3) = -p_f;
136  _vs.segment(i1, 3) = (1.0f - trust) * v0 + trust * (-dp_f);
137  pzs(i) = (1.0f - trust) * (p0(2) + p_f(2));
138  }
139 
140 
141  Eigen::Matrix<T, 28, 1> y;
142  y << _ps, _vs, pzs;
143  _xhat = _A * _xhat + _B * a;
144  Eigen::Matrix<T, 18, 18> At = _A.transpose();
145  Eigen::Matrix<T, 18, 18> Pm = _A * _P * At + Q;
146  Eigen::Matrix<T, 18, 28> Ct = _C.transpose();
147  Eigen::Matrix<T, 28, 1> yModel = _C * _xhat;
148  Eigen::Matrix<T, 28, 1> ey = y - yModel;
149  Eigen::Matrix<T, 28, 28> S = _C * Pm * Ct + R;
150 
151  // todo compute LU only once
152  Eigen::Matrix<T, 28, 1> S_ey = S.lu().solve(ey);
153  _xhat += Pm * Ct * S_ey;
154 
155  Eigen::Matrix<T, 28, 18> S_C = S.lu().solve(_C);
156  _P = (Eigen::Matrix<T, 18, 18>::Identity() - Pm * Ct * S_C) * Pm;
157 
158  Eigen::Matrix<T, 18, 18> Pt = _P.transpose();
159  _P = (_P + Pt) / T(2);
160 
161  if (_P.block(0, 0, 2, 2).determinant() > T(0.000001)) {
162  _P.block(0, 2, 2, 16).setZero();
163  _P.block(2, 0, 16, 2).setZero();
164  _P.block(0, 0, 2, 2) /= T(10);
165  }
166 
167  this->_stateEstimatorData.result->position = _xhat.block(0, 0, 3, 1);
168  this->_stateEstimatorData.result->vWorld = _xhat.block(3, 0, 3, 1);
169  this->_stateEstimatorData.result->vBody =
170  this->_stateEstimatorData.result->rBody *
171  this->_stateEstimatorData.result->vWorld;
172 }
typename Eigen::Matrix< T, 3, 3 > Mat3
Definition: cppTypes.h:54
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
Vec3< T > getHipLocation(int leg)
Definition: Quadruped.h:83
typename Eigen::Matrix< T, 4, 1 > Vec4
Definition: cppTypes.h:30
StateEstimatorData< T > _stateEstimatorData
MX f(const MX &x, const MX &u)

+ Here is the call graph for this function:

template<typename T >
void LinearKFPositionVelocityEstimator< T >::setup ( )
virtual

Implements GenericEstimator< T >.

Definition at line 4 of file PositionVelocityEstimator.cpp.

4  {
5  printf("beans 0x%lx\n", (uint64_t)this);
6  printf("beans2 0x%lx\n", (uint64_t) & (this->_stateEstimatorData));
7  printf("beans3 0x%lx\n", (uint64_t)(this->_stateEstimatorData.parameters));
8  printf("beans4 0x%lx\n",
9  (uint64_t) & (this->_stateEstimatorData.parameters->controller_dt));
10  T dt = this->_stateEstimatorData.parameters->controller_dt;
11  printf("Initialize LinearKF State Estimator with dt = %.3f\n", dt);
12  _xhat.setZero();
13  _ps.setZero();
14  _vs.setZero();
15  _A.setZero();
16  _A.block(0, 0, 3, 3) = Eigen::Matrix<T, 3, 3>::Identity();
17  _A.block(0, 3, 3, 3) = dt * Eigen::Matrix<T, 3, 3>::Identity();
18  _A.block(3, 3, 3, 3) = Eigen::Matrix<T, 3, 3>::Identity();
19  _A.block(6, 6, 12, 12) = Eigen::Matrix<T, 12, 12>::Identity();
20  _B.setZero();
21  _B.block(3, 0, 3, 3) = dt * Eigen::Matrix<T, 3, 3>::Identity();
22  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> C1(3, 6);
23  C1 << Eigen::Matrix<T, 3, 3>::Identity(), Eigen::Matrix<T, 3, 3>::Zero();
24  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> C2(3, 6);
25  C2 << Eigen::Matrix<T, 3, 3>::Zero(), Eigen::Matrix<T, 3, 3>::Identity();
26  _C.setZero();
27  _C.block(0, 0, 3, 6) = C1;
28  _C.block(3, 0, 3, 6) = C1;
29  _C.block(6, 0, 3, 6) = C1;
30  _C.block(9, 0, 3, 6) = C1;
31  _C.block(0, 6, 12, 12) = T(-1) * Eigen::Matrix<T, 12, 12>::Identity();
32  _C.block(12, 0, 3, 6) = C2;
33  _C.block(15, 0, 3, 6) = C2;
34  _C.block(18, 0, 3, 6) = C2;
35  _C.block(21, 0, 3, 6) = C2;
36  _C(27, 17) = T(1);
37  _C(26, 14) = T(1);
38  _C(25, 11) = T(1);
39  _C(24, 8) = T(1);
40  _P.setIdentity();
41  _P = T(100) * _P;
42  _Q0.setIdentity();
43  _Q0.block(0, 0, 3, 3) = (dt / 20.f) * Eigen::Matrix<T, 3, 3>::Identity();
44  _Q0.block(3, 3, 3, 3) =
45  (dt * 9.8f / 20.f) * Eigen::Matrix<T, 3, 3>::Identity();
46  _Q0.block(6, 6, 12, 12) = dt * Eigen::Matrix<T, 12, 12>::Identity();
47  _R0.setIdentity();
48 }
StateEstimatorData< T > _stateEstimatorData

Member Data Documentation

template<typename T >
Eigen::Matrix<T, 18, 18> LinearKFPositionVelocityEstimator< T >::_A
private

Definition at line 27 of file PositionVelocityEstimator.h.

template<typename T >
Eigen::Matrix<T, 18, 3> LinearKFPositionVelocityEstimator< T >::_B
private

Definition at line 31 of file PositionVelocityEstimator.h.

template<typename T >
Eigen::Matrix<T, 28, 18> LinearKFPositionVelocityEstimator< T >::_C
private

Definition at line 32 of file PositionVelocityEstimator.h.

template<typename T >
Eigen::Matrix<T, 18, 18> LinearKFPositionVelocityEstimator< T >::_P
private

Definition at line 29 of file PositionVelocityEstimator.h.

template<typename T >
Eigen::Matrix<T, 12, 1> LinearKFPositionVelocityEstimator< T >::_ps
private

Definition at line 25 of file PositionVelocityEstimator.h.

template<typename T >
Eigen::Matrix<T, 18, 18> LinearKFPositionVelocityEstimator< T >::_Q0
private

Definition at line 28 of file PositionVelocityEstimator.h.

template<typename T >
Eigen::Matrix<T, 28, 28> LinearKFPositionVelocityEstimator< T >::_R0
private

Definition at line 30 of file PositionVelocityEstimator.h.

template<typename T >
Eigen::Matrix<T, 12, 1> LinearKFPositionVelocityEstimator< T >::_vs
private

Definition at line 26 of file PositionVelocityEstimator.h.

template<typename T >
Eigen::Matrix<T, 18, 1> LinearKFPositionVelocityEstimator< T >::_xhat
private

Definition at line 24 of file PositionVelocityEstimator.h.


The documentation for this class was generated from the following files: