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