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 00029 #ifndef CGasConcentrationGridMap2D_H 00030 #define CGasConcentrationGridMap2D_H 00031 00032 #include <mrpt/utils/CDynamicGrid.h> 00033 #include <mrpt/utils/CSerializable.h> 00034 #include <mrpt/math/CMatrixD.h> 00035 #include <mrpt/utils/CLoadableOptions.h> 00036 #include <mrpt/slam/CMetricMap.h> 00037 #include <mrpt/slam/CObservationGasSensors.h> 00038 00039 #include <mrpt/maps/link_pragmas.h> 00040 00041 namespace mrpt 00042 { 00043 namespace poses { class CPose2D; } 00044 00045 namespace slam 00046 { 00047 using namespace mrpt::utils; 00048 using namespace mrpt::poses; 00049 using namespace mrpt::math; 00050 class CObservation; 00051 00052 DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE( CGasConcentrationGridMap2D , CMetricMap, MAPS_IMPEXP ) 00053 00054 /** The contents of each cell in a CGasConcentrationGridMap2D map. 00055 **/ 00056 struct MAPS_IMPEXP TGasConcentrationCell 00057 { 00058 /** Constructor 00059 */ 00060 TGasConcentrationCell( 00061 float Mean = 0.5f, 00062 float Std = 0, 00063 float W = 1e-20f, 00064 float Wr = 0 ) : mean(Mean), std(Std),w(W),wr(Wr) 00065 { 00066 } 00067 00068 /** The mean and std values of the gas concentration for this cell. 00069 */ 00070 float mean, std; 00071 00072 /** The accumulated weight and weighted-readings (See Achim's references) 00073 */ 00074 float w,wr; 00075 }; 00076 00077 /** The content of each m_lastObservations in KF3_deconv estimation 00078 */ 00079 struct MAPS_IMPEXP TdataMap 00080 { 00081 float reading; 00082 mrpt::system::TTimeStamp timestamp; 00083 float k; 00084 CPose3D sensorPose; 00085 float estimation; 00086 float reading_filtered; 00087 float speed; 00088 }; 00089 00090 /** CGasConcentrationGridMap2D represents a PDF of gas concentrations over a 2D area. 00091 **/ 00092 class MAPS_IMPEXP CGasConcentrationGridMap2D : public CMetricMap, public utils::CDynamicGrid<TGasConcentrationCell> 00093 { 00094 typedef utils::CDynamicGrid<TGasConcentrationCell> BASE; 00095 00096 // This must be added to any CSerializable derived class: 00097 DEFINE_SERIALIZABLE( CGasConcentrationGridMap2D ) 00098 public: 00099 00100 /** Calls the base CMetricMap::clear 00101 * Declared here to avoid ambiguity between the two clear() in both base classes. 00102 */ 00103 inline void clear() { CMetricMap::clear(); } 00104 00105 float cell2float(const TGasConcentrationCell& c) const 00106 { 00107 return c.mean; 00108 } 00109 00110 00111 /** The type of map representation to be used. 00112 */ 00113 enum TMapRepresentation 00114 { 00115 mrAchim = 0, 00116 mrKalmanFilter, 00117 mrKalmanApproximate 00118 }; 00119 00120 00121 /** Constructor 00122 */ 00123 CGasConcentrationGridMap2D( 00124 TMapRepresentation mapType = mrAchim, 00125 float x_min = -2, 00126 float x_max = 2, 00127 float y_min = -2, 00128 float y_max = 2, 00129 float resolution = 0.1 00130 ); 00131 00132 /** Destructor */ 00133 virtual ~CGasConcentrationGridMap2D(); 00134 00135 /** Returns true if the map is empty/no observation has been inserted. 00136 */ 00137 bool isEmpty() const; 00138 00139 00140 /** Computes the likelihood that a given observation was taken from a given pose in the world being modeled with this map. 00141 * 00142 * \param takenFrom The robot's pose the observation is supposed to be taken from. 00143 * \param obs The observation. 00144 * \return This method returns a likelihood in the range [0,1]. 00145 * 00146 * \sa Used in particle filter algorithms, see: CMultiMetricMapPDF::update 00147 */ 00148 double computeObservationLikelihood( const CObservation *obs, const CPose3D &takenFrom ); 00149 00150 /** Save the current map as a graphical file (BMP,PNG,...). 00151 * The file format will be derived from the file extension (see CImage::saveToFile ) 00152 * It depends on the map representation model: 00153 * mrAchim: Each pixel is the ratio \f$ \sum{\frac{wR}{w}} \f$ 00154 * mrKalmanFilter: Each pixel is the mean value of the Gaussian that represents each cell. 00155 * mrInformationFilter: Id. 00156 */ 00157 void saveAsBitmapFile(const std::string &filName) const; 00158 00159 00160 /** Parameters related with inserting observations into the map: 00161 */ 00162 struct MAPS_IMPEXP TInsertionOptions : public utils::CLoadableOptions 00163 { 00164 /** Default values loader: 00165 */ 00166 TInsertionOptions(); 00167 00168 /** See utils::CLoadableOptions 00169 */ 00170 void loadFromConfigFile( 00171 const mrpt::utils::CConfigFileBase &source, 00172 const std::string §ion); 00173 00174 /** See utils::CLoadableOptions 00175 */ 00176 void dumpToTextStream(CStream &out) const; 00177 00178 /** The sigma of the "Parzen"-kernel Gaussian 00179 */ 00180 float sigma; 00181 00182 /** The cutoff radius for updating cells. 00183 */ 00184 float cutoffRadius; 00185 00186 /** Limits for normalization of sensor readings. 00187 */ 00188 float R_min,R_max; 00189 00190 /** [KF model] The "sigma" for the initial covariance value between cells (in meters). 00191 */ 00192 float KF_covSigma; 00193 00194 /** [KF model] The initial standard deviation of each cell's concentration (will be stored both at each cell's structure and in the covariance matrix as variances in the diagonal) (in normalized concentration units). 00195 */ 00196 float KF_initialCellStd; 00197 00198 /** [KF model] The sensor model noise (in normalized concentration units). 00199 */ 00200 float KF_observationModelNoise; 00201 00202 /** [KF model] The default value for the mean of cells' concentration. 00203 */ 00204 float KF_defaultCellMeanValue; 00205 00206 /** [KF2 algorithm] The size of the window of neighbor cells. */ 00207 uint16_t KF_W_size; 00208 00209 /** [KF3 algortihm] The sensor type for the gas concentration map (0x0000 ->mean of all installed sensors, 0x2600, 0x6810, ...) 00210 */ 00211 uint16_t KF_sensorType; 00212 00213 /** Tau values for the rise (tauR) and decay (tauD) sensor's phases. 00214 */ 00215 float tauR; 00216 float tauD; 00217 00218 /** [KF3 algortihm] The last N GasObservations, used for the Kalman Filter with deconvolution estimation. 00219 */ 00220 TdataMap new_Obs, new_ANS; 00221 std::vector<TdataMap> m_lastObservations; 00222 std::vector<TdataMap> antiNoise_window; 00223 00224 /** [KF3 algortihm] The number of observations to keep in m_lastObservations 00225 */ 00226 uint16_t lastObservations_size; 00227 00228 /** [KF3 algortihm] The number of observations used to reduce noise on signal. 00229 */ 00230 size_t winNoise_size; 00231 00232 /** [KF3 algortihm] The decimate frecuency applied after noise filtering 00233 */ 00234 uint16_t decimate_value; 00235 00236 /** [KF3 algortihm] Measured values of K= 1/tauD for different volatile concentrations 00237 */ 00238 vector_float tauD_concentration; 00239 vector_float tauD_value; 00240 00241 /** [KF3 algortihm] Measured values of the delay (memory effect) for different robot speeds 00242 */ 00243 vector_float memory_speed; 00244 vector_float memory_delay; 00245 00246 /** [KF3 algortihm] id for the enose used to generate this map (must be < gasGrid_count) 00247 */ 00248 uint16_t enose_id; 00249 00250 /** [useMOSmodel] If true save generated gas map as a log file 00251 */ 00252 bool save_maplog; 00253 00254 /** [useMOSmodel] If true use MOS model before map algorithm 00255 */ 00256 bool useMOSmodel; 00257 00258 } insertionOptions; 00259 00260 /** Changes the size of the grid, maintaining previous contents. 00261 * \sa setSize 00262 */ 00263 void resize( float new_x_min, 00264 float new_x_max, 00265 float new_y_min, 00266 float new_y_max, 00267 const TGasConcentrationCell& defaultValueNewCells, 00268 float additionalMarginMeters = 1.0f ); 00269 00270 /** Computes the ratio in [0,1] of correspondences between "this" and the "otherMap" map, whose 6D pose relative to "this" is "otherMapPose" 00271 * In the case of a multi-metric map, this returns the average between the maps. This method always return 0 for grid maps. 00272 * \param otherMap [IN] The other map to compute the matching with. 00273 * \param otherMapPose [IN] The 6D pose of the other map as seen from "this". 00274 * \param minDistForCorr [IN] The minimum distance between 2 non-probabilistic map elements for counting them as a correspondence. 00275 * \param minMahaDistForCorr [IN] The minimum Mahalanobis distance between 2 probabilistic map elements for counting them as a correspondence. 00276 * 00277 * \return The matching ratio [0,1] 00278 * \sa computeMatchingWith2D 00279 */ 00280 float compute3DMatchingRatio( 00281 const CMetricMap *otherMap, 00282 const CPose3D &otherMapPose, 00283 float minDistForCorr = 0.10f, 00284 float minMahaDistForCorr = 2.0f 00285 ) const; 00286 00287 00288 /** The implementation in this class just calls all the corresponding method of the contained metric maps. 00289 */ 00290 void saveMetricMapRepresentationToFile( 00291 const std::string &filNamePrefix 00292 ) const; 00293 00294 /** Save a matlab ".m" file which represents as 3D surfaces the mean and a given confidence level for the concentration of each cell. 00295 * This method can only be called in a KF map model. 00296 */ 00297 void saveAsMatlab3DGraph(const std::string &filName) const; 00298 00299 /** Returns a 3D object representing the map. 00300 */ 00301 void getAs3DObject ( mrpt::opengl::CSetOfObjectsPtr &outObj ) const; 00302 00303 /** This method is called at the end of each "prediction-update-map insertion" cycle within "mrpt::slam::CMetricMapBuilderRBPF::processActionObservation". 00304 * This method should normally do nothing, but in some cases can be used to free auxiliary cached variables. 00305 */ 00306 void auxParticleFilterCleanUp(); 00307 00308 /** Return the type of the gas distribution map, according to parameters passed on construction. 00309 */ 00310 TMapRepresentation getMapType(); 00311 00312 /** Returns the prediction of the measurement at some (x,y) coordinates, and its certainty (in the form of the expected variance). 00313 * This methods is implemented differently for the different gas map types. 00314 */ 00315 void predictMeasurement( 00316 const double &x, 00317 const double &y, 00318 double &out_predict_response, 00319 double &out_predict_response_variance ); 00320 00321 /** Return the mean and covariance vector of the full Kalman filter estimate (works for all KF-based methods). */ 00322 void getMeanAndCov( vector_double &out_means, CMatrixDouble &out_cov) const; 00323 00324 protected: 00325 00326 /** [useMOSmodel] Ofstream to save to file option "save_maplog" 00327 */ 00328 std::ofstream *m_debug_dump; 00329 00330 /** [useMOSmodel] Decimate value for oversampled enose readings 00331 */ 00332 uint16_t decimate_count; 00333 00334 /** [useMOSmodel] To force e-nose samples to have fixed time increments 00335 */ 00336 double fixed_incT; 00337 bool first_incT; 00338 00339 /** The map representation type of this map. 00340 */ 00341 TMapRepresentation m_mapType; 00342 00343 00344 /** The whole covariance matrix, used for the Kalman Filter map representation. 00345 */ 00346 CMatrixD m_cov; 00347 00348 00349 /** The implementation of "insertObservation" for the Achim's map model. 00350 * \param normReading Is a [0,1] normalized concentration reading. 00351 * \param sensorPose Is the sensor pose 00352 */ 00353 void insertObservation_Achim( 00354 float normReading, 00355 const CPose3D &sensorPose ); 00356 00357 /** The implementation of "insertObservation" for the (whole) Kalman Filter map model. 00358 * \param normReading Is a [0,1] normalized concentration reading. 00359 * \param sensorPose Is the sensor pose 00360 */ 00361 void insertObservation_KF( 00362 float normReading, 00363 const CPose3D &sensorPose ); 00364 00365 /** The implementation of "insertObservation" for the Efficient Kalman Filter map model. 00366 * \param normReading Is a [0,1] normalized concentration reading. 00367 * \param sensorPose Is the sensor pose 00368 */ 00369 void insertObservation_KF2( 00370 float normReading, 00371 const CPose3D &sensorPose ); 00372 00373 00374 /** Estimates the gas concentration based on readings and sensor model 00375 */ 00376 void CGasConcentration_estimation ( 00377 float reading, 00378 const CPose3D &sensorPose, 00379 const mrpt::system::TTimeStamp timestamp); 00380 00381 /** Reduce noise by averaging with a mobile window 00382 */ 00383 void noise_filtering ( 00384 float reading, 00385 const CPose3D &sensorPose, 00386 const mrpt::system::TTimeStamp timestamp ); 00387 00388 /** Save the GAS_MAP generated into a log file for offline representation 00389 */ 00390 void save_log_map( 00391 const mrpt::system::TTimeStamp timestamp, 00392 const float reading, 00393 const float estimation, 00394 const float k, 00395 const double yaw, 00396 const float speed); 00397 00398 /** Computes the average cell concentration, or 0 if it has never been observed: 00399 */ 00400 static float computeAchimCellValue (const TGasConcentrationCell *cell ); 00401 00402 00403 /** The compressed band diagonal matrix for the KF2 implementation. 00404 * The format is a Nx(W^2+2W+1) matrix, one row per cell in the grid map with the 00405 * cross-covariances between each cell and half of the window around it in the grid. 00406 */ 00407 CMatrixD m_stackedCov; 00408 00409 mutable bool m_hasToRecoverMeanAndCov; //!< Only for the KF2 implementation. 00410 00411 /** In the KF2 implementation, takes the auxiliary matrices and from them update the cells' mean and std values. 00412 * \sa m_hasToRecoverMeanAndCov 00413 */ 00414 void recoverMeanAndCov() const; 00415 00416 /** Erase all the contents of the map 00417 */ 00418 virtual void internal_clear(); 00419 00420 /** Insert the observation information into this map. This method must be implemented 00421 * in derived classes. 00422 * \param obs The observation 00423 * \param robotPose The 3D pose of the robot mobile base in the map reference system, or NULL (default) if you want to use CPose2D(0,0,deg) 00424 * 00425 * \sa CObservation::insertObservationInto 00426 */ 00427 virtual bool internal_insertObservation( const CObservation *obs, const CPose3D *robotPose = NULL ); 00428 00429 00430 }; 00431 00432 00433 } // End of namespace 00434 } // End of namespace 00435 00436 #endif
| Page generated by Doxygen 1.7.1 for MRPT 0.9.1 SVN:exported at Fri Aug 6 10:31:35 UTC 2010 |
