This repository has been archived on 2020-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
Files
Indoor/tests/sensors/pressure/TestBarometer.cpp
FrankE a2c9e575a2 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
2016-08-29 08:18:44 +02:00

126 lines
2.8 KiB
C++

#ifdef WITH_TESTS
#include "../../Tests.h"
#include "../../../sensors/pressure/RelativePressure.h"
#include "../../../sensors/pressure/PressureTendence.h"
#include <random>
TEST(Barometer, relCalib) {
RelativePressure baro;
baro.setCalibrationTimeframe(Timestamp::fromMS(3000));
ASSERT_EQ(0, baro.getBaseAvg());
ASSERT_EQ(0, baro.getSigma());
std::minstd_rand gen;
std::normal_distribution<float> dist(930, 0.15);
for (int i = 0; i <= 4000; i += 25) {
const Timestamp ts = Timestamp::fromMS(i);
baro.add(ts, BarometerData(dist(gen)));
if (i < 3000) { // not yet calibrated
ASSERT_EQ(0, baro.getBaseAvg());
ASSERT_EQ(0, baro.getSigma());
ASSERT_EQ(0, baro.getPressureRealtiveToStart());
} else { // calibrated
ASSERT_NEAR(930, baro.getBaseAvg(), 0.1);
ASSERT_NEAR(0.15, baro.getSigma(), 0.02);
}
}
// calibration OK?
ASSERT_NEAR(930, baro.getBaseAvg(), 0.1);
ASSERT_NEAR(0.15, baro.getSigma(), 0.02);
// get a HUGE value realtive to the calibration
baro.add(Timestamp::fromMS(9000), BarometerData(2000));
ASSERT_NEAR(2000-930, baro.getPressureRealtiveToStart(), 0.1);
// has no impact on calibration
ASSERT_NEAR(930, baro.getBaseAvg(), 0.1);
ASSERT_NEAR(0.15, baro.getSigma(), 0.02);
baro.reset();
// everything back to zero
ASSERT_EQ(0, baro.getBaseAvg());
ASSERT_EQ(0, baro.getSigma());
ASSERT_EQ(0, baro.getPressureRealtiveToStart());
}
#include <fstream>
TEST(Barometer, tendence) {
std::ifstream inp ("/tmp/baro1.dat");
const float timeframe = 5.0;
PressureTendence tend(Timestamp::fromSec(timeframe));
while (inp.is_open() && !inp.eof()) {
int msec;
float hPa;
inp >> msec;
inp >> hPa;
tend.add(Timestamp::fromMS(msec), BarometerData(hPa));
tend.get();
}
sleep(1000);
}
TEST(Barometer, tendence2) {
const float timeframe = 5.0;
PressureTendence tend(Timestamp::fromSec(timeframe));
ASSERT_EQ(0, tend.get());
const float noiseSigma = 0.04; // typical barometer noise
const float start = 930; // start at 930 hPa
const float incPerSec = 0.100; // and incremrent the pressure by 0.1 hPa per second
float curhPa = start;
// add zero-mean noise
std::minstd_rand gen;
std::normal_distribution<float> dist(0, noiseSigma);
// 10 Hz for 30 seconds
for (int i = 0; i < 30000; i += 100) {
// next pressure (add increment)
curhPa += sin(i/1200.0f) * 0.02;//incPerSec * 100 / 1000;
// add noise
float curhPaNoised = curhPa + dist(gen) + sin(i/200.0f)*0.1;
// calculate tendence
tend.add(Timestamp::fromMS(i), BarometerData(curhPaNoised));
std::cout << "w/o noise: " << curhPa << "\twith noise: " << curhPaNoised << "\ttendence/sec:" << (tend.get()/timeframe) << std::endl;
}
sleep(1000);
// tendence must be clear and smaller than the sigma
ASSERT_NEAR(incPerSec * timeframe, tend.get(), noiseSigma);
}
#endif