Cheetah Software  1.0
StateEstimatorContainer.h
Go to the documentation of this file.
1 
10 #ifndef PROJECT_STATEESTIMATOR_H
11 #define PROJECT_STATEESTIMATOR_H
12 
15 #include "SimUtilities/IMUTypes.h"
17 #include "state_estimator_lcmt.hpp"
18 
22 template <typename T>
23 struct StateEstimate {
24  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
32 
36 
37  void setLcm(state_estimator_lcmt& lcm_data) {
38  for(int i = 0; i < 3; i++) {
39  lcm_data.p[i] = position[i];
40  lcm_data.vWorld[i] = vWorld[i];
41  lcm_data.vBody[i] = vBody[i];
42  lcm_data.rpy[i] = rpy[i];
43  lcm_data.omegaBody[i] = omegaBody[i];
44  lcm_data.omegaWorld[i] = omegaWorld[i];
45  }
46 
47  for(int i = 0; i < 4; i++) {
48  lcm_data.quat[i] = orientation[i];
49  }
50  }
51 };
52 
59 template <typename T>
61  StateEstimate<T>* result; // where to write the output to
67 };
68 
72 template <typename T>
74  public:
75  virtual void run() = 0;
76  virtual void setup() = 0;
77 
78  void setData(StateEstimatorData<T> data) { _stateEstimatorData = data; }
79 
80  virtual ~GenericEstimator() = default;
82 };
83 
88 template <typename T>
90  public:
91  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
92 
97  VectorNavData* vectorNavData,
98  LegControllerData<T>* legControllerData,
99  StateEstimate<T>* stateEstimate,
100  RobotControlParameters* parameters) {
101  _data.cheaterState = cheaterState;
102  _data.vectorNavData = vectorNavData;
103  _data.legControllerData = legControllerData;
104  _data.result = stateEstimate;
105  _phase = Vec4<T>::Zero();
106  _data.contactPhase = &_phase;
107  _data.parameters = parameters;
108  }
109 
113  void run(CheetahVisualization* visualization = nullptr) {
114  for (auto estimator : _estimators) {
115  estimator->run();
116  }
117  if (visualization) {
118  visualization->quat = _data.result->orientation.template cast<float>();
119  visualization->p = _data.result->position.template cast<float>();
120  // todo contact!
121  }
122  }
123 
127  const StateEstimate<T>& getResult() { return *_data.result; }
128 
132  void setContactPhase(Vec4<T>& phase) { *_data.contactPhase = phase; }
133 
138  template <typename EstimatorToAdd>
139  void addEstimator() {
140  auto* estimator = new EstimatorToAdd();
141  estimator->setData(_data);
142  estimator->setup();
143  _estimators.push_back(estimator);
144  }
145 
150  template <typename EstimatorToRemove>
152  int nRemoved = 0;
153  _estimators.erase(
154  std::remove_if(_estimators.begin(), _estimators.end(),
155  [&nRemoved](GenericEstimator<T>* e) {
156  if (dynamic_cast<EstimatorToRemove*>(e)) {
157  delete e;
158  nRemoved++;
159  return true;
160  } else {
161  return false;
162  }
163  }),
164  _estimators.end());
165  }
166 
171  for (auto estimator : _estimators) {
172  delete estimator;
173  }
174  _estimators.clear();
175  }
176 
178  for (auto estimator : _estimators) {
179  delete estimator;
180  }
181  }
182 
183  private:
185  std::vector<GenericEstimator<T>*> _estimators;
187 };
188 
189 #endif // PROJECT_STATEESTIMATOR_H
RobotControlParameters * parameters
CheaterState< double > * cheaterState
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Vec4< T > contactEstimate
typename Eigen::Matrix< T, 4, 1 > Quat
Definition: cppTypes.h:58
LegControllerData< T > * legControllerData
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
std::vector< GenericEstimator< T > * > _estimators
const StateEstimate< T > & getResult()
void setData(StateEstimatorData< T > data)
StateEstimate< T > * result
StateEstimatorData< T > _data
void run(CheetahVisualization *visualization=nullptr)
Common Leg Control Interface and Leg Control Algorithms.
typename Eigen::Matrix< T, 4, 1 > Vec4
Definition: cppTypes.h:30
void setLcm(state_estimator_lcmt &lcm_data)
EIGEN_MAKE_ALIGNED_OPERATOR_NEW StateEstimatorContainer(CheaterState< double > *cheaterState, VectorNavData *vectorNavData, LegControllerData< T > *legControllerData, StateEstimate< T > *stateEstimate, RobotControlParameters *parameters)
void setContactPhase(Vec4< T > &phase)
VectorNavData * vectorNavData
Data from IMUs.
typename Eigen::Matrix< T, 3, 3 > RotMat
Definition: cppTypes.h:18
StateEstimatorData< T > _stateEstimatorData