00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 00005 | | 00006 | Copyright (C) 2005-2010 University of Malaga | 00007 | | 00008 | This software was written by the Machine Perception and Intelligent | 00009 | Robotics Lab, University of Malaga (Spain). | 00010 | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> | 00011 | | 00012 | This file is part of the MRPT project. | 00013 | | 00014 | MRPT is free software: you can redistribute it and/or modify | 00015 | it under the terms of the GNU General Public License as published by | 00016 | the Free Software Foundation, either version 3 of the License, or | 00017 | (at your option) any later version. | 00018 | | 00019 | MRPT is distributed in the hope that it will be useful, | 00020 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00021 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 00022 | GNU General Public License for more details. | 00023 | | 00024 | You should have received a copy of the GNU General Public License | 00025 | along with MRPT. If not, see <http://www.gnu.org/licenses/>. | 00026 | | 00027 +---------------------------------------------------------------------------+ */ 00028 #ifndef CRangeBearingKFSLAM_H 00029 #define CRangeBearingKFSLAM_H 00030 00031 #include <mrpt/utils/CDebugOutputCapable.h> 00032 #include <mrpt/math/CVectorTemplate.h> 00033 #include <mrpt/math/CMatrixTemplateNumeric.h> 00034 #include <mrpt/utils/CConfigFileBase.h> 00035 #include <mrpt/utils/CLoadableOptions.h> 00036 #include <mrpt/opengl.h> 00037 #include <mrpt/bayes/CKalmanFilterCapable.h> 00038 00039 #include <mrpt/utils/safe_pointers.h> 00040 00041 #include <mrpt/slam/CSensoryFrame.h> 00042 #include <mrpt/slam/CActionCollection.h> 00043 #include <mrpt/slam/CObservationBearingRange.h> 00044 #include <mrpt/poses/CPoint3D.h> 00045 #include <mrpt/poses/CPose3DPDFGaussian.h> 00046 #include <mrpt/slam/CLandmark.h> 00047 #include <mrpt/slam/CSensFrameProbSequence.h> 00048 #include <mrpt/slam/CIncrementalMapPartitioner.h> 00049 #include <mrpt/slam/data_association.h> 00050 00051 namespace mrpt 00052 { 00053 namespace slam 00054 { 00055 using namespace mrpt::bayes; 00056 00057 /** An implementation of EKF-based SLAM with range-bearing sensors, odometry, a full 6D robot pose, and 3D landmarks. 00058 * The main method is "processActionObservation" which processes pairs of action/observation. 00059 * 00060 * The following Wiki page describes an front-end application based on this class: 00061 * http://babel.isa.uma.es/mrpt/index.php/Application:kf-slam 00062 * 00063 * \sa An implementation for 2D only: CRangeBearingKFSLAM2D 00064 */ 00065 class MRPTDLLIMPEXP CRangeBearingKFSLAM : 00066 public bayes::CKalmanFilterCapable<6 /* x y z yaw pitch roll*/,3 /* range yaw pitch */, 3 /* x y z */, 6 /* Ax Ay Az Ayaw Apitch Aroll */ > 00067 // <size_t VEH_SIZE, size_t OBS_SIZE, size_t FEAT_SIZE, size_t ACT_SIZE, size typename kftype = double> 00068 { 00069 public: 00070 /** Constructor. 00071 */ 00072 CRangeBearingKFSLAM( ); 00073 00074 /** Destructor: 00075 */ 00076 virtual ~CRangeBearingKFSLAM(); 00077 00078 void reset(); //!< Reset the state of the SLAM filter: The map is emptied and the robot put back to (0,0,0). 00079 00080 /** 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. 00081 * \param action May contain odometry 00082 * \param SF The set of observations, must contain at least one CObservationBearingRange 00083 */ 00084 void processActionObservation( 00085 CActionCollectionPtr &action, 00086 CSensoryFramePtr &SF ); 00087 00088 /** Returns the complete mean and cov. 00089 * \param out_robotPose The mean & 6x6 covariance matrix of the robot 6D pose 00090 * \param out_landmarksPositions One entry for each of the M landmark positions (3D). 00091 * \param out_landmarkIDs Each element[index] (for indices of out_landmarksPositions) gives the corresponding landmark ID. 00092 * \param out_fullState The complete state vector (6+3M). 00093 * \param out_fullCovariance The full (6+3M)x(6+3M) covariance matrix of the filter. 00094 * \sa getCurrentRobotPose 00095 */ 00096 void getCurrentState( 00097 CPose3DPDFGaussian &out_robotPose, 00098 std::vector<CPoint3D> &out_landmarksPositions, 00099 std::map<unsigned int,CLandmark::TLandmarkID> &out_landmarkIDs, 00100 CVectorDouble &out_fullState, 00101 CMatrixDouble &out_fullCovariance 00102 ) const; 00103 00104 /** Returns the mean & 6x6 covariance matrix of the robot 6D pose. 00105 * \sa getCurrentState 00106 */ 00107 void getCurrentRobotPose( 00108 CPose3DPDFGaussian &out_robotPose ) const; 00109 00110 /** Returns a 3D representation of the landmarks in the map and the robot 3D position according to the current filter state. 00111 * \param out_objects 00112 */ 00113 void getAs3DObject( mrpt::opengl::CSetOfObjectsPtr &outObj ) const; 00114 00115 /** Load options from a ini-like file/text 00116 */ 00117 void loadOptions( const mrpt::utils::CConfigFileBase &ini ); 00118 00119 /** The options for the algorithm 00120 */ 00121 struct MRPTDLLIMPEXP TOptions : utils::CLoadableOptions 00122 { 00123 /** Default values 00124 */ 00125 TOptions(); 00126 00127 /** Load from a config file/text 00128 */ 00129 void loadFromConfigFile( 00130 const mrpt::utils::CConfigFileBase &source, 00131 const std::string §ion); 00132 00133 /** This method must display clearly all the contents of the structure in textual form, sending it to a CStream. 00134 */ 00135 void dumpToTextStream(CStream &out) const; 00136 00137 /** A 6-length vector with the std. deviation of the transition model in (x,y,z,yaw,pitch,roll) used only when there is no odometry (if there is odo, its uncertainty values will be used instead); x y z: In meters, yaw pitch roll: radians (but in degrees when loading from a configuration ini-file!) 00138 */ 00139 vector_float stds_Q_no_odo; 00140 00141 /** The std. deviation of the sensor (for the matrix R in the kalman filters), in meters and radians. 00142 */ 00143 float std_sensor_range, std_sensor_yaw, std_sensor_pitch; 00144 00145 /** Additional std. dev. to sum to the motion model in the z axis (useful when there is only 2D odometry and we want to put things hard to the algorithm) (default=0) 00146 */ 00147 float std_odo_z_additional; 00148 00149 /** If set to true (default=false), map will be partitioned using the method stated by partitioningMethod 00150 */ 00151 bool doPartitioningExperiment; 00152 00153 /** Default = 3 00154 */ 00155 float quantiles_3D_representation; 00156 00157 /** Applicable only if "doPartitioningExperiment=true". 00158 * 0: Automatically detect partition through graph-cut. 00159 * N>=1: Cut every "N" observations. 00160 */ 00161 int partitioningMethod; 00162 00163 // Data association: 00164 TDataAssociationMethod data_assoc_method; 00165 TDataAssociationMetric data_assoc_metric; 00166 double data_assoc_IC_chi2_thres; //!< Threshold in [0,1] for the chi2square test for individual compatibility between predictions and observations (default: 0.99) 00167 00168 bool create_simplemap; //!< Whether to fill m_SFs (default=false) 00169 } options; 00170 00171 /** Information for data-association: 00172 * \sa getLastDataAssociation 00173 */ 00174 struct MRPTDLLIMPEXP TDataAssocInfo 00175 { 00176 TDataAssocInfo() : 00177 Y_pred_means(0,0), 00178 Y_pred_covs(0,0) 00179 { 00180 } 00181 00182 void clear() { 00183 results.clear(); 00184 predictions_IDs.clear(); 00185 newly_inserted_landmarks.clear(); 00186 } 00187 00188 // Predictions from the map: 00189 CMatrixTemplateNumeric<kftype> Y_pred_means,Y_pred_covs; 00190 mrpt::vector_size_t predictions_IDs; 00191 00192 /** Map from the 0-based index within the last observation and the landmark 0-based index in the map (the robot-map state vector) 00193 Only used for stats and so. */ 00194 std::map<size_t,size_t> newly_inserted_landmarks; 00195 00196 // DA results: 00197 TDataAssociationResults results; 00198 }; 00199 00200 /** Returns a read-only reference to the information on the last data-association */ 00201 const TDataAssocInfo & getLastDataAssociation() const { 00202 return m_last_data_association; 00203 } 00204 00205 00206 /** Return the last partition of the sequence of sensoryframes (it is NOT a partition of the map!!) 00207 * Only if options.doPartitioningExperiment = true 00208 * \sa getLastPartitionLandmarks 00209 */ 00210 void getLastPartition( std::vector<vector_uint> &parts ) 00211 { 00212 parts = m_lastPartitionSet; 00213 } 00214 00215 /** Return the partitioning of the landmarks in clusters accoring to the last partition. 00216 * Note that the same landmark may appear in different clusters (the partition is not in the space of landmarks) 00217 * Only if options.doPartitioningExperiment = true 00218 * \param landmarksMembership The i'th element of this vector is the set of clusters to which the i'th landmark in the map belongs to (landmark index != landmark ID !!). 00219 * \sa getLastPartition 00220 */ 00221 void getLastPartitionLandmarks( std::vector<vector_uint> &landmarksMembership ) const; 00222 00223 /** For testing only: returns the partitioning as "getLastPartitionLandmarks" but as if a fixed-size submaps (size K) were have been used. 00224 */ 00225 void getLastPartitionLandmarksAsIfFixedSubmaps( size_t K, std::vector<vector_uint> &landmarksMembership ); 00226 00227 00228 /** Computes the ratio of the missing information matrix elements which are ignored under a certain partitioning of the landmarks. 00229 * \sa getLastPartitionLandmarks, getLastPartitionLandmarksAsIfFixedSubmaps 00230 */ 00231 double computeOffDiagonalBlocksApproximationError( const std::vector<vector_uint> &landmarksMembership ) const; 00232 00233 00234 /** The partitioning of the entire map is recomputed again. 00235 * Only when options.doPartitioningExperiment = true. 00236 * This can be used after changing the parameters of the partitioning method. 00237 * After this method, you can call getLastPartitionLandmarks. 00238 * \sa getLastPartitionLandmarks 00239 */ 00240 void reconsiderPartitionsNow(); 00241 00242 00243 /** Provides access to the parameters of the map partitioning algorithm. 00244 */ 00245 CIncrementalMapPartitioner::TOptions * mapPartitionOptions() 00246 { 00247 return &mapPartitioner.options; 00248 } 00249 00250 /** Save the current state of the filter (robot pose & map) to a MATLAB script which displays all the elements in 2D 00251 */ 00252 void saveMapAndPath2DRepresentationAsMATLABFile( 00253 const std::string &fil, 00254 float stdCount=3.0f, 00255 const std::string &styleLandmarks = std::string("b"), 00256 const std::string &stylePath = std::string("r"), 00257 const std::string &styleRobot = std::string("r") ) const; 00258 00259 00260 00261 protected: 00262 00263 /** @name Virtual methods for Kalman Filter implementation 00264 @{ 00265 */ 00266 00267 /** Must return the action vector u. 00268 * \param out_u The action vector which will be passed to OnTransitionModel 00269 */ 00270 void OnGetAction( KFArray_ACT &out_u ); 00271 00272 /** Implements the transition model \f$ \hat{x}_{k|k-1} = f( \hat{x}_{k-1|k-1}, u_k ) \f$ 00273 * \param in_u The vector returned by OnGetAction. 00274 * \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$ . 00275 * \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 00276 */ 00277 void OnTransitionModel( 00278 const KFArray_ACT &in_u, 00279 KFArray_VEH &inout_x, 00280 bool &out_skipPrediction 00281 ); 00282 00283 /** Implements the transition Jacobian \f$ \frac{\partial f}{\partial x} \f$ 00284 * \param out_F Must return the Jacobian. 00285 * 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). 00286 */ 00287 void OnTransitionJacobian( KFMatrix_VxV &out_F ); 00288 00289 /** Implements the transition noise covariance \f$ Q_k \f$ 00290 * \param out_Q Must return the covariance matrix. 00291 * The returned matrix must be of the same size than the jacobian from OnTransitionJacobian 00292 */ 00293 void OnTransitionNoise( KFMatrix_VxV &out_Q ); 00294 00295 /** 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. 00296 * 00297 * \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. 00298 * \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. 00299 * \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". 00300 * \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. 00301 * 00302 * This method will be called just once for each complete KF iteration. 00303 * \note It is assumed that the observations are independent, i.e. there are NO cross-covariances between them. 00304 */ 00305 void OnGetObservationsAndDataAssociation( 00306 std::vector<KFArray_OBS> &out_z, 00307 vector_int &out_data_association, 00308 const vector<KFArray_OBS> &in_all_predictions, 00309 const KFMatrix &in_S, 00310 const vector_size_t &in_lm_indices_in_S, 00311 const KFMatrix_OxO &in_R 00312 ); 00313 00314 void OnObservationModel( 00315 const vector_size_t &idx_landmarks_to_predict, 00316 std::vector<KFArray_OBS> &out_predictions 00317 ); 00318 00319 /** Implements the observation Jacobians \f$ \frac{\partial h_i}{\partial x} \f$ and (when applicable) \f$ \frac{\partial h_i}{\partial y_i} \f$. 00320 * \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. 00321 * \param Hx The output Jacobian \f$ \frac{\partial h_i}{\partial x} \f$. 00322 * \param Hy The output Jacobian \f$ \frac{\partial h_i}{\partial y_i} \f$. 00323 */ 00324 void OnObservationJacobians( 00325 const size_t &idx_landmark_to_predict, 00326 KFMatrix_OxV &Hx, 00327 KFMatrix_OxF &Hy 00328 ); 00329 00330 /** Computes A=A-B, which may need to be re-implemented depending on the topology of the individual scalar components (eg, angles). 00331 */ 00332 void OnSubstractObservationVectors(KFArray_OBS &A, const KFArray_OBS &B); 00333 00334 /** Return the observation NOISE covariance matrix, that is, the model of the Gaussian additive noise of the sensor. 00335 * \param out_R The noise covariance matrix. It might be non diagonal, but it'll usually be. 00336 */ 00337 void OnGetObservationNoise(KFMatrix_OxO &out_R); 00338 00339 /** This will be called before OnGetObservationsAndDataAssociation to allow the application to reduce the number of covariance landmark predictions to be made. 00340 * For example, features which are known to be "out of sight" shouldn't be added to the output list to speed up the calculations. 00341 * \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. 00342 * \param out_LM_indices_to_predict The list of landmark indices in the map [0,getNumberOfLandmarksInTheMap()-1] that should be predicted. 00343 * \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. 00344 * \sa OnGetObservations, OnDataAssociation 00345 */ 00346 void OnPreComputingPredictions( 00347 const vector<KFArray_OBS> &in_all_prediction_means, 00348 vector_size_t &out_LM_indices_to_predict ); 00349 00350 /** If applicable to the given problem, this method implements the inverse observation model needed to extend the "map" with a new "element". 00351 * \param in_z The observation vector whose inverse sensor model is to be computed. This is actually one of the vector<> returned by OnGetObservations(). 00352 * \param out_yn The F-length vector with the inverse observation model \f$ y_n=y(x,z_n) \f$. 00353 * \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$. 00354 * \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$. 00355 * 00356 * - O: OBS_SIZE 00357 * - V: VEH_SIZE 00358 * - F: FEAT_SIZE 00359 * 00360 * \note OnNewLandmarkAddedToMap will be also called after calling this method if a landmark is actually being added to the map. 00361 */ 00362 void OnInverseObservationModel( 00363 const KFArray_OBS & in_z, 00364 KFArray_FEAT & out_yn, 00365 KFMatrix_FxV & out_dyn_dxv, 00366 KFMatrix_FxO & out_dyn_dhn ); 00367 00368 /** If applicable to the given problem, do here any special handling of adding a new landmark to the map. 00369 * \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. 00370 * \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. 00371 * \sa OnInverseObservationModel 00372 */ 00373 void OnNewLandmarkAddedToMap( 00374 const size_t in_obsIdx, 00375 const size_t in_idxNewFeat ); 00376 00377 00378 /** 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. 00379 */ 00380 void OnNormalizeStateVector(); 00381 00382 /** @} 00383 */ 00384 00385 /** Set up by processActionObservation 00386 */ 00387 CActionCollectionPtr m_action; 00388 00389 /** Set up by processActionObservation 00390 */ 00391 CSensoryFramePtr m_SF; 00392 00393 /** The mapping between landmark IDs and indexes in the Pkk cov. matrix: 00394 */ 00395 std::map<CLandmark::TLandmarkID,unsigned int> m_IDs; 00396 00397 /** The mapping between indexes in the Pkk cov. matrix and landmark IDs: 00398 */ 00399 std::map<unsigned int,CLandmark::TLandmarkID> m_IDs_inverse; 00400 00401 00402 /** Used for map partitioning experiments 00403 */ 00404 CIncrementalMapPartitioner mapPartitioner; 00405 00406 /** The sequence of all the observations and the robot path (kept for debugging, statistics,etc) 00407 */ 00408 CSensFrameProbSequence m_SFs; 00409 00410 std::vector<vector_uint> m_lastPartitionSet; 00411 00412 TDataAssocInfo m_last_data_association; //!< Last data association 00413 00414 }; // end class 00415 } // End of namespace 00416 } // End of namespace 00417 00418 #endif
| Page generated by Doxygen 1.6.2 for MRPT 0.8.1 SVN:exported at Mon Feb 15 22:01:07 UTC 2010 |
