[Merge] lp:~ricmm/qtubuntu-sensors/new-orientation into lp:qtubuntu-sensors

Ricardo Salveti rsalveti at rsalveti.net
Sat Jun 21 04:58:13 UTC 2014


Review: Needs Information

Comments inline.

Diff comments:

> === modified file 'debian/control'
> --- debian/control	2014-05-26 14:08:48 +0000
> +++ debian/control	2014-06-20 12:29:45 +0000
> @@ -5,7 +5,7 @@
>  Build-Depends: debhelper (>= 9),
>                 cmake,
>                 libgtest-dev,
> -               libubuntu-application-api-dev (>= 2.0.0),
> +               libubuntu-application-api-dev (>= 2.1.0),
>                 libprocess-cpp-dev (>= 0.0.1+14.04.20131212),
>                 ubuntu-application-api2-test,
>                 pkg-config,
> 
> === modified file 'debian/rules'
> --- debian/rules	2014-01-13 11:47:11 +0000
> +++ debian/rules	2014-06-20 12:29:45 +0000
> @@ -14,6 +14,3 @@
>  	dh_install --fail-missing
>  
>  override_dh_auto_test:
> -ifeq (, $(findstring nocheck, $(DEB_BUILD_OPTIONS)))
> -	cd obj-* && ctest --verbose
> -endif

What is the reason to remove this block?

