#pragma once #include "mesh.h" #include "Settings.h" #include #include #include #include #include #include //#include #include #include #include #include #include #include #include //#include //#include //#include //#include //#include #include //#include #include #include #include #include #include #include "FtmKalman.h" #include "Eval.h" struct MyState { /** the state's position (within the mesh) */ MyNavMeshLocation pos; /** the state's heading */ Heading heading; MyState() : pos(), heading(0) {;} MyState(Point3 p) : pos(p, nullptr), heading(0){;} MyState& operator += (const MyState& o) { pos.tria = nullptr; // impossible pos.pos += o.pos.pos; return *this; } MyState& operator /= (const double val) { pos.tria = nullptr; // impossible pos.pos /= val; return *this; } MyState operator * (const double val) const { MyState res; res.pos.pos = pos.pos * val; return res; } float getX(){ return pos.pos.x; } float getY() { return pos.pos.y; } float getZ() { return pos.pos.z; } float getBinValue(const int dim) const { switch (dim) { case 0: return this->pos.pos.x; case 1: return this->pos.pos.y; case 2: return this->pos.pos.z; case 3: return this->heading.getRAD(); } throw "cant find this value within the bin"; } }; struct MyControl { int numStepsSinceLastEval = 0; float headingChangeSinceLastEval = 0; void afterEval() { numStepsSinceLastEval = 0; headingChangeSinceLastEval = 0; } //wifi std::map wifi; //time Timestamp currentTime; //last estimation Point3 lastEstimate = Point3(26, 43, 7.5); }; struct MyObservation { //wifi std::unordered_map wifi; // deprecated std::vector ftm; //time Timestamp currentTime; }; class MyPFInitUniform : public SMC::ParticleFilterInitializer { const MyNavMesh* mesh; public: MyPFInitUniform(const MyNavMesh* mesh) : mesh(mesh) { ; } virtual void initialize(std::vector>& particles) override { /** random position and heading within the mesh */ Distribution::Uniform dHead(0, 2*M_PI); MyNavMeshRandom rnd = mesh->getRandom(); for (SMC::Particle& p : particles) { p.state.pos = rnd.draw(); p.state.heading = dHead.draw(); p.weight = 1.0 / particles.size(); } } }; class MyPFInitFixed : public SMC::ParticleFilterInitializer { const MyNavMesh* mesh; const Point3 pos; public: MyPFInitFixed(const MyNavMesh* mesh, const Point3 pos) : mesh(mesh), pos(pos) { ; } virtual void initialize(std::vector>& particles) override { /** random position and heading within the mesh */ Distribution::Uniform dHead(0, 2*M_PI); for (SMC::Particle& p : particles) { p.state.pos = mesh->getLocation(pos); p.state.heading = 1.5*M_PI;// dHead.draw(); p.weight = 1.0 / particles.size(); } } }; class MyPFTransStatic : public SMC::ParticleFilterTransition{ void transition(std::vector>& particles, const MyControl* control) override { // nop } }; struct MyPFTransRandom : public SMC::ParticleFilterTransition{ Distribution::Normal dStepSize; Distribution::Uniform dHeading; MyPFTransRandom() //: dStepSize(2.5f, 0.7f), dHeading(0, 2*M_PI) : dStepSize(2.0f, 0.5f), dHeading(0, 2 * M_PI) {} void transition(std::vector>& particles, const MyControl* control) override { #pragma omp parallel for num_threads(3) for (int i = 0; i < particles.size(); ++i) { SMC::Particle& p = particles[i]; const float angle = dHeading.draw(); const float stepSize = dStepSize.draw(); p.state.pos.pos.x += std::cos(angle) * stepSize; p.state.pos.pos.y += std::sin(angle) * stepSize; } } }; class MyPFTrans : public SMC::ParticleFilterTransition { //using MyNavMeshWalk = NM::NavMeshWalkSimple; //using MyNavMeshWalk = NM::NavMeshWalkWifiRegional; //using MyNavMeshWalk = NM::NavMeshWalkUnblockable; //using MyNavMeshWalk = NM::NavMeshWalkKLD; using MyNavMeshWalk = NM::NavMeshWalkSinkOrSwim; MyNavMeshWalk walker; public: //std::vector listRadiusSub; MyPFTrans(MyNavMesh& mesh) : walker(mesh) { // how to evaluate drawn points walker.addEvaluator(new NM::WalkEvalHeadingStartEndNormal(0.04)); walker.addEvaluator(new NM::WalkEvalDistance(0.1)); //walker.addEvaluator(new NM::WalkEvalApproachesTarget(0.9)); // 90% for particles moving towards the target } void transition(std::vector>& particles, const MyControl* control) override { // walking and heading random Distribution::Normal dStepSizeFloor(0.60f, 0.1f); Distribution::Normal dStepSizeStair(0.35f, 0.1f); Distribution::Normal dHeading(0.0, 0.1f); #pragma omp parallel for num_threads(3) for (int i = 0; i < particles.size(); ++i) { SMC::Particle& p = particles[i]; // how to walk MyNavMeshWalkParams params; params.heading = p.state.heading + control->headingChangeSinceLastEval + dHeading.draw(); params.numSteps = control->numStepsSinceLastEval; params.start = p.state.pos; params.stepSizes.stepSizeFloor_m = dStepSizeFloor.draw(); params.stepSizes.stepSizeStair_m = dStepSizeStair.draw(); if(params.stepSizes.stepSizeFloor_m < 0.1f || params.stepSizes.stepSizeStair_m < 0.1f){ params.stepSizes.stepSizeFloor_m = 0.1f; params.stepSizes.stepSizeStair_m = 0.1f; } // walk MyNavMeshWalk::ResultEntry res = walker.getOne(params); // assign back to particle's state p.weight *= res.probability; p.state.pos = res.location; p.state.heading = res.heading; } // reset the control (0 steps, 0 delta-heading) //control->afterEval(); } }; struct MyPFEval : public SMC::ParticleFilterEvaluation { // FRANK MyPFEval() { }; bool assignProps = false; std::shared_ptr> ftmKalmanFilters; virtual double evaluation(std::vector>& particles, const MyObservation& observation) override { double sum = 0; #pragma omp parallel for num_threads(3) for (int i = 0; i < particles.size(); ++i) { SMC::Particle& p = particles[i]; auto kalmanFilters = ftmKalmanFilters; if (!Settings::UseKalman) { kalmanFilters = nullptr; } double prob = ftmEval(Settings::UseRSSI ? SensorMode::RSSI : SensorMode::FTM, observation.currentTime, p.state.pos.pos, observation.ftm, kalmanFilters); if (assignProps) p.weight = prob; else p.weight *= prob; #pragma omp atomic sum += prob; } return sum; } }; using MyFilter = SMC::ParticleFilter;