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/sensors/imu/StepDetection.h
k-a-z-u 72f083d32a worked on grid-walker
started adding code for synthetic sensor simulation based on given paths
2017-10-17 17:10:23 +02:00

207 lines
4.4 KiB
C++

#ifndef STEPDETECTION_H
#define STEPDETECTION_H
#include "AccelerometerData.h"
#include "../../data/Timestamp.h"
#include <cmath>
#include <vector>
#include <KLib/misc/gnuplot/Gnuplot.h>
#include <KLib/misc/gnuplot/GnuplotSplot.h>
#include <KLib/misc/gnuplot/GnuplotSplotElementLines.h>
#include <KLib/misc/gnuplot/GnuplotPlot.h>
#include <KLib/misc/gnuplot/GnuplotPlotElementLines.h>
#include "../../Assertions.h"
#include "../../math/MovingAverageTS.h"
/**
* simple step detection based on accelerometer magnitude.
* magnitude > threshold? -> step!
* block for several msec until detecting the next one
*/
class StepDetection {
private:
MovingAverageTS<float> avgLong;
MovingAverageTS<float> avgShort;
Timestamp blockUntil;
bool waitForUp = false;
const Timestamp blockTime = Timestamp::fromMS(250); // 150-250 looks good
const float upperThreshold = +0.4*0.6f; // + is usually smaller than down (look at graphs)
const float lowerThreshold = -1.5*0.6f; // the 0.8 is for testing!
#ifdef WITH_DEBUG_PLOT
K::Gnuplot gp;
K::GnuplotPlot plot;
K::GnuplotPlotElementLines lineDet;
K::GnuplotPlotElementLines lineX;
K::GnuplotPlotElementLines lineY;
K::GnuplotPlotElementLines lineZ;
Timestamp plotRef;
int plotCnt = 0;
#endif
public:
/** ctor */
StepDetection() : avgLong(Timestamp::fromMS(500), 0), avgShort(Timestamp::fromMS(40), 0) {
#ifdef WITH_DEBUG_PLOT
plot.add(&lineX); lineX.getStroke().getColor().setHexStr("#ff0000");
plot.add(&lineY); lineY.getStroke().getColor().setHexStr("#00ff00");
plot.add(&lineZ); lineZ.getStroke().getColor().setHexStr("#0000ff");
plot.add(&lineDet); lineDet.getStroke().getColor().setHexStr("#000000");
#endif
}
/** does the given data indicate a step? */
bool add(const Timestamp ts, const AccelerometerData& acc) {
// update averages
avgLong.add(ts, acc.magnitude());
avgShort.add(ts, acc.magnitude());
// difference between long-term-average (gravity) and very-short-time average
const float delta = avgShort.get() - avgLong.get();
bool step = false;
if (blockUntil > ts) {return false;}
// wait for a rising edge
if (waitForUp && delta > upperThreshold) {
blockUntil = ts + blockTime; // block some time
waitForUp = false;
}
// wait for a falling edge
if (!waitForUp && delta < lowerThreshold) {
blockUntil = ts + blockTime; // block some time
waitForUp = true;
step = true;
}
#ifdef WITH_DEBUG_PLOT
if (plotRef.isZero()) {plotRef = ts;}
const Timestamp tsPlot = (ts-plotRef);
//lines1.add( K::GnuplotPoint2((ts-ref).ms(), _delta) );
lineX.add( K::GnuplotPoint2(tsPlot.ms(), acc.x) );
lineY.add( K::GnuplotPoint2(tsPlot.ms(), acc.y) );
lineZ.add( K::GnuplotPoint2(tsPlot.ms(), acc.z) );
lineDet.add( K::GnuplotPoint2(tsPlot.ms(), delta) );
if (++plotCnt % 25 == 0) {
gp.draw(plot);
gp.flush();
//usleep(1000*25);
}
#endif
return step;
}
//private:
// /** low pass acc-magnitude */
// float avg1 = 0;
// /** even-more low-pass acc-magnitude */
// float avg2 = 0;
//private:
// class Stepper {
// private:
// /** block for 300 ms after every step */
// const Timestamp blockTime = Timestamp::fromMS(300);
// /** the threshold for detecting a spike as step */
// const float threshold = 0.30;
// /** block until the given timestamp before detecting additional steps */
// Timestamp blockUntil;
// public:
// /** is the given (relative!) magnitude (mag - ~9.81) a step? */
// bool isStep(const Timestamp ts, const float mag) {
// // still blocking
// if (ts < blockUntil) {
// return false;
// }
// // threshold reached? -> step!
// if (mag > threshold) {
// // block x milliseconds until detecting the next step
// blockUntil = ts + blockTime;
// // we have a step
// return true;
// }
// // no step
// return false;
// }
// };
// Stepper stepper;
//public:
// /** does the given data indicate a step? */
// bool add(const Timestamp ts, const AccelerometerData& acc) {
// avg1 = avg1 * 0.91 + acc.magnitude() * 0.09; // short-time average [filtered steps]
// avg2 = avg2 * 0.97 + acc.magnitude() * 0.03; // long-time average [gravity]
// // average maginitude must be > 9.0 to be stable enough to proceed
// if (avg2 > 9) {
// // gravity-free magnitude
// const float avg = avg1 - avg2;
// // detect steps
// return stepper.isStep(ts, avg);
// } else {
// return false;
// }
// }
};
#endif // STEPDETECTION_H