-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccelerometer.h
88 lines (71 loc) · 2.21 KB
/
accelerometer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef ACCELEROMETER_H
#define ACCELEROMETER_H
#include <QObject>
#include <QtSensors/QAccelerometer>
#include <QTimer>
#include <QVector>
#include <QVector2D>
#define frictional_accel 0.5
#define accel_threshold 0.35
#define calibrationDuration 1000 // 1sec
#define accel_sampling_interval 10 // 10ms = 0.01sec
class KalmanFilter
{
public:
KalmanFilter(double processNoise, double measurementNoise, double estimationError, double initialValue)
: q(processNoise), r(measurementNoise), p(estimationError), x(initialValue) {}
double update(double measurement)
{
// Prediction update
p = p + q;
// Measurement update
double k = p / (p + r); // Kalman gain
x = x + k * (measurement - x); // Update estimate
p = (1 - k) * p; // Update error covariance
return x;
}
private:
double q; // Process noise covariance
double r; // Measurement noise covariance
double p; // Estimation error covariance
double x; // Value
};
class Accelerometer : public QObject
{
Q_OBJECT
Q_PROPERTY(bool active READ isActive NOTIFY activeChanged)
public:
explicit Accelerometer(QObject *parent = nullptr);
~Accelerometer();
bool isActive() const { return sensor->isActive(); }
Q_INVOKABLE double getXBias() const { return x_bias; }
Q_INVOKABLE double getYBias() const { return y_bias; }
public slots:
void start();
void stop();
void calibration();
signals:
void readingUpdated(const QString &output);
void activeChanged();
void calibrationFinished(const QString &output);
void newAcceleration(double x, double y, double velocityX, double velocityY);
private slots:
void onSensorReadingChanged();
void onCalibrationReadingChanged();
void onCalibrationFinished();
private:
QAccelerometer *sensor;
QTimer *timer;
QTimer *calibrationTimer;
QVector<double> x_values;
QVector<double> y_values;
double x_bias;
double y_bias;
KalmanFilter xKalman; // Kalman filter for x-axis
KalmanFilter yKalman; // Kalman filter for y-axis
double velocity;
double velocityX;
double velocityY;
QVector2D frictionalAccel(qreal velocityX, qreal velocityY);
};
#endif // ACCELEROMETER_H