Main MRPT website > C++ reference for MRPT 1.9.9
CKalmanFilterCapable.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #ifndef CKalmanFilterCapable_H
10 #define CKalmanFilterCapable_H
11 
15 #include <mrpt/math/num_jacobian.h>
16 #include <mrpt/math/utils.h>
17 #include <mrpt/math/num_jacobian.h>
19 #include <mrpt/utils/CTimeLogger.h>
24 #include <mrpt/utils/stl_containers_utils.h> // find_in_vector
25 #include <mrpt/utils/CTicTac.h>
27 #include <mrpt/utils/TEnumType.h>
29 
30 namespace mrpt
31 {
32 namespace bayes
33 {
34 /** The Kalman Filter algorithm to employ in bayes::CKalmanFilterCapable
35  * For further details on each algorithm see the tutorial:
36  * http://www.mrpt.org/Kalman_Filters
37  * \sa bayes::CKalmanFilterCapable::KF_options
38  * \ingroup mrpt_bayes_grp
39  */
41 {
46 };
47 
48 // Forward declaration:
49 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t FEAT_SIZE, size_t ACT_SIZE,
50  typename KFTYPE>
52 
53 /** Generic options for the Kalman Filter algorithm in itself.
54  * \ingroup mrpt_bayes_grp
55  */
57 {
58  TKF_options(mrpt::utils::VerbosityLevel& verb_level_ref)
59  : method(kfEKFNaive),
60  verbosity_level(verb_level_ref),
61  IKF_iterations(5),
62  enable_profiler(false),
67  {
68  }
69 
71  const mrpt::utils::CConfigFileBase& iniFile,
72  const std::string& section) override
73  {
74  method = iniFile.read_enum<TKFMethod>(section, "method", method);
75  verbosity_level = iniFile.read_enum<mrpt::utils::VerbosityLevel>(
76  section, "verbosity_level", verbosity_level);
77  MRPT_LOAD_CONFIG_VAR(IKF_iterations, int, iniFile, section);
78  MRPT_LOAD_CONFIG_VAR(enable_profiler, bool, iniFile, section);
80  use_analytic_transition_jacobian, bool, iniFile, section);
82  use_analytic_observation_jacobian, bool, iniFile, section);
84  debug_verify_analytic_jacobians, bool, iniFile, section);
87  section);
88  }
89 
90  /** This method must display clearly all the contents of the structure in
91  * textual form, sending it to a CStream. */
92  void dumpToTextStream(mrpt::utils::CStream& out) const override
93  {
94  out.printf("\n----------- [TKF_options] ------------ \n\n");
95  out.printf(
96  "method = %s\n",
98  out.printf(
99  "verbosity_level = %s\n",
102  .c_str());
103  out.printf(
104  "IKF_iterations = %i\n", IKF_iterations);
105  out.printf(
106  "enable_profiler = %c\n",
107  enable_profiler ? 'Y' : 'N');
108  out.printf("\n");
109  }
110 
111  /** The method to employ (default: kfEKFNaive) */
113  mrpt::utils::VerbosityLevel& verbosity_level;
114  /** Number of refinement iterations, only for the IKF method. */
116  /** If enabled (default=false), detailed timing information will be dumped
117  * to the console thru a CTimerLog at the end of the execution. */
119  /** (default=true) If true, OnTransitionJacobian will be called; otherwise,
120  * the Jacobian will be estimated from a numeric approximation by calling
121  * several times to OnTransitionModel. */
123  /** (default=true) If true, OnObservationJacobians will be called;
124  * otherwise, the Jacobian will be estimated from a numeric approximation by
125  * calling several times to OnObservationModel. */
127  /** (default=false) If true, will compute all the Jacobians numerically and
128  * compare them to the analytical ones, throwing an exception on mismatch.
129  */
131  /** (default-1e-2) Sets the threshold for the difference between the
132  * analytic and the numerical jacobians */
134 };
135 
136 /** Auxiliary functions, for internal usage of MRPT classes */
137 namespace detail
138 {
139 // Auxiliary functions.
140 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t FEAT_SIZE, size_t ACT_SIZE,
141  typename KFTYPE>
142 inline size_t getNumberOfLandmarksInMap(
144  obj);
145 // Specialization:
146 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t ACT_SIZE, typename KFTYPE>
147 inline size_t getNumberOfLandmarksInMap(
148  const CKalmanFilterCapable<VEH_SIZE, OBS_SIZE, 0 /*FEAT_SIZE*/, ACT_SIZE,
149  KFTYPE>& obj);
150 
151 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t FEAT_SIZE, size_t ACT_SIZE,
152  typename KFTYPE>
153 inline bool isMapEmpty(
155  obj);
156 // Specialization:
157 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t ACT_SIZE, typename KFTYPE>
158 inline bool isMapEmpty(
159  const CKalmanFilterCapable<VEH_SIZE, OBS_SIZE, 0 /*FEAT_SIZE*/, ACT_SIZE,
160  KFTYPE>& obj);
161 
162 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t FEAT_SIZE, size_t ACT_SIZE,
163  typename KFTYPE>
164 void addNewLandmarks(
166  const typename CKalmanFilterCapable<VEH_SIZE, OBS_SIZE, FEAT_SIZE, ACT_SIZE,
167  KFTYPE>::vector_KFArray_OBS& Z,
168  const vector_int& data_association,
169  const typename CKalmanFilterCapable<VEH_SIZE, OBS_SIZE, FEAT_SIZE, ACT_SIZE,
170  KFTYPE>::KFMatrix_OxO& R);
171 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t ACT_SIZE, typename KFTYPE>
172 void addNewLandmarks(
173  CKalmanFilterCapable<VEH_SIZE, OBS_SIZE, 0 /* FEAT_SIZE=0 */, ACT_SIZE,
174  KFTYPE>& obj,
175  const typename CKalmanFilterCapable<VEH_SIZE, OBS_SIZE, 0 /* FEAT_SIZE=0 */,
176  ACT_SIZE, KFTYPE>::vector_KFArray_OBS&
177  Z,
178  const vector_int& data_association,
179  const typename CKalmanFilterCapable<VEH_SIZE, OBS_SIZE, 0 /* FEAT_SIZE=0 */,
180  ACT_SIZE, KFTYPE>::KFMatrix_OxO& R);
181 }
182 
183 /** Virtual base for Kalman Filter (EKF,IEKF,UKF) implementations.
184  * This base class stores the state vector and covariance matrix of the
185  *system. It has virtual methods that must be completed
186  * by derived classes to address a given filtering problem. The main entry
187  *point of the algorithm is CKalmanFilterCapable::runOneKalmanIteration, which
188  * should be called AFTER setting the desired filter options in KF_options,
189  *as well as any options in the derived class.
190  * Note that the main entry point is protected, so derived classes must offer
191  *another method more specific to a given problem which, internally, calls
192  *runOneKalmanIteration.
193  *
194  * For further details and examples, check out the tutorial:
195  *http://www.mrpt.org/Kalman_Filters
196  *
197  * The Kalman filter algorithms are generic, but this implementation is biased
198  *to ease the implementation
199  * of SLAM-like problems. However, it can be also applied to many generic
200  *problems not related to robotics or SLAM.
201  *
202  * The meaning of the template parameters is:
203  * - VEH_SIZE: The dimension of the "vehicle state": either the full state
204  *vector or the "vehicle" part if applicable.
205  * - OBS_SIZE: The dimension of each observation (eg, 2 for pixel coordinates,
206  *3 for 3D coordinates,etc).
207  * - FEAT_SIZE: The dimension of the features in the system state (the "map"),
208  *or 0 if not applicable (the default if not implemented).
209  * - ACT_SIZE: The dimension of each "action" u_k (or 0 if not applicable).
210  * - KFTYPE: The numeric type of the matrices (default: double)
211  *
212  * Revisions:
213  * - 2007: Antonio J. Ortiz de Galisteo (AJOGD)
214  * - 2008/FEB: All KF classes corrected, reorganized, and rewritten (JLBC).
215  * - 2008/MAR: Implemented IKF (JLBC).
216  * - 2009/DEC: Totally rewritten as a generic template using fixed-size
217  *matrices where possible (JLBC).
218  *
219  * \sa mrpt::slam::CRangeBearingKFSLAM, mrpt::slam::CRangeBearingKFSLAM2D
220  * \ingroup mrpt_bayes_grp
221  */
222 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t FEAT_SIZE, size_t ACT_SIZE,
223  typename KFTYPE = double>
224 class CKalmanFilterCapable : public mrpt::utils::COutputLogger
225 {
226  public:
227  static inline size_t get_vehicle_size() { return VEH_SIZE; }
228  static inline size_t get_observation_size() { return OBS_SIZE; }
229  static inline size_t get_feature_size() { return FEAT_SIZE; }
230  static inline size_t get_action_size() { return ACT_SIZE; }
231  inline size_t getNumberOfLandmarksInTheMap() const
232  {
233  return detail::getNumberOfLandmarksInMap(*this);
234  }
235  inline bool isMapEmpty() const { return detail::isMapEmpty(*this); }
236  /** The numeric type used in the Kalman Filter (default=double) */
237  typedef KFTYPE kftype;
238  /** My class, in a shorter name! */
239  typedef CKalmanFilterCapable<VEH_SIZE, OBS_SIZE, FEAT_SIZE, ACT_SIZE,
240  KFTYPE>
242 
243  // ---------- Many useful typedefs to short the notation a bit... --------
244  typedef Eigen::Matrix<KFTYPE, Eigen::Dynamic, 1> KFVector;
246 
255 
260 
265 
270 
277 
278  inline size_t getStateVectorLength() const { return m_xkk.size(); }
279  inline KFVector& internal_getXkk() { return m_xkk; }
280  inline KFMatrix& internal_getPkk() { return m_pkk; }
281  /** Returns the mean of the estimated value of the idx'th landmark (not
282  * applicable to non-SLAM problems).
283  * \exception std::exception On idx>= getNumberOfLandmarksInTheMap()
284  */
285  inline void getLandmarkMean(size_t idx, KFArray_FEAT& feat) const
286  {
288  ::memcpy(
289  &feat[0], &m_xkk[VEH_SIZE + idx * FEAT_SIZE],
290  FEAT_SIZE * sizeof(m_xkk[0]));
291  }
292  /** Returns the covariance of the idx'th landmark (not applicable to
293  * non-SLAM problems).
294  * \exception std::exception On idx>= getNumberOfLandmarksInTheMap()
295  */
296  inline void getLandmarkCov(size_t idx, KFMatrix_FxF& feat_cov) const
297  {
298  m_pkk.extractMatrix(
299  VEH_SIZE + idx * FEAT_SIZE, VEH_SIZE + idx * FEAT_SIZE, feat_cov);
300  }
301 
302  protected:
303  /** @name Kalman filter state
304  @{ */
305 
306  /** The system state vector. */
308  /** The system full covariance matrix. */
310 
311  /** @} */
312 
314 
315  /** @name Virtual methods for Kalman Filter implementation
316  @{
317  */
318 
319  /** Must return the action vector u.
320  * \param out_u The action vector which will be passed to OnTransitionModel
321  */
322  virtual void OnGetAction(KFArray_ACT& out_u) const = 0;
323 
324  /** Implements the transition model \f$ \hat{x}_{k|k-1} = f(
325  * \hat{x}_{k-1|k-1}, u_k ) \f$
326  * \param in_u The vector returned by OnGetAction.
327  * \param inout_x At input has \f[ \hat{x}_{k-1|k-1} \f] , at output must
328  * have \f$ \hat{x}_{k|k-1} \f$ .
329  * \param out_skip Set this to true if for some reason you want to skip the
330  * prediction step (to do not modify either the vector or the covariance).
331  * Default:false
332  * \note Even if you return "out_skip=true", the value of "inout_x" MUST be
333  * updated as usual (this is to allow numeric approximation of Jacobians).
334  */
335  virtual void OnTransitionModel(
336  const KFArray_ACT& in_u, KFArray_VEH& inout_x,
337  bool& out_skipPrediction) const = 0;
338 
339  /** Implements the transition Jacobian \f$ \frac{\partial f}{\partial x} \f$
340  * \param out_F Must return the Jacobian.
341  * The returned matrix must be \f$V \times V\f$ with V being either the
342  * size of the whole state vector (for non-SLAM problems) or VEH_SIZE (for
343  * SLAM problems).
344  */
345  virtual void OnTransitionJacobian(KFMatrix_VxV& out_F) const
346  {
347  MRPT_UNUSED_PARAM(out_F);
349  }
350 
351  /** Only called if using a numeric approximation of the transition Jacobian,
352  * this method must return the increments in each dimension of the vehicle
353  * state vector while estimating the Jacobian.
354  */
356  KFArray_VEH& out_increments) const
357  {
358  for (size_t i = 0; i < VEH_SIZE; i++) out_increments[i] = 1e-6;
359  }
360 
361  /** Implements the transition noise covariance \f$ Q_k \f$
362  * \param out_Q Must return the covariance matrix.
363  * The returned matrix must be of the same size than the jacobian from
364  * OnTransitionJacobian
365  */
366  virtual void OnTransitionNoise(KFMatrix_VxV& out_Q) const = 0;
367 
368  /** This will be called before OnGetObservationsAndDataAssociation to allow
369  * the application to reduce the number of covariance landmark predictions
370  * to be made.
371  * For example, features which are known to be "out of sight" shouldn't be
372  * added to the output list to speed up the calculations.
373  * \param in_all_prediction_means The mean of each landmark predictions;
374  * the computation or not of the corresponding covariances is what we're
375  * trying to determined with this method.
376  * \param out_LM_indices_to_predict The list of landmark indices in the map
377  * [0,getNumberOfLandmarksInTheMap()-1] that should be predicted.
378  * \note This is not a pure virtual method, so it should be implemented
379  * only if desired. The default implementation returns a vector with all the
380  * landmarks in the map.
381  * \sa OnGetObservations, OnDataAssociation
382  */
384  const vector_KFArray_OBS& in_all_prediction_means,
385  mrpt::vector_size_t& out_LM_indices_to_predict) const
386  {
387  MRPT_UNUSED_PARAM(in_all_prediction_means);
388  // Default: all of them:
389  const size_t N = this->getNumberOfLandmarksInTheMap();
390  out_LM_indices_to_predict.resize(N);
391  for (size_t i = 0; i < N; i++) out_LM_indices_to_predict[i] = i;
392  }
393 
394  /** Return the observation NOISE covariance matrix, that is, the model of
395  * the Gaussian additive noise of the sensor.
396  * \param out_R The noise covariance matrix. It might be non diagonal, but
397  * it'll usually be.
398  * \note Upon call, it can be assumed that the previous contents of out_R
399  * are all zeros.
400  */
401  virtual void OnGetObservationNoise(KFMatrix_OxO& out_R) const = 0;
402 
403  /** This is called between the KF prediction step and the update step, and
404  * the application must return the observations and, when applicable, the
405  * data association between these observations and the current map.
406  *
407  * \param out_z N vectors, each for one "observation" of length OBS_SIZE, N
408  * being the number of "observations": how many observed landmarks for a
409  * map, or just one if not applicable.
410  * \param out_data_association An empty vector or, where applicable, a
411  * vector where the i'th element corresponds to the position of the
412  * observation in the i'th row of out_z within the system state vector (in
413  * the range [0,getNumberOfLandmarksInTheMap()-1]), or -1 if it is a new map
414  * element and we want to insert it at the end of this KF iteration.
415  * \param in_all_predictions A vector with the prediction of ALL the
416  * landmarks in the map. Note that, in contrast, in_S only comprises a
417  * subset of all the landmarks.
418  * \param in_S The full covariance matrix of the observation predictions
419  * (i.e. the "innovation covariance matrix"). This is a M*O x M*O matrix
420  * with M=length of "in_lm_indices_in_S".
421  * \param in_lm_indices_in_S The indices of the map landmarks (range
422  * [0,getNumberOfLandmarksInTheMap()-1]) that can be found in the matrix
423  * in_S.
424  *
425  * This method will be called just once for each complete KF iteration.
426  * \note It is assumed that the observations are independent, i.e. there
427  * are NO cross-covariances between them.
428  */
430  vector_KFArray_OBS& out_z, mrpt::vector_int& out_data_association,
431  const vector_KFArray_OBS& in_all_predictions, const KFMatrix& in_S,
432  const vector_size_t& in_lm_indices_in_S, const KFMatrix_OxO& in_R) = 0;
433 
434  /** Implements the observation prediction \f$ h_i(x) \f$.
435  * \param idx_landmark_to_predict The indices of the landmarks in the map
436  * whose predictions are expected as output. For non SLAM-like problems,
437  * this input value is undefined and the application should just generate
438  * one observation for the given problem.
439  * \param out_predictions The predicted observations.
440  */
441  virtual void OnObservationModel(
442  const mrpt::vector_size_t& idx_landmarks_to_predict,
443  vector_KFArray_OBS& out_predictions) const = 0;
444 
445  /** Implements the observation Jacobians \f$ \frac{\partial h_i}{\partial x}
446  * \f$ and (when applicable) \f$ \frac{\partial h_i}{\partial y_i} \f$.
447  * \param idx_landmark_to_predict The index of the landmark in the map
448  * whose prediction is expected as output. For non SLAM-like problems, this
449  * will be zero and the expected output is for the whole state vector.
450  * \param Hx The output Jacobian \f$ \frac{\partial h_i}{\partial x} \f$.
451  * \param Hy The output Jacobian \f$ \frac{\partial h_i}{\partial y_i}
452  * \f$.
453  */
455  const size_t& idx_landmark_to_predict, KFMatrix_OxV& Hx,
456  KFMatrix_OxF& Hy) const
457  {
458  MRPT_UNUSED_PARAM(idx_landmark_to_predict);
459  MRPT_UNUSED_PARAM(Hx);
460  MRPT_UNUSED_PARAM(Hy);
462  }
463 
464  /** Only called if using a numeric approximation of the observation
465  * Jacobians, this method must return the increments in each dimension of
466  * the vehicle state vector while estimating the Jacobian.
467  */
469  KFArray_VEH& out_veh_increments,
470  KFArray_FEAT& out_feat_increments) const
471  {
472  for (size_t i = 0; i < VEH_SIZE; i++) out_veh_increments[i] = 1e-6;
473  for (size_t i = 0; i < FEAT_SIZE; i++) out_feat_increments[i] = 1e-6;
474  }
475 
476  /** Computes A=A-B, which may need to be re-implemented depending on the
477  * topology of the individual scalar components (eg, angles).
478  */
480  KFArray_OBS& A, const KFArray_OBS& B) const
481  {
482  A -= B;
483  }
484 
485  public:
486  /** If applicable to the given problem, this method implements the inverse
487  * observation model needed to extend the "map" with a new "element".
488  * \param in_z The observation vector whose inverse sensor model is to be
489  * computed. This is actually one of the vector<> returned by
490  * OnGetObservationsAndDataAssociation().
491  * \param out_yn The F-length vector with the inverse observation model \f$
492  * y_n=y(x,z_n) \f$.
493  * \param out_dyn_dxv The \f$F \times V\f$ Jacobian of the inv. sensor
494  * model wrt the robot pose \f$ \frac{\partial y_n}{\partial x_v} \f$.
495  * \param out_dyn_dhn The \f$F \times O\f$ Jacobian of the inv. sensor
496  * model wrt the observation vector \f$ \frac{\partial y_n}{\partial h_n}
497  * \f$.
498  *
499  * - O: OBS_SIZE
500  * - V: VEH_SIZE
501  * - F: FEAT_SIZE
502  *
503  * \note OnNewLandmarkAddedToMap will be also called after calling this
504  * method if a landmark is actually being added to the map.
505  * \deprecated This version of the method is deprecated. The alternative
506  * method is preferred to allow a greater flexibility.
507  */
509  const KFArray_OBS& in_z, KFArray_FEAT& out_yn,
510  KFMatrix_FxV& out_dyn_dxv, KFMatrix_FxO& out_dyn_dhn) const
511  {
512  MRPT_UNUSED_PARAM(in_z);
513  MRPT_UNUSED_PARAM(out_yn);
514  MRPT_UNUSED_PARAM(out_dyn_dxv);
515  MRPT_UNUSED_PARAM(out_dyn_dhn);
516  MRPT_START
518  "Inverse sensor model required but not implemented in derived "
519  "class.")
520  MRPT_END
521  }
522 
523  /** If applicable to the given problem, this method implements the inverse
524  * observation model needed to extend the "map" with a new "element".
525  * The uncertainty in the new map feature comes from two parts: one from
526  * the vehicle uncertainty (through the out_dyn_dxv Jacobian),
527  * and another from the uncertainty in the observation itself. By
528  * default, out_use_dyn_dhn_jacobian=true on call, and if it's left at
529  * "true",
530  * the base KalmanFilter class will compute the uncertainty of the
531  * landmark relative position from out_dyn_dhn.
532  * Only in some problems (e.g. MonoSLAM), it'll be needed for the
533  * application to directly return the covariance matrix \a
534  * out_dyn_dhn_R_dyn_dhnT, which is the equivalent to:
535  *
536  * \f$ \frac{\partial y_n}{\partial h_n} R \frac{\partial
537  * y_n}{\partial h_n}^\top \f$.
538  *
539  * but may be computed from additional terms, or whatever needed by the
540  * user.
541  *
542  * \param in_z The observation vector whose inverse sensor model is to be
543  * computed. This is actually one of the vector<> returned by
544  * OnGetObservationsAndDataAssociation().
545  * \param out_yn The F-length vector with the inverse observation model \f$
546  * y_n=y(x,z_n) \f$.
547  * \param out_dyn_dxv The \f$F \times V\f$ Jacobian of the inv. sensor
548  * model wrt the robot pose \f$ \frac{\partial y_n}{\partial x_v} \f$.
549  * \param out_dyn_dhn The \f$F \times O\f$ Jacobian of the inv. sensor
550  * model wrt the observation vector \f$ \frac{\partial y_n}{\partial h_n}
551  * \f$.
552  * \param out_dyn_dhn_R_dyn_dhnT See the discussion above.
553  *
554  * - O: OBS_SIZE
555  * - V: VEH_SIZE
556  * - F: FEAT_SIZE
557  *
558  * \note OnNewLandmarkAddedToMap will be also called after calling this
559  * method if a landmark is actually being added to the map.
560  */
562  const KFArray_OBS& in_z, KFArray_FEAT& out_yn,
563  KFMatrix_FxV& out_dyn_dxv, KFMatrix_FxO& out_dyn_dhn,
564  KFMatrix_FxF& out_dyn_dhn_R_dyn_dhnT,
565  bool& out_use_dyn_dhn_jacobian) const
566  {
567  MRPT_UNUSED_PARAM(out_dyn_dhn_R_dyn_dhnT);
568  MRPT_START
569  OnInverseObservationModel(in_z, out_yn, out_dyn_dxv, out_dyn_dhn);
570  out_use_dyn_dhn_jacobian = true;
571  MRPT_END
572  }
573 
574  /** If applicable to the given problem, do here any special handling of
575  * adding a new landmark to the map.
576  * \param in_obsIndex The index of the observation whose inverse sensor is
577  * to be computed. It corresponds to the row in in_z where the observation
578  * can be found.
579  * \param in_idxNewFeat The index that this new feature will have in the
580  * state vector (0:just after the vehicle state, 1: after that,...). Save
581  * this number so data association can be done according to these indices.
582  * \sa OnInverseObservationModel
583  */
585  const size_t in_obsIdx, const size_t in_idxNewFeat)
586  {
587  MRPT_UNUSED_PARAM(in_obsIdx);
588  MRPT_UNUSED_PARAM(in_idxNewFeat);
589  // Do nothing in this base class.
590  }
591 
592  /** This method is called after the prediction and after the update, to give
593  * the user an opportunity to normalize the state vector (eg, keep angles
594  * within -pi,pi range) if the application requires it.
595  */
596  virtual void OnNormalizeStateVector()
597  {
598  // Do nothing in this base class.
599  }
600 
601  /** This method is called after finishing one KF iteration and before
602  * returning from runOneKalmanIteration().
603  */
604  virtual void OnPostIteration()
605  {
606  // Do nothing in this base class.
607  }
608 
609  /** @}
610  */
611 
612  public:
614  : mrpt::utils::COutputLogger("CKalmanFilterCapable"),
615  KF_options(this->m_min_verbosity_level),
617  /** Default constructor */ {}
618  /** Destructor */
621  /** Generic options for the Kalman Filter algorithm itself. */
623 
624  private:
625  // "Local" variables to runOneKalmanIteration, declared here to avoid
626  // allocating them over and over again with each call.
627  // (The variables that go into the stack remains in the function body)
630  /** The vector of all partial Jacobians dh[i]_dx for each prediction */
632  /** The vector of all partial Jacobians dh[i]_dy[i] for each prediction */
636  vector_KFArray_OBS Z; // Each entry is one observation:
637  KFMatrix K; // Kalman gain
638  KFMatrix S_1; // Inverse of S
641 
642  protected:
643  /** The main entry point, executes one complete step: prediction + update.
644  * It is protected since derived classes must provide a problem-specific
645  * entry point for users.
646  * The exact order in which this method calls the virtual method is
647  * explained in http://www.mrpt.org/Kalman_Filters
648  */
649  void runOneKalmanIteration();
650 
651  private:
652  mutable bool m_user_didnt_implement_jacobian;
653 
654  /** Auxiliary functions for Jacobian numeric estimation */
655  static void KF_aux_estimate_trans_jacobian(
656  const KFArray_VEH& x, const std::pair<KFCLASS*, KFArray_ACT>& dat,
657  KFArray_VEH& out_x);
659  const KFArray_VEH& x, const std::pair<KFCLASS*, size_t>& dat,
660  KFArray_OBS& out_x);
662  const KFArray_FEAT& x, const std::pair<KFCLASS*, size_t>& dat,
663  KFArray_OBS& out_x);
664 
665  template <size_t VEH_SIZEb, size_t OBS_SIZEb, size_t FEAT_SIZEb,
666  size_t ACT_SIZEb, typename KFTYPEb>
667  friend void detail::addNewLandmarks(
668  CKalmanFilterCapable<VEH_SIZEb, OBS_SIZEb, FEAT_SIZEb, ACT_SIZEb,
669  KFTYPEb>& obj,
670  const typename CKalmanFilterCapable<VEH_SIZEb, OBS_SIZEb, FEAT_SIZEb,
671  ACT_SIZEb,
672  KFTYPEb>::vector_KFArray_OBS& Z,
673  const vector_int& data_association,
674  const typename CKalmanFilterCapable<VEH_SIZEb, OBS_SIZEb, FEAT_SIZEb,
675  ACT_SIZEb, KFTYPEb>::KFMatrix_OxO&
676  R);
677 }; // end class
678 
679 } // end namespace
680 
681 // Specializations MUST occur at the same namespace:
682 namespace utils
683 {
684 template <>
686 {
688  static void fill(bimap<enum_t, std::string>& m_map)
689  {
690  m_map.insert(bayes::kfEKFNaive, "kfEKFNaive");
691  m_map.insert(bayes::kfEKFAlaDavison, "kfEKFAlaDavison");
692  m_map.insert(bayes::kfIKFFull, "kfIKFFull");
693  m_map.insert(bayes::kfIKF, "kfIKF");
694  }
695 };
696 } // End of namespace
697 } // end namespace
698 
699 // Template implementation:
701 
702 #endif
mrpt::utils::CTimeLogger & getProfiler()
bool use_analytic_observation_jacobian
(default=true) If true, OnObservationJacobians will be called; otherwise, the Jacobian will be estima...
Eigen::Matrix< KFTYPE, Eigen::Dynamic, 1 > KFVector
virtual void OnTransitionNoise(KFMatrix_VxV &out_Q) const =0
Implements the transition noise covariance .
virtual void OnNormalizeStateVector()
This method is called after the prediction and after the update, to give the user an opportunity to n...
static void fill(bimap< enum_t, std::string > &m_map)
virtual void OnObservationJacobians(const size_t &idx_landmark_to_predict, KFMatrix_OxV &Hx, KFMatrix_OxF &Hy) const
Implements the observation Jacobians and (when applicable) .
virtual void OnInverseObservationModel(const KFArray_OBS &in_z, KFArray_FEAT &out_yn, KFMatrix_FxV &out_dyn_dxv, KFMatrix_FxO &out_dyn_dhn, KFMatrix_FxF &out_dyn_dhn_R_dyn_dhnT, bool &out_use_dyn_dhn_jacobian) const
If applicable to the given problem, this method implements the inverse observation model needed to ex...
mrpt::math::CMatrixFixedNumeric< KFTYPE, FEAT_SIZE, VEH_SIZE > KFMatrix_FxV
bool enable_profiler
If enabled (default=false), detailed timing information will be dumped to the console thru a CTimerLo...
mrpt::aligned_containers< KFMatrix_OxV >::vector_t Hxs
The vector of all partial Jacobians dh[i]_dx for each prediction.
#define THROW_EXCEPTION(msg)
void dumpToTextStream(mrpt::utils::CStream &out) const override
This method must display clearly all the contents of the structure in textual form, sending it to a CStream.
static void KF_aux_estimate_obs_Hx_jacobian(const KFArray_VEH &x, const std::pair< KFCLASS *, size_t > &dat, KFArray_OBS &out_x)
CArrayNumeric is an array for numeric types supporting several mathematical operations (actually...
Definition: CArrayNumeric.h:26
virtual void OnGetObservationNoise(KFMatrix_OxO &out_R) const =0
Return the observation NOISE covariance matrix, that is, the model of the Gaussian additive noise of ...
double debug_verify_analytic_jacobians_threshold
(default-1e-2) Sets the threshold for the difference between the analytic and the numerical jacobians...
void getLandmarkCov(size_t idx, KFMatrix_FxF &feat_cov) const
Returns the covariance of the idx&#39;th landmark (not applicable to non-SLAM problems).
void runOneKalmanIteration()
The main entry point, executes one complete step: prediction + update.
Only specializations of this class are defined for each enum type of interest.
Definition: TEnumType.h:24
virtual void OnTransitionJacobian(KFMatrix_VxV &out_F) const
Implements the transition Jacobian .
mrpt::math::CArrayNumeric< KFTYPE, VEH_SIZE > KFArray_VEH
KFMatrix m_pkk
The system full covariance matrix.
virtual void OnTransitionModel(const KFArray_ACT &in_u, KFArray_VEH &inout_x, bool &out_skipPrediction) const =0
Implements the transition model .
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
virtual void OnObservationJacobiansNumericGetIncrements(KFArray_VEH &out_veh_increments, KFArray_FEAT &out_feat_increments) const
Only called if using a numeric approximation of the observation Jacobians, this method must return th...
This class allows loading and storing values and vectors of different types from a configuration text...
mrpt::math::CMatrixFixedNumeric< KFTYPE, OBS_SIZE, OBS_SIZE > KFMatrix_OxO
virtual void OnSubstractObservationVectors(KFArray_OBS &A, const KFArray_OBS &B) const
Computes A=A-B, which may need to be re-implemented depending on the topology of the individual scala...
mrpt::utils::CTimeLogger m_timLogger
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
A numeric matrix of compile-time fixed size.
CKalmanFilterCapable()
Default constructor.
#define MRPT_END
TKF_options KF_options
Generic options for the Kalman Filter algorithm itself.
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
A helper class that can convert an enum value into its textual representation, and viceversa...
Definition: TEnumType.h:38
void getLandmarkMean(size_t idx, KFArray_FEAT &feat) const
Returns the mean of the estimated value of the idx&#39;th landmark (not applicable to non-SLAM problems)...
TKF_options(mrpt::utils::VerbosityLevel &verb_level_ref)
mrpt::aligned_containers< KFArray_OBS >::vector_t vector_KFArray_OBS
Virtual base for Kalman Filter (EKF,IEKF,UKF) implementations.
size_t getNumberOfLandmarksInMap(const CKalmanFilterCapable< VEH_SIZE, OBS_SIZE, FEAT_SIZE, ACT_SIZE, KFTYPE > &obj)
A bidirectional version of std::map, declared as bimap<KEY,VALUE> and which actually contains two std...
Definition: bimap.h:34
static void KF_aux_estimate_trans_jacobian(const KFArray_VEH &x, const std::pair< KFCLASS *, KFArray_ACT > &dat, KFArray_VEH &out_x)
Auxiliary functions for Jacobian numeric estimation.
virtual ~CKalmanFilterCapable()
Destructor.
virtual void OnNewLandmarkAddedToMap(const size_t in_obsIdx, const size_t in_idxNewFeat)
If applicable to the given problem, do here any special handling of adding a new landmark to the map...
mrpt::math::CArrayNumeric< KFTYPE, ACT_SIZE > KFArray_ACT
void addNewLandmarks(CKalmanFilterCapable< VEH_SIZE, OBS_SIZE, FEAT_SIZE, ACT_SIZE, KFTYPE > &obj, const typename CKalmanFilterCapable< VEH_SIZE, OBS_SIZE, FEAT_SIZE, ACT_SIZE, KFTYPE >::vector_KFArray_OBS &Z, const vector_int &data_association, const typename CKalmanFilterCapable< VEH_SIZE, OBS_SIZE, FEAT_SIZE, ACT_SIZE, KFTYPE >::KFMatrix_OxO &R)
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::math::CMatrixFixedNumeric< KFTYPE, OBS_SIZE, FEAT_SIZE > KFMatrix_OxF
mrpt::utils::VerbosityLevel & verbosity_level
ENUMTYPE read_enum(const std::string &section, const std::string &name, const ENUMTYPE &defaultValue, bool failIfNotFound=false) const
Reads an "enum" value, where the value in the config file can be either a numerical value or the symb...
virtual void OnTransitionJacobianNumericGetIncrements(KFArray_VEH &out_increments) const
Only called if using a numeric approximation of the transition Jacobian, this method must return the ...
Generic options for the Kalman Filter algorithm in itself.
mrpt::aligned_containers< KFMatrix_OxF >::vector_t Hys
The vector of all partial Jacobians dh[i]_dy[i] for each prediction.
KFTYPE kftype
The numeric type used in the Kalman Filter (default=double)
#define MRPT_START
mrpt::math::CArrayNumeric< KFTYPE, OBS_SIZE > KFArray_OBS
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
mrpt::math::CMatrixTemplateNumeric< KFTYPE > KFMatrix
const float R
virtual void OnInverseObservationModel(const KFArray_OBS &in_z, KFArray_FEAT &out_yn, KFMatrix_FxV &out_dyn_dxv, KFMatrix_FxO &out_dyn_dhn) const
If applicable to the given problem, this method implements the inverse observation model needed to ex...
void loadFromConfigFile(const mrpt::utils::CConfigFileBase &iniFile, const std::string &section) override
This method load the options from a ".ini"-like file or memory-stored string list.
bool debug_verify_analytic_jacobians
(default=false) If true, will compute all the Jacobians numerically and compare them to the analytica...
bool isMapEmpty(const CKalmanFilterCapable< VEH_SIZE, OBS_SIZE, FEAT_SIZE, ACT_SIZE, KFTYPE > &obj)
TKFMethod
The Kalman Filter algorithm to employ in bayes::CKalmanFilterCapable For further details on each algo...
mrpt::math::CMatrixFixedNumeric< KFTYPE, FEAT_SIZE, OBS_SIZE > KFMatrix_FxO
#define ASSERT_(f)
A versatile "profiler" that logs the time spent within each pair of calls to enter(X)-leave(X), among other stats.
Definition: CTimeLogger.h:45
virtual void OnGetObservationsAndDataAssociation(vector_KFArray_OBS &out_z, mrpt::vector_int &out_data_association, const vector_KFArray_OBS &in_all_predictions, const KFMatrix &in_S, const vector_size_t &in_lm_indices_in_S, const KFMatrix_OxO &in_R)=0
This is called between the KF prediction step and the update step, and the application must return th...
mrpt::math::CMatrixFixedNumeric< KFTYPE, OBS_SIZE, VEH_SIZE > KFMatrix_OxV
std::vector< size_t > vector_size_t
Definition: types_simple.h:26
virtual void OnObservationModel(const mrpt::vector_size_t &idx_landmarks_to_predict, vector_KFArray_OBS &out_predictions) const =0
Implements the observation prediction .
std::vector< int32_t > vector_int
Definition: types_simple.h:24
mrpt::math::CMatrixFixedNumeric< KFTYPE, VEH_SIZE, VEH_SIZE > KFMatrix_VxV
TKFMethod method
The method to employ (default: kfEKFNaive)
static void KF_aux_estimate_obs_Hy_jacobian(const KFArray_FEAT &x, const std::pair< KFCLASS *, size_t > &dat, KFArray_OBS &out_x)
virtual void OnGetAction(KFArray_ACT &out_u) const =0
Must return the action vector u.
mrpt::math::CMatrixFixedNumeric< KFTYPE, ACT_SIZE, ACT_SIZE > KFMatrix_AxA
void insert(const KEY &k, const VALUE &v)
Insert a new pair KEY<->VALUE in the bi-map.
Definition: bimap.h:75
#define MRPT_LOAD_CONFIG_VAR( variableName, variableType, configFileObject, sectionNameStr)
An useful macro for loading variables stored in a INI-like file under a key with the same name that t...
GLenum GLint x
Definition: glext.h:3538
mrpt::math::CMatrixFixedNumeric< KFTYPE, FEAT_SIZE, FEAT_SIZE > KFMatrix_FxF
mrpt::math::CArrayNumeric< KFTYPE, FEAT_SIZE > KFArray_FEAT
virtual void OnPreComputingPredictions(const vector_KFArray_OBS &in_all_prediction_means, mrpt::vector_size_t &out_LM_indices_to_predict) const
This will be called before OnGetObservationsAndDataAssociation to allow the application to reduce the...
KFVector m_xkk
The system state vector.
mrpt::math::CMatrixFixedNumeric< KFTYPE, VEH_SIZE, OBS_SIZE > KFMatrix_VxO
std::vector< TYPE1, Eigen::aligned_allocator< TYPE1 > > vector_t
bool use_analytic_transition_jacobian
(default=true) If true, OnTransitionJacobian will be called; otherwise, the Jacobian will be estimate...
This is a virtual base class for sets of options than can be loaded from and/or saved to configuratio...
virtual int printf(const char *fmt,...) MRPT_printf_format_check(2
Writes a string to the stream in a textual form.
Definition: CStream.cpp:597
int IKF_iterations
Number of refinement iterations, only for the IKF method.
mrpt::math::CMatrixFixedNumeric< KFTYPE, VEH_SIZE, FEAT_SIZE > KFMatrix_VxF
virtual void OnPostIteration()
This method is called after finishing one KF iteration and before returning from runOneKalmanIteratio...
void memcpy(void *dest, size_t destSize, const void *src, size_t copyCount) noexcept
An OS and compiler independent version of "memcpy".
Definition: os.cpp:355
CKalmanFilterCapable< VEH_SIZE, OBS_SIZE, FEAT_SIZE, ACT_SIZE, KFTYPE > KFCLASS
My class, in a shorter name!
std::vector< int32_t > vector_int
std::vector< size_t > vector_size_t



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019