MRPT  1.9.9
CKalmanFilterCapable.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
14 #include <mrpt/containers/stl_containers_utils.h> // find_in_vector
16 #include <mrpt/math/CMatrixFixed.h>
17 #include <mrpt/math/CVectorFixed.h>
18 #include <mrpt/math/num_jacobian.h>
19 #include <mrpt/math/utils.h>
21 #include <mrpt/system/CTicTac.h>
25 #include <vector>
26 
27 namespace mrpt
28 {
29 namespace bayes
30 {
31 /** The Kalman Filter algorithm to employ in bayes::CKalmanFilterCapable
32  * For further details on each algorithm see the tutorial:
33  * https://www.mrpt.org/Kalman_Filters
34  * \sa bayes::CKalmanFilterCapable::KF_options
35  * \ingroup mrpt_bayes_grp
36  */
38 {
43 };
44 
45 // Forward declaration:
46 template <
47  size_t VEH_SIZE, size_t OBS_SIZE, size_t FEAT_SIZE, size_t ACT_SIZE,
48  typename KFTYPE>
50 
51 /** Generic options for the Kalman Filter algorithm in itself.
52  * \ingroup mrpt_bayes_grp
53  */
55 {
57  : verbosity_level(verb_level_ref)
58  {
59  }
60 
63  const std::string& section) override
64  {
65  method = iniFile.read_enum<TKFMethod>(section, "method", method);
67  section, "verbosity_level", verbosity_level);
78  section);
79  }
80 
81  /** This method must display clearly all the contents of the structure in
82  * textual form, sending it to a CStream. */
83  void dumpToTextStream(std::ostream& out) const override
84  {
85  out << mrpt::format("\n----------- [TKF_options] ------------ \n\n");
86  out << mrpt::format(
87  "method = %s\n",
89  out << mrpt::format(
90  "verbosity_level = %s\n",
93  .c_str());
94  out << mrpt::format(
95  "IKF_iterations = %i\n", IKF_iterations);
96  out << mrpt::format(
97  "enable_profiler = %c\n",
98  enable_profiler ? 'Y' : 'N');
99  out << mrpt::format("\n");
100  }
101 
102  /** The method to employ (default: kfEKFNaive) */
105  /** Number of refinement iterations, only for the IKF method. */
107  /** If enabled (default=false), detailed timing information will be dumped
108  * to the console thru a CTimerLog at the end of the execution. */
109  bool enable_profiler{false};
110  /** (default=true) If true, OnTransitionJacobian will be called; otherwise,
111  * the Jacobian will be estimated from a numeric approximation by calling
112  * several times to OnTransitionModel. */
114  /** (default=true) If true, OnObservationJacobians will be called;
115  * otherwise, the Jacobian will be estimated from a numeric approximation by
116  * calling several times to OnObservationModel. */
118  /** (default=false) If true, will compute all the Jacobians numerically and
119  * compare them to the analytical ones, throwing an exception on mismatch.
120  */
122  /** (default-1e-2) Sets the threshold for the difference between the
123  * analytic and the numerical jacobians */
125 };
126 
127 /** Auxiliary functions, for internal usage of MRPT classes */
128 namespace detail
129 {
130 // Auxiliary functions.
131 template <
132  size_t VEH_SIZE, size_t OBS_SIZE, size_t FEAT_SIZE, size_t ACT_SIZE,
133  typename KFTYPE>
134 inline size_t getNumberOfLandmarksInMap(
136  obj);
137 // Specialization:
138 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t ACT_SIZE, typename KFTYPE>
139 inline size_t getNumberOfLandmarksInMap(
140  const CKalmanFilterCapable<
141  VEH_SIZE, OBS_SIZE, 0 /*FEAT_SIZE*/, ACT_SIZE, KFTYPE>& obj);
142 
143 template <
144  size_t VEH_SIZE, size_t OBS_SIZE, size_t FEAT_SIZE, size_t ACT_SIZE,
145  typename KFTYPE>
146 inline bool isMapEmpty(
148  obj);
149 // Specialization:
150 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t ACT_SIZE, typename KFTYPE>
151 inline bool isMapEmpty(
152  const CKalmanFilterCapable<
153  VEH_SIZE, OBS_SIZE, 0 /*FEAT_SIZE*/, ACT_SIZE, KFTYPE>& obj);
154 
155 template <
156  size_t VEH_SIZE, size_t OBS_SIZE, size_t FEAT_SIZE, size_t ACT_SIZE,
157  typename KFTYPE>
158 void addNewLandmarks(
160  const typename CKalmanFilterCapable<
161  VEH_SIZE, OBS_SIZE, FEAT_SIZE, ACT_SIZE, KFTYPE>::vector_KFArray_OBS& Z,
162  const std::vector<int>& data_association,
163  const typename CKalmanFilterCapable<
164  VEH_SIZE, OBS_SIZE, FEAT_SIZE, ACT_SIZE, KFTYPE>::KFMatrix_OxO& R);
165 template <size_t VEH_SIZE, size_t OBS_SIZE, size_t ACT_SIZE, typename KFTYPE>
166 void addNewLandmarks(
168  VEH_SIZE, OBS_SIZE, 0 /* FEAT_SIZE=0 */, ACT_SIZE, KFTYPE>& obj,
169  const typename CKalmanFilterCapable<
170  VEH_SIZE, OBS_SIZE, 0 /* FEAT_SIZE=0 */, ACT_SIZE,
171  KFTYPE>::vector_KFArray_OBS& Z,
172  const std::vector<int>& data_association,
173  const typename CKalmanFilterCapable<
174  VEH_SIZE, OBS_SIZE, 0 /* FEAT_SIZE=0 */, ACT_SIZE,
175  KFTYPE>::KFMatrix_OxO& R);
176 } // namespace detail
177 
178 /** Virtual base for Kalman Filter (EKF,IEKF,UKF) implementations.
179  * This base class stores the state vector and covariance matrix of the
180  *system. It has virtual methods that must be completed
181  * by derived classes to address a given filtering problem. The main entry
182  *point of the algorithm is CKalmanFilterCapable::runOneKalmanIteration, which
183  * should be called AFTER setting the desired filter options in KF_options,
184  *as well as any options in the derived class.
185  * Note that the main entry point is protected, so derived classes must offer
186  *another method more specific to a given problem which, internally, calls
187  *runOneKalmanIteration.
188  *
189  * For further details and examples, check out the tutorial:
190  *http://www.mrpt.org/Kalman_Filters
191  *
192  * The Kalman filter algorithms are generic, but this implementation is biased
193  *to ease the implementation
194  * of SLAM-like problems. However, it can be also applied to many generic
195  *problems not related to robotics or SLAM.
196  *
197  * The meaning of the template parameters is:
198  * - VEH_SIZE: The dimension of the "vehicle state": either the full state
199  *vector or the "vehicle" part if applicable.
200  * - OBS_SIZE: The dimension of each observation (eg, 2 for pixel coordinates,
201  *3 for 3D coordinates,etc).
202  * - FEAT_SIZE: The dimension of the features in the system state (the "map"),
203  *or 0 if not applicable (the default if not implemented).
204  * - ACT_SIZE: The dimension of each "action" u_k (or 0 if not applicable).
205  * - KFTYPE: The numeric type of the matrices (default: double)
206  *
207  * Revisions:
208  * - 2007: Antonio J. Ortiz de Galisteo (AJOGD)
209  * - 2008/FEB: All KF classes corrected, reorganized, and rewritten (JLBC).
210  * - 2008/MAR: Implemented IKF (JLBC).
211  * - 2009/DEC: Totally rewritten as a generic template using fixed-size
212  *matrices where possible (JLBC).
213  *
214  * \sa mrpt::slam::CRangeBearingKFSLAM, mrpt::slam::CRangeBearingKFSLAM2D
215  * \ingroup mrpt_bayes_grp
216  */
217 template <
218  size_t VEH_SIZE, size_t OBS_SIZE, size_t FEAT_SIZE, size_t ACT_SIZE,
219  typename KFTYPE = double>
221 {
222  public:
223  static constexpr size_t get_vehicle_size() { return VEH_SIZE; }
224  static constexpr size_t get_observation_size() { return OBS_SIZE; }
225  static constexpr size_t get_feature_size() { return FEAT_SIZE; }
226  static constexpr size_t get_action_size() { return ACT_SIZE; }
227  inline size_t getNumberOfLandmarksInTheMap() const
228  {
229  return detail::getNumberOfLandmarksInMap(*this);
230  }
231  inline bool isMapEmpty() const { return detail::isMapEmpty(*this); }
232  /** The numeric type used in the Kalman Filter (default=double) */
233  using kftype = KFTYPE;
234  /** My class, in a shorter name! */
235  using KFCLASS =
237 
238  // ---------- Many useful typedefs to short the notation a bit... --------
241 
246 
253 
257  using vector_KFArray_OBS = std::vector<KFArray_OBS>;
259 
260  inline size_t getStateVectorLength() const { return m_xkk.size(); }
261  inline KFVector& internal_getXkk() { return m_xkk; }
262  inline KFMatrix& internal_getPkk() { return m_pkk; }
263  /** Returns the mean of the estimated value of the idx'th landmark (not
264  * applicable to non-SLAM problems).
265  * \exception std::exception On idx>= getNumberOfLandmarksInTheMap()
266  */
267  inline void getLandmarkMean(size_t idx, KFArray_FEAT& feat) const
268  {
270  ::memcpy(
271  &feat[0], &m_xkk[VEH_SIZE + idx * FEAT_SIZE],
272  FEAT_SIZE * sizeof(m_xkk[0]));
273  }
274  /** Returns the covariance of the idx'th landmark (not applicable to
275  * non-SLAM problems).
276  * \exception std::exception On idx>= getNumberOfLandmarksInTheMap()
277  */
278  inline void getLandmarkCov(size_t idx, KFMatrix_FxF& feat_cov) const
279  {
280  feat_cov = m_pkk.blockCopy<FEAT_SIZE, FEAT_SIZE>(
281  VEH_SIZE + idx * FEAT_SIZE, VEH_SIZE + idx * FEAT_SIZE);
282  }
283 
284  protected:
285  /** @name Kalman filter state
286  @{ */
287 
288  /** The system state vector. */
290  /** The system full covariance matrix. */
292 
293  /** @} */
294 
296 
297  /** @name Virtual methods for Kalman Filter implementation
298  @{
299  */
300 
301  /** Must return the action vector u.
302  * \param out_u The action vector which will be passed to OnTransitionModel
303  */
304  virtual void OnGetAction(KFArray_ACT& out_u) const = 0;
305 
306  /** Implements the transition model \f$ \hat{x}_{k|k-1} = f(
307  * \hat{x}_{k-1|k-1}, u_k ) \f$
308  * \param in_u The vector returned by OnGetAction.
309  * \param inout_x At input has \f[ \hat{x}_{k-1|k-1} \f] , at output must
310  * have \f$ \hat{x}_{k|k-1} \f$ .
311  * \param out_skip Set this to true if for some reason you want to skip the
312  * prediction step (to do not modify either the vector or the covariance).
313  * Default:false
314  * \note Even if you return "out_skip=true", the value of "inout_x" MUST be
315  * updated as usual (this is to allow numeric approximation of Jacobians).
316  */
317  virtual void OnTransitionModel(
318  const KFArray_ACT& in_u, KFArray_VEH& inout_x,
319  bool& out_skipPrediction) const = 0;
320 
321  /** Implements the transition Jacobian \f$ \frac{\partial f}{\partial x} \f$
322  * \param out_F Must return the Jacobian.
323  * The returned matrix must be \f$V \times V\f$ with V being either the
324  * size of the whole state vector (for non-SLAM problems) or VEH_SIZE (for
325  * SLAM problems).
326  */
327  virtual void OnTransitionJacobian(KFMatrix_VxV& out_F) const
328  {
329  MRPT_UNUSED_PARAM(out_F);
331  }
332 
333  /** Only called if using a numeric approximation of the transition Jacobian,
334  * this method must return the increments in each dimension of the vehicle
335  * state vector while estimating the Jacobian.
336  */
338  KFArray_VEH& out_increments) const
339  {
340  for (size_t i = 0; i < VEH_SIZE; i++) out_increments[i] = 1e-6;
341  }
342 
343  /** Implements the transition noise covariance \f$ Q_k \f$
344  * \param out_Q Must return the covariance matrix.
345  * The returned matrix must be of the same size than the jacobian from
346  * OnTransitionJacobian
347  */
348  virtual void OnTransitionNoise(KFMatrix_VxV& out_Q) const = 0;
349 
350  /** This will be called before OnGetObservationsAndDataAssociation to allow
351  * the application to reduce the number of covariance landmark predictions
352  * to be made.
353  * For example, features which are known to be "out of sight" shouldn't be
354  * added to the output list to speed up the calculations.
355  * \param in_all_prediction_means The mean of each landmark predictions;
356  * the computation or not of the corresponding covariances is what we're
357  * trying to determined with this method.
358  * \param out_LM_indices_to_predict The list of landmark indices in the map
359  * [0,getNumberOfLandmarksInTheMap()-1] that should be predicted.
360  * \note This is not a pure virtual method, so it should be implemented
361  * only if desired. The default implementation returns a vector with all the
362  * landmarks in the map.
363  * \sa OnGetObservations, OnDataAssociation
364  */
366  const vector_KFArray_OBS& in_all_prediction_means,
367  std::vector<size_t>& out_LM_indices_to_predict) const
368  {
369  MRPT_UNUSED_PARAM(in_all_prediction_means);
370  // Default: all of them:
371  const size_t N = this->getNumberOfLandmarksInTheMap();
372  out_LM_indices_to_predict.resize(N);
373  for (size_t i = 0; i < N; i++) out_LM_indices_to_predict[i] = i;
374  }
375 
376  /** Return the observation NOISE covariance matrix, that is, the model of
377  * the Gaussian additive noise of the sensor.
378  * \param out_R The noise covariance matrix. It might be non diagonal, but
379  * it'll usually be.
380  * \note Upon call, it can be assumed that the previous contents of out_R
381  * are all zeros.
382  */
383  virtual void OnGetObservationNoise(KFMatrix_OxO& out_R) const = 0;
384 
385  /** This is called between the KF prediction step and the update step, and
386  * the application must return the observations and, when applicable, the
387  * data association between these observations and the current map.
388  *
389  * \param out_z N vectors, each for one "observation" of length OBS_SIZE, N
390  * being the number of "observations": how many observed landmarks for a
391  * map, or just one if not applicable.
392  * \param out_data_association An empty vector or, where applicable, a
393  * vector where the i'th element corresponds to the position of the
394  * observation in the i'th row of out_z within the system state vector (in
395  * the range [0,getNumberOfLandmarksInTheMap()-1]), or -1 if it is a new map
396  * element and we want to insert it at the end of this KF iteration.
397  * \param in_all_predictions A vector with the prediction of ALL the
398  * landmarks in the map. Note that, in contrast, in_S only comprises a
399  * subset of all the landmarks.
400  * \param in_S The full covariance matrix of the observation predictions
401  * (i.e. the "innovation covariance matrix"). This is a M*O x M*O matrix
402  * with M=length of "in_lm_indices_in_S".
403  * \param in_lm_indices_in_S The indices of the map landmarks (range
404  * [0,getNumberOfLandmarksInTheMap()-1]) that can be found in the matrix
405  * in_S.
406  *
407  * This method will be called just once for each complete KF iteration.
408  * \note It is assumed that the observations are independent, i.e. there
409  * are NO cross-covariances between them.
410  */
412  vector_KFArray_OBS& out_z, std::vector<int>& out_data_association,
413  const vector_KFArray_OBS& in_all_predictions, const KFMatrix& in_S,
414  const std::vector<size_t>& in_lm_indices_in_S,
415  const KFMatrix_OxO& in_R) = 0;
416 
417  /** Implements the observation prediction \f$ h_i(x) \f$.
418  * \param idx_landmark_to_predict The indices of the landmarks in the map
419  * whose predictions are expected as output. For non SLAM-like problems,
420  * this input value is undefined and the application should just generate
421  * one observation for the given problem.
422  * \param out_predictions The predicted observations.
423  */
424  virtual void OnObservationModel(
425  const std::vector<size_t>& idx_landmarks_to_predict,
426  vector_KFArray_OBS& out_predictions) const = 0;
427 
428  /** Implements the observation Jacobians \f$ \frac{\partial h_i}{\partial x}
429  * \f$ and (when applicable) \f$ \frac{\partial h_i}{\partial y_i} \f$.
430  * \param idx_landmark_to_predict The index of the landmark in the map
431  * whose prediction is expected as output. For non SLAM-like problems, this
432  * will be zero and the expected output is for the whole state vector.
433  * \param Hx The output Jacobian \f$ \frac{\partial h_i}{\partial x} \f$.
434  * \param Hy The output Jacobian \f$ \frac{\partial h_i}{\partial y_i}
435  * \f$.
436  */
438  const size_t& idx_landmark_to_predict, KFMatrix_OxV& Hx,
439  KFMatrix_OxF& Hy) const
440  {
441  MRPT_UNUSED_PARAM(idx_landmark_to_predict);
442  MRPT_UNUSED_PARAM(Hx);
443  MRPT_UNUSED_PARAM(Hy);
445  }
446 
447  /** Only called if using a numeric approximation of the observation
448  * Jacobians, this method must return the increments in each dimension of
449  * the vehicle state vector while estimating the Jacobian.
450  */
452  KFArray_VEH& out_veh_increments,
453  KFArray_FEAT& out_feat_increments) const
454  {
455  for (size_t i = 0; i < VEH_SIZE; i++) out_veh_increments[i] = 1e-6;
456  for (size_t i = 0; i < FEAT_SIZE; i++) out_feat_increments[i] = 1e-6;
457  }
458 
459  /** Computes A=A-B, which may need to be re-implemented depending on the
460  * topology of the individual scalar components (eg, angles).
461  */
463  KFArray_OBS& A, const KFArray_OBS& B) const
464  {
465  A -= B;
466  }
467 
468  public:
469  /** If applicable to the given problem, this method implements the inverse
470  * observation model needed to extend the "map" with a new "element".
471  * \param in_z The observation vector whose inverse sensor model is to be
472  * computed. This is actually one of the vector<> returned by
473  * OnGetObservationsAndDataAssociation().
474  * \param out_yn The F-length vector with the inverse observation model \f$
475  * y_n=y(x,z_n) \f$.
476  * \param out_dyn_dxv The \f$F \times V\f$ Jacobian of the inv. sensor
477  * model wrt the robot pose \f$ \frac{\partial y_n}{\partial x_v} \f$.
478  * \param out_dyn_dhn The \f$F \times O\f$ Jacobian of the inv. sensor
479  * model wrt the observation vector \f$ \frac{\partial y_n}{\partial h_n}
480  * \f$.
481  *
482  * - O: OBS_SIZE
483  * - V: VEH_SIZE
484  * - F: FEAT_SIZE
485  *
486  * \note OnNewLandmarkAddedToMap will be also called after calling this
487  * method if a landmark is actually being added to the map.
488  * \deprecated This version of the method is deprecated. The alternative
489  * method is preferred to allow a greater flexibility.
490  */
492  const KFArray_OBS& in_z, KFArray_FEAT& out_yn,
493  KFMatrix_FxV& out_dyn_dxv, KFMatrix_FxO& out_dyn_dhn) const
494  {
495  MRPT_UNUSED_PARAM(in_z);
496  MRPT_UNUSED_PARAM(out_yn);
497  MRPT_UNUSED_PARAM(out_dyn_dxv);
498  MRPT_UNUSED_PARAM(out_dyn_dhn);
499  MRPT_START
501  "Inverse sensor model required but not implemented in derived "
502  "class.");
503  MRPT_END
504  }
505 
506  /** If applicable to the given problem, this method implements the inverse
507  * observation model needed to extend the "map" with a new "element".
508  * The uncertainty in the new map feature comes from two parts: one from
509  * the vehicle uncertainty (through the out_dyn_dxv Jacobian),
510  * and another from the uncertainty in the observation itself. By
511  * default, out_use_dyn_dhn_jacobian=true on call, and if it's left at
512  * "true",
513  * the base KalmanFilter class will compute the uncertainty of the
514  * landmark relative position from out_dyn_dhn.
515  * Only in some problems (e.g. MonoSLAM), it'll be needed for the
516  * application to directly return the covariance matrix \a
517  * out_dyn_dhn_R_dyn_dhnT, which is the equivalent to:
518  *
519  * \f$ \frac{\partial y_n}{\partial h_n} R \frac{\partial
520  * y_n}{\partial h_n}^\top \f$.
521  *
522  * but may be computed from additional terms, or whatever needed by the
523  * user.
524  *
525  * \param in_z The observation vector whose inverse sensor model is to be
526  * computed. This is actually one of the vector<> returned by
527  * OnGetObservationsAndDataAssociation().
528  * \param out_yn The F-length vector with the inverse observation model \f$
529  * y_n=y(x,z_n) \f$.
530  * \param out_dyn_dxv The \f$F \times V\f$ Jacobian of the inv. sensor
531  * model wrt the robot pose \f$ \frac{\partial y_n}{\partial x_v} \f$.
532  * \param out_dyn_dhn The \f$F \times O\f$ Jacobian of the inv. sensor
533  * model wrt the observation vector \f$ \frac{\partial y_n}{\partial h_n}
534  * \f$.
535  * \param out_dyn_dhn_R_dyn_dhnT See the discussion above.
536  *
537  * - O: OBS_SIZE
538  * - V: VEH_SIZE
539  * - F: FEAT_SIZE
540  *
541  * \note OnNewLandmarkAddedToMap will be also called after calling this
542  * method if a landmark is actually being added to the map.
543  */
545  const KFArray_OBS& in_z, KFArray_FEAT& out_yn,
546  KFMatrix_FxV& out_dyn_dxv, KFMatrix_FxO& out_dyn_dhn,
547  KFMatrix_FxF& out_dyn_dhn_R_dyn_dhnT,
548  bool& out_use_dyn_dhn_jacobian) const
549  {
550  MRPT_UNUSED_PARAM(out_dyn_dhn_R_dyn_dhnT);
551  MRPT_START
552  OnInverseObservationModel(in_z, out_yn, out_dyn_dxv, out_dyn_dhn);
553  out_use_dyn_dhn_jacobian = true;
554  MRPT_END
555  }
556 
557  /** If applicable to the given problem, do here any special handling of
558  * adding a new landmark to the map.
559  * \param in_obsIndex The index of the observation whose inverse sensor is
560  * to be computed. It corresponds to the row in in_z where the observation
561  * can be found.
562  * \param in_idxNewFeat The index that this new feature will have in the
563  * state vector (0:just after the vehicle state, 1: after that,...). Save
564  * this number so data association can be done according to these indices.
565  * \sa OnInverseObservationModel
566  */
568  const size_t in_obsIdx, const size_t in_idxNewFeat)
569  {
570  MRPT_UNUSED_PARAM(in_obsIdx);
571  MRPT_UNUSED_PARAM(in_idxNewFeat);
572  // Do nothing in this base class.
573  }
574 
575  /** This method is called after the prediction and after the update, to give
576  * the user an opportunity to normalize the state vector (eg, keep angles
577  * within -pi,pi range) if the application requires it.
578  */
579  virtual void OnNormalizeStateVector()
580  {
581  // Do nothing in this base class.
582  }
583 
584  /** This method is called after finishing one KF iteration and before
585  * returning from runOneKalmanIteration().
586  */
587  virtual void OnPostIteration()
588  {
589  // Do nothing in this base class.
590  }
591 
592  /** @}
593  */
594 
595  public:
597  : mrpt::system::COutputLogger("CKalmanFilterCapable"),
599  {
600  }
601  /** Destructor */
602  ~CKalmanFilterCapable() override = default;
604  /** Generic options for the Kalman Filter algorithm itself. */
606 
607  private:
608  // "Local" variables to runOneKalmanIteration, declared here to avoid
609  // allocating them over and over again with each call.
610  // (The variables that go into the stack remains in the function body)
612  std::vector<size_t> m_predictLMidxs;
613  /** The vector of all partial Jacobians dh[i]_dx for each prediction */
614  std::vector<KFMatrix_OxV> m_Hxs;
615  /** The vector of all partial Jacobians dh[i]_dy[i] for each prediction */
616  std::vector<KFMatrix_OxF> m_Hys;
618  vector_KFArray_OBS m_Z; // Each entry is one observation:
619  KFMatrix m_K; // Kalman gain
620  KFMatrix m_S_1; // Inverse of m_S
623 
624  protected:
625  /** The main entry point, executes one complete step: prediction + update.
626  * It is protected since derived classes must provide a problem-specific
627  * entry point for users.
628  * The exact order in which this method calls the virtual method is
629  * explained in https://www.mrpt.org/Kalman_Filters
630  */
631  void runOneKalmanIteration();
632 
633  private:
634  mutable bool m_user_didnt_implement_jacobian{true};
635 
636  /** Auxiliary functions for Jacobian numeric estimation */
637  static void KF_aux_estimate_trans_jacobian(
638  const KFArray_VEH& x, const std::pair<KFCLASS*, KFArray_ACT>& dat,
639  KFArray_VEH& out_x);
641  const KFArray_VEH& x, const std::pair<KFCLASS*, size_t>& dat,
642  KFArray_OBS& out_x);
644  const KFArray_FEAT& x, const std::pair<KFCLASS*, size_t>& dat,
645  KFArray_OBS& out_x);
646 
647  template <
648  size_t VEH_SIZEb, size_t OBS_SIZEb, size_t FEAT_SIZEb, size_t ACT_SIZEb,
649  typename KFTYPEb>
650  friend void detail::addNewLandmarks(
652  VEH_SIZEb, OBS_SIZEb, FEAT_SIZEb, ACT_SIZEb, KFTYPEb>& obj,
653  const typename CKalmanFilterCapable<
654  VEH_SIZEb, OBS_SIZEb, FEAT_SIZEb, ACT_SIZEb,
655  KFTYPEb>::vector_KFArray_OBS& Z,
656  const std::vector<int>& data_association,
657  const typename CKalmanFilterCapable<
658  VEH_SIZEb, OBS_SIZEb, FEAT_SIZEb, ACT_SIZEb, KFTYPEb>::KFMatrix_OxO&
659  R);
660 }; // end class
661 
662 } // namespace bayes
663 } // namespace mrpt
664 
666 using namespace mrpt::bayes;
672 
673 // Template implementation:
674 #define CKalmanFilterCapable_H
virtual void OnPreComputingPredictions(const vector_KFArray_OBS &in_all_prediction_means, std::vector< size_t > &out_LM_indices_to_predict) const
This will be called before OnGetObservationsAndDataAssociation to allow the application to reduce the...
A compile-time fixed-size numeric matrix container.
Definition: CMatrixFixed.h:33
bool use_analytic_observation_jacobian
(default=true) If true, OnObservationJacobians will be called; otherwise, the Jacobian will be estima...
#define MRPT_START
Definition: exceptions.h:241
VerbosityLevel
Enumeration of available verbosity levels.
MRPT_FILL_ENUM(kfEKFNaive)
This is a virtual base class for sets of options than can be loaded from and/or saved to configuratio...
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...
mrpt::math::CMatrixFixed< KFTYPE, VEH_SIZE, VEH_SIZE > KFMatrix_VxV
~CKalmanFilterCapable() override=default
Destructor.
virtual void OnObservationJacobians(const size_t &idx_landmark_to_predict, KFMatrix_OxV &Hx, KFMatrix_OxF &Hy) const
Implements the observation Jacobians and (when applicable) .
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
virtual void OnObservationModel(const std::vector< size_t > &idx_landmarks_to_predict, vector_KFArray_OBS &out_predictions) const =0
Implements the observation prediction .
void loadFromConfigFile(const mrpt::config::CConfigFileBase &iniFile, const std::string &section) override
This method load the options from a ".ini"-like file or memory-stored string list.
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...
virtual void OnGetObservationsAndDataAssociation(vector_KFArray_OBS &out_z, std::vector< int > &out_data_association, const vector_KFArray_OBS &in_all_predictions, const KFMatrix &in_S, const std::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...
static constexpr size_t get_action_size()
The namespace for Bayesian filtering algorithm: different particle filters and Kalman filter algorith...
bool enable_profiler
If enabled (default=false), detailed timing information will be dumped to the console thru a CTimerLo...
static void KF_aux_estimate_obs_Hx_jacobian(const KFArray_VEH &x, const std::pair< KFCLASS *, size_t > &dat, KFArray_OBS &out_x)
size_type size() const
Get a 2-vector with [NROWS NCOLS] (as in MATLAB command size(x))
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.
static constexpr size_t get_feature_size()
virtual void OnTransitionJacobian(KFMatrix_VxV &out_F) const
Implements the transition Jacobian .
mrpt::math::CVectorFixed< 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:4085
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...
std::vector< KFMatrix_OxF > m_Hys
The vector of all partial Jacobians dh[i]_dy[i] for each prediction.
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...
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
void dumpToTextStream(std::ostream &out) const override
This method must display clearly all the contents of the structure in textual form, sending it to a CStream.
This class allows loading and storing values and vectors of different types from a configuration text...
mrpt::math::CMatrixDynamic< KFTYPE > KFMatrix
mrpt::system::VerbosityLevel & verbosity_level
TKF_options KF_options
Generic options for the Kalman Filter algorithm itself.
A helper class that can convert an enum value into its textual representation, and viceversa...
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 std::vector< int > &data_association, const typename CKalmanFilterCapable< VEH_SIZE, OBS_SIZE, FEAT_SIZE, ACT_SIZE, KFTYPE >::KFMatrix_OxO &R)
Versatile class for consistent logging and management of output messages.
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)...
Virtual base for Kalman Filter (EKF,IEKF,UKF) implementations.
size_t getNumberOfLandmarksInMap(const CKalmanFilterCapable< VEH_SIZE, OBS_SIZE, FEAT_SIZE, ACT_SIZE, KFTYPE > &obj)
string iniFile(myDataDir+string("benchmark-options.ini"))
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 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...
#define MRPT_ENUM_TYPE_END()
Definition: TEnumType.h:78
double kftype
The numeric type used in the Kalman Filter (default=double)
std::vector< KFMatrix_OxV > m_Hxs
The vector of all partial Jacobians dh[i]_dx for each prediction.
GLsizei const GLchar ** string
Definition: glext.h:4116
virtual void OnTransitionJacobianNumericGetIncrements(KFArray_VEH &out_increments) const
Only called if using a numeric approximation of the transition Jacobian, this method must return the ...
mrpt::math::CVectorFixed< KFTYPE, FEAT_SIZE > KFArray_FEAT
Generic options for the Kalman Filter algorithm in itself.
#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...
static constexpr size_t get_observation_size()
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
A versatile "profiler" that logs the time spent within each pair of calls to enter(X)-leave(X), among other stats.
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...
bool debug_verify_analytic_jacobians
(default=false) If true, will compute all the Jacobians numerically and compare them to the analytica...
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
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...
COutputLogger()
Default class constructor.
#define MRPT_END
Definition: exceptions.h:245
static constexpr size_t get_vehicle_size()
CMatrixFixed< Scalar, BLOCK_ROWS, BLOCK_COLS > blockCopy(int start_row=0, int start_col=0) const
const blockCopy(): Returns a copy of the given block
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.
GLenum GLint x
Definition: glext.h:3542
mrpt::math::CVectorFixed< KFTYPE, OBS_SIZE > KFArray_OBS
VerbosityLevel m_min_verbosity_level
Provided messages with VerbosityLevel smaller than this value shall be ignored.
#define MRPT_ENUM_TYPE_BEGIN(_ENUM_TYPE_WITH_NS)
Definition: TEnumType.h:62
TKF_options(mrpt::system::VerbosityLevel &verb_level_ref)
mrpt::system::CTimeLogger & getProfiler()
KFVector m_xkk
The system state vector.
bool use_analytic_transition_jacobian
(default=true) If true, OnTransitionJacobian will be called; otherwise, the Jacobian will be estimate...
mrpt::system::CTimeLogger m_timLogger
int IKF_iterations
Number of refinement iterations, only for the IKF method.
mrpt::math::CMatrixFixed< KFTYPE, OBS_SIZE, OBS_SIZE > KFMatrix_OxO
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:358
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019