173 lines
4.1 KiB
C++
173 lines
4.1 KiB
C++
#ifdef WITH_TESTS
|
|
|
|
#include "../../Tests.h"
|
|
|
|
#include "../../../sensors/pressure/RelativePressure.h"
|
|
#include "../../../sensors/pressure/PressureTendence.h"
|
|
#include "../../../sensors/pressure/ActivityButterPressure.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, LIVE_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, LIVE_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);
|
|
|
|
}
|
|
|
|
TEST(Barometer, Activity) {
|
|
ActivityButterPressure act;
|
|
|
|
//read file
|
|
std::string line;
|
|
std::string filename = getDataFile("baro/logfile_UAH_R2_S3_baro.dat");
|
|
std::ifstream infile(filename);
|
|
|
|
while (std::getline(infile, line))
|
|
{
|
|
std::istringstream iss(line);
|
|
int ts;
|
|
double value;
|
|
|
|
while (iss >> ts >> value) {
|
|
ActivityButterPressure::Activity currentAct = act.add(Timestamp::fromMS(ts), BarometerData(value));
|
|
}
|
|
}
|
|
|
|
std::vector<float> sum = act.getSumHistory();
|
|
std::vector<float> interpolated = act.getInterpolatedHistory();
|
|
std::vector<float> raw = act.getSensorHistory();
|
|
std::vector<float> butter = act.getOutputHistory();
|
|
std::vector<ActivityButterPressure::History> actHist = act.getActHistory();
|
|
|
|
K::Gnuplot gp;
|
|
K::GnuplotPlot plot;
|
|
K::GnuplotPlotElementLines rawLines;
|
|
|
|
for(int i=0; i < actHist.size()-1; ++i){
|
|
|
|
K::GnuplotPoint2 input_p1(actHist[i].ts.sec(), actHist[i].data.hPa);
|
|
K::GnuplotPoint2 input_p2(actHist[i+1].ts.sec(), actHist[i+1].data.hPa);
|
|
|
|
rawLines.addSegment(input_p1, input_p2);
|
|
}
|
|
|
|
plot.add(&rawLines);
|
|
|
|
gp.draw(plot);
|
|
gp.flush();
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
|
|
#endif
|