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