MRPT  1.9.9
CRangeBearingKFSLAM2D.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-2018, 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 CRangeBearingKFSLAM2D_H
10 #define CRangeBearingKFSLAM2D_H
11 
17 
19 #include <mrpt/containers/bimap.h>
20 
21 #include <mrpt/obs/CSensoryFrame.h>
25 #include <mrpt/maps/CLandmark.h>
26 #include <mrpt/maps/CSimpleMap.h>
29 
30 namespace mrpt::slam
31 {
32 /** An implementation of EKF-based SLAM with range-bearing sensors, odometry,
33  *and a 2D (+heading) robot pose, and 2D landmarks.
34  * The main method is "processActionObservation" which processes pairs of
35  *action/observation.
36  *
37  * The following pages describe front-end applications based on this class:
38  * - http://www.mrpt.org/Application:2d-slam-demo
39  * - http://www.mrpt.org/Application:kf-slam
40  *
41  * \sa CRangeBearingKFSLAM \ingroup metric_slam_grp
42  */
44  : public bayes::CKalmanFilterCapable<3 /* x y yaw */, 2 /* range yaw */,
45  2 /* x y */, 3 /* Ax Ay Ayaw */>
46 // <size_t VEH_SIZE, size_t OBS_SIZE, size_t FEAT_SIZE, size_t ACT_SIZE, size
47 // typename kftype = double>
48 {
49  public:
50  /** Either mrpt::math::TPoint2D or mrpt::math::TPoint3D */
52 
53  /** Default constructor */
55  /** Destructor */
56  virtual ~CRangeBearingKFSLAM2D();
57  /** Reset the state of the SLAM filter: The map is emptied and the robot put
58  * back to (0,0,0). */
59  void reset();
60 
61  /** Process one new action and observations to update the map and robot pose
62  *estimate. See the description of the class at the top of this page.
63  * \param action May contain odometry
64  * \param SF The set of observations, must contain at least one
65  *CObservationBearingRange
66  */
70 
71  /** Returns the complete mean and cov.
72  * \param out_robotPose The mean & 3x3 covariance matrix of the robot 2D
73  * pose
74  * \param out_landmarksPositions One entry for each of the M landmark
75  * positions (2D).
76  * \param out_landmarkIDs Each element[index] (for indices of
77  * out_landmarksPositions) gives the corresponding landmark ID.
78  * \param out_fullState The complete state vector (3+2M).
79  * \param out_fullCovariance The full (3+2M)x(3+2M) covariance matrix of
80  * the filter.
81  * \sa getCurrentRobotPose
82  */
83  void getCurrentState(
84  mrpt::poses::CPosePDFGaussian& out_robotPose,
85  std::vector<mrpt::math::TPoint2D>& out_landmarksPositions,
86  std::map<unsigned int, mrpt::maps::CLandmark::TLandmarkID>&
87  out_landmarkIDs,
88  mrpt::math::CVectorDouble& out_fullState,
89  mrpt::math::CMatrixDouble& out_fullCovariance) const;
90 
91  /** Returns the mean & 3x3 covariance matrix of the robot 2D pose.
92  * \sa getCurrentState
93  */
95  mrpt::poses::CPosePDFGaussian& out_robotPose) const;
96 
97  /** Returns a 3D representation of the landmarks in the map and the robot 3D
98  * position according to the current filter state.
99  * \param out_objects
100  */
102 
103  /** Load options from a ini-like file/text
104  */
106 
107  /** The options for the algorithm
108  */
110  {
111  /** Default values */
112  TOptions();
113 
114  void loadFromConfigFile(
116  const std::string& section) override; // See base docs
117  void dumpToTextStream(
118  std::ostream& out) const override; // See base docs
119 
120  /** A 3-length vector with the std. deviation of the transition model in
121  * (x,y,phi) used only when there is no odometry (if there is odo, its
122  * uncertainty values will be used instead); x y: In meters, phi:
123  * radians (but in degrees when loading from a configuration ini-file!)
124  */
126  /** The std. deviation of the sensor (for the matrix R in the kalman
127  * filters), in meters and radians. */
129  /** Default = 3 */
131  /** Whether to fill m_SFs (default=false) */
133 
134  // Data association:
137  /** Threshold in [0,1] for the chi2square test for individual
138  * compatibility between predictions and observations (default: 0.99) */
140  /** Whether to use mahalanobis (->chi2 criterion) vs. Matching
141  * likelihood. */
143  /** Only if data_assoc_IC_metric==ML, the log-ML threshold (Default=0.0)
144  */
146  };
147 
148  /** The options for the algorithm */
150 
151  /** Save the current state of the filter (robot pose & map) to a MATLAB
152  * script which displays all the elements in 2D
153  */
155  const std::string& fil, float stdCount = 3.0f,
156  const std::string& styleLandmarks = std::string("b"),
157  const std::string& stylePath = std::string("r"),
158  const std::string& styleRobot = std::string("r")) const;
159 
160  /** Information for data-association:
161  * \sa getLastDataAssociation
162  */
164  {
166  void clear()
167  {
168  results.clear();
169  predictions_IDs.clear();
170  newly_inserted_landmarks.clear();
171  }
172 
173  // Predictions from the map:
175  std::vector<size_t> predictions_IDs;
176 
177  /** Map from the 0-based index within the last observation and the
178  landmark 0-based index in the map (the robot-map state vector)
179  Only used for stats and so. */
180  std::map<size_t, size_t> newly_inserted_landmarks;
181 
182  // DA results:
184  };
185 
186  /** Returns a read-only reference to the information on the last
187  * data-association */
189  {
191  }
192 
193  protected:
194  /** @name Virtual methods for Kalman Filter implementation
195  @{
196  */
197 
198  /** Must return the action vector u.
199  * \param out_u The action vector which will be passed to OnTransitionModel
200  */
201  void OnGetAction(KFArray_ACT& out_u) const;
202 
203  /** Implements the transition model \f$ \hat{x}_{k|k-1} = f(
204  * \hat{x}_{k-1|k-1}, u_k ) \f$
205  * \param in_u The vector returned by OnGetAction.
206  * \param inout_x At input has \f[ \hat{x}_{k-1|k-1} \f] , at output must
207  * have \f$ \hat{x}_{k|k-1} \f$ .
208  * \param out_skip Set this to true if for some reason you want to skip the
209  * prediction step (to do not modify either the vector or the covariance).
210  * Default:false
211  */
212  void OnTransitionModel(
213  const KFArray_ACT& in_u, KFArray_VEH& inout_x,
214  bool& out_skipPrediction) const;
215 
216  /** Implements the transition Jacobian \f$ \frac{\partial f}{\partial x} \f$
217  * \param out_F Must return the Jacobian.
218  * The returned matrix must be \f$V \times V\f$ with V being either the
219  * size of the whole state vector (for non-SLAM problems) or VEH_SIZE (for
220  * SLAM problems).
221  */
222  void OnTransitionJacobian(KFMatrix_VxV& out_F) const;
223 
224  /** Only called if using a numeric approximation of the transition Jacobian,
225  * this method must return the increments in each dimension of the vehicle
226  * state vector while estimating the Jacobian.
227  */
229  KFArray_VEH& out_increments) const;
230 
231  /** Implements the transition noise covariance \f$ Q_k \f$
232  * \param out_Q Must return the covariance matrix.
233  * The returned matrix must be of the same size than the jacobian from
234  * OnTransitionJacobian
235  */
236  void OnTransitionNoise(KFMatrix_VxV& out_Q) const;
237 
238  /** This is called between the KF prediction step and the update step, and
239  * the application must return the observations and, when applicable, the
240  * data association between these observations and the current map.
241  *
242  * \param out_z N vectors, each for one "observation" of length OBS_SIZE, N
243  * being the number of "observations": how many observed landmarks for a
244  * map, or just one if not applicable.
245  * \param out_data_association An empty vector or, where applicable, a
246  * vector where the i'th element corresponds to the position of the
247  * observation in the i'th row of out_z within the system state vector (in
248  * the range [0,getNumberOfLandmarksInTheMap()-1]), or -1 if it is a new map
249  * element and we want to insert it at the end of this KF iteration.
250  * \param in_S The full covariance matrix of the observation predictions
251  * (i.e. the "innovation covariance matrix"). This is a M*O x M*O matrix
252  * with M=length of "in_lm_indices_in_S".
253  * \param in_lm_indices_in_S The indices of the map landmarks (range
254  * [0,getNumberOfLandmarksInTheMap()-1]) that can be found in the matrix
255  * in_S.
256  *
257  * This method will be called just once for each complete KF iteration.
258  * \note It is assumed that the observations are independent, i.e. there
259  * are NO cross-covariances between them.
260  */
262  vector_KFArray_OBS& out_z, std::vector<int>& out_data_association,
263  const vector_KFArray_OBS& in_all_predictions, const KFMatrix& in_S,
264  const std::vector<size_t>& in_lm_indices_in_S,
265  const KFMatrix_OxO& in_R);
266 
267  void OnObservationModel(
268  const std::vector<size_t>& idx_landmarks_to_predict,
269  vector_KFArray_OBS& out_predictions) const;
270 
271  /** Implements the observation Jacobians \f$ \frac{\partial h_i}{\partial x}
272  * \f$ and (when applicable) \f$ \frac{\partial h_i}{\partial y_i} \f$.
273  * \param idx_landmark_to_predict The index of the landmark in the map
274  * whose prediction is expected as output. For non SLAM-like problems, this
275  * will be zero and the expected output is for the whole state vector.
276  * \param Hx The output Jacobian \f$ \frac{\partial h_i}{\partial x} \f$.
277  * \param Hy The output Jacobian \f$ \frac{\partial h_i}{\partial y_i}
278  * \f$.
279  */
281  const size_t& idx_landmark_to_predict, KFMatrix_OxV& Hx,
282  KFMatrix_OxF& Hy) const;
283 
284  /** Only called if using a numeric approximation of the observation
285  * Jacobians, this method must return the increments in each dimension of
286  * the vehicle state vector while estimating the Jacobian.
287  */
289  KFArray_VEH& out_veh_increments,
290  KFArray_FEAT& out_feat_increments) const;
291 
292  /** Computes A=A-B, which may need to be re-implemented depending on the
293  * topology of the individual scalar components (eg, angles).
294  */
296  KFArray_OBS& A, const KFArray_OBS& B) const;
297 
298  /** Return the observation NOISE covariance matrix, that is, the model of
299  * the Gaussian additive noise of the sensor.
300  * \param out_R The noise covariance matrix. It might be non diagonal, but
301  * it'll usually be.
302  */
303  void OnGetObservationNoise(KFMatrix_OxO& out_R) const;
304 
305  /** This will be called before OnGetObservationsAndDataAssociation to allow
306  * the application to reduce the number of covariance landmark predictions
307  * to be made.
308  * For example, features which are known to be "out of sight" shouldn't be
309  * added to the output list to speed up the calculations.
310  * \param in_all_prediction_means The mean of each landmark predictions;
311  * the computation or not of the corresponding covariances is what we're
312  * trying to determined with this method.
313  * \param out_LM_indices_to_predict The list of landmark indices in the map
314  * [0,getNumberOfLandmarksInTheMap()-1] that should be predicted.
315  * \note This is not a pure virtual method, so it should be implemented
316  * only if desired. The default implementation returns a vector with all the
317  * landmarks in the map.
318  * \sa OnGetObservations, OnDataAssociation
319  */
321  const vector_KFArray_OBS& in_all_prediction_means,
322  std::vector<size_t>& out_LM_indices_to_predict) const;
323 
324  /** If applicable to the given problem, this method implements the inverse
325  * observation model needed to extend the "map" with a new "element".
326  * \param in_z The observation vector whose inverse sensor model is to be
327  * computed. This is actually one of the vector<> returned by
328  * OnGetObservations().
329  * \param out_yn The F-length vector with the inverse observation model \f$
330  * y_n=y(x,z_n) \f$.
331  * \param out_dyn_dxv The \f$F \times V\f$ Jacobian of the inv. sensor
332  * model wrt the robot pose \f$ \frac{\partial y_n}{\partial x_v} \f$.
333  * \param out_dyn_dhn The \f$F \times O\f$ Jacobian of the inv. sensor
334  * model wrt the observation vector \f$ \frac{\partial y_n}{\partial h_n}
335  * \f$.
336  *
337  * - O: OBS_SIZE
338  * - V: VEH_SIZE
339  * - F: FEAT_SIZE
340  *
341  * \note OnNewLandmarkAddedToMap will be also called after calling this
342  * method if a landmark is actually being added to the map.
343  */
345  const KFArray_OBS& in_z, KFArray_FEAT& out_yn,
346  KFMatrix_FxV& out_dyn_dxv, KFMatrix_FxO& out_dyn_dhn) const;
347 
348  /** If applicable to the given problem, do here any special handling of
349  * adding a new landmark to the map.
350  * \param in_obsIndex The index of the observation whose inverse sensor is
351  * to be computed. It corresponds to the row in in_z where the observation
352  * can be found.
353  * \param in_idxNewFeat The index that this new feature will have in the
354  * state vector (0:just after the vehicle state, 1: after that,...). Save
355  * this number so data association can be done according to these indices.
356  * \sa OnInverseObservationModel
357  */
359  const size_t in_obsIdx, const size_t in_idxNewFeat);
360 
361  /** This method is called after the prediction and after the update, to give
362  * the user an opportunity to normalize the state vector (eg, keep angles
363  * within -pi,pi range) if the application requires it.
364  */
365  void OnNormalizeStateVector();
366 
367  /** @}
368  */
371  std::map<unsigned int, mrpt::maps::CLandmark::TLandmarkID>&
372  out_id2index) const
373  {
374  out_id2index = m_IDs.getInverseMap();
375  }
376 
377  protected:
378  /** Set up by processActionObservation */
380 
381  /** Set up by processActionObservation */
383 
384  /** The mapping between landmark IDs and indexes in the Pkk cov. matrix: */
386  m_IDs;
387 
388  /** The sequence of all the observations and the robot path (kept for
389  * debugging, statistics,etc) */
391 
392  /** Last data association */
394 }; // end class
395 }
396 #endif
397 
398 
mrpt::math::CMatrixFixedNumeric< double, OBS_SIZE, VEH_SIZE > KFMatrix_OxV
mrpt::math::CVectorFloat stds_Q_no_odo
A 3-length vector with the std.
An implementation of EKF-based SLAM with range-bearing sensors, odometry, and a 2D (+heading) robot p...
TOptions options
The options for the algorithm.
This is a virtual base class for sets of options than can be loaded from and/or saved to configuratio...
This class stores a sequence of <Probabilistic Pose,SensoryFrame> pairs, thus a "metric map" can be t...
Definition: CSimpleMap.h:33
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)
This is called between the KF prediction step and the update step, and the application must return th...
bool create_simplemap
Whether to fill m_SFs (default=false)
void OnTransitionJacobian(KFMatrix_VxV &out_F) const
Implements the transition Jacobian .
void OnTransitionNoise(KFMatrix_VxV &out_Q) const
Implements the transition noise covariance .
mrpt::math::CArrayNumeric< double, VEH_SIZE > KFArray_VEH
void reset()
Reset the state of the SLAM filter: The map is emptied and the robot put back to (0,0,0).
mrpt::math::CMatrixTemplateNumeric< double > KFMatrix
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:44
mrpt::obs::CActionCollection::Ptr m_action
Set up by processActionObservation.
void loadFromConfigFile(const mrpt::config::CConfigFileBase &source, const std::string &section) override
This method load the options from a ".ini"-like file or memory-stored string list.
mrpt::math::CMatrixTemplateNumeric< kftype > Y_pred_means
void OnNormalizeStateVector()
This method is called after the prediction and after the update, to give the user an opportunity to n...
void OnGetObservationNoise(KFMatrix_OxO &out_R) const
Return the observation NOISE covariance matrix, that is, the model of the Gaussian additive noise of ...
mrpt::math::CArrayNumeric< double, FEAT_SIZE > KFArray_FEAT
void OnTransitionJacobianNumericGetIncrements(KFArray_VEH &out_increments) const
Only called if using a numeric approximation of the transition Jacobian, this method must return the ...
mrpt::maps::CSimpleMap m_SFs
The sequence of all the observations and the robot path (kept for debugging, statistics,etc)
mrpt::math::CMatrixFixedNumeric< double, VEH_SIZE, VEH_SIZE > KFMatrix_VxV
const std::map< VALUE, KEY > & getInverseMap() const
Return a read-only reference to the internal map KEY->VALUES.
Definition: bimap.h:61
mrpt::math::CMatrixTemplateNumeric< kftype > Y_pred_covs
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...
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...
This class allows loading and storing values and vectors of different types from a configuration text...
void getCurrentState(mrpt::poses::CPosePDFGaussian &out_robotPose, std::vector< mrpt::math::TPoint2D > &out_landmarksPositions, std::map< unsigned int, mrpt::maps::CLandmark::TLandmarkID > &out_landmarkIDs, mrpt::math::CVectorDouble &out_fullState, mrpt::math::CMatrixDouble &out_fullCovariance) const
Returns the complete mean and cov.
void OnObservationJacobians(const size_t &idx_landmark_to_predict, KFMatrix_OxV &Hx, KFMatrix_OxF &Hy) const
Implements the observation Jacobians and (when applicable) .
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...
Declares a class that represents a Probability Density function (PDF) of a 2D pose ...
mrpt::math::CArrayNumeric< double, OBS_SIZE > KFArray_OBS
TDataAssociationMetric
Different metrics for data association, used in mrpt::slam::data_association For a comparison of both...
std::map< size_t, size_t > newly_inserted_landmarks
Map from the 0-based index within the last observation and the landmark 0-based index in the map (the...
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...
double data_assoc_IC_ml_threshold
Only if data_assoc_IC_metric==ML, the log-ML threshold (Default=0.0)
Virtual base for Kalman Filter (EKF,IEKF,UKF) implementations.
void OnTransitionModel(const KFArray_ACT &in_u, KFArray_VEH &inout_x, bool &out_skipPrediction) const
Implements the transition model .
mrpt::math::CMatrixFixedNumeric< double, OBS_SIZE, OBS_SIZE > KFMatrix_OxO
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::aligned_std_vector< KFArray_OBS > vector_KFArray_OBS
mrpt::math::CArrayNumeric< double, ACT_SIZE > KFArray_ACT
TDataAssocInfo m_last_data_association
Last data association.
CRangeBearingKFSLAM2D()
Default constructor.
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...
void getCurrentRobotPose(mrpt::poses::CPosePDFGaussian &out_robotPose) const
Returns the mean & 3x3 covariance matrix of the robot 2D pose.
double data_assoc_IC_chi2_thres
Threshold in [0,1] for the chi2square test for individual compatibility between predictions and obser...
mrpt::math::CMatrixFixedNumeric< double, FEAT_SIZE, VEH_SIZE > KFMatrix_FxV
void getLandmarkIDsFromIndexInStateVector(std::map< unsigned int, mrpt::maps::CLandmark::TLandmarkID > &out_id2index) const
TDataAssociationMetric data_assoc_IC_metric
Whether to use mahalanobis (->chi2 criterion) vs.
void OnObservationModel(const std::vector< size_t > &idx_landmarks_to_predict, vector_KFArray_OBS &out_predictions) const
GLsizei GLsizei GLchar * source
Definition: glext.h:4082
mrpt::math::CMatrixFixedNumeric< double, OBS_SIZE, FEAT_SIZE > KFMatrix_OxF
void loadOptions(const mrpt::config::CConfigFileBase &ini)
Load options from a ini-like file/text.
The results from mrpt::slam::data_association.
TDataAssociationMethod
Different algorithms for data association, used in mrpt::slam::data_association.
Lightweight 2D point.
void saveMapAndPath2DRepresentationAsMATLABFile(const std::string &fil, float stdCount=3.0f, const std::string &styleLandmarks=std::string("b"), const std::string &stylePath=std::string("r"), const std::string &styleRobot=std::string("r")) const
Save the current state of the filter (robot pose & map) to a MATLAB script which displays all the ele...
void getAs3DObject(mrpt::opengl::CSetOfObjects::Ptr &outObj) const
Returns a 3D representation of the landmarks in the map and the robot 3D position according to the cu...
const TDataAssocInfo & getLastDataAssociation() const
Returns a read-only reference to the information on the last data-association.
mrpt::math::CMatrixFixedNumeric< double, FEAT_SIZE, OBS_SIZE > KFMatrix_FxO
void processActionObservation(mrpt::obs::CActionCollection::Ptr &action, mrpt::obs::CSensoryFrame::Ptr &SF)
Process one new action and observations to update the map and robot pose estimate.
mrpt::containers::bimap< mrpt::maps::CLandmark::TLandmarkID, unsigned int > m_IDs
The mapping between landmark IDs and indexes in the Pkk cov.
void dumpToTextStream(std::ostream &out) const override
This method should clearly display all the contents of the structure in textual form, sending it to a std::ostream.
mrpt::obs::CSensoryFrame::Ptr m_SF
Set up by processActionObservation.
void OnGetAction(KFArray_ACT &out_u) const
Must return the action vector u.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020