Main MRPT website > C++ reference for MRPT 1.9.9
CMonteCarloLocalization3D.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, 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 
10 #include "slam-precomp.h" // Precompiled headerss
11 
13 #include <mrpt/obs/CSensoryFrame.h>
14 
15 #include <mrpt/math/utils.h>
16 #include <mrpt/utils/round.h>
18 
19 using namespace std;
20 using namespace mrpt;
21 using namespace mrpt::bayes;
22 using namespace mrpt::poses;
23 using namespace mrpt::math;
24 using namespace mrpt::utils;
25 using namespace mrpt::obs;
26 using namespace mrpt::maps;
27 
30 
32 
33 namespace mrpt
34 {
35 namespace slam
36 {
37 /** Fills out a "TPoseBin3D" variable, given a path hypotesis and (if not set to
38  * nullptr) a new pose appended at the end, using the KLD params in "options".
39  */
40 template <>
42  mrpt::slam::detail::TPoseBin3D& outBin, const TKLDParams& opts,
43  const CMonteCarloLocalization3D::CParticleDataContent* currentParticleValue,
44  const TPose3D* newPoseToBeInserted)
45 {
46  // 3D pose approx: Use the latest pose only:
47  if (newPoseToBeInserted)
48  {
49  outBin.x = round(newPoseToBeInserted->x / opts.KLD_binSize_XY);
50  outBin.y = round(newPoseToBeInserted->y / opts.KLD_binSize_XY);
51  outBin.z = round(newPoseToBeInserted->z / opts.KLD_binSize_XY);
52 
53  outBin.yaw = round(newPoseToBeInserted->yaw / opts.KLD_binSize_PHI);
54  outBin.pitch = round(newPoseToBeInserted->pitch / opts.KLD_binSize_PHI);
55  outBin.roll = round(newPoseToBeInserted->roll / opts.KLD_binSize_PHI);
56  }
57  else
58  {
59  ASSERT_(currentParticleValue)
60  outBin.x = round(currentParticleValue->x() / opts.KLD_binSize_XY);
61  outBin.y = round(currentParticleValue->y() / opts.KLD_binSize_XY);
62  outBin.z = round(currentParticleValue->z() / opts.KLD_binSize_XY);
63 
64  outBin.yaw = round(currentParticleValue->yaw() / opts.KLD_binSize_PHI);
65  outBin.pitch =
66  round(currentParticleValue->pitch() / opts.KLD_binSize_PHI);
67  outBin.roll =
68  round(currentParticleValue->roll() / opts.KLD_binSize_PHI);
69  }
70 }
71 
72 /*---------------------------------------------------------------
73  ctor
74  ---------------------------------------------------------------*/
75 // Passing a "this" pointer at this moment is not a problem since it will be NOT
76 // access until the object is fully initialized
77 CMonteCarloLocalization3D::CMonteCarloLocalization3D(size_t M)
78  : m_poseParticles(M)
79 {
80  this->setLoggerName("CMonteCarloLocalization3D");
81 }
82 
83 /*---------------------------------------------------------------
84  Dtor
85  ---------------------------------------------------------------*/
88  const size_t i, bool& is_valid_pose) const
89 {
90  if (i >= m_poseParticles.m_particles.size())
91  THROW_EXCEPTION("Particle index out of bounds!");
92  is_valid_pose = true;
94  return TPose3D(*m_poseParticles.m_particles[i].d);
95 }
96 
97 /*---------------------------------------------------------------
98 
99  prediction_and_update
100 
101  ---------------------------------------------------------------*/
102 template <typename T>
104  const mrpt::obs::CActionCollection* actions,
105  const mrpt::obs::CSensoryFrame* sf,
107 {
108  MRPT_START
109 
110  if (sf)
111  { // A map MUST be supplied!
113  if (!options.metricMap)
114  ASSERT_(
116  }
117 
118  T::template PF_SLAM_implementation<
121  actions, sf, PF_options, options.KLD_params, *this);
122 
123  MRPT_END
124 }
125 
126 /*---------------------------------------------------------------
127  PF_SLAM_computeObservationLikelihoodForParticle
128  ---------------------------------------------------------------*/
129 double
131  const CParticleFilter::TParticleFilterOptions& PF_options,
132  const size_t particleIndexForMap, const CSensoryFrame& observation,
133  const CPose3D& x) const
134 {
135  MRPT_UNUSED_PARAM(PF_options);
136  ASSERT_(
137  options.metricMap || particleIndexForMap < options.metricMaps.size())
138 
139  CMetricMap* map =
140  (options.metricMap) ? options.metricMap : // All particles, one map
141  options.metricMaps[particleIndexForMap]; // One map per particle
142 
143  // For each observation:
144  double ret = 1;
145  for (CSensoryFrame::const_iterator it = observation.begin();
146  it != observation.end(); ++it)
147  ret += map->computeObservationLikelihood(
148  it->get(), x); // Compute the likelihood:
149 
150  // Done!
151  return ret;
152 }
153 
154 // Specialization for my kind of particles:
157  CPose3D* particleData, const TPose3D& newPose) const
158 {
159  *particleData = CPose3D(newPose);
160 }
161 
163  CParticleList& old_particles, const vector<TPose3D>& newParticles,
164  const vector<double>& newParticlesWeight,
165  const vector<size_t>& newParticlesDerivedFromIdx) const
166 {
167  MRPT_UNUSED_PARAM(newParticlesDerivedFromIdx);
168  ASSERT_(size_t(newParticlesWeight.size()) == newParticles.size())
169 
170  // ---------------------------------------------------------------------------------
171  // Substitute old by new particle set:
172  // Old are in "m_particles"
173  // New are in "newParticles",
174  // "newParticlesWeight","newParticlesDerivedFromIdx"
175  // ---------------------------------------------------------------------------------
176  // Free old m_particles (automatically done via smart ptr)
177 
178  // Copy into "m_particles"
179  const size_t N = newParticles.size();
180  old_particles.resize(N);
181  for (size_t i = 0; i < N; i++)
182  {
183  old_particles[i].log_w = newParticlesWeight[i];
184  old_particles[i].d.reset(new CPose3D(newParticles[i]));
185  }
186 }
187 
190  const mrpt::obs::CSensoryFrame* observation,
193 {
194  switch (PF_algorithm)
195  {
196  case CParticleFilter::pfStandardProposal:
198  *this, action, observation, stats);
199  break;
200  case CParticleFilter::pfAuxiliaryPFStandard:
202  *this, action, observation, stats);
203  break;
204  case CParticleFilter::pfAuxiliaryPFOptimal:
206  *this, action, observation, stats);
207  break;
208  default:
209  {
210  THROW_EXCEPTION("Invalid particle filter algorithm selection!");
211  }
212  break;
213  }
214 
215 }
216 
217 }
218 }
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:135
double PF_SLAM_computeObservationLikelihoodForParticle(const mrpt::bayes::CParticleFilter::TParticleFilterOptions &PF_options, const size_t particleIndexForMap, const mrpt::obs::CSensoryFrame &observation, const mrpt::poses::CPose3D &x) const
Evaluate the observation likelihood for one particle at a given location.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
double roll
Roll coordinate (rotation angle over X coordinate).
The namespace for Bayesian filtering algorithm: different particle filters and Kalman filter algorith...
#define THROW_EXCEPTION(msg)
This file contains the implementations of the template members declared in mrpt::slam::PF_implementat...
double x
X,Y,Z, coords.
void PF_SLAM_implementation_custom_update_particle_with_new_pose(CParticleDataContent *particleData, const mrpt::math::TPose3D &newPose) const
mrpt::poses::CPose3DPDFParticles::CParticleList CParticleList
double pitch() const
Get the PITCH angle (in radians)
Definition: CPose3D.h:539
double yaw() const
Get the YAW angle (in radians)
Definition: CPose3D.h:533
Option set for KLD algorithm.
Definition: TKLDParams.h:20
STL namespace.
mrpt::maps::TMetricMapList metricMaps
[update stage] Alternative way (if metricMap==nullptr): A metric map is supplied for each particle: T...
double yaw
Yaw coordinate (rotation angle over Z axis).
A generic implementation of the PF method "pfStandardProposal" (standard proposal distribution...
Statistics for being returned from the "execute" method.
mrpt::maps::CMetricMap * metricMap
[update stage] Must be set to a metric map used to estimate the likelihood of observations ...
Declares a class for storing a collection of robot actions.
void prediction_and_update(const mrpt::obs::CActionCollection *action, const mrpt::obs::CSensoryFrame *observation, const bayes::CParticleFilter::TParticleFilterOptions &PF_options)
Update the m_particles, predicting the posterior of robot pose and map after a movement command...
const_iterator begin() const
Returns a constant iterator to the first observation: this is an example of usage: ...
TMonteCarloLocalizationParams options
MCL parameters.
CParticleList m_particles
The array of particles.
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
This namespace contains representation of robot actions and observations.
Declares a class for storing a "sensory frame", a set of "observations" taken by the robot approximat...
Definition: CSensoryFrame.h:54
double KLD_binSize_XY
Parameters for the KLD adaptive sample size algorithm (see Dieter Fox&#39;s papers), which is used only i...
Definition: TKLDParams.h:35
This class acts as a common interface to the different interfaces (see CParticleFilter::TParticleFilt...
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:17
double roll() const
Get the ROLL angle (in radians)
Definition: CPose3D.h:545
double pitch
Pitch coordinate (rotation angle over Y axis).
Auxiliary structure used in KLD-sampling in particle filters.
#define MRPT_START
void executeOn(PARTICLEFILTERCAPABLE &obj, const mrpt::obs::CActionCollection *action, const mrpt::obs::CSensoryFrame *observation, TParticleFilterStats *stats=nullptr)
Executes a complete prediction + update step of the selected particle filtering algorithm.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define ASSERTDEB_(f)
Defines an assertion mechanism - only when compiled in debug.
TParticleFilterAlgorithm
Defines different types of particle filter algorithms.
Declares a virtual base class for all metric maps storage classes.
Definition: CMetricMap.h:55
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
mrpt::poses::CPose3DPDFParticles m_poseParticles
void executeOn(mrpt::bayes::CParticleFilter &pf, const mrpt::obs::CActionCollection *action, const mrpt::obs::CSensoryFrame *observation, mrpt::bayes::CParticleFilter::TParticleFilterStats *stats, mrpt::bayes::CParticleFilter::TParticleFilterAlgorithm PF_algorithm)
The configuration of a particle filter.
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
std::deque< CObservation::Ptr >::const_iterator const_iterator
You can use CSensoryFrame::begin to get a iterator to the first element.
#define ASSERT_(f)
void PF_SLAM_implementation_replaceByNewParticleSet(CParticleList &old_particles, const std::vector< mrpt::math::TPose3D > &newParticles, const std::vector< double > &newParticlesWeight, const std::vector< size_t > &newParticlesDerivedFromIdx) const
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:25
GLenum GLint x
Definition: glext.h:3538
const_iterator end() const
Returns a constant iterator to the end of the list of observations: this is an example of usage: ...
mrpt::math::TPose3D getLastPose(const size_t i, bool &is_valid_pose) const override
Return the robot pose for the i&#39;th particle.
TKLDParams KLD_params
Parameters for dynamic sample size, KLD method.
void KLF_loadBinFromParticle(mrpt::slam::detail::TPoseBin3D &outBin, const TKLDParams &opts, const CMonteCarloLocalization3D::CParticleDataContent *currentParticleValue, const TPose3D *newPoseToBeInserted)
Fills out a "TPoseBin3D" variable, given a path hypotesis and (if not set to nullptr) a new pose appe...



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019