00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef PF_implementations_data_H
00029 #define PF_implementations_data_H
00030
00031 #include <mrpt/slam/CActionRobotMovement2D.h>
00032 #include <mrpt/bayes/CParticleFilterData.h>
00033 #include <mrpt/math/lightweight_geom_data.h>
00034 #include <mrpt/poses/CPose3D.h>
00035 #include <mrpt/poses/CPose3DPDFGaussian.h>
00036 #include <mrpt/poses/CPoseRandomSampler.h>
00037
00038 namespace mrpt
00039 {
00040 namespace slam
00041 {
00042 using namespace std;
00043 using namespace mrpt::poses;
00044 using namespace mrpt::bayes;
00045 using namespace mrpt::math;
00046
00047
00048
00049 template <class PARTICLETYPE, class BINTYPE>
00050 void KLF_loadBinFromParticle(
00051 BINTYPE &outBin,
00052 const TKLDParams &opts,
00053 const PARTICLETYPE *currentParticleValue = NULL,
00054 const TPose3D *newPoseToBeInserted = NULL );
00055
00056
00057
00058
00059 template <class PARTICLE_TYPE>
00060 class PF_implementation :
00061 public mrpt::bayes::CParticleFilterData<PARTICLE_TYPE>,
00062 public mrpt::bayes::CParticleFilterCapable
00063 {
00064 public:
00065 PF_implementation() :
00066 m_accumRobotMovement2DIsValid(false),
00067 m_accumRobotMovement3DIsValid(false)
00068 {
00069 }
00070
00071 typedef mrpt::bayes::CParticleFilterData<PARTICLE_TYPE> BASE;
00072
00073 protected:
00074
00075
00076
00077
00078 CActionRobotMovement2D m_accumRobotMovement2D;
00079 bool m_accumRobotMovement2DIsValid;
00080 CPose3DPDFGaussian m_accumRobotMovement3D;
00081 bool m_accumRobotMovement3DIsValid;
00082
00083 CPoseRandomSampler m_movementDrawer;
00084 mutable vector_double m_pfAuxiliaryPFOptimal_estimatedProb;
00085 mutable vector_double m_pfAuxiliaryPFStandard_estimatedProb;
00086 mutable vector_double m_pfAuxiliaryPFOptimal_maxLikelihood;
00087 mutable std::vector<TPose3D> m_pfAuxiliaryPFOptimal_maxLikDrawnMovement;
00088 std::vector<bool> m_pfAuxiliaryPFOptimal_maxLikMovementDrawHasBeenUsed;
00089
00090
00091
00092
00093
00094
00095
00096 template <class BINTYPE>
00097 static double PF_SLAM_particlesEvaluator_AuxPFStandard(
00098 const CParticleFilter::TParticleFilterOptions &PF_options,
00099 const CParticleFilterCapable *obj,
00100 size_t index,
00101 const void * action,
00102 const void * observation );
00103
00104 template <class BINTYPE>
00105 static double PF_SLAM_particlesEvaluator_AuxPFOptimal(
00106 const CParticleFilter::TParticleFilterOptions &PF_options,
00107 const CParticleFilterCapable *obj,
00108 size_t index,
00109 const void *action,
00110 const void *observation );
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130 template <class BINTYPE>
00131 void PF_SLAM_implementation_pfAuxiliaryPFOptimal(
00132 const CActionCollection * actions,
00133 const CSensoryFrame * sf,
00134 const CParticleFilter::TParticleFilterOptions &PF_options,
00135 const TKLDParams &KLD_options);
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 template <class BINTYPE>
00148 void PF_SLAM_implementation_pfAuxiliaryPFStandard(
00149 const CActionCollection * actions,
00150 const CSensoryFrame * sf,
00151 const CParticleFilter::TParticleFilterOptions &PF_options,
00152 const TKLDParams &KLD_options);
00153
00154
00155
00156
00157
00158
00159
00160 template <class BINTYPE>
00161 void PF_SLAM_implementation_pfStandardProposal(
00162 const CActionCollection * actions,
00163 const CSensoryFrame * sf,
00164 const CParticleFilter::TParticleFilterOptions &PF_options,
00165 const TKLDParams &KLD_options);
00166
00167
00168
00169
00170
00171
00172
00173
00174 virtual const TPose3D * getLastPose(const size_t i) const = 0;
00175
00176 virtual void PF_SLAM_implementation_custom_update_particle_with_new_pose(
00177 PARTICLE_TYPE *particleData,
00178 const TPose3D &newPose) const = 0;
00179
00180
00181
00182
00183
00184
00185
00186 virtual void PF_SLAM_implementation_replaceByNewParticleSet(
00187 typename BASE::CParticleList &old_particles,
00188 const vector<TPose3D> &newParticles,
00189 const vector_double &newParticlesWeight,
00190 const vector<size_t> &newParticlesDerivedFromIdx ) const
00191 {
00192
00193
00194
00195
00196
00197 const size_t N = newParticles.size();
00198 typename BASE::CParticleList newParticlesArray(N);
00199
00200
00201
00202 std::vector<bool> oldParticleAlreadyCopied(old_particles.size(),false);
00203
00204 size_t i;
00205 typename BASE::CParticleList::iterator newPartIt;
00206 for (newPartIt=newParticlesArray.begin(),i=0;newPartIt!=newParticlesArray.end();newPartIt++,i++)
00207 {
00208
00209 newPartIt->log_w = newParticlesWeight[i];
00210
00211
00212 PARTICLE_TYPE *newPartData;
00213 if (!oldParticleAlreadyCopied[newParticlesDerivedFromIdx[i]])
00214 {
00215
00216 newPartData = old_particles[ newParticlesDerivedFromIdx[i] ].d;
00217 oldParticleAlreadyCopied[newParticlesDerivedFromIdx[i]] = true;
00218 }
00219 else
00220 {
00221
00222 newPartData = new PARTICLE_TYPE( *old_particles[ newParticlesDerivedFromIdx[i] ].d );
00223 }
00224
00225 newPartIt->d = newPartData;
00226 }
00227
00228
00229
00230
00231 for (newPartIt=newParticlesArray.begin(),i=0;i<N;++newPartIt,++i)
00232 PF_SLAM_implementation_custom_update_particle_with_new_pose( newPartIt->d, newParticles[i] );
00233
00234
00235 for (size_t i=0;i<old_particles.size();i++)
00236 if (!oldParticleAlreadyCopied[i])
00237 mrpt::utils::delete_safe( old_particles[ i ].d );
00238
00239
00240 old_particles.resize( newParticlesArray.size() );
00241 typename BASE::CParticleList::iterator trgPartIt;
00242 for (newPartIt=newParticlesArray.begin(),trgPartIt=old_particles.begin(); newPartIt!=newParticlesArray.end(); ++newPartIt, ++trgPartIt )
00243 {
00244 trgPartIt->log_w = newPartIt->log_w;
00245 trgPartIt->d = newPartIt->d;
00246 }
00247 }
00248
00249
00250
00251 virtual bool PF_SLAM_implementation_doWeHaveValidObservations(
00252 const typename BASE::CParticleList &particles,
00253 const CSensoryFrame *sf) const
00254 {
00255 return true;
00256 }
00257
00258
00259 virtual bool PF_SLAM_implementation_skipRobotMovement() const
00260 {
00261 return false;
00262 }
00263
00264
00265 virtual double PF_SLAM_computeObservationLikelihoodForParticle(
00266 const CParticleFilter::TParticleFilterOptions &PF_options,
00267 const size_t particleIndexForMap,
00268 const CSensoryFrame &observation,
00269 const CPose3D &x ) const = 0;
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 template <class BINTYPE>
00280 bool PF_SLAM_implementation_gatherActionsCheckBothActObs(
00281 const CActionCollection * actions,
00282 const CSensoryFrame * sf );
00283
00284 private:
00285
00286 template <class BINTYPE>
00287 void PF_SLAM_implementation_pfAuxiliaryPFStandardAndOptimal(
00288 const CActionCollection * actions,
00289 const CSensoryFrame * sf,
00290 const CParticleFilter::TParticleFilterOptions &PF_options,
00291 const TKLDParams &KLD_options,
00292 const bool USE_OPTIMAL_SAMPLING );
00293
00294 template <class BINTYPE>
00295 void PF_SLAM_aux_perform_one_rejection_sampling_step(
00296 const bool USE_OPTIMAL_SAMPLING,
00297 const bool doResample,
00298 const double maxMeanLik,
00299 size_t k,
00300 const CSensoryFrame * sf,
00301 const CParticleFilter::TParticleFilterOptions &PF_options,
00302 CPose3D & out_newPose,
00303 double & out_newParticleLogWeight);
00304
00305
00306 };
00307 }
00308 }
00309
00310 #endif