1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtSensors module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qslamgesturerecognizer.h" |
41 | #include "qtsensorgesturesensorhandler.h" |
42 | |
43 | |
44 | #include <QtCore/qmath.h> |
45 | |
46 | QT_BEGIN_NAMESPACE |
47 | |
48 | QSlamSensorGestureRecognizer::QSlamSensorGestureRecognizer(QObject *parent) : |
49 | QSensorGestureRecognizer(parent), |
50 | orientationReading(0), |
51 | accelRange(0), |
52 | active(0), |
53 | lastX(0), |
54 | lastY(0), |
55 | lastZ(0), |
56 | detectedX(0), |
57 | detecting(0), |
58 | accelX(0), |
59 | roll(0), |
60 | resting(0), |
61 | lastTimestamp(0), |
62 | lapsedTime(0), |
63 | timerActive(0) |
64 | { |
65 | } |
66 | |
67 | QSlamSensorGestureRecognizer::~QSlamSensorGestureRecognizer() |
68 | { |
69 | } |
70 | |
71 | void QSlamSensorGestureRecognizer::create() |
72 | { |
73 | } |
74 | |
75 | |
76 | QString QSlamSensorGestureRecognizer::id() const |
77 | { |
78 | return QString("QtSensors.slam" ); |
79 | } |
80 | |
81 | bool QSlamSensorGestureRecognizer::start() |
82 | { |
83 | if (QtSensorGestureSensorHandler::instance()->startSensor(sensor: QtSensorGestureSensorHandler::Accel)) { |
84 | if (QtSensorGestureSensorHandler::instance()->startSensor(sensor: QtSensorGestureSensorHandler::Orientation)) { |
85 | active = true; |
86 | accelRange = QtSensorGestureSensorHandler::instance()->accelRange; |
87 | connect(sender: QtSensorGestureSensorHandler::instance(),SIGNAL(orientationReadingChanged(QOrientationReading*)), |
88 | receiver: this,SLOT(orientationReadingChanged(QOrientationReading*))); |
89 | |
90 | connect(sender: QtSensorGestureSensorHandler::instance(),SIGNAL(accelReadingChanged(QAccelerometerReading*)), |
91 | receiver: this,SLOT(accelChanged(QAccelerometerReading*))); |
92 | } else { |
93 | QtSensorGestureSensorHandler::instance()->stopSensor(sensor: QtSensorGestureSensorHandler::Accel); |
94 | active = false; |
95 | } |
96 | } else { |
97 | active = false; |
98 | } |
99 | return active; |
100 | } |
101 | |
102 | bool QSlamSensorGestureRecognizer::stop() |
103 | { |
104 | QtSensorGestureSensorHandler::instance()->stopSensor(sensor: QtSensorGestureSensorHandler::Accel); |
105 | QtSensorGestureSensorHandler::instance()->stopSensor(sensor: QtSensorGestureSensorHandler::Orientation); |
106 | disconnect(sender: QtSensorGestureSensorHandler::instance(),SIGNAL(orientationReadingChanged(QOrientationReading*)), |
107 | receiver: this,SLOT(orientationReadingChanged(QOrientationReading*))); |
108 | |
109 | disconnect(sender: QtSensorGestureSensorHandler::instance(),SIGNAL(accelReadingChanged(QAccelerometerReading*)), |
110 | receiver: this,SLOT(accelChanged(QAccelerometerReading*))); |
111 | detecting = false; |
112 | restingList.clear(); |
113 | active = false; |
114 | return active; |
115 | } |
116 | |
117 | bool QSlamSensorGestureRecognizer::isActive() |
118 | { |
119 | return active; |
120 | } |
121 | |
122 | void QSlamSensorGestureRecognizer::orientationReadingChanged(QOrientationReading *reading) |
123 | { |
124 | orientationReading = reading; |
125 | } |
126 | |
127 | #define SLAM_DETECTION_FACTOR 0.3 // 11.7 |
128 | #define SLAM_RESTING_FACTOR 2.5 |
129 | #define SLAM_RESTING_COUNT 5 |
130 | #define SLAM_ZERO_FACTOR .02 |
131 | |
132 | void QSlamSensorGestureRecognizer::accelChanged(QAccelerometerReading *reading) |
133 | { |
134 | const qreal x = reading->x(); |
135 | const qreal y = reading->y(); |
136 | const qreal z = reading->z(); |
137 | quint64 timestamp = reading->timestamp(); |
138 | |
139 | if (qAbs(t: lastX - x) < SLAM_RESTING_FACTOR |
140 | && qAbs(t: lastY - y) < SLAM_RESTING_FACTOR |
141 | && qAbs(t: lastZ - z) < SLAM_RESTING_FACTOR) { |
142 | resting = true; |
143 | } else { |
144 | resting = false; |
145 | } |
146 | |
147 | if (restingList.count() > SLAM_RESTING_COUNT) |
148 | restingList.removeLast(); |
149 | restingList.insert(i: 0, t: resting); |
150 | |
151 | |
152 | if (timerActive && lastTimestamp > 0) |
153 | lapsedTime += (timestamp - lastTimestamp )/1000; |
154 | |
155 | if (timerActive && lapsedTime >= 250) { |
156 | doSlam(); |
157 | } |
158 | lastTimestamp = timestamp; |
159 | |
160 | if (orientationReading == 0) { |
161 | return; |
162 | } |
163 | |
164 | const qreal difference = lastX - x; |
165 | |
166 | if (!detecting |
167 | && orientationReading->orientation() == QOrientationReading::TopUp |
168 | && resting |
169 | && hasBeenResting()) { |
170 | detectedX = x; |
171 | // start of gesture |
172 | detecting = true; |
173 | if (difference > 0) |
174 | wasNegative = false; |
175 | else |
176 | wasNegative = true; |
177 | restingList.clear(); |
178 | } |
179 | if (detecting |
180 | && qAbs(t: difference) > (accelRange * SLAM_DETECTION_FACTOR)) { |
181 | timerActive = true; |
182 | } |
183 | if (detecting && |
184 | (qAbs(t: difference) < SLAM_ZERO_FACTOR && qAbs(t: difference) > 0)) { |
185 | detecting = false; |
186 | } |
187 | lastX = x; |
188 | lastY = y; |
189 | lastZ = z; |
190 | } |
191 | |
192 | bool QSlamSensorGestureRecognizer::hasBeenResting() |
193 | { |
194 | for (int i = 0; i < restingList.count() - 1; i++) { |
195 | if (!restingList.at(i)) { |
196 | return false; |
197 | } |
198 | } |
199 | return true; |
200 | } |
201 | |
202 | void QSlamSensorGestureRecognizer::doSlam() |
203 | { |
204 | if (detecting && (orientationReading->orientation() == QOrientationReading::RightUp |
205 | || orientationReading->orientation() == QOrientationReading::LeftUp)) { |
206 | Q_EMIT slam(); |
207 | Q_EMIT detected("slam" ); |
208 | restingList.clear(); |
209 | detecting = false; |
210 | } |
211 | timerActive = false; |
212 | lapsedTime = 0; |
213 | } |
214 | |
215 | QT_END_NAMESPACE |
216 | |