Active Topics

 


Reply
Thread Tools
Posts: 47 | Thanked: 1 time | Joined on Feb 2010
#1
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]
 
Posts: 81 | Thanked: 45 times | Joined on Dec 2009
#2
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's Avatar
Posts: 1,559 | Thanked: 1,786 times | Joined on Oct 2009 @ Boston
#3
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
__________________

Unofficial PR1.3/Meego 1.1 FAQ

***
Classic example of arbitrary Nokia decision making. Couldn't just fallback to the no brainer of tagging with lat/lon if network isn't accessible, could you Nokia?
MAME: an arcade in your pocket
Accelemymote: make your accelerometer more joy-ful
 
pelago's Avatar
Posts: 2,121 | Thanked: 1,540 times | Joined on Mar 2008 @ Oxford, UK
#4
Is there an API for reading the accelerometer? Reading /sys/class/ seems a bit low level.
 
Posts: 102 | Thanked: 22 times | Joined on Oct 2009
#5
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's Avatar
Posts: 1,559 | Thanked: 1,786 times | Joined on Oct 2009 @ Boston
#6
Originally Posted by pelago View Post
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.

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

Unofficial PR1.3/Meego 1.1 FAQ

***
Classic example of arbitrary Nokia decision making. Couldn't just fallback to the no brainer of tagging with lat/lon if network isn't accessible, could you Nokia?
MAME: an arcade in your pocket
Accelemymote: make your accelerometer more joy-ful
 
Posts: 282 | Thanked: 337 times | Joined on Dec 2009 @ Austin, TX, USA
#7
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's Avatar
Posts: 1,559 | Thanked: 1,786 times | Joined on Oct 2009 @ Boston
#8
Originally Posted by rmerren View Post
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.
__________________

Unofficial PR1.3/Meego 1.1 FAQ

***
Classic example of arbitrary Nokia decision making. Couldn't just fallback to the no brainer of tagging with lat/lon if network isn't accessible, could you Nokia?
MAME: an arcade in your pocket
Accelemymote: make your accelerometer more joy-ful
 

The Following User Says Thank You to Flandry For This Useful Post:
Reply


 
Forum Jump


All times are GMT. The time now is 05:25.