Reducing accelerometer sensor noise with Qt
hamishwillee
(Talk | contribs) m (Hamishwillee - Bot fixing redirect link.) |
hamishwillee
(Talk | contribs) m (Hamishwillee - Addition to article of: Category:MeeGo Category:Symbian. (Add platform categories)) |
||
| 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]] [[Category:Symbian]] |
Revision as of 08:34, 15 February 2012
This code snippet shows how to reduce accelerometer noise using Qt.
Contents |
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.
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.
Preconditions
- Qt 4.7 or higher is installed.
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());
}
Postconditions
This code snippet demonstrated how to reduce accelerometer noise using Qt.
Article Metadata
Tested with
Compatibility
Article

