forked from bardia16/CPS_Android_HW
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgyroscope.h
83 lines (66 loc) · 1.9 KB
/
gyroscope.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
#ifndef GYROSCOPE_H
#define GYROSCOPE_H
#include <QObject>
#include <QtSensors/QGyroscope>
#include <QTimer>
#define calibrationDuration 1000 // 1 second
#define gyro_sampling_interval 10 // 10 ms
#define gyro_threshold 0.5 // Threshold for gyro noise
class GyroscopeKalmanFilter
{
public:
GyroscopeKalmanFilter(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;
}
void reset(double initialValue)
{
p = q;
x = initialValue;
}
private:
double q; // Process noise covariance
double r; // Measurement noise covariance
double p; // Estimation error covariance
double x; // Value
};
class Gyroscope : public QObject
{
Q_OBJECT
Q_PROPERTY(bool active READ isActive NOTIFY activeChanged)
public:
explicit Gyroscope(QObject *parent = nullptr);
~Gyroscope();
bool isActive() const { return sensor->isActive(); }
Q_INVOKABLE void calibration();
Q_INVOKABLE void reset();
public slots:
void start();
void stop();
signals:
void readingUpdated(const QString &output);
void activeChanged();
void calibrationFinished(const QString &output);
void newRotation(double alpha);
private slots:
void onSensorReadingChanged();
void onCalibrationReadingChanged();
void onCalibrationFinished();
private:
QGyroscope *sensor;
QTimer *timer;
QTimer *calibrationTimer;
QVector<double> z_values;
double z_bias;
double currentAngle;
GyroscopeKalmanFilter angleKalman;
};
#endif // GYROSCOPE_H