×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:How to get accelerometer data of N900 using Qt

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Archived - no longer relevant because correct approach is to use Qt Mobility APIs)
hamishwillee (Talk | contribs)
m (Text replace - "<code cpp>" to "<code cpp-qt>")
 
(6 intermediate revisions by one user not shown)
Line 1: Line 1:
[[Category:Maemo]][[Category:Qt]]
 
 
{{Archived|Qt applications should now access accelerometer data using the cross platform Qt Mobility [http://doc.qt.nokia.com/qtmobility-1.2.0-beta1/qtsensors.html#details Sensor API]}}
 
{{Archived|Qt applications should now access accelerometer data using the cross platform Qt Mobility [http://doc.qt.nokia.com/qtmobility-1.2.0-beta1/qtsensors.html#details Sensor API]}}
 +
{{ArticleMetaData <!-- v1.2 -->
 +
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 +
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 +
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 +
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
 +
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 +
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20100324
 +
|author= [[User:Flaviofabricioferreira]]
 +
}}[[Category:Maemo]][[Category:Qt]]
 
{{FeaturedArticle|timestamp=20100502}}
 
{{FeaturedArticle|timestamp=20100502}}
 
{{Abstract|Archived: This article demonstrates how to develop a Qt Application that will to get the information from the accelerometer sensor at N900 device using native methods. }}
 
{{Abstract|Archived: This article demonstrates how to develop a Qt Application that will to get the information from the accelerometer sensor at N900 device using native methods. }}
Line 16: Line 37:
 
The values are in mG (milli G). 1000 = 1 G. The '''g-force''' experienced by an object is its acceleration relative to free-fall like described above.
 
The values are in mG (milli G). 1000 = 1 G. The '''g-force''' experienced by an object is its acceleration relative to free-fall like described above.
  
At N900 device , the values are connected to the position showed below.
+
At N900 device, the values are connected to the position showed below.
  
 
[[File:postN900Accelerometer.png|700px|900px]]
 
[[File:postN900Accelerometer.png|700px|900px]]
Line 24: Line 45:
 
*At starts, i created a QThread. This thread will read the file with data at all the time.
 
*At starts, i created a QThread. This thread will read the file with data at all the time.
  
<code cpp>
+
<code cpp-qt>
 
class aThread : public QThread
 
class aThread : public QThread
 
{
 
{
Line 46: Line 67:
 
</code>
 
</code>
  
*At implementation of our thread , the '''updateCoords()''' method is called with 20ms interval.
+
*At implementation of our thread, the '''updateCoords()''' method is called with 20ms interval.
  
<code cpp>
+
<code cpp-qt>
 
void aThread::run()
 
void aThread::run()
 
{
 
{
Line 60: Line 81:
 
*This method open the file, read and process the string found on the file.
 
*This method open the file, read and process the string found on the file.
  
<code cpp>
+
<code cpp-qt>
 
void aThread::updateCoords()
 
void aThread::updateCoords()
 
{
 
{
Line 74: Line 95:
 
*Then, in the processing of information, the string found is split by the white spaces creating three different strings.These strings are the X,Y,Z coordinates of accelerometer.The signal '''deviceOrientation()''' is emitted to allowing that other class can connect yours slots to this signal.  
 
*Then, in the processing of information, the string found is split by the white spaces creating three different strings.These strings are the X,Y,Z coordinates of accelerometer.The signal '''deviceOrientation()''' is emitted to allowing that other class can connect yours slots to this signal.  
  
<code cpp>
+
<code cpp-qt>
 
void aThread::processCoords(QString &data)
 
void aThread::processCoords(QString &data)
 
{
 
{
Line 87: Line 108:
 
</code>
 
</code>
  
*At constructor of our superior class , i created the thread described above . Then i connected the signal '''deviceOrientation()''' of our thread with the slot '''showData''' of superior class.  
+
*At constructor of our superior class, i created the thread described above . Then i connected the signal '''deviceOrientation()''' of our thread with the slot '''showData''' of superior class.  
  
<code cpp>
+
<code cpp-qt>
 
Accelerometer::Accelerometer(QWidget *parent)
 
Accelerometer::Accelerometer(QWidget *parent)
 
     : QWidget(parent)
 
     : QWidget(parent)
Line 103: Line 124:
 
*Then the '''showData''' method shows the three axis information of accelerometer sensor at labels at the screen.
 
*Then the '''showData''' method shows the three axis information of accelerometer sensor at labels at the screen.
  
<code cpp>
+
<code cpp-qt>
 
void Accelerometer::showData(QString x, QString y, QString z)
 
void Accelerometer::showData(QString x, QString y, QString z)
 
{
 
{
Line 130: Line 151:
 
[[File:Copyn900.png|400px|left]]  
 
[[File:Copyn900.png|400px|left]]  
  
[[File:Pasten900.png|400px|rigth]][[Category:Qt for Maemo]][[Category:How To]]
+
[[File:Pasten900.png|400px|rigth]][[Category:MeeGo Harmattan]] [[Category:Symbian]]

Latest revision as of 04:19, 11 October 2012

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Qt applications should now access accelerometer data using the cross platform Qt Mobility Sensor API

Article Metadata
Compatibility
Platform(s):
Symbian
Article
Created: flaviofabricioferreira (24 Mar 2010)
Last edited: hamishwillee (11 Oct 2012)
Featured Article
02 May
2010

Archived: This article demonstrates how to develop a Qt Application that will to get the information from the accelerometer sensor at N900 device using native methods.

An accelerometer is a device that measures proper acceleration, the acceleration experienced relative to free-fall.Single- and multi-axis models are available to detect magnitude and direction of the acceleration as a vector quantity, and can be used to sense orientation, vibration and shock. Micro-machined accelerometers are increasingly present in portable electronic devices like mobile phones and video game controllers, to detect the orientation of the device or provide for game input.

Contents

[edit] Accelerometer Data at N900

The accelerometer data is sysfs file information. Sysfs is a virtual file system provided by Linux 2.6. Sysfs exports information about devices and drivers from the kernel device model to userspace, and is also used for configuration.

The three coordinates of accelerometer sensor is in a text file provided on one line and separated by white space. This file is at path below:

/sys/class/i2c-adapter/i2c-3/3-001d/coord

The values are in mG (milli G). 1000 = 1 G. The g-force experienced by an object is its acceleration relative to free-fall like described above.

At N900 device, the values are connected to the position showed below.

PostN900Accelerometer.png

[edit] The Code

  • At starts, i created a QThread. This thread will read the file with data at all the time.
class aThread : public QThread
{
Q_OBJECT
 
public:
aThread(QObject *parent = 0);
void run();
 
QString x;
QString y;
QString z;
 
public slots:
void updateCoords();
void processCoords(QString &data);
 
signals:
void deviceOrientation(QString x, QString y, QString z);
};
  • At implementation of our thread, the updateCoords() method is called with 20ms interval.
void aThread::run()
{
QTimer timer;
connect(&timer, SIGNAL(timeout()), this, SLOT(updateCoords()));
timer.start(20); // 50 Hz update rate
exec();
}
  • This method open the file, read and process the string found on the file.
void aThread::updateCoords()
{
QFile file("/sys/class/i2c-adapter/i2c-3/3-001d/coord");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
QString data = in.readAll();
processCoords(data);
}
  • Then, in the processing of information, the string found is split by the white spaces creating three different strings.These strings are the X,Y,Z coordinates of accelerometer.The signal deviceOrientation() is emitted to allowing that other class can connect yours slots to this signal.
void aThread::processCoords(QString &data)
{
QStringList data_splited = data.split(" ");
 
x = data_splited[0];
y = data_splited[1];
z = data_splited[2];
 
emit deviceOrientation(x, y, z);
}
  • At constructor of our superior class, i created the thread described above . Then i connected the signal deviceOrientation() of our thread with the slot showData of superior class.
Accelerometer::Accelerometer(QWidget *parent)
: QWidget(parent)
{
...
myThread = new aThread(this);
connect(myThread, SIGNAL(deviceOrientation(QString, QString, QString)),
this, SLOT(showData(QString, QString, QString)));
myThread->start(QThread::NormalPriority);
...
  • Then the showData method shows the three axis information of accelerometer sensor at labels at the screen.
void Accelerometer::showData(QString x, QString y, QString z)
{
xlabel->setText(x);
ylabel->setText(y);
zlabel->setText(z);
}

[edit] Screenshots

PostN900AccelerometerSnapshoots.png

[edit] Source Code

File:QtAccelerometer.zip


Note.pngNote: The N900 allow that you use shortcuts with the combination of keys.


Minimizan900.png

rigth

Copyn900.png

rigth

This page was last modified on 11 October 2012, at 04:19.
285 page views in the last 30 days.
×