×
Namespaces

Variants
Actions
(Difference between revisions)

Reducing accelerometer sensor noise with Qt

From Nokia Developer Wiki
Jump to: navigation, search
makivioj (Talk | contribs)
(Makivioj -)
 
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
(10 intermediate revisions by 5 users not shown)
Line 1: Line 1:
[[Category:Maemo]][[Category:MeeGo]][[Category:Qt]][[Category:Qt Mobility]][[Category:Symbian]]
+
[[Category:Qt Mobility]]
 
{{Abstract|This code snippet shows how to reduce accelerometer noise using Qt.}}
 
{{Abstract|This code snippet shows how to reduce accelerometer noise using Qt.}}
  
== Introduction ==  
+
== Introduction ==  
In the [http://www.developer.nokia.com/Community/Wiki/CS000915_-_Reducing_accelerometer_sensor_noise CS000915 - Reducing accelerometer sensor noise] code snippet, it was shown how to reduce accelerometer noise in Symbian/C++. This code snippet will show how this same noise filtering can be achieved using Qt.
+
In the [http://www.developer.nokia.com/Community/Wiki/CS000915_-_Reducing_accelerometer_sensor_noise CS000915 - Reducing accelerometer sensor noise] code snippet, it was shown how to reduce accelerometer noise in Symbian/C++. This code snippet will show how this same noise filtering can be achieved using [[Qt]]. You can refer the code example [http://www.developer.nokia.com/Resources/Code_examples/ Qt Bubble Level v1.3] also where sensor noise reduction is shown.
  
 
== Summary ==  
 
== Summary ==  
Line 9: Line 9:
  
 
== Preconditions ==
 
== Preconditions ==
Qt 4.7 or higher is installed.
+
* Qt 4.7 or higher is installed.
  
 
== Sources ==  
 
== Sources ==  
 
'''accelerometerfilter.h'''   
 
'''accelerometerfilter.h'''   
<code cpp>   
+
<code cpp-qt>   
 
#include <QAccelerometerFilter>  
 
#include <QAccelerometerFilter>  
 
#include <QList>  
 
#include <QList>  
Line 35: Line 35:
  
 
'''accelerometerfilter.cpp'''
 
'''accelerometerfilter.cpp'''
<code cpp>   
+
<code cpp-qt>   
 
#include "accelerometerfilter.h"   
 
#include "accelerometerfilter.h"   
  
Line 47: Line 47:
 
     if (bufferPos < BUFFER_SIZE) {
 
     if (bufferPos < BUFFER_SIZE) {
 
         readingBuffer->append(reading);
 
         readingBuffer->append(reading);
         bufferPos++;         return false;
+
         bufferPos++;
 +
        return false; // Don't send a notification yet
 
     }
 
     }
 
     else {
 
     else {
Line 61: Line 62:
 
         reading->setY(ySum / BUFFER_SIZE);
 
         reading->setY(ySum / BUFFER_SIZE);
 
         reading->setZ(zSum / BUFFER_SIZE);
 
         reading->setZ(zSum / BUFFER_SIZE);
         return true;
+
         return true; // Send a notification that the reading has changed
 
     }
 
     }
 
}
 
}
Line 67: Line 68:
  
 
'''myaccelerometer.h'''
 
'''myaccelerometer.h'''
<code cpp>   
+
<code cpp-qt>   
 
#include <QAccelerometer>  
 
#include <QAccelerometer>  
 
#include <QVariant>   
 
#include <QVariant>   
Line 80: Line 81:
 
     QAccelerometer *accelerometer;   
 
     QAccelerometer *accelerometer;   
 
signals:
 
signals:
     void accelerationChanged(QVariant, QVariant, QVariant);  
+
     void accelerationChanged(qreal, qreal, qreal);  
 
public slots:
 
public slots:
 
     void updateAcceleration();
 
     void updateAcceleration();
Line 86: Line 87:
  
 
'''myaccelerometer.cpp'''   
 
'''myaccelerometer.cpp'''   
<code cpp>   
+
<code cpp-qt>   
 
#include <QAccelerometerReading>  
 
#include <QAccelerometerReading>  
 
#include "myaccelerometer.h"  
 
#include "myaccelerometer.h"  
Line 93: Line 94:
 
MyAccelerometer::MyAccelerometer(QObject *parent) :
 
