diff options
-rw-r--r-- | doc/src/images/FaceDown.gif | bin | 6681 -> 0 bytes | |||
-rw-r--r-- | doc/src/images/FaceUp.gif | bin | 6736 -> 0 bytes | |||
-rw-r--r-- | doc/src/images/LeftUp.gif | bin | 7210 -> 0 bytes | |||
-rw-r--r-- | doc/src/images/RightUp.gif | bin | 7146 -> 0 bytes | |||
-rw-r--r-- | doc/src/images/TopDown.gif | bin | 7455 -> 0 bytes | |||
-rw-r--r-- | doc/src/images/TopUp.gif | bin | 7172 -> 0 bytes | |||
-rw-r--r-- | doc/src/imports/qtsensors5.qdoc | 6 | ||||
-rw-r--r-- | examples/sensors/maze/Maze.qml | 11 | ||||
-rw-r--r-- | examples/sensors/qmlqtsensors5/main.qml | 131 | ||||
-rw-r--r-- | src/imports/sensors2/qsensor2tilt.cpp | 362 | ||||
-rw-r--r-- | src/imports/sensors2/qsensor2tilt.h | 68 | ||||
-rw-r--r-- | tests/auto/sensors2qmlapi/qdeclaccelerometer.h | 2 | ||||
-rw-r--r-- | tests/auto/sensors2qmlapi/tst_sensors2qmlapi.cpp | 214 |
13 files changed, 431 insertions, 363 deletions
diff --git a/doc/src/images/FaceDown.gif b/doc/src/images/FaceDown.gif Binary files differdeleted file mode 100644 index 9236a8a7..00000000 --- a/doc/src/images/FaceDown.gif +++ /dev/null diff --git a/doc/src/images/FaceUp.gif b/doc/src/images/FaceUp.gif Binary files differdeleted file mode 100644 index a53894f0..00000000 --- a/doc/src/images/FaceUp.gif +++ /dev/null diff --git a/doc/src/images/LeftUp.gif b/doc/src/images/LeftUp.gif Binary files differdeleted file mode 100644 index 2c8d57ae..00000000 --- a/doc/src/images/LeftUp.gif +++ /dev/null diff --git a/doc/src/images/RightUp.gif b/doc/src/images/RightUp.gif Binary files differdeleted file mode 100644 index 8e29cbc9..00000000 --- a/doc/src/images/RightUp.gif +++ /dev/null diff --git a/doc/src/images/TopDown.gif b/doc/src/images/TopDown.gif Binary files differdeleted file mode 100644 index 6314fc91..00000000 --- a/doc/src/images/TopDown.gif +++ /dev/null diff --git a/doc/src/images/TopUp.gif b/doc/src/images/TopUp.gif Binary files differdeleted file mode 100644 index f6747c46..00000000 --- a/doc/src/images/TopUp.gif +++ /dev/null diff --git a/doc/src/imports/qtsensors5.qdoc b/doc/src/imports/qtsensors5.qdoc index 80fde894..97acacfa 100644 --- a/doc/src/imports/qtsensors5.qdoc +++ b/doc/src/imports/qtsensors5.qdoc @@ -54,9 +54,9 @@ \qml TiltSensor { id: tilt - radian: false - measureFrom: TiltSensor.FaceUp - running: false + enabled: false + unit: TiltSensor.Degrees + accuracy: 1.0 } \endqml diff --git a/examples/sensors/maze/Maze.qml b/examples/sensors/maze/Maze.qml index b529f089..9a79e0f7 100644 --- a/examples/sensors/maze/Maze.qml +++ b/examples/sensors/maze/Maze.qml @@ -155,14 +155,15 @@ Rectangle { //Start the Tilt reader timer tiltTimer.running = true; + tiltSensor.calibrate(); } } TiltSensor{ id: tiltSensor - radian: false - measureFrom: TiltSensor.FaceUp - running: true + unit: TiltSensor.Degrees + enabled: true + accuracy: 5.0 } //Timer to read out the x and y rotation of the TiltSensor @@ -170,8 +171,8 @@ Rectangle { id: tiltTimer interval: 50; running: false; repeat: true onTriggered: { - if (!tiltSensor.running) - tiltSensor.running = true; + if (!tiltSensor.enabled) + tiltSensor.enabled = true; if (mouseCtrl === null) return; diff --git a/examples/sensors/qmlqtsensors5/main.qml b/examples/sensors/qmlqtsensors5/main.qml index e1de1048..75cb4509 100644 --- a/examples/sensors/qmlqtsensors5/main.qml +++ b/examples/sensors/qmlqtsensors5/main.qml @@ -45,13 +45,14 @@ import QtSensors 5.0 /* Layout tiltrect / -------------------------------/ -| @ Facing Up <-------------------- selButtonFacingUp -| O Left Up <-------------------- selButtonLeftUp -| O Top Down <-------------------- selButtonTopDown -| O Face Down <-------------------- selButtonFaceDown -| O Right Up <-------------------- selButtonRightUp -| O Top Up <-------------------- selButtonTopUp +-----------------------------------------------------------/ +| Accuracy <----- textAccuracy +| @ 0 Degree <- selButton0DegreeAccuracy +| O 1 Degree <- selButton1DegreeAccuracy +| O 5 Degree <- selButton5DegreeAccuracy +| ----------- +| |calibrate| <------------------ calibrate +| ----------- | --------- | |degree | <-------------------- useRadian | --------- @@ -72,7 +73,7 @@ import QtSensors 5.0 | --------- | |start | <-------------------- proxiStart | --------- ------------------------------- +------------------------------------------------------------ */ @@ -90,99 +91,74 @@ Rectangle { TiltSensor { id: tilt - radian: false - measureFrom: TiltSensor.FaceUp - running: false + unit: TiltSensor.Degrees + enabled: false + accuracy: 1.0 } - - SelectionButton{ - id: selButtonFacingUp + Text{ + id: textAccuracy x: 5 y: 20 - checked: true - text: "Facing Up" - onClicked:{ - selButtonLeftUp.setCheck(false); - selButtonTopDown.setCheck(false); - selButtonFaceDown.setCheck(false); - selButtonRightUp.setCheck(false); - selButtonTopUp.setCheck(false); - tilt.measureFrom=TiltSensor.FaceUp; - } + text: "Accuracy" } SelectionButton{ - id: selButtonLeftUp + id: selButton0DegreeAccuracy x: 5 y: 45 - checked: false - text: "Left Up" + checked: true + text: "0 Degree" onClicked:{ - selButtonFacingUp.setCheck(false); - selButtonTopDown.setCheck(false); - selButtonFaceDown.setCheck(false); - selButtonRightUp.setCheck(false); - selButtonTopUp.setCheck(false); - tilt.measureFrom=TiltSensor.LeftUp; + selButton1DegreeAccuracy.setCheck(false); + selButton5DegreeAccuracy.setCheck(false); + tilt.accuracy = 0.0; } } SelectionButton{ - id: selButtonTopDown + id: selButton1DegreeAccuracy x: 5 y: 70 checked: false - text: "Top Down" + text: "1 Degree" onClicked:{ - selButtonFacingUp.setCheck(false); - selButtonLeftUp.setCheck(false); - selButtonFaceDown.setCheck(false); - selButtonRightUp.setCheck(false); - selButtonTopUp.setCheck(false); - tilt.measureFrom=TiltSensor.TopDown; + selButton0DegreeAccuracy.setCheck(false); + selButton5DegreeAccuracy.setCheck(false); + tilt.accuracy = (tilt.unit == TiltSensor.Degrees ? 1.0 : (3.14 / 180.0)); } } SelectionButton{ - id: selButtonFaceDown + id: selButton5DegreeAccuracy x: 5 y: 95 checked: false - text: "Face Down" - onClicked:{ - selButtonFacingUp.setCheck(false); - selButtonLeftUp.setCheck(false); - selButtonTopDown.setCheck(false); - selButtonRightUp.setCheck(false); - selButtonTopUp.setCheck(false); - tilt.measureFrom=TiltSensor.FaceDown; - } - } - SelectionButton{ - id: selButtonRightUp - x: 5 - y: 120 - checked: false - text: "Right Up" + text: "5 Degree" onClicked:{ - selButtonFacingUp.setCheck(false); - selButtonLeftUp.setCheck(false); - selButtonTopDown.setCheck(false); - selButtonFaceDown.setCheck(false); - selButtonTopUp.setCheck(false); - tilt.measureFrom=TiltSensor.RightUp; + selButton0DegreeAccuracy.setCheck(false); + selButton1DegreeAccuracy.setCheck(false); + tilt.accuracy = (tilt.unit == TiltSensor.Degrees ? 5.0 : (5.0 * 3.14 / 180.0)); } } - SelectionButton{ - id: selButtonTopUp + + Button{ + id: calibrate x: 5 y: 145 + text: "calibrate" + checkColor: "lightblue" + unCheckColor: "lightyellow" checked: false - text: "Top Up" + color: "lightyellow" + onClicked:{ - selButtonFacingUp.setCheck(false); - selButtonLeftUp.setCheck(false); - selButtonTopDown.setCheck(false); - selButtonFaceDown.setCheck(false); - selButtonRightUp.setCheck(false); - tilt.measureFrom=TiltSensor.TopUp; + tilt.calibrate(); + unchecktimer.running = true; + } + Timer { + id: unchecktimer + interval: 1000; running: false; repeat: false + onTriggered: { + calibrate.checked = false; + calibrate.color = "lightyellow"; + } } } @@ -193,11 +169,11 @@ Rectangle { text: "degree" checkColor: "lightblue" unCheckColor: "lightyellow" - checked: tilt.radian + checked: (tilt.unit == TiltSensor.Radians ? true : false) color: "lightyellow" onClicked:{ - tilt.radian = useRadian.checked; + tilt.unit = (useRadian.checked ? TiltSensor.Radians : TiltSensor.Degrees); if (useRadian.checked) useRadian.text = "radian"; else @@ -224,11 +200,11 @@ Rectangle { text: "start" checkColor: "lightblue" unCheckColor: "lightyellow" - checked: tilt.running + checked: tilt.enabled color: "lightyellow" onClicked:{ - tilt.running = tiltStart.checked; + tilt.enabled = tiltStart.checked; if (tiltStart.checked) tiltStart.text = "running"; else @@ -327,4 +303,3 @@ Rectangle { } } } - diff --git a/src/imports/sensors2/qsensor2tilt.cpp b/src/imports/sensors2/qsensor2tilt.cpp index b9507593..4af6e015 100644 --- a/src/imports/sensors2/qsensor2tilt.cpp +++ b/src/imports/sensors2/qsensor2tilt.cpp @@ -42,8 +42,8 @@ #include "qsensor2tilt.h" #define _USE_MATH_DEFINES #include <math.h> -#include <errno.h> #include <QtCore/QDebug> +#include <QtCore/QStringList> QT_BEGIN_NAMESPACE @@ -55,53 +55,17 @@ QT_BEGIN_NAMESPACE \brief The TiltSensor element provides tilt datas from x and y rotation of the device using the accelerometer sensor. This element is part of the \bold{QtSensors 5} module. - - \target tiltreferenceenum - \section1 Enums - \section2 TiltSensor::TiltReference - - This enum describes the orientation of the device. - - \table - \row - \o TiltSensor::TopUp - \o The top of the device points up - \o - \image TopUp.gif - \row - \o TiltSensor::TopDown - \o The top of the device points down - \o - \image TopDown.gif - \row - \o TiltSensor::LeftUp - \o The left site of the device points up - \o - \image LeftUp.gif - \row - \o TiltSensor::RightUp - \o The right site of the device points up - \o - \image RightUp.gif - \row - \o TiltSensor::FaceUp - \o The screen of the device points up - \o - \image FaceUp.gif - \row - \o TiltSensor::FaceDown - \o The screen of the device points down - \o - \image FaceDown.gif - \endtable - */ QSensor2Tilt::QSensor2Tilt(QObject* parent) : QObject(parent) - , _measureFrom(QSensor2Tilt::FaceUp) , _yRotation(0) , _xRotation(0) - , _useRadian(true) + , _radAccuracy(M_PI / 180) + , _unit(QSensor2Tilt::Radians) + , _pitch(0) + , _roll(0) + , _calibratedPitch(0) + , _calibratedRoll(0) { _accel = new QAccelerometer(this); _accel->addFilter(this); @@ -115,10 +79,6 @@ QSensor2Tilt::~QSensor2Tilt() \qmlproperty bool QtSensors5::TiltSensor::dataRate Holds the data rate that the sensor should be run at. */ -/*! - \qmlsignal QtSensors5::TiltSensor::onDataRateChanged() - This signal is emitted whenever the value of the property dataRate has been changed. -*/ int QSensor2Tilt::dataRate() { return _accel->dataRate(); @@ -133,21 +93,17 @@ void QSensor2Tilt::setDataRate(int val) } /*! - \qmlproperty bool QtSensors5::TiltSensor::running - Holds the identication if the sensor runs or not. -*/ -/*! - \qmlsignal QtSensors5::TiltSensor::onRunningChanged() - This signal is emitted whenever the value of the property running has been changed. + \qmlproperty bool QtSensors5::QSensor2Tilt::enabled + This property can be used to activate or deactivate the sensor. */ -bool QSensor2Tilt::running() +bool QSensor2Tilt::enabled() { return _accel->isActive(); } -void QSensor2Tilt::setRunning(bool val) +void QSensor2Tilt::setEnabled(bool val) { - bool active = running(); + bool active = enabled(); if (active != val){ if (val){ bool ret = _accel->start(); @@ -156,49 +112,39 @@ void QSensor2Tilt::setRunning(bool val) } else _accel->stop(); - emit runningChanged(); + emit enabledChanged(); } } /*! - \qmlproperty QSensor2Tilt::TiltReference QtSensors5::TiltSensor::measureFrom - Holds the orientation in which the rotation should be calculated. - \sa {tiltreferenceenum} {TiltReference::TiltReference} -*/ -/*! - \qmlsignal QtSensors5::TiltSensor::onMeasureFromChanged() - This signal is emitted whenever the value of the property measureFrom has been changed. + \target unit_property + \qmlproperty enumeration QtSensors5::TiltSensor::unit + Returns the unit of the rotation which can be one of: + \table + \row + \o TiltSensor.Radians + \o The unit of the rotation angle is radians. + \row + \o TiltSensor.Degrees + \o The unit of the rotation angle is degrees. + \endtable */ -QSensor2Tilt::TiltReference QSensor2Tilt::measureFrom() +QSensor2Tilt::Unit QSensor2Tilt::unit() { - return _measureFrom; + return _unit; } -void QSensor2Tilt::setMeasureFrom(QSensor2Tilt::TiltReference val) +void QSensor2Tilt::setUnit(const QSensor2Tilt::Unit val) { - if (val != _measureFrom){ - _measureFrom = val; - emit measureFromChanged(); + if (_unit != val){ + _unit = val; + emit unitChanged(); } } -/*! - \qmlproperty bool QtSensors5::TiltSensor::radian - Holds the unit of the rotation. True is rad otherwise deg. true = report values in radians. false = report values in degrees. -*/ -bool QSensor2Tilt::radian() -{ - return _useRadian; -} - -void QSensor2Tilt::setRadian(bool val) -{ - _useRadian = val; -} - /*! - \qmlproperty float QtSensors5::TiltSensor::yRotation + \qmlproperty qreal QtSensors5::TiltSensor::yRotation Holds the rotation arround the y axis. \table @@ -209,17 +155,16 @@ void QSensor2Tilt::setRadian(bool val) \image YAngleNegative.gif \endtable */ -/*! - \qmlsignal QtSensors5::TiltSensor::onYRotationChanged() - This signal is emitted whenever the value of the property yRotation has been changed. -*/ -float QSensor2Tilt::yRotation() +qreal QSensor2Tilt::yRotation() { + if (_unit == QSensor2Tilt::Degrees) + return _yRotation * 180 / M_PI; + return _yRotation; } /*! - \qmlproperty float QtSensors5::TiltSensor::xRotation + \qmlproperty qreal QtSensors5::TiltSensor::xRotation Holds the rotation arround the x axis. \table \row @@ -229,12 +174,11 @@ float QSensor2Tilt::yRotation() \image XAngleNegative.gif \endtable */ -/*! - \qmlsignal QtSensors5::TiltSensor::onXRotationChanged() - This signal is emitted whenever the value of the property XRotation has been changed. -*/ -float QSensor2Tilt::xRotation() +qreal QSensor2Tilt::xRotation() { + if (_unit == QSensor2Tilt::Degrees) + return _xRotation * 180 / M_PI; + return _xRotation; } @@ -244,32 +188,96 @@ float QSensor2Tilt::xRotation() pitch = arctan| ----------------------- | | sqrt(Ay * Ay + Az * Az)| */ -inline float calcPitch(double Ax, double Ay, double Az) +inline qreal calcPitch(double Ax, double Ay, double Az) { - errno = 0; - float ret = (float)-atan(Ax / (sqrt(Ay * Ay + Az * Az))); - if (errno == EDOM){ - ret = 0.0; - errno = 0; - } - return ret; + return (float)-atan2(Ax, sqrt(Ay * Ay + Az * Az)); } /* Angle between Ground and Y | Ay | - pitch = arctan| ----------------------- | + roll = arctan| ----------------------- | | sqrt(Ax * Ax + Az * Az)| */ -inline float calcRoll(double Ax, double Ay, double Az) +inline qreal calcRoll(double Ax, double Ay, double Az) { - errno = 0; - float ret = (float)atan(Ay / (sqrt(Ax * Ax + Az * Az))); - if (errno == EDOM){ - ret = 0.0; - errno = 0; + return (float)atan2(Ay, (sqrt(Ax * Ax + Az * Az))); +} + +/* + Angle between Z Axis relative to the Gravity + | sqrt(Ax * Ax + Ay* Ay) | + theta = arctan| ----------------------- | + | Az | +*/ +inline qreal calcTheta(double Ax, double Ay, double Az) +{ + return (float)atan2((sqrt(Ax * Ax + Ay * Ay)), Az); +} + +/*! + \qmlproperty qreal QtSensors5::TiltSensor::accuracy + This property contains the accuracy (in degrees) in which the rotation should be measured. + This can be used to minimize signal emiting and therefore saving of performance. + Default value is 1 degree. +*/ +/*! + \qmlsignal QtSensors5::TiltSensor::tiltChanged(qreal deltaX, qreal deltaY) + This signal is emitted whenever the change from at leat one of the rotation values was higher than the accuracy. + The angle value is based on the specified unit (Degree or Radian) \sa {unit_property} {TiltSensor.unit}. +*/ +qreal QSensor2Tilt::accuracy() +{ + //return in degree + return 180 * _radAccuracy / M_PI; +} + +void QSensor2Tilt::setAccuracy(qreal val) +{ + //save in rad to save convertion calc in filter function + _radAccuracy = M_PI * val / 180; +} + +/*! + \qmlproperty void QtSensors5::TiltSensor::calibrate + The call of this function calibrates the tilt from x and y to the current position. +*/ +void QSensor2Tilt::calibrate() +{ + _calibratedPitch = _pitch; + _calibratedRoll = _roll; +#ifdef LOGCALIBRATION + qDebug() << "--------- calibrate --------"; + qDebug() << "_calibratedPitch: " << _calibratedPitch; + qDebug() << "_calibratedRoll: " << _calibratedRoll; + qDebug() << "----------------------------"; +#endif +} + +/*! + \qmlproperty qreal QtSensors5::TiltSensor::settings + This property contains the setting of the current state. + It can be used for saving and reloading previously saved calibrations. +*/ +QByteArray QSensor2Tilt::settings() const +{ + QByteArray arr; + arr.append(QString::number((double)_calibratedPitch)); + arr.append(";"); + arr.append(QString::number((double)_calibratedRoll)); + return arr; +} + +void QSensor2Tilt::setSettings(const QByteArray val) +{ + QString str(val); + if (str.indexOf(";") > 0){ + QStringList strlist = str.split(";"); + if (strlist.length() == 2){ + _calibratedPitch = strlist.at(0).toDouble(); + _calibratedRoll = strlist.at(1).toDouble(); + } } - return ret; } bool QSensor2Tilt::filter(QAccelerometerReading* reading) @@ -280,86 +288,68 @@ bool QSensor2Tilt::filter(QAccelerometerReading* reading) |/___ x */ - int x = reading->x(); - int y = reading->y(); - int z = reading->z(); - float xrot = 0; - float yrot = 0; - - switch (_measureFrom) - { - case QSensor2Tilt::FaceUp: - /* - y - | => Ax = x, Ay = y, Az = z - |___ x - */ - yrot = calcPitch(x, y, z); - xrot = calcRoll(x, y, z); - break; - case QSensor2Tilt::LeftUp: - /* - z - | => Ax = y, Ay = z, Az = x - |___ y - */ - yrot = calcPitch(y, z, x); - xrot = -calcRoll(y, z, x); - break; - case QSensor2Tilt::TopDown: - /* - z - | => Ax = -x, Ay = z, Az = y - |___ -x - */ - yrot = -calcPitch(x, z, y); - xrot = -calcRoll(x, z, y); - break; - case QSensor2Tilt::FaceDown: - /* - -y - | => Ax = x, Ay = -y, Az = -z - |___ x - */ - yrot = calcPitch(-x, -y, z); - xrot = -calcRoll(-x, -y, z); - break; - case QSensor2Tilt::RightUp: - /* - z - | => Ax = -y, Ay = z, Az = -x - |___ -y - */ - yrot = calcPitch(-y, z, -x); - xrot = -calcRoll(-y, z, -x); - break; - case QSensor2Tilt::TopUp: - /* - z - | => Ax = x, Ay = z, Az = -y - |___ x - */ - yrot = calcPitch(x, z, -y); - xrot = -calcRoll(x, z, -y); - break; - } - - - - if (!_useRadian){ - yrot = 180 * yrot / M_PI; - xrot = 180 * xrot / M_PI; - } - - if (xrot != _xRotation){ + 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 beteen 0 and 180 or 0 -180 + qreal aG = 1 * sin(xrot); + qreal aK = 1 * cos(xrot); + xrot = atan2(aG, aK); + if (xrot > M_PI_2) + xrot = M_PI - xrot; + else if (xrot < -M_PI_2) + xrot = -(M_PI + xrot); + aG = 1 * sin(yrot); + aK = 1 * cos(yrot); + yrot = atan2(aG, aK); + if (yrot > M_PI_2) + yrot = M_PI - yrot; + else if (yrot < -M_PI_2) + yrot = -(M_PI + yrot); + + +#ifdef LOGCALIBRATION + qDebug() << "new xrot: " << xrot; + qDebug() << "new yrot: " << yrot; + qDebug() << "----------------------------------"; +#endif + qreal dxrot = xrot - _xRotation; + qreal dyrot = yrot - _yRotation; + if (dxrot < 0) dxrot = -dxrot; + if (dyrot < 0) dyrot = -dyrot; + + bool change = false; + if (dxrot >= _radAccuracy){ _xRotation = xrot; emit xRotationChanged(); + change = true; } - if (yrot != _yRotation){ + if (dyrot >= _radAccuracy){ _yRotation = yrot; emit yRotationChanged(); + change = true; + } + if (change){ + if (_unit == QSensor2Tilt::Degrees) + emit tiltChanged(dxrot * 180 / M_PI, dyrot * 180 / M_PI); + else + emit tiltChanged(dxrot, dyrot); } - return false; } diff --git a/src/imports/sensors2/qsensor2tilt.h b/src/imports/sensors2/qsensor2tilt.h index 23f27271..8ad03271 100644 --- a/src/imports/sensors2/qsensor2tilt.h +++ b/src/imports/sensors2/qsensor2tilt.h @@ -50,59 +50,63 @@ QT_BEGIN_NAMESPACE class QSensor2Tilt : public QObject, public QAccelerometerFilter { Q_OBJECT - Q_ENUMS(TiltReference) - Q_PROPERTY(TiltReference measureFrom READ measureFrom WRITE setMeasureFrom NOTIFY measureFromChanged) - Q_PROPERTY(float yRotation READ yRotation NOTIFY yRotationChanged) - Q_PROPERTY(float xRotation READ xRotation NOTIFY xRotationChanged) - Q_PROPERTY(bool radian READ radian WRITE setRadian) + Q_ENUMS(Unit) + Q_PROPERTY(qreal yRotation READ yRotation NOTIFY yRotationChanged) + Q_PROPERTY(qreal xRotation READ xRotation NOTIFY xRotationChanged) + Q_PROPERTY(Unit unit READ unit WRITE setUnit NOTIFY unitChanged) Q_PROPERTY(int dataRate READ dataRate WRITE setDataRate NOTIFY dataRateChanged) - Q_PROPERTY(bool running READ running WRITE setRunning NOTIFY runningChanged) + Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged) + Q_PROPERTY(qreal accuracy READ accuracy WRITE setAccuracy NOTIFY accuracyChanged) + Q_PROPERTY(QByteArray settings READ settings WRITE setSettings) public: QSensor2Tilt(QObject* parent = 0); virtual ~QSensor2Tilt(); + Q_INVOKABLE void calibrate(); - enum TiltReference{ - TopUp = 0, - TopDown, - LeftUp, - RightUp, - FaceUp, - FaceDown + enum Unit{ + Radians = 0 + , Degrees }; Q_SIGNALS: - void measureFromChanged(); + void unitChanged(); void yRotationChanged(); void xRotationChanged(); void dataRateChanged(); - void runningChanged(); + void enabledChanged(); + void tiltChanged(qreal deltaX, qreal deltaY); + void accuracyChanged(); private: // Override of QAcclerometerFilter::filter(QAccelerometerReading*) bool filter(QAccelerometerReading* reading); - - TiltReference measureFrom(); - void setMeasureFrom(TiltReference val); - float yRotation(); - float xRotation(); - bool radian(); - void setRadian(bool val); + qreal yRotation(); + qreal xRotation(); + Unit unit(); + void setUnit(const Unit val); int dataRate(); - void setDataRate(int val); - bool running(); - void setRunning(bool val); + void setDataRate(const int val); + bool enabled(); + void setEnabled(bool val); + qreal accuracy(); + void setAccuracy(const qreal val); + QByteArray settings() const; + void setSettings(const QByteArray val); QAccelerometer* _accel; - TiltReference _measureFrom; - float _yRotation; - float _xRotation; - bool _useRadian; - bool _init; + qreal _yRotation; + qreal _xRotation; + qreal _radAccuracy; + Unit _unit; + qreal _pitch; + qreal _roll; + qreal _calibratedPitch; + qreal _calibratedRoll; }; -QML_DECLARE_TYPE(QT_PREPEND_NAMESPACE(QSensor2Tilt)) - QT_END_NAMESPACE +QML_DECLARE_TYPE(QSensor2Tilt) + #endif // QSEONSOR2TILT_H diff --git a/tests/auto/sensors2qmlapi/qdeclaccelerometer.h b/tests/auto/sensors2qmlapi/qdeclaccelerometer.h index 0ec2061f..a94bd62f 100644 --- a/tests/auto/sensors2qmlapi/qdeclaccelerometer.h +++ b/tests/auto/sensors2qmlapi/qdeclaccelerometer.h @@ -62,7 +62,7 @@ public: void stop() { _active = false; } bool isActive() { return _active; } - void test(int x, int y, int z) + void test(float x, float y, float z) { if (sensor()->filters().count() > 0){ QAccelerometerFilter* af = (QAccelerometerFilter*)sensor()->filters().at(0); diff --git a/tests/auto/sensors2qmlapi/tst_sensors2qmlapi.cpp b/tests/auto/sensors2qmlapi/tst_sensors2qmlapi.cpp index 9ffafb94..6d277502 100644 --- a/tests/auto/sensors2qmlapi/tst_sensors2qmlapi.cpp +++ b/tests/auto/sensors2qmlapi/tst_sensors2qmlapi.cpp @@ -64,6 +64,8 @@ private slots: void testTilt_data(); void testTilt(); void testTilt_receivedSignalsCount(); + void testTiltAccuracy(); + void testTiltCalibration(); void testProximity(); void testAmbientLight(); @@ -143,54 +145,67 @@ void tst_Sensors2QMLAPI::testAmbientLight() void tst_Sensors2QMLAPI::testTilt_data() { - QTest::addColumn<int>("pos"); + QTest::addColumn<qreal>("pitch"); + QTest::addColumn<qreal>("roll"); QTest::addColumn<int>("x"); QTest::addColumn<int>("y"); QTest::addColumn<int>("z"); QTest::addColumn<int>("xRotation"); QTest::addColumn<int>("yRotation"); - QTest::newRow("TopUp001") << (int)QSensor2Tilt::TopUp << 0 << 0 << 1 << -90 << 0; - QTest::newRow("TopDown001") << (int)QSensor2Tilt::TopDown << 0 << 0 << 1 << -90 << 0; - QTest::newRow("LeftUp001") << (int)QSensor2Tilt::LeftUp << 0 << 0 << 1 << -90 << 0; - QTest::newRow("RightUp001") << (int)QSensor2Tilt::RightUp << 0 << 0 << 1 << -90 << 0; - QTest::newRow("FaceUp001") << (int)QSensor2Tilt::FaceUp << 0 << 0 << 1 << 0 << 0; - QTest::newRow("FaceDown001") << (int)QSensor2Tilt::FaceDown << 0 << 0 << 1 << 0 << 0; - - QTest::newRow("TopUp010") << (int)QSensor2Tilt::TopUp << 0 << 1 << 0 << 0 << 0; - QTest::newRow("TopDown010") << (int)QSensor2Tilt::TopDown << 0 << 1 << 0 << 0 << 0; - QTest::newRow("LeftUp010") << (int)QSensor2Tilt::LeftUp << 0 << 1 << 0 << 0 << -90; - QTest::newRow("RightUp010") << (int)QSensor2Tilt::RightUp << 0 << 1 << 0 << 0 << 90; - QTest::newRow("FaceUp010") << (int)QSensor2Tilt::FaceUp << 0 << 1 << 0 << 90 << 0; - QTest::newRow("FaceDown010") << (int)QSensor2Tilt::FaceDown << 0 << 1 << 0 << 90 << 0; - - QTest::newRow("TopUp100") << (int)QSensor2Tilt::TopUp << 1 << 0 << 0 << 0 << -90; - QTest::newRow("TopDown100") << (int)QSensor2Tilt::TopDown << 1 << 0 << 0 << 0 << 90; - QTest::newRow("LeftUp100") << (int)QSensor2Tilt::LeftUp << 1 << 0 << 0 << 0 << 0; - QTest::newRow("RightUp100") << (int)QSensor2Tilt::RightUp << 1 << 0 << 0 << 0 << 0; - QTest::newRow("FaceUp100") << (int)QSensor2Tilt::FaceUp << 1 << 0 << 0 << 0 << -90; - QTest::newRow("FaceDown100") << (int)QSensor2Tilt::FaceDown << 1 << 0 << 0 << 0 << 90; - - QTest::newRow("TopUp101") << (int)QSensor2Tilt::TopUp << 1 << 0 << 1 << -45 << -45; - QTest::newRow("TopDown101") << (int)QSensor2Tilt::TopDown << 1 << 0 << 1 << -45 << 45; - QTest::newRow("LeftUp101") << (int)QSensor2Tilt::LeftUp << 1 << 0 << 1 << -45 << 0; - QTest::newRow("RightUp101") << (int)QSensor2Tilt::RightUp << 1 << 0 << 1 << -45 << 0; - QTest::newRow("FaceUp101") << (int)QSensor2Tilt::FaceUp << 1 << 0 << 1 << 0 << -45; - QTest::newRow("FaceDown101") << (int)QSensor2Tilt::FaceDown << 1 << 0 << 1 << 0 << 45; - - QTest::newRow("TopUp110") << (int)QSensor2Tilt::TopUp << 1 << 1 << 0 << 0 << -45; - QTest::newRow("TopDown110") << (int)QSensor2Tilt::TopDown << 1 << 1 << 0 << 0 << 45; - QTest::newRow("LeftUp110") << (int)QSensor2Tilt::LeftUp << 1 << 1 << 0 << 0 << -45; - QTest::newRow("RightUp110") << (int)QSensor2Tilt::RightUp << 1 << 1 << 0 << 0 << 45; - QTest::newRow("FaceUp110") << (int)QSensor2Tilt::FaceUp << 1 << 1 << 0 << 45 << -45; - QTest::newRow("FaceDown110") << (int)QSensor2Tilt::FaceDown << 1 << 1 << 0 << 45 << 45; - - QTest::newRow("TopUp011") << (int)QSensor2Tilt::TopUp << 0 << 1 << 1 << -45 << 0; - QTest::newRow("TopDown011") << (int)QSensor2Tilt::TopDown << 0 << 1 << 1 << -45 << 0; - QTest::newRow("LeftUp011") << (int)QSensor2Tilt::LeftUp << 0 << 1 << 1 << -45 << -45; - QTest::newRow("RightUp011") << (int)QSensor2Tilt::RightUp << 0 << 1 << 1 << -45 << 45; - QTest::newRow("FaceUp011") << (int)QSensor2Tilt::FaceUp << 0 << 1 << 1 << 45 << 0; - QTest::newRow("FaceDown011") << (int)QSensor2Tilt::FaceDown << 0 << 1 << 1 << 45 << 0; + //Face up + // Pitch Roll x y z xRot yRot + QTest::newRow("FaceUp1") << 0.0 << 0.0 << 0 << 0 << 1 << 0 << 0; + QTest::newRow("FaceUp2") << 0.0 << 0.0 << 0 << 1 << 0 << 90 << 0; + QTest::newRow("FaceUp3") << 0.0 << 0.0 << 1 << 0 << 0 << 0 << -90; + QTest::newRow("FaceUp4") << 0.0 << 0.0 << 1 << 0 << 1 << 0 << -45; + QTest::newRow("FaceUp5") << 0.0 << 0.0 << 1 << 1 << 0 << 45 << -45; + QTest::newRow("FaceUp6") << 0.0 << 0.0 << 0 << 1 << 1 << 45 << 0; + + //top up roll = 90 + // Pitch Roll x y z xRot yRot + QTest::newRow("TopUp1") << 0.0 << 90.0 << 0 << 0 << 1 << -90 << 0; + QTest::newRow("TopUp2") << 0.0 << 90.0 << 0 << 1 << 0 << 0 << 0; + QTest::newRow("TopUp3") << 0.0 << 90.0 << 1 << 0 << 0 << -90 << -90; + QTest::newRow("TopUp4") << 0.0 << 90.0 << 1 << 0 << 1 << -90 << -45; + QTest::newRow("TopUp5") << 0.0 << 90.0 << 1 << 1 << 0 << -45 << -45; + QTest::newRow("TopUp6") << 0.0 << 90.0 << 0 << 1 << 1 << -45 << 0; + + //top down roll = -90 + // Pitch Roll x y z xRot yRot + QTest::newRow("TopDown1") << 0.0 <<-90.0 << 0 << 0 << 1 << 90 << 0; + QTest::newRow("TopDown2") << 0.0 <<-90.0 << 0 << 1 << 0 << 0 << 0; + QTest::newRow("TopDown3") << 0.0 <<-90.0 << 1 << 0 << 0 << 90 << -90; + QTest::newRow("TopDown4") << 0.0 <<-90.0 << 1 << 0 << 1 << 90 << -45; + QTest::newRow("TopDown5") << 0.0 <<-90.0 << 1 << 1 << 0 << 45 << -45; + QTest::newRow("TopDown6") << 0.0 <<-90.0 << 0 << 1 << 1 << 45 << 0; + + //Left up Pitch = -90 Roll = 90 + // Pitch Roll x y z xRot yRot + QTest::newRow("LeftUp1") <<-90.0 << 90.0 << 0 << 0 << 1 << -90 << 90; + QTest::newRow("LeftUp2") <<-90.0 << 90.0 << 0 << 1 << 0 << 0 << 90; + QTest::newRow("LeftUp3") <<-90.0 << 90.0 << 1 << 0 << 0 << -90 << 0; + QTest::newRow("LeftUp4") <<-90.0 << 90.0 << 1 << 0 << 1 << -90 << 45; + QTest::newRow("LeftUp5") <<-90.0 << 90.0 << 1 << 1 << 0 << -45 << 45; + QTest::newRow("LeftUp6") <<-90.0 << 90.0 << 0 << 1 << 1 << -45 << 90; + + //Right up Pitch = -90 Roll = 90 + // Pitch Roll x y z xRot yRot + QTest::newRow("RightUp1") << 90.0 << 90.0 << 0 << 0 << 1 << -90 << -90; + QTest::newRow("RightUp2") << 90.0 << 90.0 << 0 << 1 << 0 << 0 << -90; + QTest::newRow("RightUp3") << 90.0 << 90.0 << 1 << 0 << 0 << -90 << 0; + QTest::newRow("RightUp4") << 90.0 << 90.0 << 1 << 0 << 1 << -90 << -45; + QTest::newRow("RightUp5") << 90.0 << 90.0 << 1 << 1 << 0 << -45 << -45; + QTest::newRow("RightUp6") << 90.0 << 90.0 << 0 << 1 << 1 << -45 << -90; + + //Face Down Pitch = -180 Roll = -180 + // Pitch Roll x y z xRot yRot + QTest::newRow("FaceDown1") <<-180.0<<-180.0<< 0 << 0 << 1 << 0 << 0; + QTest::newRow("FaceDown2") <<-180.0<<-180.0<< 0 << 1 << 0 << -90 << 0; + QTest::newRow("FaceDown3") <<-180.0<<-180.0<< 1 << 0 << 0 << 0 << 90; + QTest::newRow("FaceDown4") <<-180.0<<-180.0<< 1 << 0 << 1 << 0 << 45; + QTest::newRow("FaceDown5") <<-180.0<<-180.0<< 1 << 1 << 0 << -45 << 45; + QTest::newRow("FaceDown6") <<-180.0<<-180.0<< 0 << 1 << 1 << -45 << 0; } int xrotch = 0; @@ -198,7 +213,8 @@ int yrotch = 0; int datarate = 10; void tst_Sensors2QMLAPI::testTilt() { - QFETCH(int, pos); + QFETCH(qreal, pitch); + QFETCH(qreal, roll); QFETCH(int, x); QFETCH(int, y); QFETCH(int, z); @@ -208,23 +224,30 @@ void tst_Sensors2QMLAPI::testTilt() if (!_tilt) _tilt = new QSensor2Tilt(this); - QSignalSpy spy(_tilt, SIGNAL(runningChanged())); - _tilt->setProperty("running", QVariant(true)); + QSignalSpy spy(_tilt, SIGNAL(enabledChanged())); + _tilt->setProperty("enabled", QVariant(true)); QCOMPARE(spy.count() , 1); spy.clear(); - _tilt->setProperty("running", QVariant(true)); + _tilt->setProperty("enabled", QVariant(true)); QCOMPARE(spy.count() , 0); - _tilt->setProperty("radian", QVariant(false)); + _tilt->setProperty("unit", QVariant((int)QSensor2Tilt::Radians)); + QSignalSpy spyunitch(_tilt, SIGNAL(unitChanged())); + _tilt->setProperty("unit", QVariant((int)QSensor2Tilt::Degrees)); + QCOMPARE(spyunitch.count() , 1); - QSignalSpy spymf(_tilt, SIGNAL(measureFromChanged())); - _tilt->setProperty("measureFrom", QVariant(pos)); - QVERIFY((int)_tilt->property("measureFrom").toInt() == pos); - QCOMPARE(spymf.count() , 1); - spymf.clear(); - _tilt->setProperty("measureFrom", QVariant(pos)); - QCOMPARE(spymf.count() , 0); + QByteArray settings; + settings.append(QString::number((double)-5.0)); + settings.append(";"); + settings.append(QString::number((double)-5.0)); + _tilt->setProperty("settings", QVariant((QByteArray)settings)); + + QByteArray settings1; + settings1.append(QString::number((double)(pitch * M_PI / 180), 'g', 20)); + settings1.append(";"); + settings1.append(QString::number((double)(roll * M_PI / 180), 'g', 20)); + _tilt->setProperty("settings", QVariant((QByteArray)settings1)); QDeclAccelerometer* accel = _plugin.stAccel; @@ -238,10 +261,11 @@ void tst_Sensors2QMLAPI::testTilt() QCOMPARE(yRotation, (int)_tilt->property("yRotation").toFloat()); spy.clear(); - _tilt->setProperty("running", QVariant(false)); + _tilt->setProperty("enabled", QVariant(false)); QCOMPARE(spy.count() , 1); + spy.clear(); - _tilt->setProperty("running", QVariant(false)); + _tilt->setProperty("enabled", QVariant(false)); QCOMPARE(spy.count() , 0); datarate++; @@ -250,10 +274,84 @@ void tst_Sensors2QMLAPI::testTilt() QCOMPARE(spydr.count() , 1); } +void tst_Sensors2QMLAPI::testTiltAccuracy() +{ + if (!_tilt) + _tilt = new QSensor2Tilt(this); + + QByteArray settings; + settings.append(QString::number((double)(0))); + settings.append(";"); + settings.append(QString::number((double)(0))); + _tilt->setProperty("settings", QVariant((QByteArray)settings)); + _tilt->setProperty("enable", QVariant(true)); + + QDeclAccelerometer* accel = _plugin.stAccel; + + //be sure we set rotation to 0 + _tilt->setProperty("accuracy", QVariant((double)0)); + accel->test(0,0,1); + + //now set accuracy a little bigger then Pi / 4 in deg + _tilt->setProperty("accuracy", QVariant((double)180 / 4 + 0.1)); + QSignalSpy spyxrot(_tilt, SIGNAL(xRotationChanged())); + QSignalSpy spyyrot(_tilt, SIGNAL(yRotationChanged())); + + accel->test(1, 1, 0); + QCOMPARE(spyxrot.count() , 0); + QCOMPARE(spyyrot.count() , 0); + + spyxrot.clear(); + spyyrot.clear(); + _tilt->setProperty("accuracy", QVariant((double)180 / 4 + 0.1 - 0.1)); + accel->test(1, 1, 0); + QCOMPARE(spyxrot.count() , 1); + QCOMPARE(spyyrot.count() , 1); + +} + void tst_Sensors2QMLAPI::testTilt_receivedSignalsCount() { - QCOMPARE(xrotch , 9); - QCOMPARE(yrotch , 23); + QCOMPARE(xrotch , 23); + QCOMPARE(yrotch , 21); +} + +void tst_Sensors2QMLAPI::testTiltCalibration() +{ + if (!_tilt) + _tilt = new QSensor2Tilt(this); + + QByteArray settings; + settings.append(QString::number((double)(0))); + settings.append(";"); + settings.append(QString::number((double)(0))); + _tilt->setProperty("settings", QVariant((QByteArray)settings)); + _tilt->setProperty("enable", QVariant(true)); + _tilt->setProperty("unit", QVariant((int)QSensor2Tilt::Degrees)); + + QDeclAccelerometer* accel = _plugin.stAccel; + + //be sure we set rotation to 0 + _tilt->setProperty("accuracy", QVariant((double)0)); + accel->test(-3.59904, 5.52114, 7.07059); + _tilt->calibrate(); + accel->test(-3.59904, 5.52114, 7.07059); + qreal xRotation = (qreal)_tilt->property("xRotation").toFloat(); + qreal yRotation = (qreal)_tilt->property("yRotation").toFloat(); + if (xRotation < 0) xRotation = -xRotation; + if (yRotation < 0) yRotation = -yRotation; + QVERIFY(xRotation < 0.001); + QVERIFY(yRotation < 0.001); + + accel->test(0.519752, 6.82543, 7.06079); + _tilt->calibrate(); + accel->test(0.539366, 6.79601, 7.0804); + xRotation = (qreal)_tilt->property("xRotation").toFloat(); + yRotation = (qreal)_tilt->property("yRotation").toFloat(); + if (xRotation < 0) xRotation = -xRotation; + if (yRotation < 0) yRotation = -yRotation; + QVERIFY(xRotation < 0.25); + QVERIFY(yRotation < 0.12); } QTEST_MAIN(tst_Sensors2QMLAPI) |