huge commit

- worked on about everything
- grid walker using plugable modules
- wifi models
- new distributions
- worked on geometric data-structures
- added typesafe timestamps
- worked on grid-building
- added sensor-classes
- added sensor analysis (step-detection, turn-detection)
- offline data reader
- many test-cases
This commit is contained in:
2016-08-29 08:18:44 +02:00
parent 99ee95ce7b
commit a2c9e575a2
94 changed files with 8298 additions and 257 deletions

View File

@@ -10,26 +10,50 @@
*/
class AccessPoint {
public:
private:
/** the AP's MAC-Address */
const MACAddress mac;
MACAddress mac;
/** the AP's readable SSID */
const std::string ssid;
/** OPTIONAL the AP's readable SSID */
std::string ssid;
public:
/** ctor */
/** empty ctor */
AccessPoint() {
;
}
/** ctor with MAC and SSID */
AccessPoint(const MACAddress& mac, const std::string& ssid) : mac(mac), ssid(ssid) {
;
}
/** ctor */
/** ctor with MAC and SSID */
AccessPoint(const std::string& mac, const std::string& ssid) : mac(mac), ssid(ssid) {
;
}
/** ctor with MAC and without SSID */
AccessPoint(const MACAddress& mac) : mac(mac), ssid() {
;
}
/** ctor with MAC and without SSID */
AccessPoint(const std::string& mac) : mac(mac), ssid() {
;
}
public:
/** get the AP's MAC address */
inline const MACAddress& getMAC() const {return mac;}
/** OPTIONAL: get the AP's ssid (if any) */
inline const std::string& getSSID() const {return ssid;}
};
#endif // ACCESSPOINT_H

View File

