Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

LocalizationParticle.h

Go to the documentation of this file.
00001 #ifndef INCLUDED_LocalizationParticle_h
00002 #define INCLUDED_LocalizationParticle_h
00003 
00004 #include "Shared/ParticleFilter.h"
00005 #include "Shared/Measures.h"
00006 #include "Shared/zignor.h"
00007 #include <iostream>
00008 #include <cmath>
00009 
00010 template<typename ParticleT> class LocalizationParticleDistributionPolicy;
00011 
00012 //! Each Particle represents a hypothesis about the robot's 2D position and heading
00013 class LocalizationParticle : public ParticleBase<LocalizationParticle> {
00014 public:
00015   float x; //!< X position of robot in the world
00016   float y; //!< Y position of robot in the world
00017   AngTwoPi theta; //!< Orientation of robot in world
00018   
00019   //! defines a default DistributionPolicy for the particle type
00020   typedef LocalizationParticleDistributionPolicy<LocalizationParticle> DistributionPolicy;
00021   
00022   //! constructor
00023   LocalizationParticle() : ParticleBase<LocalizationParticle>(), x(0), y(0), theta(0) {}
00024   
00025   //! constructor, allows you to define the particle's position
00026   LocalizationParticle(float xp, float yp, AngTwoPi tp) : ParticleBase<LocalizationParticle>(), x(xp), y(yp), theta(tp) {}
00027   
00028   //! returns a straightforward sum squared error of each of the fields
00029   float sumSqErr(const LocalizationParticle& lp) const {
00030     //const LocalizationParticle& lp = static_cast<const LocalizationParticle&>(p);
00031     float dx=x-lp.x;
00032     float dy=y-lp.y;
00033     float dt=theta-lp.theta;
00034     return dx*dx+dy*dy+dt*dt;
00035   }
00036 
00037 };
00038 
00039 //! Provides parameters and methods for randomizing and tweaking LocalizationParticles
00040 template<typename ParticleT>
00041 class LocalizationParticleDistributionPolicy : public ParticleFilter<ParticleT>::DistributionPolicy {
00042 public:
00043   typedef ParticleT particle_type;  //!< just for convenience
00044   typedef typename ParticleFilter<ParticleT>::index_t index_t; //!< just for convenience
00045   
00046   float mapMinX; //!< specifies the low end of x coordinates during randomize()
00047   float mapWidth; //!< along with #mapMinX, specifies the range of x coordinates to be used during randomize()
00048   float mapMinY; //!< specifies the low end of y coordinates during randomize()
00049   float mapHeight; //!< along with #mapMinY, specifies the range of y coordinates to be used during randomize()
00050   float positionVariance; //!< controls how much the x and y parameters will be modified during jiggle()
00051   float orientationVariance; //!< controls how much the orientation (theta) parameter will be modified during jiggle()
00052   
00053   //! constructor -- by default, coordinates will range from -1000 to 1000 for x and y, with variance of 50 and 0.18 for position and orientation
00054   LocalizationParticleDistributionPolicy()
00055     : mapMinX(-1000), mapWidth(2000), mapMinY(-1000), mapHeight(2000),
00056       positionVariance(50), orientationVariance(0.18f)
00057   {}
00058   
00059   virtual void randomize(particle_type* begin, index_t num) {
00060     particle_type* end=&begin[num]; 
00061     while(begin!=end) { 
00062       begin->x = float(rand())/RAND_MAX * mapWidth + mapMinX;
00063       begin->y = float(rand())/RAND_MAX * mapHeight + mapMinY;
00064       begin->theta = direction_t(rand())/RAND_MAX * 2 * direction_t(M_PI);
00065       //(begin++)->randomize();
00066       ++begin;
00067     }
00068   }
00069   virtual void jiggle(float var, particle_type* begin, index_t num) {
00070     if(var==0)
00071       return;
00072     particle_type* end=&begin[num]; 
00073     while(begin!=end) {
00074       begin->x+=(float)DRanNormalZig32()*positionVariance*var;
00075       begin->y+=(float)DRanNormalZig32()*positionVariance*var;
00076       begin->theta+=(float)DRanNormalZig32()*orientationVariance*var;
00077       //(begin++)->jiggle(var);
00078       ++begin;
00079     }
00080   }
00081 };
00082 
00083 //! dump a particle's state
00084 inline std::ostream& operator << (std::ostream& os, const LocalizationParticle &p) {
00085   os << "Particle(p=" << p.weight
00086      << ", dx=" << p.x
00087      << ", dy=" << p.y
00088      << ", th=" << p.theta
00089      << ")";
00090   return os;
00091 }
00092 
00093 
00094 
00095 /*! @file
00096  * @brief 
00097  * @author ejt (Creator)
00098  */
00099 
00100 #endif

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:44 2016 by Doxygen 1.6.3