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



Page generated by Doxygen 1.8.12 for MRPT 1.3.2 SVN: at Thu Nov 10 13:22:34 UTC 2016