ShapeSLAMParticleFilter.h
Go to the documentation of this file.00001
00002
00003 #ifndef _LOADED_ShapeSLAMParticleFilter_h_
00004 #define _LOADED_ShapeSLAMParticleFilter_h_
00005
00006 #include <vector>
00007 #include <iostream>
00008 #include <cmath>
00009 #include "DeadReckoningBehavior.h"
00010 #include "LocalizationParticle.h"
00011
00012 namespace DualCoding {
00013
00014 class ShapeSpace;
00015 class ShapeSLAMParticleDistributionPolicy;
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 class ShapeSLAMParticle : public LocalizationParticle {
00028 public:
00029
00030 typedef LocalizationParticleDistributionPolicy<ShapeSLAMParticle> DistributionPolicy;
00031
00032
00033 ShapeSLAMParticle() : LocalizationParticle(), addLocal(), deleteWorld() {}
00034
00035 std::vector<bool> addLocal;
00036 std::vector<bool> deleteWorld;
00037 };
00038
00039
00040 class ShapeSLAMParticleEvaluator : public LocalShapeEvaluator {
00041 public:
00042
00043 ShapeSLAMParticleEvaluator(ShapeSpace &localShS, ShapeSpace &worldShS, float addPenalty);
00044 using LocalShapeEvaluator::evaluate;
00045 void evaluate(ShapeSLAMParticle& part);
00046 protected:
00047
00048 void determineAdditions(ShapeSLAMParticle& part, unsigned int const nLocals,
00049 int localMatches[], float localScores[]);
00050
00051 void determineDeletions(ShapeSLAMParticle& part, unsigned int const nLocals, int const localMatches[],
00052 float const particleViewX[], float const particleViewY[],
00053 float const particleViewX2[], float const particleViewY2[]);
00054 bool localMobile;
00055 bool worldMobile;
00056 const float ADDITION_PENALTY;
00057 };
00058
00059
00060
00061
00062
00063
00064 template<typename ParticleT>
00065 class ShapeSLAMSensorModel : public ParticleFilter<ParticleT>::SensorModel {
00066 public:
00067 typedef typename ParticleFilter<ParticleT>::SensorModel::particle_type particle_type;
00068 typedef typename ParticleFilter<ParticleT>::SensorModel::particle_collection particle_collection;
00069 typedef typename ParticleFilter<ParticleT>::SensorModel::index_t index_t;
00070
00071
00072 ShapeSLAMSensorModel(ShapeSpace &localShS, ShapeSpace &worldShS) :
00073 stdevSq(60*60), addPenalty(50), lShS(localShS), wShS(worldShS),
00074 particleLocalLandmarks(0), particleWorldLandmarks(0)
00075 {}
00076
00077
00078 float stdevSq;
00079
00080 float addPenalty;
00081
00082
00083 virtual void evaluate(particle_collection& particles, particle_type& estimate) {
00084 float bestWeight=-FLT_MAX;
00085 ShapeSLAMParticleEvaluator eval(lShS,wShS,addPenalty);
00086
00087 if(eval.localLms.size()>particleLocalLandmarks || eval.localLms.size()<particleLocalLandmarks/2)
00088 for(typename particle_collection::iterator it=particles.begin(); it!=particles.end(); ++it)
00089 it->addLocal.resize(particleLocalLandmarks);
00090
00091 if(eval.worldLms.size()>particleWorldLandmarks || eval.worldLms.size()<particleWorldLandmarks/2)
00092 for(typename particle_collection::iterator it=particles.begin(); it!=particles.end(); ++it)
00093 it->deleteWorld.resize(particleWorldLandmarks);
00094
00095 for(typename particle_collection::size_type p=0; p<particles.size(); ++p) {
00096 eval.evaluate(particles[p]);
00097 if(particles[p].weight>bestWeight) {
00098 bestWeight=particles[p].weight;
00099 }
00100 }
00101 }
00102
00103 ShapeSpace& getLocalShS() const { return lShS; }
00104 ShapeSpace& getWorldShS() const { return wShS; }
00105
00106 protected:
00107 ShapeSpace &lShS;
00108 ShapeSpace &wShS;
00109
00110 unsigned int particleLocalLandmarks;
00111 unsigned int particleWorldLandmarks;
00112
00113
00114
00115
00116 inline float normpdf(float const distsq) { return std::exp(-distsq/stdevSq); }
00117 };
00118
00119
00120
00121
00122
00123 class ShapeSLAMParticleFilter : public ParticleFilter<ShapeSLAMParticle> {
00124 public:
00125
00126 ShapeSLAMParticleFilter(ShapeSpace &localShS, ShapeSpace &worldShS, unsigned int numParticles=2000)
00127 : ParticleFilter<ShapeSLAMParticle>(numParticles, new DeadReckoningBehavior<ShapeSLAMParticle>),
00128 sensorModel(new ShapeSLAMSensorModel<ShapeSLAMParticle>(localShS,worldShS))
00129 {
00130 if(BehaviorBase* motBeh = dynamic_cast<BehaviorBase*>(motion))
00131 motBeh->start();
00132 }
00133
00134 virtual ~ShapeSLAMParticleFilter() {
00135 if(BehaviorBase* motBeh = dynamic_cast<BehaviorBase*>(motion)) {
00136 motBeh->stop();
00137
00138 motion=NULL;
00139 }
00140 delete sensorModel;
00141 }
00142
00143
00144 virtual void update(bool updateMot=true, bool doResample=true) { updateSensors(*sensorModel,updateMot,doResample); }
00145
00146
00147 virtual ShapeSLAMSensorModel<ShapeSLAMParticle>& getSensorModel() const { return *sensorModel; }
00148
00149
00150 virtual void setSensorModel(ShapeSLAMSensorModel<ShapeSLAMParticle>* customSensorModel) { delete sensorModel; sensorModel=customSensorModel; }
00151
00152
00153 virtual void setAgent() const;
00154
00155
00156 virtual void displayParticles(float const howmany=100) const;
00157
00158 protected:
00159 ShapeSLAMSensorModel<ShapeSLAMParticle> * sensorModel;
00160
00161 private:
00162 ShapeSLAMParticleFilter(const ShapeSLAMParticleFilter&);
00163 ShapeSLAMParticleFilter& operator=(const ShapeSLAMParticleFilter&);
00164 };
00165
00166
00167 std::ostream& operator<< (std::ostream& os, const ShapeSLAMParticle &p);
00168
00169 }
00170
00171 #endif