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