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 "qpickupsensorgesturerecognizer.h" |
41 | |
42 | #include <QtCore/qmath.h> |
43 | |
44 | #define TIMER_TIMEOUT 250 |
45 | |
46 | QT_BEGIN_NAMESPACE |
47 | |
48 | QPickupSensorGestureRecognizer::QPickupSensorGestureRecognizer(QObject *parent) |
49 | : QSensorGestureRecognizer(parent) |
50 | , accelReading(0) |
51 | , active(0) |
52 | , pXaxis(0) |
53 | , pYaxis(0) |
54 | , pZaxis(0) |
55 | , lastpitch(0) |
56 | , detecting(0) |
57 | { |
58 | } |
59 | |
60 | QPickupSensorGestureRecognizer::~QPickupSensorGestureRecognizer() |
61 | { |
62 | } |
63 | |
64 | void QPickupSensorGestureRecognizer::create() |
65 | { |
66 | } |
67 | |
68 | QString QPickupSensorGestureRecognizer::id() const |
69 | { |
70 | return QString("QtSensors.pickup" ); |
71 | } |
72 | |
73 | bool QPickupSensorGestureRecognizer::start() |
74 | { |
75 | if (QtSensorGestureSensorHandler::instance()->startSensor(sensor: QtSensorGestureSensorHandler::Accel)) { |
76 | active = true; |
77 | connect(sender: QtSensorGestureSensorHandler::instance(),SIGNAL(accelReadingChanged(QAccelerometerReading*)), |
78 | receiver: this,SLOT(accelChanged(QAccelerometerReading*))); |
79 | } else { |
80 | QtSensorGestureSensorHandler::instance()->stopSensor(sensor: QtSensorGestureSensorHandler::Accel); |
81 | active = false; |
82 | } |
83 | clear(); |
84 | |
85 | return active; |
86 | |
87 | } |
88 | |
89 | bool QPickupSensorGestureRecognizer::stop() |
90 | { |
91 | QtSensorGestureSensorHandler::instance()->stopSensor(sensor: QtSensorGestureSensorHandler::Accel); |
92 | disconnect(sender: QtSensorGestureSensorHandler::instance(),SIGNAL(accelReadingChanged(QAccelerometerReading*)), |
93 | receiver: this,SLOT(accelChanged(QAccelerometerReading*))); |
94 | active = false; |
95 | |
96 | return active; |
97 | } |
98 | |
99 | bool QPickupSensorGestureRecognizer::isActive() |
100 | { |
101 | return active; |
102 | } |
103 | |
104 | #define PICKUP_BOTTOM_THRESHOLD 25 |
105 | #define PICKUP_TOP_THRESHOLD 80 |
106 | #define PICKUP_ANGLE_THRESHOLD 25 |
107 | #define PICKUP_ROLL_THRESHOLD 13 |
108 | |
109 | void QPickupSensorGestureRecognizer::accelChanged(QAccelerometerReading *reading) |
110 | { |
111 | accelReading = reading; |
112 | const qreal x = reading->x(); |
113 | const qreal y = reading->y(); |
114 | const qreal z = reading->z(); |
115 | const qreal xdiff = pXaxis - x; |
116 | const qreal ydiff = pYaxis - y; |
117 | const qreal zdiff = pZaxis - z; |
118 | |
119 | qreal pitch = qRadiansToDegrees(radians: qAtan(v: y / qSqrt(v: x * x + z * z))); |
120 | qreal roll = qRadiansToDegrees(radians: qAtan(v: x / qSqrt(v: y * y + z * z))); |
121 | |
122 | if ((qAbs(t: xdiff) < 0.7 && qAbs(t: ydiff) < .7 && qAbs(t: zdiff) < .7) |
123 | || z < 0) { |
124 | detecting = false; |
125 | } else if (pitch > PICKUP_BOTTOM_THRESHOLD && pitch < PICKUP_TOP_THRESHOLD) { |
126 | detecting = true; |
127 | } |
128 | |
129 | if ( pitchList.count() > 21) { |
130 | pitchList.removeFirst(); |
131 | } |
132 | if ( rollList.count() > 21) { |
133 | rollList.removeFirst(); |
134 | } |
135 | |
136 | if (pitch > 1) { |
137 | pitchList.append(t: pitch); |
138 | } |
139 | if (roll > 1) { |
140 | rollList.append(t: roll); |
141 | } |
142 | |
143 | if (detecting && pitchList.count() > 5 ) { |
144 | timeout(); |
145 | } |
146 | |
147 | lastpitch = pitch; |
148 | pXaxis = x; |
149 | pYaxis = y; |
150 | pZaxis = z; |
151 | } |
152 | |
153 | void QPickupSensorGestureRecognizer::timeout() |
154 | { |
155 | qreal averageRoll = 0; |
156 | for (int r = 0; r < rollList.count(); r++) { |
157 | averageRoll += rollList.at(i: r); |
158 | } |
159 | averageRoll /= rollList.count(); |
160 | |
161 | if (averageRoll > PICKUP_ROLL_THRESHOLD) { |
162 | clear(); |
163 | return; |
164 | } |
165 | if (pitchList.isEmpty() |
166 | || pitchList.at(i: 0) > PICKUP_BOTTOM_THRESHOLD) { |
167 | clear(); |
168 | return; |
169 | } |
170 | |
171 | qreal previousPitch = 0; |
172 | qreal startPitch = -1.0; |
173 | int goodCount = 0; |
174 | |
175 | qreal averagePitch = 0; |
176 | for (int i = 0; i < pitchList.count(); i++) { |
177 | averagePitch += pitchList.at(i); |
178 | if (previousPitch < pitchList.at(i) |
179 | && qAbs(t: pitchList.at(i)) - qAbs(t: previousPitch) < 20) { |
180 | if (goodCount == 1 && previousPitch != 0) { |
181 | startPitch = previousPitch; |
182 | } |
183 | goodCount++; |
184 | } |
185 | |
186 | previousPitch = pitchList.at(i); |
187 | } |
188 | averagePitch /= pitchList.count(); |
189 | |
190 | if (averagePitch < 5) { |
191 | clear(); |
192 | return; |
193 | } |
194 | |
195 | if (goodCount >= 3 && |
196 | (pitchList.last() < PICKUP_TOP_THRESHOLD |
197 | && pitchList.last() > PICKUP_BOTTOM_THRESHOLD) |
198 | && startPitch > 0 |
199 | && (pitchList.last() - startPitch) > PICKUP_ANGLE_THRESHOLD) { |
200 | Q_EMIT pickup(); |
201 | Q_EMIT detected("pickup" ); |
202 | } |
203 | clear(); |
204 | } |
205 | |
206 | void QPickupSensorGestureRecognizer::clear() |
207 | { |
208 | pitchList.clear(); |
209 | detecting = false; |
210 | } |
211 | |
212 | QT_END_NAMESPACE |
213 | |
214 | |