@@ -6,7 +6,7 @@
#include "../../floorplan/v2/Floorplan.h"
/**
* describes an access-point including its position
* describes an access-point including its position (in meter)
*/
class LocatedAccessPoint : public AccessPoint, public Point3 {

172
sensors/radio/VAPGrouper.h Normal file
View File

@@ -0,0 +1,172 @@
#ifndef VAPGROUPER_H
#define VAPGROUPER_H
#include <vector>
#include <unordered_map>
#include <cmath>
#include "../../math/Median.h"
#include "WiFiMeasurements.h"
class VAPGrouper {
public:
/** the mode denotes the algorithm that is used for grouping VAPs together */
enum class Mode {
/** group VAPs by setting the MAC's last digit to zero */
LAST_MAC_DIGIT_TO_ZERO,
};
/** describes how to calculate the final signal-strengh of the VAP-grouped entry */
enum class Aggregation {
/** use the average signal-strength of all grouped APs */
AVERAGE,
/** use the median signal-strength of all grouped APs */
MEDIAN,
/** use the maximum signal-strength of all grouped APs */
MAXIMUM,
};
private:
/** the mode to use for grouping VAPs */
const Mode mode;
/** the signal-strength aggregation algorithm to use */
const Aggregation agg;
public:
/** ctor */
VAPGrouper(const Mode mode, const Aggregation agg) : mode(mode), agg(agg) {
}
/** get a vap-grouped version of the given input */
WiFiMeasurements group(const WiFiMeasurements& original) const {
// first, group all VAPs into a vector [one vector per VAP-group]
std::unordered_map<MACAddress, std::vector<WiFiMeasurement>> grouped;
for (const WiFiMeasurement& m : original.entries) {
// the vap-base-mac this entry belongs to
const MACAddress baseMAC = getBaseMAC(m.getAP().getMAC());
grouped[baseMAC].push_back(m);
}
// output
WiFiMeasurements result;
// perform aggregation on each VAP-group
for (auto it : grouped) {
const MACAddress& base = it.first;
const std::vector<WiFiMeasurement>& vaps = it.second;
// group all VAPs into one measurement
const WiFiMeasurement groupedMeasurement = groupVAPs(base, vaps);
// add it to the result-vector
result.entries.push_back(groupedMeasurement);
}
// done
return result;
}
/** get the VAP-base-MAC-Address that is the same for all APs that belong to a VAP-Group */
MACAddress getBaseMAC(const MACAddress& mac) const {
switch(mode) {
case Mode::LAST_MAC_DIGIT_TO_ZERO: return lastMacDigitToZero(mac);
default: throw Exception("unsupported vap-grouping mode given");
}
}
private:
/** combine all of the given VAPs into one entry using the configured aggregation method */
WiFiMeasurement groupVAPs(const MACAddress& baseMAC, const std::vector<WiFiMeasurement>& vaps) const {
// the resulting entry is an AP with the base-MAC all of the given VAPs have in common
const AccessPoint baseAP(baseMAC);
// the resultign timestamp
const Timestamp baseTS = vaps.front().getTimestamp();
// calculate the rssi using the configured aggregate function
float rssi = NAN;
switch(agg) {
case Aggregation::AVERAGE: rssi = getAVG(vaps); break;
case Aggregation::MEDIAN: rssi = getMedian(vaps); break;
case Aggregation::MAXIMUM: rssi = getMax(vaps); break;
default: throw Exception("unsupported vap-aggregation method");
}
// create the result measurement
return WiFiMeasurement(baseAP, rssi, baseTS);
}
private:
/** get the average signal strength */
inline float getAVG(const std::vector<WiFiMeasurement>& vaps) const {
float rssi = 0;
for (const WiFiMeasurement& vap : vaps) {
rssi += vap.getRSSI();
}
return rssi / vaps.size();
}
/** get the median signal strength */
inline float getMedian(const std::vector<WiFiMeasurement>& vaps) const {
Median<float> median;
for (const WiFiMeasurement& vap : vaps) {
median.add(vap.getRSSI());
}
return median.get();
}
/** get the maximum signal strength */
inline float getMax(const std::vector<WiFiMeasurement>& vaps) const {
float max = -9999999;
for (const WiFiMeasurement& vap : vaps) {
if (vap.getRSSI() > max) {max = vap.getRSSI();}
}
return max;
}
private:
/** convert the MAC by setting the last (right-most) digit to zero (0) */
static inline MACAddress lastMacDigitToZero(const MACAddress& mac) {
std::string str = mac.asString();
str.back() = '0';
return MACAddress(str);
}
};
#endif // VAPGROUPER_H

View File

@@ -5,6 +5,7 @@
#include "model/WiFiModel.h"
#include "WiFiGridNode.h"
#include "../../Assertions.h"
#include "../../floorplan/v2/Floorplan.h"
#include <fstream>
@@ -16,6 +17,25 @@ class WiFiGridEstimator {
public:
/**
* convenience method
*/
template <typename Node> static void estimate(Grid<Node>& grid, WiFiModel& mdl, const Floorplan::IndoorMap* im) {
// list of all APs
std::vector<LocatedAccessPoint> aps;
for (const Floorplan::Floor* f : im->floors) {
for (const Floorplan::AccessPoint* ap : f->accesspoints) {
aps.push_back(LocatedAccessPoint(*ap));
}
}
// perform estimation
estimate(grid, mdl, aps);
}
/**
* perform a signal-strength estimation for all of the given access points
* using the provided signal-strength prediction model.

View File

@@ -79,13 +79,6 @@ template <int maxAccessPoints> struct WiFiGridNode {
WiFiGridNode() {;}
/** shared vector of all accesspoints the are present within the map. the IDX referes to this vector */
static std::vector<AccessPoint>& getMapAPs() {
static std::vector<AccessPoint> list;
return list;
}
/** get the maximum number of APs each node is able to store */
int getMaxAPs() const {return maxAccessPoints;}
@@ -104,7 +97,7 @@ template <int maxAccessPoints> struct WiFiGridNode {
float getRSSI(const MACAddress mac) const {
for (const WiFiGridNodeAP ap : strongestAPs) {
if (!ap.isValid()) {break;} // reached the end
if (getMapAPs()[ap.getAPIdx()].mac == mac) {return ap.getRSSI();}
if (getMapAPs()[ap.getAPIdx()].getMAC() == mac) {return ap.getRSSI();}
}
return 0;
}
@@ -117,6 +110,44 @@ template <int maxAccessPoints> struct WiFiGridNode {
return getMaxAPs();
}
/** shared vector of all accesspoints the are present within the map. the IDX referes to this vector */
static std::vector<AccessPoint>& getMapAPs() {
static std::vector<AccessPoint> list;
return list;
}
protected:
/** serialize static members */
static void staticSerialize(std::ostream& out) {
// number of APs within map
const int numAPs = getMapAPs().size();
// serialize number and APs within map
out.write((const char*) &numAPs, sizeof(numAPs));
out.write((const char*) getMapAPs().data(), sizeof(getMapAPs()[0])*numAPs);
}
/** deserialize static members */
static void staticDeserialize(std::istream& inp) {
// get number of APs within map
int numAPs;
inp.read((char*) &numAPs, sizeof(numAPs));
// allocate
getMapAPs().resize(numAPs);
// deserialize APs within map
inp.read((char*) getMapAPs().data(), sizeof(getMapAPs()[0])*numAPs);
}
};// __attribute__((packed));

View File

@@ -0,0 +1,50 @@
#ifndef WIFIMEASUREMENT_H
#define WIFIMEASUREMENT_H
#include "AccessPoint.h"
#include "../../data/Timestamp.h"
/**
* describes a measurement received for one access-point at a given time
*/
class WiFiMeasurement {
private:
friend class VAPGrouper;
/** the access-point we got a measurement for */
AccessPoint ap;
/** the measured signal strength */
float rssi;
/** OPTIONAL. timestamp the measurement was recorded at */
Timestamp ts;
public:
/** ctor */
WiFiMeasurement(const AccessPoint& ap, const float rssi) : ap(ap), rssi(rssi) {
;
}
/** ctor with timestamp */
WiFiMeasurement(const AccessPoint& ap, const float rssi, const Timestamp ts) : ap(ap), rssi(rssi), ts(ts) {
;
}
public:
/** get the AP we got the measurement for */
const AccessPoint& getAP() const {return ap;}
/** get the measurement's signal strength */
float getRSSI() const {return rssi;}
/** OPTIONAL: get the measurement's timestamp (if known!) */
const Timestamp& getTimestamp() const {return ts;}
};
#endif // WIFIMEASUREMENT_H

View File

@@ -0,0 +1,18 @@
#ifndef WIFIMEASUREMENTS_H
#define WIFIMEASUREMENTS_H
#include <vector>
#include "WiFiMeasurement.h"
/**
* group of several wifi measurements
*/
struct WiFiMeasurements {
/** all contained measurements */
std::vector<WiFiMeasurement> entries;
};
#endif // WIFIMEASUREMENTS_H

View File

@@ -1,100 +0,0 @@
#ifndef WIFIOBSERVATION_H
#define WIFIOBSERVATION_H
#include <vector>
#include <fstream>
#include "../MACAddress.h"
#include "../../math/Distributions.h"
/** observed one AP with the given signal strength */
struct WiFiObservationEntry {
/** AP's MAC address */
MACAddress mac;
/** AP's RSSI */
float rssi;
/** ctor */
WiFiObservationEntry(const MACAddress& mac, const float rssi) : mac(mac), rssi(rssi) {;}
};
/** all observed APs and their signal strength */
struct WiFiObservation {
/** one entry per AP */
std::vector<WiFiObservationEntry> entries;
};
class WiFiObserver {
private:
float sigma = 8.0f;
public:
/** ctor */
WiFiObserver(const float sigma) : sigma(sigma) {
;
}
/**
* get the given GridNode's probability based on the provided WiFi measurements
*/
template <typename Node> double getProbability(const Node& n, const WiFiObservation& obs, const int age_ms = 0) {
double prob = 0;
// sigma grows with measurement age
const double sigma = this->sigma * (1 + age_ms / 500.0f);
// process each observed AP
for (const WiFiObservationEntry& ap : obs.entries) {
// the RSSI from the scan
const float measuredRSSI = ap.rssi;
// the RSSI from the model (if available!)
const float modelRSSI = n.getRSSI(ap.mac);
// no model RSSI available?
if (modelRSSI == 0) {continue;}
// compare both
const double p = Distribution::Normal<double>::getProbability(measuredRSSI, sigma, modelRSSI);
// adjust using log
prob += std::log(p);
}
//return std::pow(std::exp(prob), 0.1);
return std::exp(prob);
}
/** gnuplot debug dump */
template <typename Node> void dump(Grid<Node>& grid, const WiFiObservation& obs, const std::string& fileName) {
std::ofstream out(fileName);
out << "splot '-' with points palette\n";
for (const Node& n : grid) {
const float p = getProbability(n, obs);
out << n.x_cm << " " << n.y_cm << " " << n.z_cm << " " << p << "\n";
}
out << "e\n";
out.close();
}
};
#endif // WIFIOBSERVATION_H

View File

@@ -0,0 +1,15 @@
#ifndef WIFPROBABILITY_H
#define WIFPROBABILITY_H
#include "WiFiMeasurements.h"
/**
* base class for all WiFi probability calculators.
* such a calculator determines the probabilty for a location (e.g. x,y,z)
* given WiFiMeasurements
*/
class WiFiProbability {
};
#endif // WIFPROBABILITY_H

View File

@@ -0,0 +1,77 @@
#ifndef WIFIPROBABILITYFREE_H
#define WIFIPROBABILITYFREE_H
#include "WiFiProbability.h"
#include "model/WiFiModel.h"
#include "../../math/Distributions.h"
#include <unordered_map>
/**
* compare WiFi-Measurements within LIVE (exact) predictions
* just based on the distance to the access-point
*/
class WiFiObserverFree : public WiFiProbability {
private:
const float sigma = 8.0f;
const float sigmaPerSecond = 1.5f;
WiFiModel& model;
std::unordered_map<MACAddress, LocatedAccessPoint> aps;
Floorplan::IndoorMap* map;
public:
WiFiObserverFree(const float sigma, WiFiModel& model, const std::vector<LocatedAccessPoint>& aps) : sigma(sigma), model(model) {
for (const LocatedAccessPoint& ap : aps) {
this->aps.insert(std::pair<MACAddress, LocatedAccessPoint>(ap.getMAC(), ap));
}
}
double getProbability(const Point3& pos, const Timestamp curTime, const WiFiMeasurements& obs) const {
double prob = 1.0;
for (const WiFiMeasurement& entry : obs.entries) {
auto it = aps.find(entry.getAP().getMAC());
// AP is unknown
if (it == aps.end()) {continue;}
// get the AP
const LocatedAccessPoint& ap = it->second;
// model and scan rssi
const float modelRSSI = model.getRSSI(ap, pos);
const float scanRSSI = entry.getRSSI();
// the measurement's age
const Timestamp age = curTime - entry.getTimestamp();
// sigma grows with measurement age
const float sigma = this->sigma + this->sigmaPerSecond * age.sec();
// update probability
prob *= Distribution::Normal<double>::getProbability(modelRSSI, sigma, scanRSSI);
}
return prob;
}
template <typename Node> double getProbability(const Node& n, const Timestamp curTime, const WiFiMeasurements& obs, const int age_ms = 0) const {
throw "todo??";
}
};
#endif // WIFIPROBABILITYFREE_H

View File

@@ -0,0 +1,92 @@
#ifndef WIFIPROBABILITYGRID_H
#define WIFIPROBABILITYGRID_H
#include <fstream>
#include "../../math/Distributions.h"
#include "../../data/Timestamp.h"
#include "WiFiProbability.h"
/**
* probability is calculated by comparing pre-calculated wifi-signal-strengths
* attached to each grid-node with a given WiFiMeasurements data structure
*/
class WiFiObserverGrid : public WiFiProbability {
private:
/** base-sigma to use for comparison */
float sigma = 8.0f;
/** additional sigma-per-second (measurement age) to*/
float sigmaPerSecond = 1.5;
public:
/** ctor with uncertainty */
WiFiObserverGrid(const float sigma) : sigma(sigma) {
;
}
/**
* get the given GridNode's probability.
* compares the predicted signal-strengths stored on the given node
* with the provided WiFi measurements
*/
template <typename Node> double getProbability(const Node& n, const Timestamp curTime, const WiFiMeasurements& obs) const {
double prob = 0;
// process each observed measurement
for (const WiFiMeasurement& measurement : obs.entries) {
// determine the age for this measurement
const Timestamp age = curTime - measurement.getTimestamp();
// sigma grows with measurement age
const float sigma = this->sigma + this->sigmaPerSecond * age.sec();
// the RSSI from the scan
const float measuredRSSI = measurement.getRSSI();
// the RSSI from the model (if available!)
const float modelRSSI = n.getRSSI(measurement.getAP().getMAC());
// no model RSSI available?
if (modelRSSI == 0) {continue;}
// compare both
const double p = Distribution::Normal<double>::getProbability(measuredRSSI, sigma, modelRSSI);
// adjust using log
prob += std::log(p);
}
//return std::pow(std::exp(prob), 0.1);
return std::exp(prob);
}
/** gnuplot debug dump */
template <typename Node> void dump(Grid<Node>& grid, const Timestamp curTime, const WiFiMeasurements& obs, const std::string& fileName) {
std::ofstream out(fileName);
out << "splot '-' with points palette\n";
for (const Node& n : grid) {
const float p = getProbability(n, curTime, obs);
out << n.x_cm << " " << n.y_cm << " " << n.z_cm << " " << p << "\n";
}
out << "e\n";
out.close();
}
};
#endif // WIFIPROBABILITYGRID_H

View File

@@ -0,0 +1,65 @@
#ifndef WIFIMODELLOGDISTCEILING_H
#define WIFIMODELLOGDISTCEILING_H
#include "../../../floorplan/v2/Floorplan.h"
#include "WiFiModel.h"
#include "LogDistanceModel.h"
/**
* signal-strength estimation using log-distance model
* including ceilings between AP and position
*/
class WiFiModelLogDistCeiling : public WiFiModel {
private:
float txp; // sending power (-40)
float exp; // path-loss-exponent (~2.0 - 4.0)
float waf; // attenuation per ceiling/floor (~-8.0)
/** position (height) of all ceilings (in meter) */
std::vector<float> ceilingsAtHeight_m;
public:
/** ctor */
WiFiModelLogDistCeiling(const float txp, const float exp, const float waf, const Floorplan::IndoorMap* map) : txp(txp), exp(exp), waf(waf) {
Assert::isTrue(waf <= 0, "WAF must be a negative number!");
// position of all ceilings
for (Floorplan::Floor* f : map->floors) {
ceilingsAtHeight_m.push_back(f->atHeight);
}
}
/** get the given access-point's RSSI at the provided location */
float getRSSI(const LocatedAccessPoint& ap, const Point3 p) override {
const int numCeilings = numCeilingsBetween(ap.z, p.z);
const float rssi = LogDistanceModel::distanceToRssi(txp, exp, ap.getDistance(p));
return rssi + (numCeilings * waf);
}
private:
/** get the number of ceilings between z1 and z2 */
int numCeilingsBetween(const float z1, const float z2) const {
int cnt = 0;
const float zMin = std::min(z1, z2);
const float zMax = std::max(z1, z2);
for (float z : ceilingsAtHeight_m) {
if (zMin < z && zMax > z) {++cnt;}
}
return cnt;
}
};
#endif // WIFIMODELLOGDISTCEILING_H