Main MRPT website > C++ reference
MRPT logo
PF_implementations_data.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2014, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef PF_implementations_data_H
10 #define PF_implementations_data_H
11 
16 #include <mrpt/poses/CPose3D.h>
19 #include <mrpt/slam/TKLDParams.h>
20 
21 #include <mrpt/slam/link_pragmas.h>
22 
23 namespace mrpt
24 {
25  namespace slam
26  {
27  using namespace std;
28  using namespace mrpt::poses;
29  using namespace mrpt::bayes;
30  using namespace mrpt::math;
31 
32 
33  // Frwd decl:
34  template <class PARTICLETYPE, class BINTYPE>
36  BINTYPE &outBin,
37  const TKLDParams &opts,
38  const PARTICLETYPE *currentParticleValue = NULL,
39  const TPose3D *newPoseToBeInserted = NULL );
40 
41 
42  /** A set of common data shared by PF implementations for both SLAM and localization
43  * \ingroup mrpt_slam_grp
44  */
45  template <class PARTICLE_TYPE, class MYSELF>
47  {
48  public:
50  m_accumRobotMovement2DIsValid(false),
51  m_accumRobotMovement3DIsValid(false)
52  {
53  }
54  protected:
55  /** \name Data members and methods used by generic PF implementations
56  @{ */
57 
62 
63  CPoseRandomSampler m_movementDrawer; //!< Used in al PF implementations. \sa PF_SLAM_implementation_gatherActionsCheckBothActObs
64  mutable CVectorDouble m_pfAuxiliaryPFOptimal_estimatedProb; //!< Auxiliary variable used in the "pfAuxiliaryPFOptimal" algorithm.
65  mutable CVectorDouble m_pfAuxiliaryPFStandard_estimatedProb; //!< Auxiliary variable used in the "pfAuxiliaryPFStandard" algorithm.
66  mutable CVectorDouble m_pfAuxiliaryPFOptimal_maxLikelihood; //!< Auxiliary variable used in the "pfAuxiliaryPFOptimal" algorithm.
67  mutable std::vector<TPose3D> m_pfAuxiliaryPFOptimal_maxLikDrawnMovement; //!< Auxiliary variable used in the "pfAuxiliaryPFOptimal" algorithm.
69 
70  /** Compute w[i]·p(z_t | mu_t^i), with mu_t^i being
71  * the mean of the new robot pose
72  *
73  * \param action MUST be a "const CPose3D*"
74  * \param observation MUST be a "const CSensoryFrame*"
75  */
76  template <class BINTYPE> // Template arg. actually not used, just to allow giving the definition in another file later on
77  static double PF_SLAM_particlesEvaluator_AuxPFStandard(
79  const CParticleFilterCapable *obj,
80  size_t index,
81  const void * action,
82  const void * observation );
83 
84  template <class BINTYPE> // Template arg. actually not used, just to allow giving the definition in another file later on
85  static double PF_SLAM_particlesEvaluator_AuxPFOptimal(
87  const CParticleFilterCapable *obj,
88  size_t index,
89  const void *action,
90  const void *observation );
91 
92  /** @} */
93 
94  /** \name The generic PF implementations for localization & SLAM.
95  @{ */
96 
97  /** A generic implementation of the PF method "prediction_and_update_pfAuxiliaryPFOptimal" (optimal sampling with rejection sampling approximation),
98  * common to both localization and mapping.
99  *
100  * - BINTYPE: TPoseBin or whatever to discretize the sample space for KLD-sampling.
101  *
102  * This method implements optimal sampling with a rejection sampling-based approximation of the true posterior.
103  * For details, see the papers:
104  *
105  * J.-L. Blanco, J. González, and J.-A. Fernández-Madrigal,
106  * "An Optimal Filtering Algorithm for Non-Parametric Observation Models in
107  * Robot Localization," in Proc. IEEE International Conference on Robotics
108  * and Automation (ICRA'08), 2008, pp. 461–466.
109  */
110  template <class BINTYPE>
111  void PF_SLAM_implementation_pfAuxiliaryPFOptimal(
112  const CActionCollection * actions,
113  const CSensoryFrame * sf,
114  const CParticleFilter::TParticleFilterOptions &PF_options,
115  const TKLDParams &KLD_options);
116 
117  /** A generic implementation of the PF method "prediction_and_update_pfAuxiliaryPFStandard" (Auxiliary particle filter with the standard proposal),
118  * common to both localization and mapping.
119  *
120  * - BINTYPE: TPoseBin or whatever to discretize the sample space for KLD-sampling.
121  *
122  * This method is described in the paper:
123  * Pitt, M.K.; Shephard, N. (1999). "Filtering Via Simulation: Auxiliary Particle Filters".
124  * Journal of the American Statistical Association 94 (446): 590–591. doi:10.2307/2670179.
125  *
126  */
127  template <class BINTYPE>
128  void PF_SLAM_implementation_pfAuxiliaryPFStandard(
129  const CActionCollection * actions,
130  const CSensoryFrame * sf,
131  const CParticleFilter::TParticleFilterOptions &PF_options,
132  const TKLDParams &KLD_options);
133 
134 
135  /** A generic implementation of the PF method "pfStandardProposal" (standard proposal distribution, that is, a simple SIS particle filter),
136  * common to both localization and mapping.
137  *
138  * - BINTYPE: TPoseBin or whatever to discretize the sample space for KLD-sampling.
139  */
140  template <class BINTYPE>
141  void PF_SLAM_implementation_pfStandardProposal(
142  const CActionCollection * actions,
143  const CSensoryFrame * sf,
144  const CParticleFilter::TParticleFilterOptions &PF_options,
145  const TKLDParams &KLD_options);
146 
147  /** @} */
148 
149 
150  public:
151  /** \name Virtual methods that the PF_implementations assume exist.
152  @{ */
153 
154  /** Return a pointer to the last robot pose in the i'th particle (or NULL if it's a path and it's empty). */
155  virtual const TPose3D * getLastPose(const size_t i) const = 0;
156 
157  virtual void PF_SLAM_implementation_custom_update_particle_with_new_pose(
158  PARTICLE_TYPE *particleData,
159  const TPose3D &newPose) const = 0;
160 
161  /** This is the default algorithm to efficiently replace one old set of samples by another new set.
162  * The method uses pointers to make fast copies the first time each particle is duplicated, then
163  * makes real copies for the next ones.
164  *
165  * Note that more efficient specializations might exist for specific particle data structs.
166  */
169  const vector<TPose3D> &newParticles,
170  const vector<double> &newParticlesWeight,
171  const vector<size_t> &newParticlesDerivedFromIdx ) const
172  {
173  // ---------------------------------------------------------------------------------
174  // Substitute old by new particle set:
175  // Old are in "m_particles"
176  // New are in "newParticles", "newParticlesWeight","newParticlesDerivedFromIdx"
177  // ---------------------------------------------------------------------------------
178  const size_t N = newParticles.size();
179  typename MYSELF::CParticleList newParticlesArray(N);
180 
181  // For efficiency, just copy the "CParticleData" from the old particle into the
182  // new one, but this can be done only once:
183  std::vector<bool> oldParticleAlreadyCopied(old_particles.size(),false);
184 
185  size_t i;
186  typename MYSELF::CParticleList::iterator newPartIt;
187  for (newPartIt=newParticlesArray.begin(),i=0;newPartIt!=newParticlesArray.end();newPartIt++,i++)
188  {
189  // The weight:
190  newPartIt->log_w = newParticlesWeight[i];
191 
192  // The data (CParticleData):
193  PARTICLE_TYPE *newPartData;
194  if (!oldParticleAlreadyCopied[newParticlesDerivedFromIdx[i]])
195  {
196  // The first copy of this old particle:
197  newPartData = old_particles[ newParticlesDerivedFromIdx[i] ].d;
198  oldParticleAlreadyCopied[newParticlesDerivedFromIdx[i]] = true;
199  }
200  else
201  {
202  // Make a copy:
203  newPartData = new PARTICLE_TYPE( *old_particles[ newParticlesDerivedFromIdx[i] ].d );
204  }
205 
206  newPartIt->d = newPartData;
207  } // end for "newPartIt"
208 
209  // Now add the new robot pose to the paths:
210  // (this MUST be done after the above loop, separately):
211  // Update the particle with the new pose: this part is caller-dependant and must be implemented there:
212  for (newPartIt=newParticlesArray.begin(),i=0;i<N;++newPartIt,++i)
213  PF_SLAM_implementation_custom_update_particle_with_new_pose( newPartIt->d, newParticles[i] );
214 
215  // Free those old m_particles not being copied into the new ones:
216  for (size_t i=0;i<old_particles.size();i++)
217  if (!oldParticleAlreadyCopied[i])
218  mrpt::utils::delete_safe( old_particles[ i ].d );
219 
220  // Copy into "m_particles"
221  old_particles.resize( newParticlesArray.size() );
222  typename MYSELF::CParticleList::iterator trgPartIt;
223  for (newPartIt=newParticlesArray.begin(),trgPartIt=old_particles.begin(); newPartIt!=newParticlesArray.end(); ++newPartIt, ++trgPartIt )
224  {
225  trgPartIt->log_w = newPartIt->log_w;
226  trgPartIt->d = newPartIt->d;
227  }
228  } // end of PF_SLAM_implementation_replaceByNewParticleSet
229 
230 
231 
233  const typename CParticleFilterData<PARTICLE_TYPE>::CParticleList &particles,
234  const CSensoryFrame *sf) const
235  {
236  MRPT_UNUSED_PARAM(particles); MRPT_UNUSED_PARAM(sf);
237  return true; // By default, always process the SFs.
238  }
239 
240  /** Make a specialization if needed, eg. in the first step in SLAM. */
242  {
243  return false; // By default, always allow the robot to move!
244  }
245 
246  /** Evaluate the observation likelihood for one particle at a given location */
247  virtual double PF_SLAM_computeObservationLikelihoodForParticle(
248  const CParticleFilter::TParticleFilterOptions &PF_options,
249  const size_t particleIndexForMap,
250  const CSensoryFrame &observation,
251  const CPose3D &x ) const = 0;
252 
253  /** @} */
254 
255 
256  /** Auxiliary method called by PF implementations: return true if we have both action & observation,
257  * otherwise, return false AND accumulate the odometry so when we have an observation we didn't lose a thing.
258  * On return=true, the "m_movementDrawer" member is loaded and ready to draw samples of the increment of pose since last step.
259  * This method is smart enough to accumulate CActionRobotMovement2D or CActionRobotMovement3D, whatever comes in.
260  */
261  template <class BINTYPE> // Template arg. actually not used, just to allow giving the definition in another file later on
262  bool PF_SLAM_implementation_gatherActionsCheckBothActObs(
263  const CActionCollection * actions,
264  const CSensoryFrame * sf );
265 
266  private:
267  /** The shared implementation body of two PF methods: APF and Optimal-APF, depending on USE_OPTIMAL_SAMPLING */
268  template <class BINTYPE>
269  void PF_SLAM_implementation_pfAuxiliaryPFStandardAndOptimal(
270  const CActionCollection * actions,
271  const CSensoryFrame * sf,
272  const CParticleFilter::TParticleFilterOptions &PF_options,
273  const TKLDParams &KLD_options,
274  const bool USE_OPTIMAL_SAMPLING );
275 
276  template <class BINTYPE>
277  void PF_SLAM_aux_perform_one_rejection_sampling_step(
278  const bool USE_OPTIMAL_SAMPLING,
279  const bool doResample,
280  const double maxMeanLik,
281  size_t k, // The particle from the old set "m_particles[]"
282  const CSensoryFrame * sf,
283  const CParticleFilter::TParticleFilterOptions &PF_options,
284  CPose3D & out_newPose,
285  double & out_newParticleLogWeight);
286 
287 
288  }; // end PF_implementation
289  }
290 }
291 
292 #endif
The namespace for Bayesian filtering algorithm: different particle filters and Kalman filter algorith...
Scalar * iterator
Definition: eigen_plugins.h:23
std::vector< TPose3D > m_pfAuxiliaryPFOptimal_maxLikDrawnMovement
Auxiliary variable used in the "pfAuxiliaryPFOptimal" algorithm.
Option set for KLD algorithm.
Definition: TKLDParams.h:24
CVectorDouble m_pfAuxiliaryPFStandard_estimatedProb
Auxiliary variable used in the "pfAuxiliaryPFStandard" algorithm.
STL namespace.
CVectorDouble m_pfAuxiliaryPFOptimal_estimatedProb
Auxiliary variable used in the "pfAuxiliaryPFOptimal" algorithm.
CPoseRandomSampler m_movementDrawer
Used in al PF implementations.
Declares a class for storing a collection of robot actions.
A set of common data shared by PF implementations for both SLAM and localization. ...
This base provides a set of functions for maths stuff.
Definition: CArray.h:18
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
This virtual class defines the interface that any particles based PDF class must implement in order t...
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:17
CActionRobotMovement2D m_accumRobotMovement2D
virtual bool PF_SLAM_implementation_skipRobotMovement() const
Make a specialization if needed, eg.
CVectorDouble m_pfAuxiliaryPFOptimal_maxLikelihood
Auxiliary variable used in the "pfAuxiliaryPFOptimal" algorithm.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void delete_safe(T *&ptr)
Calls "delete" to free an object only if the pointer is not NULL, then set the pointer to NULL...
Represents a probabilistic 2D movement of the robot mobile base.
void KLF_loadBinFromParticle(BINTYPE &outBin, const TKLDParams &opts, const PARTICLETYPE *currentParticleValue=NULL, const TPose3D *newPoseToBeInserted=NULL)
std::deque< CParticleData > CParticleList
Use this type to refer to the list of particles m_particles.
Declares a class for storing a "sensory frame", a set of "observations" taken by the robot approximat...
Definition: CSensoryFrame.h:53
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:69
The configuration of a particle filter.
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
Declares a class that represents a Probability Density function (PDF) of a 3D pose ...
dynamic_vector< double > CVectorDouble
Column vector, like Eigen::MatrixXd, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:53
virtual bool PF_SLAM_implementation_doWeHaveValidObservations(const typename CParticleFilterData< PARTICLE_TYPE >::CParticleList &particles, const CSensoryFrame *sf) const
An efficient generator of random samples drawn from a given 2D (CPosePDF) or 3D (CPose3DPDF) pose pro...
virtual void PF_SLAM_implementation_replaceByNewParticleSet(typename CParticleFilterData< PARTICLE_TYPE >::CParticleList &old_particles, const vector< TPose3D > &newParticles, const vector< double > &newParticlesWeight, const vector< size_t > &newParticlesDerivedFromIdx) const
This is the default algorithm to efficiently replace one old set of samples by another new set...
std::vector< bool > m_pfAuxiliaryPFOptimal_maxLikMovementDrawHasBeenUsed



Page generated by Doxygen 1.8.8 for MRPT 1.2.2 SVN:Unversioned directory at Tue Oct 14 02:14:08 UTC 2014