×
Namespaces

Variants
Actions
(Difference between revisions)

Reducing accelerometer sensor noise with Qt

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Remove platform specifier categories (only need specifier for Qt if there is some platform specific behaviour))
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
(4 intermediate revisions by 2 users not shown)
Line 2: Line 2:
 
{{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 13: Line 13:
 
== 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 68: Line 68:
  
 
'''myaccelerometer.h'''
 
'''myaccelerometer.h'''
<code cpp>   
+
<code cpp-qt>   
 
#include <QAccelerometer>  
 
#include <QAccelerometer>  
 
#include <QVariant>   
 
#include <QVariant>   
Line 87: Line 87:
  
 
'''myaccelerometer.cpp'''   
 
'''myaccelerometer.cpp'''   
<code cpp>   
+
<code cpp-qt>   
 
#include <QAccelerometerReading>  
 
#include <QAccelerometerReading>  
 
#include "myaccelerometer.h"  
 
#include "myaccelerometer.h"  
Line 121: 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.
79 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×