Add variance reporting (#1135)

Add variance to mqtt
This commit is contained in:
Darrell 2023-11-29 16:48:08 -05:00 committed by GitHub
parent 472f557e7a
commit c002b1fd80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 1934 additions and 1909 deletions

View File

@ -422,6 +422,7 @@ bool BleFingerprint::seen(BLEAdvertisedDevice *advertisedDevice) {
raw = pow(10, float(get1mRssi() - rssi) / (10.0f * BleFingerprintCollection::absorption));
filteredDistance.addMeasurement(raw);
dist = filteredDistance.getDistance();
vari = filteredDistance.getVariance();
if (!added) {
added = true;
@ -442,6 +443,7 @@ bool BleFingerprint::fill(JsonObject *doc) {
if (isnormal(raw)) (*doc)[F("raw")] = serialized(String(raw, 2));
if (isnormal(dist)) (*doc)[F("distance")] = serialized(String(dist, 2));
if (isnormal(vari)) (*doc)[F("var")] = serialized(String(vari, 2));
if (close) (*doc)[F("close")] = true;
(*doc)[F("int")] = (millis() - firstSeenMillis) / seenCount;

View File

@ -132,7 +132,7 @@ class BleFingerprint {
int rssi = NO_RSSI;
int8_t calRssi = NO_RSSI, bcnRssi = NO_RSSI, mdRssi = NO_RSSI, asRssi = NO_RSSI;
unsigned int qryAttempts = 0, qryDelayMillis = 0;
float raw = 0, dist = 0, lastReported = 0, temp = 0, humidity = 0;
float raw = 0, dist = 0, vari = 0, lastReported = 0, temp = 0, humidity = 0;
unsigned long firstSeenMillis, lastSeenMillis = 0, lastReportedMillis = 0, lastQryMillis = 0;
unsigned long seenCount = 1, lastSeenCount = 0;
uint16_t mv = 0;

View File

@ -7,7 +7,7 @@
#include <vector>
FilteredDistance::FilteredDistance(float minCutoff, float beta, float dcutoff)
: minCutoff(minCutoff), beta(beta), dcutoff(dcutoff), x(0), dx(0), lastDist(0), lastTime(0), total(0), readIndex(0) {
: minCutoff(minCutoff), beta(beta), dcutoff(dcutoff), x(0), dx(0), lastDist(0), lastTime(0), total(0), totalSquared(0), readIndex(0) {
}
void FilteredDistance::initSpike(float dist) {
@ -15,12 +15,17 @@ void FilteredDistance::initSpike(float dist) {
readings[i] = dist;
}
total = dist * NUM_READINGS;
totalSquared = dist * dist * NUM_READINGS; // Initialize sum of squared distances
}
float FilteredDistance::removeSpike(float dist) {
total -= readings[readIndex]; // Subtract the last reading
readings[readIndex] = dist; // Read the sensor
totalSquared -= readings[readIndex] * readings[readIndex]; // Subtract the square of the last reading
readings[readIndex] = dist; // Read the sensor
total += readings[readIndex]; // Add the reading to the total
totalSquared += readings[readIndex] * readings[readIndex]; // Add the square of the reading
readIndex = (readIndex + 1) % NUM_READINGS; // Advance to the next position in the array
auto average = total / static_cast<float>(NUM_READINGS); // Calculate the average
@ -62,3 +67,9 @@ float FilteredDistance::getAlpha(float cutoff, float dT) {
float tau = 1.0f / (2 * M_PI * cutoff);
return 1.0f / (1.0f + tau / dT);
}
const float FilteredDistance::getVariance() const {
float mean = total / static_cast<float>(NUM_READINGS);
float meanOfSquares = totalSquared / static_cast<float>(NUM_READINGS);
return meanOfSquares - (mean * mean); // Variance formula: E(X^2) - (E(X))^2
}

View File

@ -4,7 +4,7 @@
#include <Arduino.h>
#define SPIKE_THRESHOLD 1.0f // Threshold for spike detection
#define NUM_READINGS 10 // Number of readings to keep track of
#define NUM_READINGS 12 // Number of readings to keep track of
class FilteredDistance {
public:
@ -12,6 +12,8 @@ class FilteredDistance {
void addMeasurement(float dist);
const float getMedianDistance() const;
const float getDistance() const;
const float getVariance() const;
bool hasValue() const { return lastTime != 0; }
private:
@ -27,6 +29,7 @@ class FilteredDistance {
float readings[NUM_READINGS]; // Array to store readings
int readIndex; // Current position in the array
float total; // Total of the readings
float totalSquared; // Total of the squared readings
void initSpike(float dist);
float removeSpike(float dist);

File diff suppressed because it is too large Load Diff

View File

@ -31,6 +31,14 @@
sortable: true,
class: "px-0 py-0 whitespace-nowrap",
},
{
key: "var",
title: "Var",
value: (v) => v.var ?? 0,
renderValue: (v) => v.var === undefined ? "n/a" : `${v.var?.toLocaleString(undefined, { minimumFractionDigits: 2 })} m`,
sortable: true,
class: "px-0 py-0 whitespace-nowrap",
},
{
key: "id",
title: "ID",