ethzasl-msf - Modular Sensor Fusion
Time delay compensated single and multi sensor fusion framework based on an EKF
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
msf_state.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2013 Simon Lynen, ASL, ETH Zurich, Switzerland
3  * You can contact the author at <slynen at ethz dot ch>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef MSF_STATE_H_
18 #define MSF_STATE_H_
19 
20 #include <msf_core/msf_types.hpp>
21 #include <msf_core/msf_tmp.h>
23 #include <Eigen/Dense>
24 #include <Eigen/Geometry>
25 #include <vector>
26 #include <utility>
28 #include <sensor_fusion_comm/ExtState.h>
29 #include <sensor_fusion_comm/DoubleArrayStamped.h>
30 #include <geometry_msgs/PoseWithCovarianceStamped.h>
31 
32 namespace msf_core {
33 
37 template<typename type_T, int name_T, int STATETYPE, int OPTIONS>
38 struct StateVar_T {
39  typedef type_T value_t;
44  enum {
46  statetype_ = STATETYPE,
48  options_ = OPTIONS,
50  name_ = name_T,
52  sizeInCorrection_ = msf_tmp::CorrectionStateLengthForType<
53  const StateVar_T<type_T, name_T>&>::value,
55  sizeInState_ = msf_tmp::StateLengthForType<const StateVar_T<type_T, name_T>&>::value
56  };
57  typedef Eigen::Matrix<double, sizeInCorrection_, sizeInCorrection_> Q_T;
58 
59  Q_T Q;
61  bool hasResetValue; //<Indicating that this statevariable has a reset value
62  // to be applied to the state on init.
64  hasResetValue = false;
65  Q.setZero();
66  }
67  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
68 };
69 
74 template<typename StateSeq_T, typename StateDef_T>
76  public:
78  typedef StateSeq_T StateSequence_T;
80  typedef StateDef_T StateDefinition_T;
81 
82  friend class msf_core::MSF_Core<
84  friend struct msf_core::copyNonPropagationStates<GenericState_T>;
85  friend class msf_core::MSF_InitMeasurement<
87 
88  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
89  enum {
90  nStateVarsAtCompileTime = boost::fusion::result_of::size < StateSequence_T
91  > ::type::value,
93  msf_tmp::CorrectionStateLengthForType>::value,
95  msf_tmp::StateLengthForType>::value,
97  msf_tmp::CoreStateLengthForType>::value,
99  msf_tmp::PropagatedCoreStateLengthForType>::value,
101  StateSequence_T, msf_tmp::PropagatedCoreErrorStateLengthForType>::value
102  };
103 
104  private:
105 
111  template<int INDEX>
112  inline typename boost::fusion::result_of::at_c<StateSequence_T, INDEX>::type
113  getStateVar();
114 
120  template<int INDEX>
121  inline typename msf_tmp::StripReference<
122  typename boost::fusion::result_of::at_c<StateSequence_T, INDEX>::type>::result_t::value_t&
123  get();
124 
125  public:
126 
127  typedef Eigen::Matrix<double, nErrorStatesAtCompileTime,
129  // covariance matrix.
130  typedef P_type F_type;
131  typedef P_type Q_type;
132 
134 
135  // system inputs
136  Eigen::Matrix<double, 3, 1> w_m;
137  Eigen::Matrix<double, 3, 1> a_m;
138 
139  double time;
143 
145  time = -1;
146  P.setZero();
147  Qd.setZero();
148  Fd.setIdentity();
149  reset();
150  }
151 
155  inline void correct(
156  const Eigen::Matrix<double, nErrorStatesAtCompileTime, 1>& correction);
157 
162  template<int INDEX>
163  inline typename msf_tmp::StripReference<
164  typename boost::fusion::result_of::at_c<StateSequence_T, INDEX>::type>::result_t::Q_T&
165  getQBlock();
166 
171  template<int INDEX>
172  inline const typename msf_tmp::StripReference<
173  typename boost::fusion::result_of::at_c<StateSequence_T, INDEX>::type>::result_t::Q_T&
174  getQBlock() const;
175 
184  void reset(
186  nullptr);
187 
191  void getPoseCovariance(
192  geometry_msgs::PoseWithCovariance::_covariance_type & cov);
193 
198  void toPoseMsg(geometry_msgs::PoseWithCovarianceStamped & pose);
199 
204  void toExtStateMsg(sensor_fusion_comm::ExtState & state);
205 
206  /***
207  * \brief Assemble a DoubleArrayStamped message from the state.
208  * \note It does not set the header.
209  */
210  void toFullStateMsg(sensor_fusion_comm::DoubleArrayStamped & state);
211 
216  void toCoreStateMsg(sensor_fusion_comm::DoubleArrayStamped & state);
217 
221  Eigen::Matrix<double, nCoreStatesAtCompileTime, 1> toEigenVector();
222 
227  std::vector<std::tuple<int, int, int> >& vec);
228 
232  std::string print();
233 
237  bool checkStateForNumeric();
238 
243  template<int INDEX>
244  inline const typename msf_tmp::StripReference<
245  typename boost::fusion::result_of::at_c<StateSequence_T, INDEX>::type>::result_t::value_t&
246  get() const;
247 
252  template<int INDEX>
253  inline typename msf_tmp::AddConstReference<
254  typename boost::fusion::result_of::at_c<StateSequence_T, INDEX>::type>::result_t
255  getStateVar() const;
256 
261  template<int INDEX>
262  inline void
263  set(const typename msf_tmp::StripConstReference<
264  typename boost::fusion::result_of::at_c<StateSequence_T, INDEX>::type>::result_t::value_t& newvalue);
265 
266  /*
267  * Clears the crosscovariance entries of a given state in P
268  */
269  template<int INDEX>
270  inline void
271  clearCrossCov();
272 
273 };
274 
278 template<typename stateSequence_T, typename stateDefinition_T>
279 class sortStates {
280  public:
287  return (lhs.time_ < rhs.time_);
288  }
289 };
290 
291 }
292 
294 
295 #endif /* MSF_STATE_H_ */