From 4439123e5bedf3f0e1d18d65fbc718525bb38641 Mon Sep 17 00:00:00 2001 From: kazu Date: Tue, 28 Mar 2017 21:03:17 +0200 Subject: [PATCH] fixed some issues some new helper methods added listener-support to offline-reader --- sensors/offline/FileReader.h | 44 ++++++++++++++++--- sensors/offline/Listener.h | 2 +- sensors/radio/WiFiMeasurements.h | 10 +++++ sensors/radio/WiFiProbabilityFree.h | 4 +- sensors/radio/model/WiFiModelLogDistCeiling.h | 4 +- sensors/radio/setup/WiFiFingerprint.h | 9 ++++ sensors/radio/setup/WiFiFingerprints.h | 27 +++++++++--- 7 files changed, 82 insertions(+), 18 deletions(-) diff --git a/sensors/offline/FileReader.h b/sensors/offline/FileReader.h index b5372ef..60b8e47 100644 --- a/sensors/offline/FileReader.h +++ b/sensors/offline/FileReader.h @@ -24,6 +24,7 @@ #include "Splitter.h" #include "Sensors.h" +#include "Listener.h" #warning "adjust to to use the new splitter for all parsers [gps, compass, etc. already do!]" @@ -49,6 +50,8 @@ namespace Offline { static constexpr char sep = ';'; + Listener* listener = nullptr; + public: /** empty ctor. call open() */ @@ -62,7 +65,8 @@ namespace Offline { } /** open the given file */ - void open(const std::string& file) { + void open(const std::string& file, Listener* listener = nullptr) { + this->listener = listener; parse(file); } @@ -142,20 +146,27 @@ namespace Offline { TS elem(ts, LinearAccelerationData(std::stof(x), std::stof(y), std::stof(z))); lin_acc.push_back(elem); entries.push_back(Entry(Sensor::LIN_ACC, ts, lin_acc.size()-1)); + } void parseGravity(const uint64_t ts, const std::string& data){ + GravityData gravData; + const auto pos1 = data.find(';'); const auto pos2 = data.find(';', pos1+1); - const std::string x = data.substr(0, pos1); - const std::string y = data.substr(pos1+1, pos2-pos1-1); - const std::string z = data.substr(pos2+1); + gravData.x = std::stof(data.substr(0, pos1)); + gravData.y = std::stof(data.substr(pos1+1, pos2-pos1-1)); + gravData.z = std::stof(data.substr(pos2+1)); - TS elem(ts, GravityData(std::stof(x), std::stof(y), std::stof(z))); + TS elem(ts, gravData); gravity.push_back(elem); entries.push_back(Entry(Sensor::GRAVITY, ts, gravity.size()-1)); + + // inform listener + if (listener) {listener->onGravity(Timestamp::fromMS(ts), gravData);} + } void parseAccelerometer(const uint64_t ts, const std::string& data) { @@ -167,10 +178,14 @@ namespace Offline { const std::string y = data.substr(pos1+1, pos2-pos1-1); const std::string z = data.substr(pos2+1); - TS elem(ts, AccelerometerData(std::stof(x), std::stof(y), std::stof(z))); + const AccelerometerData accData(std::stof(x), std::stof(y), std::stof(z)); + TS elem(ts, accData); acc.push_back(elem); entries.push_back(Entry(Sensor::ACC, ts, acc.size()-1)); + // inform listener + if (listener) {listener->onAccelerometer(Timestamp::fromMS(ts), accData);} + } void parseGyroscope(const uint64_t ts, const std::string& data) { @@ -182,10 +197,14 @@ namespace Offline { const std::string y = data.substr(pos1+1, pos2-pos1-1); const std::string z = data.substr(pos2+1); - TS elem(ts, GyroscopeData(std::stof(x), std::stof(y), std::stof(z))); + const GyroscopeData gyroData(std::stof(x), std::stof(y), std::stof(z)); + TS elem(ts, gyroData); gyro.push_back(elem); entries.push_back(Entry(Sensor::GYRO, ts, gyro.size()-1)); + // inform listener + if (listener) {listener->onGyroscope(Timestamp::fromMS(ts), gyroData);} + } void parseWiFi(const uint64_t ts, const std::string& data) { @@ -210,6 +229,9 @@ namespace Offline { this->wifi.push_back(TS(ts, wifi)); entries.push_back(Entry(Sensor::WIFI, ts, this->wifi.size()-1)); + // inform listener + if (listener) {listener->onWiFi(Timestamp::fromMS(ts), wifi);} + } void parseBeacons(const uint64_t ts, const std::string& data) { @@ -250,6 +272,9 @@ namespace Offline { barometer.push_back(elem); entries.push_back(Entry(Sensor::BARO, ts, barometer.size()-1)); + // inform listener + if (listener) {listener->onBarometer(Timestamp::fromMS(ts), baro);} + } void parseCompass(const uint64_t ts, const std::string& data) { @@ -264,6 +289,9 @@ namespace Offline { this->compass.push_back(elem); entries.push_back(Entry(Sensor::COMPASS, ts, this->compass.size()-1)); + // inform listener + if (listener) {listener->onCompass(Timestamp::fromMS(ts), compass);} + } /** parse the given GPS entry */ @@ -282,6 +310,8 @@ namespace Offline { this->gps.push_back(elem); entries.push_back(Entry(Sensor::GPS, ts, this->gps.size()-1)); + // inform listener + if (listener) {listener->onGPS(Timestamp::fromMS(ts), gps);} } diff --git a/sensors/offline/Listener.h b/sensors/offline/Listener.h index d9be0a6..83cc28c 100644 --- a/sensors/offline/Listener.h +++ b/sensors/offline/Listener.h @@ -20,7 +20,7 @@ namespace Offline { virtual void onGyroscope(const Timestamp ts, const GyroscopeData data) = 0; virtual void onAccelerometer(const Timestamp ts, const AccelerometerData data) = 0; - virtual void onGravity(const Timestamp ts, const AccelerometerData data) = 0; + virtual void onGravity(const Timestamp ts, const GravityData data) = 0; virtual void onWiFi(const Timestamp ts, const WiFiMeasurements data) = 0; virtual void onBarometer(const Timestamp ts, const BarometerData data) = 0; virtual void onGPS(const Timestamp ts, const GPSData data) = 0; diff --git a/sensors/radio/WiFiMeasurements.h b/sensors/radio/WiFiMeasurements.h index a846251..267075b 100644 --- a/sensors/radio/WiFiMeasurements.h +++ b/sensors/radio/WiFiMeasurements.h @@ -22,6 +22,16 @@ struct WiFiMeasurements { return res; } + /** get the measurements for the given MAC [if available] otherwise null */ + WiFiMeasurement* getForMac(const MACAddress& mac) { + for (WiFiMeasurement& m : entries) { + if (m.getAP().getMAC() == mac) { + return &m; + } + } + return nullptr; + } + }; #endif // WIFIMEASUREMENTS_H diff --git a/sensors/radio/WiFiProbabilityFree.h b/sensors/radio/WiFiProbabilityFree.h index e850101..5f3b5ac 100644 --- a/sensors/radio/WiFiProbabilityFree.h +++ b/sensors/radio/WiFiProbabilityFree.h @@ -17,7 +17,7 @@ class WiFiObserverFree : public WiFiProbability { private: - const float sigma = 8.0f; + const float sigma; const float sigmaPerSecond = 3.0f; @@ -58,7 +58,7 @@ public: const Timestamp age = curTime - entry.getTimestamp(); Assert::isTrue(age.ms() >= 0, "found a negative wifi measurement age. this does not make sense"); - //Assert::isTrue(age.ms() <= 40000, "found a 40 second old wifi measurement. maybe there is a coding error?"); + Assert::isTrue(age.ms() <= 60000, "found a 60 second old wifi measurement. maybe there is a coding error?"); // sigma grows with measurement age const float sigma = this->sigma + this->sigmaPerSecond * age.sec(); diff --git a/sensors/radio/model/WiFiModelLogDistCeiling.h b/sensors/radio/model/WiFiModelLogDistCeiling.h index 328bf01..37d80e4 100644 --- a/sensors/radio/model/WiFiModelLogDistCeiling.h +++ b/sensors/radio/model/WiFiModelLogDistCeiling.h @@ -60,12 +60,12 @@ public: } /** load AP information from the floorplan. use the given fixed TXP/EXP/WAF for all APs */ - void loadAPs(const Floorplan::IndoorMap* map, const float txp = -40.0f, const float exp = 2.5f, const float waf = -8.0f) { + void loadAPs(const Floorplan::IndoorMap* map, const float txp = -40.0f, const float exp = 2.5f, const float waf = -8.0f, const bool assertSafe = true) { for (const Floorplan::Floor* floor : map->floors) { for (const Floorplan::AccessPoint* ap : floor->accesspoints) { const APEntry ape(ap->getPos(floor), txp, exp, waf); - addAP(MACAddress(ap->mac), ape); + addAP(MACAddress(ap->mac), ape, assertSafe); } } diff --git a/sensors/radio/setup/WiFiFingerprint.h b/sensors/radio/setup/WiFiFingerprint.h index 940faa4..2938c5d 100644 --- a/sensors/radio/setup/WiFiFingerprint.h +++ b/sensors/radio/setup/WiFiFingerprint.h @@ -57,6 +57,15 @@ struct WiFiFingerprint { } + /** get all measurements for the given MAC */ + std::vector getAllForMAC(const MACAddress& mac) const { + std::vector res; + for (const WiFiMeasurement& m : measurements.entries) { + if (m.getAP().getMAC() == mac) {res.push_back(m);} + } + return res; + } + /** serialize */ void write(std::ostream& out) const { out << "pos: " << pos_m.x << " " << pos_m.y << " " << pos_m.z << "\n"; diff --git a/sensors/radio/setup/WiFiFingerprints.h b/sensors/radio/setup/WiFiFingerprints.h index 9dd1b2f..8745b2b 100644 --- a/sensors/radio/setup/WiFiFingerprints.h +++ b/sensors/radio/setup/WiFiFingerprints.h @@ -15,22 +15,37 @@ class WiFiFingerprints { private: - /** the file to save the calibration model to */ - std::string file; +// /** the file to save the calibration model to */ +// std::string file; /** all fingerprints (position -> measurements) within the model */ std::vector fingerprints; public: - WiFiFingerprints(const std::string& file) : file(file) { - load(); + /** empty ctor */ + WiFiFingerprints() { + + } + + /** ctor from file */ + WiFiFingerprints(const std::string& file) { + load(file); } const std::vector& getFingerprints() const { return fingerprints; } + std::vector& getFingerprints() { + return fingerprints; + } + + /** attach the given fingerprint */ + void add(const WiFiFingerprint& fp) { + fingerprints.push_back(fp); + } + /** get all fingerprints that measured exactly the given mac [no VAP grouping] */ const std::vector getFingerprintsFor(const MACAddress& mac) const { @@ -58,7 +73,7 @@ public: } /** deserialize the model */ - void load() { + void load(const std::string& file) { // open and check std::ifstream inp(file.c_str()); @@ -87,7 +102,7 @@ public: /** serialize the model */ - void save() { + void save(const std::string& file) { // open and check std::ofstream out(file.c_str());