maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   Is this suffice way to detect movement using Python? (https://talk.maemo.org/showthread.php?t=45949)

ste.richards 2010-02-26 15:01

Is this suffice way to detect movement using Python?
 
This is my first day in both Python and N900 development and I have wrote this piece of code to detect movement on the N900. It doesn't work brilliantly but am I heading in the right direction? I'm not even sure if I have the right axis, but that isn't what interests me at the moment.

Code:

def get_rotation():
        f = open("/sys/class/i2c-adapter/i2c-3/3-001d/coord", 'r' )
        coords = [int(w) for w in f.readline().split()]
        f.close()
        return coords

tempA = 0
tempB = 0
action = 0

while (True):
        a = get_rotation()

        # Left and right
        if (a[1] + 30) > tempA and action != 1:
                print 'Moving left'
                action = 1

        if (a[1] - 30) < tempA and action != 2:
                print 'Moving right'
                action = 2

        # Forwards and backwards
        if (a[2] + 30) > tempB and action != 3:
                print 'Moving forward'
                action = 3

        if (a[2] - 30) < tempB and action != 4:
                print 'Moving backwards'
                action = 4

        tempA = a[1]
        tempB = a[2]


dov 2010-02-26 15:06

Re: Is this suffice way to detect movement using Python?
 
Yes, but you probably don't want to read the data in a tight loop, but set up a timer event so that you only read the data e.g. once every 1/10th second. If you are going to write a gui look for timer events for gtk (g_timeout_add() ) or Qt.

Flandry 2010-02-26 15:09

Re: Is this suffice way to detect movement using Python?
 
Presumably you tried it and it works somewhat, just not "brilliantly". What's your question?

It looks ok to me but you may want to put in a delay (sleep) step so that (1) you don't peg the CPU at 100% and (2) it doesn't read more frequently than the accelerometer can update.

Edit: what he said :)

pelago 2010-02-26 15:15

Re: Is this suffice way to detect movement using Python?
 
Is there an API for reading the accelerometer? Reading /sys/class/ seems a bit low level.

shadow12 2010-02-26 15:21

Re: Is this suffice way to detect movement using Python?
 
Once you get a bit more proficient you should take a look at threads since they are ideally suited for these type of situations, but they can be tricky to get right. Qt provides a threading framework.

Flandry 2010-02-26 15:28

Re: Is this suffice way to detect movement using Python?
 
Quote:

Originally Posted by pelago (Post 547660)
Is there an API for reading the accelerometer? Reading /sys/class/ seems a bit low level.

I think i saw mention that Qt provides a higher level function, but you can't really go all that much higher and maintain the level of control that you need to exert for using this resource. For example, how frequently to poll. It gives you instantaneous x, y, and z acceleration...that seems to be about the highest level one can expect from an accelerometer. :D

There is an example usage code for smoothing in the wiki page for it.

rmerren 2010-03-10 19:16

Re: Is this suffice way to detect movement using Python?
 
Ideally, what I would like to use (and, I guess, what I will start writing--but don't hold your breath because I have a day job) is a QT widget that exposes signals such as shakeLeft(int violence) and shakeRight(int violence), with the violence being, perhaps, a value from 1 to 10 (or 1-16 for folks with a binary hangup; or whatever granularity might be reasonable given the data stream; or perhaps just with an enumeration of low, medium, high; or perhaps with all of the above). We'd have all 6 cardinal directions available.

I would also create a mock object for use in desktop testing/debugging that provides the user buttons to emulate the various shakes in a desktop environment.

The use I am thinking of is: user clicks on a field with numbers in it and shakes left or right to decrement or increment the numbers. The more violent the shake, the greater the number changes. Having a QT-based library available would make it insanely simple to roll out these kinds of apps.

Anyone know of anything existing like this before I reinvent the wheel?

Flandry 2010-03-11 14:39

Re: Is this suffice way to detect movement using Python?
 
Quote:

Originally Posted by rmerren (Post 562862)
Anyone know of anything existing like this before I reinvent the wheel?

You'll probably have to poll the accelerometer often enough to cause a noticeable CPU load to get the information for a function like that. That's the only downside i see with it.

I thought i saw something in Qt about accelerometer support, you might look at the latest classes. Other than that, this is functionality i plan to add to accelemymote. It currently just sends angles (as joystick events), but the next step is to take the existing framework and add a layer to detect more complex gestures (such as a shake) to map to button presses. I'll probably borrow your numbers if you get around to implementing the shake detection for the Qt class before i add it the the joystick driver: it's the optimization of mapping the accelerometer readings to gestures that's the most difficult part.

Feel free to check out the ugly source for it if you want to start with something that works.


All times are GMT. The time now is 21:02.

vBulletin® Version 3.8.8