diff options
9 files changed, 150 insertions, 159 deletions
diff --git a/examples/sensors/shakeit/shakeit.qml b/examples/sensors/shakeit/shakeit.qml index 25b78bc..f553b93 100644 --- a/examples/sensors/shakeit/shakeit.qml +++ b/examples/sensors/shakeit/shakeit.qml @@ -127,7 +127,7 @@ Rectangle { y: triangle2.y + triangle2.height; } }, State { - name: "twisted" + name: "twistedR" PropertyChanges { target: triangle1; rotation: 270; x:window.width - triangle1.width; } @@ -140,6 +140,19 @@ Rectangle { } }, State { + name: "twistedL" + PropertyChanges { target: triangle1; rotation: 270; + x:0; + } + PropertyChanges { target: triangle2; rotation: 180; + x:0; + } + PropertyChanges { target: triangle3; rotation: 90; + x:0; + y:triangle2.y + triangle2.height; + } + }, + State { name: "covered" PropertyChanges { target: triangle1; rotation: 0; x: window.width / 3 - triangle1.width / 2; @@ -235,7 +248,11 @@ Rectangle { timer.start() } if (gesture == "twistRight") { - window.state == "twisted" ? window.state = "default" : window.state = "twisted" + window.state == "twistedR" ? window.state = "default" : window.state = "twistedR" + timer.start() + } + if (gesture == "twistLeft") { + window.state == "twistedL" ? window.state = "default" : window.state = "twistedL" timer.start() } if (gesture == "cover") { diff --git a/src/plugins/sensorgestures/qtsensors/qpickupsensorgesturerecognizer.cpp b/src/plugins/sensorgestures/qtsensors/qpickupsensorgesturerecognizer.cpp index e90f52d..f0dbbe0 100644 --- a/src/plugins/sensorgestures/qtsensors/qpickupsensorgesturerecognizer.cpp +++ b/src/plugins/sensorgestures/qtsensors/qpickupsensorgesturerecognizer.cpp @@ -41,29 +41,15 @@ #include "qpickupsensorgesturerecognizer.h" -#define _USE_MATH_DEFINES #include <QtCore/qmath.h> -#ifndef M_PI -#define M_PI 3.14159265358979323846264338327950288419717 -#endif -#ifndef M_PI_2 -#define M_PI_2 1.57079632679489661923 -#endif -QT_BEGIN_NAMESPACE +#define RADIANS_TO_DEGREES 57.2957795 -inline qreal calcPitch(double Ax, double Ay, double Az) -{ - return (float)-qAtan2(Ax, qSqrt(Ay * Ay + Az * Az)); -} -inline qreal calcRoll(double Ax, double Ay, double Az) -{ - return (float)qAtan2(Ay, (qSqrt(Ax * Ax + Az * Az))); -} +QT_BEGIN_NAMESPACE QPickupSensorGestureRecognizer::QPickupSensorGestureRecognizer(QObject *parent) : QSensorGestureRecognizer(parent),atRest(1),okToSignal(1), - lastRoll(0) + lastpitch(0), detecting(0) { } @@ -110,7 +96,7 @@ bool QPickupSensorGestureRecognizer::isActive() return active; } -#define PICKUP_BOTTOM_THRESHOLD 15 +#define PICKUP_BOTTOM_THRESHOLD 30 #define PICKUP_TOP_THRESHOLD 60 void QPickupSensorGestureRecognizer::accelChanged() @@ -122,33 +108,27 @@ void QPickupSensorGestureRecognizer::accelChanged() qreal ydiff = pYaxis - y; qreal zdiff = pZaxis - z; - roll = calc(calcRoll(x, y, z)); + pitch = qAtan(y / qSqrt(x*x + z*z)) * RADIANS_TO_DEGREES; - if (xdiff < 0.7 && ydiff < .7 && zdiff < .7) { + if (qAbs(xdiff) < 0.7 && qAbs(ydiff) < .7 && qAbs(zdiff) < .7) { atRest = true; } else { atRest = false; } - if (roll > PICKUP_BOTTOM_THRESHOLD - && (qAbs(lastRoll - roll) > PICKUP_BOTTOM_THRESHOLD)) { - okToSignal = true; - detectedRoll = roll; - } - if (atRest - && okToSignal - && (roll < PICKUP_TOP_THRESHOLD && roll > PICKUP_BOTTOM_THRESHOLD)) { + + if (!atRest && (lastpitch - pitch < -PICKUP_BOTTOM_THRESHOLD)) { + detecting = true; if (!timer->isActive()) { timer->start(); } + lastpitch = pitch; } - // initial at ~0, z = 9.8 - // at 'focus' when y == 6 to 8 && z == 5 to 7 - pXaxis = x; pYaxis = y; pZaxis = z; - lastRoll = roll; + if (atRest && !detecting) + lastpitch = pitch; } void QPickupSensorGestureRecognizer::timeout() @@ -157,11 +137,9 @@ void QPickupSensorGestureRecognizer::timeout() qreal y = accel->reading()->y(); qreal z = accel->reading()->z(); - qreal pitch = calc(calcPitch(x, y, z)); - - if (atRest - && (pitch > -6 && pitch < 6) - && (roll < PICKUP_TOP_THRESHOLD && roll > PICKUP_BOTTOM_THRESHOLD) + qreal roll = qAtan(x / qSqrt(y*y + z*z)) * RADIANS_TO_DEGREES; + if ((roll > -6 && roll < 6) + && (pitch < PICKUP_TOP_THRESHOLD && pitch > PICKUP_BOTTOM_THRESHOLD) && (y > 5.0 && y < 8.9) && (z > 5.0 && z < 7.9)) { Q_EMIT pickup(); @@ -169,20 +147,7 @@ void QPickupSensorGestureRecognizer::timeout() okToSignal = false; } -} - -qreal QPickupSensorGestureRecognizer::calc(qreal yrot) -{ - qreal aG = 1 * sin(yrot); - qreal aK = 1 * cos(yrot); - - yrot = qAtan2(aG, aK); - if (yrot > M_PI_2) - yrot = M_PI - yrot; - else if (yrot < -M_PI_2) - yrot = -(M_PI + yrot); - - return yrot * 180 / M_PI; + detecting = false; } QT_END_NAMESPACE diff --git a/src/plugins/sensorgestures/qtsensors/qpickupsensorgesturerecognizer.h b/src/plugins/sensorgestures/qtsensors/qpickupsensorgesturerecognizer.h index 3870fd0..05577df 100644 --- a/src/plugins/sensorgestures/qtsensors/qpickupsensorgesturerecognizer.h +++ b/src/plugins/sensorgestures/qtsensors/qpickupsensorgesturerecognizer.h @@ -82,12 +82,10 @@ private: qreal pYaxis; qreal pZaxis; - qreal roll; - - qreal calc(qreal yrot); - qreal lastRoll; - qreal detectedRoll; + qreal pitch; + qreal lastpitch; + bool detecting; }; QT_END_NAMESPACE #endif // QPICKUPSENSORGESTURERECOGNIZER_H diff --git a/src/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.cpp b/src/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.cpp index f45bdca..900f8df 100644 --- a/src/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.cpp +++ b/src/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.cpp @@ -114,9 +114,12 @@ bool QSlamSensorGestureRecognizer::isActive() } #define SLAM_FACTOR -20.0 +#define SLAM_WIGGLE_FACTOR 0.95 void QSlamSensorGestureRecognizer::accelChanged() { + qreal x = accel->reading()->x(); + qreal y = accel->reading()->y(); qreal z = accel->reading()->z(); //// very hacky @@ -126,7 +129,8 @@ void QSlamSensorGestureRecognizer::accelChanged() z = z - 9.8; } - qreal diffZ = lastZ - z; + qreal diffX = lastX - x; + qreal diffY = lastY - y; if (detecting && slamMap.count() > 5 && slamMap.at(5) == true) { checkForSlam(); @@ -135,7 +139,9 @@ void QSlamSensorGestureRecognizer::accelChanged() if (slamMap.count() > 5) slamMap.removeLast(); - if (z < SLAM_FACTOR) { + if (z < SLAM_FACTOR + && qAbs(diffX) < (accelRange *SLAM_WIGGLE_FACTOR) + && qAbs(diffY) < (accelRange *SLAM_WIGGLE_FACTOR)) { slamMap.insert(0,true); if (!detecting && !timer->isActive()) { timer->start(); @@ -145,6 +151,19 @@ void QSlamSensorGestureRecognizer::accelChanged() slamMap.insert(0,false); } lastZ = z; + + if (negativeList.count() > 5) + negativeList.removeLast(); + + if (((x < 0 && lastX > 0 || x > 0 && lastX < 0) && qAbs(diffX) > (accelRange * 0.5)) + || (y < 0 && lastY > 0 || y > 0 && lastY < 0) && qAbs(diffY) > (accelRange * 0.5)) { + negativeList.insert(0,true); + } else { + negativeList.insert(0,false); + } + + lastX = x; lastY = y; + } void QSlamSensorGestureRecognizer::timeout() @@ -169,8 +188,16 @@ void QSlamSensorGestureRecognizer:: checkForSlam() } } if (slamOk) { - Q_EMIT slam(); - Q_EMIT detected("slam"); + bool ok = true; + for (int i = 0; i < negativeList.count() - 1; i++) { + if (negativeList.at(i)) { + ok = false; + } + } + if (ok) { + Q_EMIT slam(); + Q_EMIT detected("slam"); + } detecting = false; slamMap.clear(); timer->stop(); diff --git a/src/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.h b/src/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.h index 2fecb5a..38777b8 100644 --- a/src/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.h +++ b/src/plugins/sensorgestures/qtsensors/qslamgesturerecognizer.h @@ -89,6 +89,10 @@ private: void checkForSlam(); + QList <bool> negativeList; + + //QList<qreal> yList; + }; QT_END_NAMESPACE diff --git a/src/plugins/sensorgestures/qtsensors/qtwistsensorgesturerecognizer.cpp b/src/plugins/sensorgestures/qtsensors/qtwistsensorgesturerecognizer.cpp index df99f21..7f1d984 100644 --- a/src/plugins/sensorgestures/qtsensors/qtwistsensorgesturerecognizer.cpp +++ b/src/plugins/sensorgestures/qtsensors/qtwistsensorgesturerecognizer.cpp @@ -42,30 +42,16 @@ #include "qtwistsensorgesturerecognizer.h" -#define _USE_MATH_DEFINES #include <QtCore/qmath.h> -#ifndef M_PI -#define M_PI 3.14159265358979323846264338327950288419717 -#endif -#ifndef M_PI_2 -#define M_PI_2 1.57079632679489661923 -#endif QT_BEGIN_NAMESPACE -// from qsensor2tilt -inline qreal calcPitch(double Ax, double Ay, double Az) -{ - return (float)-qAtan2(Ax, qSqrt(Ay * Ay + Az * Az)); -} - -inline qreal calcRoll(double Ax, double Ay, double Az) -{ - return (float)qAtan2(Ay, (qSqrt(Ax * Ax + Az * Az))); -} +#define RADIANS_TO_DEGREES 57.2957795 QTwistSensorGestureRecognizer::QTwistSensorGestureRecognizer(QObject *parent) : - QSensorGestureRecognizer(parent), detecting(0), lastDegree(0), lastX(0) + QSensorGestureRecognizer(parent), + accelRange(0), lastRoll(0), active(0), pitch(0), detecting(0), lastDegree(0), + lastX(0), lastY(0), lastZ() { } @@ -77,10 +63,10 @@ void QTwistSensorGestureRecognizer::create() { accel = new QAccelerometer(this); accel->connectToBackend(); + accel->setDataRate(5); orientation = new QOrientationSensor(this); orientation->connectToBackend(); - timer = new QTimer(this); qoutputrangelist outputranges = accel->outputRanges(); @@ -124,8 +110,8 @@ bool QTwistSensorGestureRecognizer::isActive() return active; } -#define RESTING_VARIANCE 20 -#define THRESHOLD_DEGREES 70 +#define RESTING_VARIANCE 25 +#define THRESHOLD_DEGREES 60 void QTwistSensorGestureRecognizer::accelChanged() { @@ -133,29 +119,26 @@ void QTwistSensorGestureRecognizer::accelChanged() qreal y = accel->reading()->y(); qreal z = accel->reading()->z(); - pitch = calcPitch(x, y, z); - roll = calcRoll(x, y, z); - - qreal degrees = calc(pitch); + qreal diffX = lastX - x; + qreal diffY = lastY - y; + pitch = qAtan(y / qSqrt(x*x + z*z)) * RADIANS_TO_DEGREES; -// qDebug() << Q_FUNC_INFO << degrees << calc(roll) << lastX; + qreal degrees = qAtan(x / qSqrt(y*y + z*z)) * RADIANS_TO_DEGREES; - if (xList.count() > 4) { - if (detecting && (degrees > 0 && lastX < 0 - || degrees < 0 && lastX > 0)) { - // if shake-like: + if (rollList.count() > 4) { + if (detecting + && isShake()) { + // if shake-like: detecting = false; timer->stop(); - lastX = degrees; -// qDebug() << Q_FUNC_INFO << "stop detecting"; + lastRoll = degrees; } - if (detecting && qAbs(degrees) < RESTING_VARIANCE - && qAbs(calc(roll)) < RESTING_VARIANCE - && (qAbs(lastX + degrees) > (degrees / 2)) + && qAbs(pitch) < RESTING_VARIANCE + && (qAbs(lastRoll + degrees) > (degrees / 2)) ) { - if (lastX < 0 ) { + if (lastRoll > 0 ) { Q_EMIT twistLeft(); Q_EMIT detected("twistLeft"); } else { @@ -165,52 +148,61 @@ void QTwistSensorGestureRecognizer::accelChanged() // don't give two signals for same gestures detecting = false; timer->stop(); - lastX = degrees; + lastRoll = degrees; } if (!detecting && qAbs(degrees) > THRESHOLD_DEGREES - && calc(roll) < RESTING_VARIANCE) { + && pitch < RESTING_VARIANCE) { detecting = true; timer->start(); - lastX = degrees; + lastRoll = degrees; lastOrientation = orientation->reading()->orientation(); -// qDebug() << Q_FUNC_INFO << "start detecting" << lastOrientation; } if (detecting && (orientation->reading()->orientation() == QOrientationReading::TopUp || orientation->reading()->orientation() == QOrientationReading::TopDown)) { + detecting = false; timer->stop(); - lastX = degrees; + lastRoll = degrees; } } - if (xList.count() > 5) - xList.removeLast(); - xList.insert(0,degrees); + if (negativeList.count() > 5) + negativeList.removeLast(); + + if ((((x < 0 && lastX > 0) || (x > 0 && lastX < 0)) && qAbs(diffX) > (accelRange * 0.5)) + || (((y < 0 && lastY > 0) || (y > 0 && lastY < 0)) && qAbs(diffY) > (accelRange * 0.5))) { + negativeList.insert(0,true); + } else { + negativeList.insert(0,false); + } + + if (rollList.count() > 5) + rollList.removeLast(); + rollList.insert(0,degrees); lastDegree = degrees; + lastX = x; lastY = y; } void QTwistSensorGestureRecognizer::timeout() { detecting = false; - lastX = 0; + lastRoll = 0; lastOrientation = QOrientationReading::Undefined; } -qreal QTwistSensorGestureRecognizer::calc(qreal yrot) +bool QTwistSensorGestureRecognizer::isShake() { - qreal aG = 1 * sin(yrot); - qreal aK = 1 * cos(yrot); + for (int i = 0; i < negativeList.count() - 1; i++) { + if (negativeList.at(i)) { + return true; + } + } + return false; +} - yrot = qAtan2(aG, aK); - if (yrot > M_PI_2) - yrot = M_PI - yrot; - else if (yrot < -M_PI_2) - yrot = -(M_PI + yrot); - return yrot * 180 / M_PI; -} QT_END_NAMESPACE diff --git a/src/plugins/sensorgestures/qtsensors/qtwistsensorgesturerecognizer.h b/src/plugins/sensorgestures/qtsensors/qtwistsensorgesturerecognizer.h index 45d2d51..df36606 100644 --- a/src/plugins/sensorgestures/qtsensors/qtwistsensorgesturerecognizer.h +++ b/src/plugins/sensorgestures/qtsensors/qtwistsensorgesturerecognizer.h @@ -76,17 +76,20 @@ private: QOrientationSensor *orientation; QTimer *timer; int accelRange; - qreal lastX; + qreal lastRoll; bool active; - QList<int> xList; + QList<int> rollList; qreal pitch; - qreal roll; - qreal calc(qreal yrot); bool detecting; qreal lastDegree; QOrientationReading::Orientation lastOrientation; + QList <bool> negativeList; + bool isShake(); + qreal lastX; + qreal lastY; + qreal lastZ; }; QT_END_NAMESPACE #endif // QWFLICKSENSORGESTURERECOGNIZER_H diff --git a/src/plugins/sensorgestures/qtsensors/qwhipsensorgesturerecognizer.cpp b/src/plugins/sensorgestures/qtsensors/qwhipsensorgesturerecognizer.cpp index 93e147a..676b42a 100644 --- a/src/plugins/sensorgestures/qtsensors/qwhipsensorgesturerecognizer.cpp +++ b/src/plugins/sensorgestures/qtsensors/qwhipsensorgesturerecognizer.cpp @@ -42,23 +42,10 @@ #include "qwhipsensorgesturerecognizer.h" -#define _USE_MATH_DEFINES #include <QtCore/qmath.h> -#ifndef M_PI -#define M_PI 3.14159265358979323846264338327950288419717 -#endif -#ifndef M_PI_2 -#define M_PI_2 1.57079632679489661923 -#endif - QT_BEGIN_NAMESPACE -inline qreal calcYaw(double Ax, double Ay, double Az) -{ - return (float)qAtan2(Az, (qSqrt(Ax * Ax + Ay * Ay))); -} - QWhipSensorGestureRecognizer::QWhipSensorGestureRecognizer(QObject *parent) : QSensorGestureRecognizer(parent), whipIt(0), lastX(0), detectedX(0) @@ -119,10 +106,17 @@ bool QWhipSensorGestureRecognizer::isActive() return active; } -#define WHIP_THRESHOLD_FACTOR 0.95 //37 +#define WHIP_THRESHOLD_FACTOR 0.85 //37 #define WHIP_DETECTION_FACTOR 0.3 // 11.7 + #define WHIP_DEGREES 25 +#define WHIP_Y_DEGREES 15 +#define WHIP_DEGREES_BELOW + +#define RADIANS_TO_DEGREES 57.2957795 + + void QWhipSensorGestureRecognizer::accelChanged() { qreal x = accel->reading()->x(); @@ -133,21 +127,26 @@ void QWhipSensorGestureRecognizer::accelChanged() qreal y = accel->reading()->y(); qreal z = accel->reading()->z(); - qreal degreesZ = calc(calcYaw(x,y,z)); + qreal roll = qAtan(x / qSqrt(y*y + z*z)) * RADIANS_TO_DEGREES; + + QOrientationReading::Orientation currentOrientation = orientation->reading()->orientation(); if (whipIt) { - if (((!wasNegative && difference > accelRange * WHIP_THRESHOLD_FACTOR) - || (wasNegative && difference < -accelRange * WHIP_THRESHOLD_FACTOR)) - && qAbs(degreesZ) < WHIP_DEGREES) { + + if (currentOrientation == QOrientationReading::TopUp + && roll > WHIP_Y_DEGREES + && ((!wasNegative && qAbs(detectedX - x) > accelRange * WHIP_THRESHOLD_FACTOR) + || (wasNegative && detectedX - x > (accelRange * WHIP_THRESHOLD_FACTOR))) ) { Q_EMIT whip(); Q_EMIT detected("whip"); whipIt = false; + timer->stop(); } - - } else if (((difference > 0 && difference < accelRange * WHIP_DETECTION_FACTOR) - || (difference < 0 && difference > -accelRange * WHIP_DETECTION_FACTOR)) - && qAbs(degreesZ) < WHIP_DEGREES - && orientation->reading()->orientation() != QOrientationReading::FaceUp) { + } else if (!timer->isActive() + && currentOrientation == QOrientationReading::TopUp + && roll < -WHIP_Y_DEGREES + && ((difference > accelRange * WHIP_DETECTION_FACTOR) + || (difference < -accelRange * WHIP_DETECTION_FACTOR))) { detectedX = x; // start of gesture timer->start(); @@ -165,18 +164,4 @@ void QWhipSensorGestureRecognizer::timeout() whipIt = false; } -qreal QWhipSensorGestureRecognizer::calc(qreal yrot) -{ - qreal aG = 1 * sin(yrot); - qreal aK = 1 * cos(yrot); - - yrot = qAtan2(aG, aK); - if (yrot > M_PI_2) - yrot = M_PI - yrot; - else if (yrot < -M_PI_2) - yrot = -(M_PI + yrot); - - return yrot * 180 / M_PI; -} - QT_END_NAMESPACE diff --git a/src/plugins/sensorgestures/qtsensors/qwhipsensorgesturerecognizer.h b/src/plugins/sensorgestures/qtsensors/qwhipsensorgesturerecognizer.h index fb1b46d..6d22af6 100644 --- a/src/plugins/sensorgestures/qtsensors/qwhipsensorgesturerecognizer.h +++ b/src/plugins/sensorgestures/qtsensors/qwhipsensorgesturerecognizer.h @@ -82,7 +82,7 @@ private: bool active; qreal accelX; - qreal calc(qreal rot); + qreal roll; }; QT_END_NAMESPACE |