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

example/TestParticleFilter.cpp

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 
00039 #include "example/ParticleFilter1D.hpp"
00040 #include <vector>
00041 #include <ctime>
00042 #include <cstdlib>
00043 #include <iostream>
00044 
00046 double rand_d()
00047 {
00048     return static_cast<double>(rand()) / RAND_MAX;
00049 }
00050 
00051 using namespace Estimation;
00052 using namespace std;
00053 
00055 typedef Particle<double,double,double> Particle1Dd;
00056 
00063 double randomParticleGenerator(void* v)
00064 {
00065     return 10.0 * rand_d();
00066 }
00067 
00069 int main(int argc, char* argv[])
00070 {
00071     int nParticles = 25;
00072     ParticleFilter1D pf(nParticles);
00073     shared_ptr<ParticleFilter1DBase::CmdModelType>
00074         cMdl(new PF1DTransCmdModel());
00075     shared_ptr<ParticleFilter1DBase::MeasModelType>
00076         mMdl(new PF1DObsModel());
00077     vector<double> initial_particles;
00078 
00079     pf.setCommandModel(cMdl);
00080     pf.setMeasurementModel(mMdl);
00081     pf.setRandomInjectionFunction(randomParticleGenerator);
00082     pf.setRandomInjection(true);
00083     
00084     pf.setFixedRandomInjectionRate(0.1);
00085     pf.setDynamicRandomInjection(false);
00086     
00087     pf.setElitism(true);
00088 
00089     if(pf.getRandomInjection())
00090         cout << "random injection on!\n";
00091     else
00092         exit(0);
00093 
00094     srand(static_cast<unsigned>(time(0)));
00095 
00096     for (int i=0; i<nParticles; i++)
00097     {
00098         initial_particles.push_back(randomParticleGenerator(NULL));
00099     }
00100     pf.initialize(initial_particles);
00101 
00102     double truePos = 3;
00103     int nSteps = 100;
00104     cout << "Starting state: "<<truePos<<endl;
00105     for (int i=0; i<nSteps; i++)
00106     {
00107         double cmd = rand_d() - 0.5; // generate a command
00108         // "kidnap the robot" with some frequency
00109         if(i % 50 == 37)
00110         {
00111             truePos = 10.0 * rand_d();
00112             cout << "\nRobot kidnapped to "<<truePos<<"!\n\n";
00113         }
00114         truePos = cMdl->updateState(truePos, cmd);    // implement it
00115         // take a measurement
00116         double obs = truePos + 0.1*(rand_d() - 0.5);
00117         // udpate filter
00118         pf.update(cmd, obs);
00119         // display estimate and true state
00120         cout << "Issued command "<<cmd<<", moving to "<<truePos
00121             <<", and observed "<<obs<<"\n";
00122         cout << "Mean: "<<pf.getMean()<<", Weighted: "<<pf.getWeightedMean()
00123             <<", Mode: "<<pf.getMode()<<"\n";
00124         cout << "Number of particles: "<<pf.getNumParticles()<<"\n";
00125         cout << endl;
00126     }
00127 
00128     return 0;
00129 }

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