> 
> === modified file 'plugins/sensors/core_orientation_sensor.cpp'
> --- plugins/sensors/core_orientation_sensor.cpp	2014-01-07 07:26:33 +0000
> +++ plugins/sensors/core_orientation_sensor.cpp	2014-06-20 12:29:45 +0000
> @@ -18,10 +18,29 @@
>  
>  #include "core_shared_accelerometer.h"
>  
> +#include <math.h>
> +
>  #include <QDebug>
>  
>  const float core::OrientationSensor::m_accelDelta = 7.35;
>  
> +#define CLOCKWISE           1
> +#define COUNTER_CLOCKWISE   0
> +
> +#define RAD_TO_DEG(x)   (x * 57.2957)
> +
> +// Values taken from Android's orientation helper
> +const float core::OrientationSensor::m_minAccel = 5.8;
> +const float core::OrientationSensor::m_maxAccel = 13.8;
> +const int core::OrientationSensor::m_maxTilt = 75;
> +
> +const int core::OrientationSensor::m_tiltTolerance[4][2] = {
> +    { -25, 70 },
> +    { -25, 65 },
> +    { -25, 60 },
> +    { -25, 65 }
> +};
> +
>  core::OrientationSensor::OrientationSensor(QSensor *sensor)
>      : QSensorBackend(sensor)
>  {
> @@ -51,6 +70,7 @@
>  void core::OrientationSensor::start()
>  {
>      core::SharedAccelerometer::instance().start();
> +    core::SharedAccelerometer::instance().setDelay(66667);
>  }
>  
>  void core::OrientationSensor::stop()
> @@ -58,27 +78,122 @@
>      core::SharedAccelerometer::instance().stop();
>  }
>  
> +int nearestRotation = 0;
> +
>  void core::OrientationSensor::onAccelerometerReadingChanged(QSharedPointer<QAccelerometerReading> reading)
>  {
> -    // Interpret the accelerometer data into a meaningful orientation
> -    if (reading->y() > m_accelDelta)
> -        m_reading.setOrientation(QOrientationReading::TopUp);
> -    else if (reading->y() < -m_accelDelta)
> -        m_reading.setOrientation(QOrientationReading::TopDown);
> -    else if (reading->x() > m_accelDelta)
> -        m_reading.setOrientation(QOrientationReading::RightUp);
> -    else if (reading->x() < -m_accelDelta)
> -        m_reading.setOrientation(QOrientationReading::LeftUp);
> -    else if (reading->z() > m_accelDelta)
> -        m_reading.setOrientation(QOrientationReading::FaceUp);
> -    else if (reading->z() < -m_accelDelta)
> -        m_reading.setOrientation(QOrientationReading::FaceDown);
> -
> +    float x = reading->x();
> +    float y = reading->y();
> +    float z = reading->z();
> +
> +    /*
> +     * Low pass filter to remove jitter.
> +     * Code inspired by Android's orientation helper
> +     */  
> +    unsigned long now = reading->timestamp();
> +    unsigned long then = m_lastFilter;
> +
> +    double timeDelta = (now-then)/1000000;
> +    float alpha = timeDelta / (200 + timeDelta);
> +    x = alpha * (x - m_lastX) + m_lastX;
> +    y = alpha * (y - m_lastY) + m_lastY;
> +    z = alpha * (z - m_lastZ) + m_lastZ;
> +
> +    m_lastX = x;
> +    m_lastY = y;
> +    m_lastZ = z;
> +    m_lastFilter = now;
> +
> +    int orientation = round(RAD_TO_DEG((atan2(-x,y))));
> +
> +    if (orientation < 0)
> +        orientation += 360;
> +
> +    if (m_lastOrientation == orientation) // static hit, not considering
> +        return;
> +
> +    m_lastOrientation = orientation;
> +    
> +    float magnitude = sqrt(x*x + y*y + z*z);
> +
> +    /*
> +     * External acceleration and tilt decisions
> +     * Some code inspired by Android's orientation helper
> +     */
> +    if (magnitude < m_minAccel || magnitude > m_maxAccel)
> +        return;
> +
> +    int tiltAngle = round(RAD_TO_DEG(asin(z / magnitude)));
> +
> +    if (tiltAngle > m_maxTilt)
> +        return;
> +      
> +    if (m_readingCache.orientation() == QOrientationReading::Undefined)
> +    {
> +        m_readingCache.setOrientation(QOrientationReading::TopUp);
> +        nearestRotation = 0;
> +    }
> +    else
> +    {
> +        nearestRotation = (orientation + 45) / 90;
> +        
> +        if (nearestRotation > 3)
> +            nearestRotation = 0;
> +
> +        if (!(tiltAngle >= m_tiltTolerance[nearestRotation][0] && tiltAngle <= m_tiltTolerance[nearestRotation][1]))
> +            return;
> +
> +        /*
> +         * Calculate rotation-hint offsets
> +         * Inspired by Android code
> +         */
> +        if (m_lastRotation == nearestRotation
> +                || nearestRotation == (m_lastRotation + 1) % 4) {
> +            int lowerBound = (nearestRotation*90) - 45 + 45/2;
> +            if (nearestRotation == 0) {
> +                if (orientation >= 315 && orientation < lowerBound + 360) {
> +                    return;
> +                }
> +            }
> +            else
> +            {
> +                if (orientation < lowerBound) {
> +                    return;
> +                }
> +            }
> +        }
> +
> +        if (m_lastRotation == nearestRotation
> +                || nearestRotation == (m_lastRotation + 3) % 4) {
> +            int upperBound = (nearestRotation*90) + 45 - 45/2;
> +            if (nearestRotation == 0) {
> +                if (orientation <= 45 && orientation > upperBound) {
> +                    return;
> +                }
> +            }
> +            else
> +            {
> +                if (orientation > upperBound) {
> +                    return;                    
> +                }
> +            }
> +        }
> +    }
> +   
> +    
> +    switch (nearestRotation)
> +    {
> +    case 0: m_reading.setOrientation(QOrientationReading::TopUp); break;
> +    case 1: m_reading.setOrientation(QOrientationReading::LeftUp); break;
> +    case 2: m_reading.setOrientation(QOrientationReading::TopDown); break;
> +    case 3: m_reading.setOrientation(QOrientationReading::RightUp); break;
> +    }
> +    
>      if (m_reading.orientation() != m_readingCache.orientation())
>      {
> -        // Emit readingChanged signal only if orientation actually changes
>          newReadingAvailable();
> +        m_readingCache.setOrientation(m_reading.orientation());
>      }
>  
> -    m_readingCache.setOrientation(m_reading.orientation());
> +    m_lastRotation = nearestRotation;
>  }
> 
> === modified file 'plugins/sensors/core_orientation_sensor.h'
> --- plugins/sensors/core_orientation_sensor.h	2014-01-07 07:26:33 +0000
> +++ plugins/sensors/core_orientation_sensor.h	2014-06-20 12:29:45 +0000
> @@ -19,6 +19,7 @@
>  
>  #include <QAccelerometerReading>
>  #include <QOrientationReading>
> +#include <QVector>
>  
>  #include <qsensorbackend.h>
>  
> @@ -50,9 +51,21 @@
>      // The distance from the center, right or left, that will trigger an
>      // orientation change when the user rotates the target device.
>      static const float m_accelDelta;
> +    static const float m_minAccel;
> +    static const float m_maxAccel;
> +    static const int m_maxTilt;
> +    static const int m_tiltTolerance[4][2];
> +
> +    int m_lastX;
> +    int m_lastY;
> +    int m_lastZ;
> +    unsigned long m_lastFilter;
>  
>      QOrientationReading m_reading;
>      QOrientationReading m_readingCache;
> +    int m_lastOrientation;
> +    int m_lastRotation;
> +    QVector<bool> m_directionHistory;
>  };
>  }
>  
> 
> === modified file 'plugins/sensors/core_shared_accelerometer.cpp'
> --- plugins/sensors/core_shared_accelerometer.cpp	2014-02-18 17:18:00 +0000
> +++ plugins/sensors/core_shared_accelerometer.cpp	2014-06-20 12:29:45 +0000
> @@ -94,6 +94,11 @@
>      return m_resolution;
>  }
>  
> +void core::SharedAccelerometer::setDelay(quint32 delay_us)
> +{
> +    ua_sensors_accelerometer_set_event_rate(m_accelerometer, delay_us * 1000);
> +}
> +
>  void core::SharedAccelerometer::onAccelerometerReadingCb(UASAccelerometerEvent *event, void *context)
>  {
>      SharedAccelerometer* ac = static_cast<SharedAccelerometer*>(context);
> 
> === modified file 'plugins/sensors/core_shared_accelerometer.h'
> --- plugins/sensors/core_shared_accelerometer.h	2014-01-30 11:44:37 +0000
> +++ plugins/sensors/core_shared_accelerometer.h	2014-06-20 12:29:45 +0000
> @@ -39,6 +39,7 @@
>      qreal getMinValue() const;
>      qreal getMaxValue() const;
>      qreal getResolution() const;
> +    void setDelay(quint32 delay_us);
>  
>  Q_SIGNALS:
>      void accelerometerReadingChanged(QSharedPointer<QAccelerometerReading> reading);
> 


-- 
https://code.launchpad.net/~ricmm/qtubuntu-sensors/new-orientation/+merge/223576
Your team Ubuntu Phablet Team is subscribed to branch lp:qtubuntu-sensors.



More information about the Ubuntu-reviews mailing list