My blog has been moved to

Friday, March 05, 2010

n900 and its accelerometer

There is a bunch of code out there (which can be copied and pasted) to grab the acceleration values from Nokia N900. Here I contribute one more, it's Qt-based and using the QtDBus module. The values in x, y, z will have the unit of G.

    QDBusConnection connection(QDBusConnection::systemBus());
    QDBusInterface interface("", "/com/nokia/icd", QString(), connection);
    QDBusPendingReply<QString, QString, QString, int, int, int> reply;
    reply = interface.asyncCall("get_device_orientation");
    x = static_cast<qreal>(reply.argumentAt<3>()) / 1000;
    y = static_cast<qreal>(reply.argumentAt<4>()) / 1000;
    z = static_cast<qreal>(reply.argumentAt<5>()) / 1000;

As usual, error checking is omitted (left as an exercise for the reader). Like Star Wars, there is also a reason I skip the first 3 QStrings. Debug it yourself to see what you would get. In addition, I found out that at most it would take 25 ms to grab all three values. It means, if you run your application at > 40 fps, then better put this function is a separate thread. Actually, consider that you don't want QDBusPendingReply::waitForFinished() to block your entire GUI, this is likely a good idea anyway.

For a full-version of an accelerometer tool, check out the X2 repository under the sub-directory sensor/accelview. Note that technically acceleration > 1 G is always possible, I clamp the values in this example to keep the UI simple.


Fri13 said...

Would it be possible to build a virtual plumbline or rotate a object (like a ragdoll) so it would always stay straight when turning the device around without big lag?

André Somers said...

Of course that is possible. Add a little bit of physics (or something that is close enough), and you'll even be able to make it move in a natural way.

Chris said...

Look like we need an abstraction here - there are much more different cases to access accel's than the nokia one.

Fri13 said...

It could be fun for kids (or other freeminded people! ;)) if there could be a physic airplane what could be rolled around making sounds and the plane would turn to be always on correct position and react to turns and speed correctly.

Could such thing teach kids for physics better way than just playing around with toy plane/car? ;)

Add there a camera support to render a video for background and you get "framed" plane/car what kids could play when gets bored.

And this is just idea for kids. What all other things could be done with accelerometer for adults (and serious minded people) :D

Leo S said...

25ms?? Yikes. The real question is what the heck is taking so long.

Ian Monroe said...

@Chris the QtMobility project has this I believe. So it will be part of Qt eventually.

@Ariya I'm not sure if threads would speed anything up. Clearly you don't want to lock your program waiting for dbus, but thats what the event loop and DBus's async api is for.

@Leo my guess is dbus marshaling or similar... I'm not sure if dbus is the best way to communicate this info. Imagine using DBus to communicate keypresses.

Sunil Thaha said...

This would be helpful too, I believe

The DeviceOrientationNotifier -