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-2018, 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/core/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::obs;
25 using namespace mrpt::maps;
26 
28 
29 namespace mrpt::slam
30 {
31 /** Fills out a "TPoseBin3D" variable, given a path hypotesis and (if not set to
32  * nullptr) a new pose appended at the end, using the KLD params in "options".
33  */
34 template <>
36  mrpt::slam::detail::TPoseBin3D& outBin, const TKLDParams& opts,
37  const CMonteCarloLocalization3D::CParticleDataContent* currentParticleValue,
38  const TPose3D* newPoseToBeInserted)
39 {
40  // 3D pose approx: Use the latest pose only:
41  if (newPoseToBeInserted)
42  {
43  outBin.x = round(newPoseToBeInserted->x / opts.KLD_binSize_XY);
44  outBin.y = round(newPoseToBeInserted->y / opts.KLD_binSize_XY);
45  outBin.z = round(newPoseToBeInserted->z / opts.KLD_binSize_XY);
46 
47  outBin.yaw = round(newPoseToBeInserted->yaw / opts.KLD_binSize_PHI);
48  outBin.pitch = round(newPoseToBeInserted->pitch / opts.KLD_binSize_PHI);
49  outBin.roll = round(newPoseToBeInserted->roll / opts.KLD_binSize_PHI);
50  }
51  else
52  {
53  ASSERT_(currentParticleValue);
54  outBin.x = round(currentParticleValue->x / opts.KLD_binSize_XY);
55  outBin.y = round(currentParticleValue->y / opts.KLD_binSize_XY);
56  outBin.z = round(currentParticleValue->z / opts.KLD_binSize_XY);
57 
58  outBin.yaw = round(currentParticleValue->yaw / opts.KLD_binSize_PHI);
59  outBin.pitch =
60  round(currentParticleValue->pitch / opts.KLD_binSize_PHI);
61  outBin.roll = round(currentParticleValue->roll / opts.KLD_binSize_PHI);
62  }
63 }
64 }
65 
67 
68 using namespace mrpt::slam;
69 
70 /*---------------------------------------------------------------
71  ctor
72  ---------------------------------------------------------------*/
73 // Passing a "this" pointer at this moment is not a problem since it will be NOT
74 // access until the object is fully initialized
75 CMonteCarloLocalization3D::CMonteCarloLocalization3D(size_t M)
77 {
78  this->setLoggerName("CMonteCarloLocalization3D");
79 }
80 
82  const size_t i, bool& is_valid_pose) const
83 {
84  if (i >= m_particles.size())
85  THROW_EXCEPTION("Particle index out of bounds!");
86  is_valid_pose = true;
87  return m_particles[i].d;
88 }
89 
90 /*---------------------------------------------------------------
91 
92  prediction_and_update_pfStandardProposal
93 
94  ---------------------------------------------------------------*/
96  const mrpt::obs::CActionCollection* actions,
97  const mrpt::obs::CSensoryFrame* sf,
99 {
100  MRPT_START
101 
102  if (sf)
103  { // A map MUST be supplied!
104  ASSERT_(options.metricMap || options.metricMaps.size() > 0);
105  if (!options.metricMap)
106  ASSERT_(options.metricMaps.size() == m_particles.size());
107  }
108 
109  PF_SLAM_implementation_pfStandardProposal<mrpt::slam::detail::TPoseBin3D>(
110  actions, sf, PF_options, options.KLD_params);
111 
112  MRPT_END
113 }
114 
115 /*---------------------------------------------------------------
116 
117  prediction_and_update_pfAuxiliaryPFStandard
118 
119  ---------------------------------------------------------------*/
121  const mrpt::obs::CActionCollection* actions,
122  const mrpt::obs::CSensoryFrame* sf,
124 {
125  MRPT_START
126 
127  if (sf)
128  { // A map MUST be supplied!
129  ASSERT_(options.metricMap || options.metricMaps.size() > 0);
130  if (!options.metricMap)
131  ASSERT_(options.metricMaps.size() == m_particles.size());
132  }
133 
136  actions, sf, PF_options, options.KLD_params);
137 
138  MRPT_END
139 }
140 
141 /*---------------------------------------------------------------
142 
143  prediction_and_update_pfAuxiliaryPFOptimal
144 
145  ---------------------------------------------------------------*/
147  const mrpt::obs::CActionCollection* actions,
148  const mrpt::obs::CSensoryFrame* sf,
150 {
151  MRPT_START
152 
153  if (sf)
154  { // A map MUST be supplied!
155  ASSERT_(options.metricMap || options.metricMaps.size() > 0);
156  if (!options.metricMap)
157  ASSERT_(options.metricMaps.size() == m_particles.size());
158  }
159 
160  PF_SLAM_implementation_pfAuxiliaryPFOptimal<mrpt::slam::detail::TPoseBin3D>(
161  actions, sf, PF_options, options.KLD_params);
162 
163  MRPT_END
164 }
165 
166 /*---------------------------------------------------------------
167  PF_SLAM_computeObservationLikelihoodForParticle
168  ---------------------------------------------------------------*/
169 double
171  [[maybe_unused]] const CParticleFilter::TParticleFilterOptions& PF_options,
172  const size_t particleIndexForMap, const CSensoryFrame& observation,
173  const CPose3D& x) const
174 {
175  ASSERT_(
176  options.metricMap || particleIndexForMap < options.metricMaps.size());
177 
178  CMetricMap* map =
179  (options.metricMap) ? options.metricMap : // All particles, one map
180  options.metricMaps[particleIndexForMap]; // One map per particle
181 
182  // For each observation:
183  double ret = 1;
184  for (CSensoryFrame::const_iterator it = observation.begin();
185  it != observation.end(); ++it)
186  ret += map->computeObservationLikelihood(
187  it->get(), x); // Compute the likelihood:
188 
189  // Done!
190  return ret;
191 }
192 
193 // Specialization for my kind of particles:
196  TPose3D* particleData, const TPose3D& newPose) const
197 {
198  *particleData = newPose;
199 }
200 
202  CParticleList& old_particles, const vector<TPose3D>& newParticles,
203  const vector<double>& newParticlesWeight,
204  [[maybe_unused]] const vector<size_t>& newParticlesDerivedFromIdx) const
205 {
206  ASSERT_(size_t(newParticlesWeight.size()) == newParticles.size());
207  // ---------------------------------------------------------------------------------
208  // Substitute old by new particle set:
209  // Old are in "m_particles"
210  // New are in "newParticles",
211  // "newParticlesWeight","newParticlesDerivedFromIdx"
212  // ---------------------------------------------------------------------------------
213  // Free old m_particles (automatically done via smart ptr)
214 
215  // Copy into "m_particles"
216  const size_t N = newParticles.size();
217  old_particles.resize(N);
218  for (size_t i = 0; i < N; i++)
219  {
220  old_particles[i].log_w = newParticlesWeight[i];
221  old_particles[i].d = newParticles[i];
222  }
223 }
void prediction_and_update_pfAuxiliaryPFStandard(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...
#define MRPT_START
Definition: exceptions.h:262
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
double roll
Roll coordinate (rotation angle over X coordinate).
The namespace for Bayesian filtering algorithm: different particle filters and Kalman filter algorith...
void PF_SLAM_implementation_pfAuxiliaryPFStandard(const mrpt::obs::CActionCollection *actions, const mrpt::obs::CSensoryFrame *sf, const mrpt::bayes::CParticleFilter::TParticleFilterOptions &PF_options, const TKLDParams &KLD_options)
A generic implementation of the PF method "prediction_and_update_pfAuxiliaryPFStandard" (Auxiliary pa...
This file contains the implementations of the template members declared in mrpt::slam::PF_implementat...
double x
X,Y,Z, coords.
Option set for KLD algorithm.
Definition: TKLDParams.h:18
STL namespace.
void prediction_and_update_pfStandardProposal(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...
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).
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.
const_iterator begin() const
Returns a constant iterator to the first observation: this is an example of usage: ...
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
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.
TMonteCarloLocalizationParams options
MCL parameters.
This base provides a set of functions for maths stuff.
void setLoggerName(const std::string &name)
Set the name of the COutputLogger instance.
std::deque< CObservation::Ptr >::const_iterator const_iterator
You can use CSensoryFrame::begin to get a iterator to the first element.
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:52
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:32
void prediction_and_update_pfAuxiliaryPFOptimal(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...
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
double pitch
Pitch coordinate (rotation angle over Y axis).
Auxiliary structure used in KLD-sampling in particle filters.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Declares a virtual base class for all metric maps storage classes.
Definition: CMetricMap.h:54
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:86
The configuration of a particle filter.
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
#define MRPT_END
Definition: exceptions.h:266
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
void PF_SLAM_implementation_custom_update_particle_with_new_pose(CParticleDataContent *particleData, const mrpt::math::TPose3D &newPose) const
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.
Declares a class that represents a Probability Density function (PDF) of a 3D pose.
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...
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:23



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020