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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "generictiltsensor.h"
#include <QDebug>
#include <qmath.h>
char const * const GenericTiltSensor::id("generic.tilt");
GenericTiltSensor::GenericTiltSensor(QSensor *sensor)
: QSensorBackend(sensor)
, radAccuracy(qDegreesToRadians(qreal(1)))
, pitch(0)
, roll(0)
, calibratedPitch(0)
, calibratedRoll(0)
, xRotation(0)
, yRotation(0)
{
accelerometer = new QAccelerometer(this);
accelerometer->addFilter(this);
accelerometer->connectToBackend();
setReading<QTiltReading>(&m_reading);
setDataRates(accelerometer);
}
void GenericTiltSensor::start()
{
accelerometer->setDataRate(sensor()->dataRate());
accelerometer->setAlwaysOn(sensor()->isAlwaysOn());
accelerometer->start();
if (!accelerometer->isActive())
sensorStopped();
if (accelerometer->isBusy())
sensorBusy();
}
void GenericTiltSensor::stop()
{
accelerometer->stop();
}
/*
Angle between Ground and X
*/
static inline qreal calcPitch(double Ax, double Ay, double Az)
{
return qAtan2(-Ax, qSqrt(Ay * Ay + Az * Az));
}
/*
Angle between Ground and Y
*/
static inline qreal calcRoll(double /*Ax*/, double Ay, double Az)
{
return qAtan2(Ay, Az);
}
void GenericTiltSensor::calibrate()
{
calibratedPitch = pitch;
calibratedRoll = roll;
}
bool GenericTiltSensor::filter(QAccelerometerReading *reading)
{
/*
z y
| /
|/___ x
*/
qreal ax = reading->x();
qreal ay = reading->y();
qreal az = reading->z();
#ifdef LOGCALIBRATION
qDebug() << "------------ new value -----------";
qDebug() << "old _pitch: " << pitch;
qDebug() << "old _roll: " << roll;
qDebug() << "_calibratedPitch: " << calibratedPitch;
qDebug() << "_calibratedRoll: " << calibratedRoll;
#endif
pitch = calcPitch(ax, ay, az);
roll = calcRoll (ax, ay, az);
#ifdef LOGCALIBRATION
qDebug() << "_pitch: " << pitch;
qDebug() << "_roll: " << roll;
#endif
qreal xrot = roll - calibratedRoll;
qreal yrot = pitch - calibratedPitch;
//get angle between 0 and 180 or 0 -180
xrot = qAtan2(qSin(xrot), qCos(xrot));
yrot = qAtan2(qSin(yrot), qCos(yrot));
#ifdef LOGCALIBRATION
qDebug() << "new xrot: " << xrot;
qDebug() << "new yrot: " << yrot;
qDebug() << "----------------------------------";
#endif
qreal dxrot = qRadiansToDegrees(xrot) - xRotation;
qreal dyrot = qRadiansToDegrees(yrot) - yRotation;
if (dxrot < 0) dxrot = -dxrot;
if (dyrot < 0) dyrot = -dyrot;
bool setNewReading = false;
if (dxrot >= qRadiansToDegrees(radAccuracy) || !sensor()->skipDuplicates()) {
xRotation = qRadiansToDegrees(xrot);
setNewReading = true;
}
if (dyrot >= qRadiansToDegrees(radAccuracy) || !sensor()->skipDuplicates()) {
yRotation = qRadiansToDegrees(yrot);
setNewReading = true;
}
if (setNewReading || m_reading.timestamp() == 0) {
m_reading.setTimestamp(reading->timestamp());
m_reading.setXRotation(xRotation);
m_reading.setYRotation(yRotation);
newReadingAvailable();
}
return false;
}
bool GenericTiltSensor::isFeatureSupported(QSensor::Feature feature) const
{
return (feature == QSensor::Feature::SkipDuplicates);
}
|