MyAccelerometer::MyAccelerometer(QObject *parent) :
 
     QObject(parent), accelerometer(new QAccelerometer(this)) {
 
     QObject(parent), accelerometer(new QAccelerometer(this)) {
     accelerometer->addFilter(new AccelerometerFilter(this));
+
     accelerometer->addFilter(new AccelerometerFilter(this)); // Adding the filter
 
     connect(accelerometer, SIGNAL(readingChanged()), this, SLOT(updateAcceleration()));
 
     connect(accelerometer, SIGNAL(readingChanged()), this, SLOT(updateAcceleration()));
 
     accelerometer->start();
 
     accelerometer->start();
Line 100: Line 101:
 
void MyAccelerometer::updateAcceleration() {
 
void MyAccelerometer::updateAcceleration() {
 
     QAccelerometerReading *reading = accelerometer->reading();
 
     QAccelerometerReading *reading = accelerometer->reading();
     emit accelerationChanged(QVariant(reading->x()), QVariant(reading->y()), QVariant(reading->z()));
+
     emit accelerationChanged(reading->x(), reading->y(), reading->z());
 
}
 
}
 
</code>
 
</code>
Line 120: Line 121:
 
|id= <!-- Article Id (Knowledge base articles only) -->  
 
|id= <!-- Article Id (Knowledge base articles only) -->  
 
|creationdate=Jul 04, 2011 <!-- Format DD month, YYYY -->  
 
|creationdate=Jul 04, 2011 <!-- Format DD month, YYYY -->  
|author=[[User:makivioj]] <!-- Display as link [[User:username]] --> }}
+
|author=[[User:makivioj]] <!-- Display as link [[User:username]] --> }}[[Category:MeeGo Harmattan]] [[Category:Symbian]]

Latest revision as of 04:23, 11 October 2012

This code snippet shows how to reduce accelerometer noise using Qt.

Contents

[edit] Introduction

In the CS000915 - Reducing accelerometer sensor noise code snippet, it was shown how to reduce accelerometer noise in Symbian/C++. This code snippet will show how this same noise filtering can be achieved using Qt. You can refer the code example Qt Bubble Level v1.3 also where sensor noise reduction is shown.

[edit] Summary

Qt provides a possibility to add custom filters for QSensor objects. These filters can be used for decreasing the sensors notifying frequency. For the accelerometer a filter can be added by implementing the QAccelerometerFilter interface and adding it for the QAccelerometer object via the addFilter method. In this code snippet sixteen sensor readings are collected to a buffer and the average of these readings is set as the notified reading.

[edit] Preconditions

  • Qt 4.7 or higher is installed.

[edit] Sources

accelerometerfilter.h

#include <QAccelerometerFilter> 
#include <QList>
 
QTM_USE_NAMESPACE
 
#define BUFFER_SIZE 16
 
class AccelerometerFilter : public QObject, public QAccelerometerFilter
{
Q_OBJECT
 
public:
explicit AccelerometerFilter(QObject *parent = 0);
private:
QList<QAccelerometerReading*> *readingBuffer;
int bufferPos;
public slots:
bool filter(QAccelerometerReading *reading);

accelerometerfilter.cpp

#include "accelerometerfilter.h"  
 
AccelerometerFilter::AccelerometerFilter(QObject *parent) :
QAccelerometerFilter(), readingBuffer(new QList<QAccelerometerReading*>())
{
}
 
bool AccelerometerFilter::filter(QAccelerometerReading *reading)
{
if (bufferPos < BUFFER_SIZE) {
readingBuffer->append(reading);
bufferPos++;
return false; // Don't send a notification yet
}
else {
bufferPos = 0;
qreal xSum = 0, ySum = 0, zSum = 0;
for (int i = 0; i < readingBuffer->count(); i++) {
xSum += readingBuffer->at(i)->x();
ySum += readingBuffer->at(i)->y();
zSum += readingBuffer->at(i)->z();
}
readingBuffer->clear();
reading->setX(xSum / BUFFER_SIZE);
reading->setY(ySum / BUFFER_SIZE);
reading->setZ(zSum / BUFFER_SIZE);
return true; // Send a notification that the reading has changed
}
}

myaccelerometer.h

#include <QAccelerometer> 
#include <QVariant>
 
QTM_USE_NAMESPACE
 
class MyAccelerometer : public QObject {
Q_OBJECT
public:
explicit MyAccelerometer(QObject *parent = 0);
private:
QAccelerometer *accelerometer;
signals:
void accelerationChanged(qreal, qreal, qreal);
public slots:
void updateAcceleration();

myaccelerometer.cpp

#include <QAccelerometerReading> 
#include "myaccelerometer.h"
#include "accelerometerfilter.h"
 
MyAccelerometer::MyAccelerometer(QObject *parent) :
QObject(parent), accelerometer(new QAccelerometer(this)) {
accelerometer->addFilter(new AccelerometerFilter(this)); // Adding the filter
connect(accelerometer, SIGNAL(readingChanged()), this, SLOT(updateAcceleration()));
accelerometer->start();
}
 
void MyAccelerometer::updateAcceleration() {
QAccelerometerReading *reading = accelerometer->reading();
emit accelerationChanged(reading->x(), reading->y(), reading->z());
}

[edit] Postconditions

This code snippet demonstrated how to reduce accelerometer noise using Qt.

Article Metadata
Tested with
Devices(s): Nokia N8-00, Nokia E7-00
Compatibility
Platform(s): Symbian, Maemo, Harmattan, Qt 4.7
Symbian
Article
Keywords: sensors, accelerometer, filter, noise
Created: makivioj (04 Jul 2011)
Last edited: hamishwillee (11 Oct 2012)
This page was last modified on 11 October 2012, at 04:23.
68 page views in the last 30 days.