• Main Page
  • Classes
  • Files
  • File List
  • File Members

example/ParticleFilter1D.hpp

Go to the documentation of this file.
00001 /*
00002  *  MAPS-TFSS, Tactile Force Sensor Simulator
00003  * 
00004  *  Copyright (C) 2010 Zachary Pezzementi and Gregory Hager
00005  *
00006  *  This program is free software: you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation, either version 3 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00026 #ifndef ParticleFilter1D_HPP
00027 #define ParticleFilter1D_HPP
00028 
00029 #include "Estimation/BayesFilters/ParticleFilter/ParticleFilter.hpp"
00030 #include <cmath>
00031 
00032 namespace Estimation
00033 {
00045     class PF1DTransCmdModel : public PFCommandModel<double, double>
00046     {
00047         public:
00060         double updateState(const double &curState, const double &translation) const
00061         {
00062             double randOffset = translation * .2 * (static_cast<double>(rand()) / RAND_MAX - 0.5);
00063             double newPos = curState + translation + randOffset;
00064             if(newPos < 0.0) return 0.0;
00065             if(newPos > 10.0) return 10.0;
00066             return newPos;
00067         }
00068     };
00069 
00079     class PF1DObsModel : public PFMeasurementModel<double, double>
00080     {
00081         public:
00088         PF1DObsModel(double var=0.3) : m_variance(var)
00089         {}
00090 
00103         double getLikelihood(const double &measurement, const double &stateEst)
00104         {
00105             double diff = measurement - stateEst;
00106             return exp(-(diff * diff) / m_variance);
00107         }
00108 
00110         double getVariance() const { return m_variance; }
00111 
00113         void setVariance(double v){ m_variance = v; }
00114 
00115         private:
00116         double m_variance;
00117     };
00118 
00123     typedef ParticleFilter<double, double, double> ParticleFilter1DBase;
00124 
00133     class ParticleFilter1D : public ParticleFilter1DBase
00134     {
00135     public:
00137         typedef ParticleFilter1DBase::ParticleType Particle1D;
00138 
00144         ParticleFilter1D(unsigned nParticles)
00145             : ParticleFilter1DBase(nParticles)
00146         {
00147         }
00148 
00156         double getMean() const
00157         {
00158             std::vector<Particle1D>::const_iterator i;
00159             double mean = 0.0;
00160             for (i=m_particles.begin(); i!=m_particles.end(); ++i)
00161             {
00162                     mean += i->getState();
00163             }
00164             return mean / m_particles.size();
00165         }
00166 
00175         double getWeightedMean() const
00176         {
00177             std::vector<Particle1D>::const_iterator i;
00178             double mean = 0.0;
00179             double total = 0.0;
00180             for (i=m_particles.begin(); i!=m_particles.end(); ++i)
00181             {
00182                     mean += i->getState() * i->getLikelihood();
00183                     total += i->getLikelihood();
00184             }
00185             return mean / total;
00186         }
00187 
00188     };
00189 
00190 }
00191 
00192 #endif

Generated on Mon Oct 25 2010 13:26:13 for MAPS-PF by  doxygen 1.7.1