Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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;
00108
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);
00115
00116 double obs = truePos + 0.1*(rand_d() - 0.5);
00117
00118 pf.update(cmd, obs);
00119
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 }