Trouble using gyroscope data

225 views
Skip to first unread message

Cam J

unread,
Oct 19, 2015, 8:00:34 PM10/19/15
to Kivy users support
Hi,

I'm trying to create an app that uses the gyroscope data. I want a single line that remains in line with the horizon regardless of the phone orientation (see attachment). I'm being able to read the sensor data, which provides an (x,y,z) vector. But I don't know how to translate this information into anything meaningful or how to use the raw info to make the line stay aligned with the horizon. I'm new to kivy and python, and I would really appreciate any help and/or pointers.

Thanks in advance!

Here is my code:

import kivy
import plyer
from kivy.app import App #for the main app
from kivy.uix.floatlayout import FloatLayout #the UI layout
from kivy.uix.label import Label #a label to show information
from plyer import gyroscope #object to read the accelerometer
from kivy.clock import Clock #clock to schedule a method

class UI(FloatLayout):#the app ui
    def __init__(self, **kwargs):
        super(UI, self).__init__(**kwargs)
        self.lblAcce = Label(text="Gyroscope: ") #create a label at the center
        self.add_widget(self.lblAcce) #add the label at the screen
        try:
            gyroscope.enable() #enable the accelerometer
            #if you want do disable it, just run: accelerometer.disable()
        except:
            self.lblAcce.text = "Failed to start gyroscope" #error
       
        Clock.schedule_interval(self.update, 1.0/24) #24 calls per second
       
    def update(self, dt):
        txt = ""
        try:
            txt = "Gyroscope:\nX = %.2f\nY = %.2f\nZ = %2.f " %(
                gyroscope.orientation[0], #read the X value
                gyroscope.orientation[1], # Y
                gyroscope.orientation[2]) # Z
        except:
            txt = "Cannot read gyroscope!" #error
        self.lblAcce.text = txt #add the correct text

class Gyroscope(App): #our app
    def build(self):
        ui = UI()# create the UI
        return ui #show it

if __name__ == "__main__":
    Gyroscope().run()
App.pdf

Andrei Sima

unread,
Oct 20, 2015, 2:53:06 AM10/20/15
to kivy-...@googlegroups.com
Hi,

I do not know what values does giro returns (my bet is on degrees), but from those values you need to extract or compute from them the slope angle for the desired axis . After that you need to update the widget canvas that contains the line in order to display the correct line angle in relation to horizon. 

Also you might need to decrease callback time to 1/60 of a second instead of 1/24 of a second since kivy is capable of 60 fps. On 24 fps you might experience a "video lag" or a slight "frame lost". If you want this effect than is fine. 

I will try the code later and post here what i have managed to do.

Since i had in mind to explore giro any way this might be a good oportunity. 

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
_____________ - [x]- ______________

Andrei Sima
andre...@gmail.com
+40 723 223 883

____________ - [end x] -____________

Cam J

unread,
Oct 20, 2015, 11:26:07 AM10/20/15
to Kivy users support


Hi Andrei,


thanks for your reply. The output generated by the sensor is in radians per second, at least that's what I read on the Android developers website: http://developer.android.com/guide/topics/sensors/sensors_motion.html
Yes, I'm aware I need to do some kind of transformation to compute the slope angle. I'm just not sure how to do that :/
Thanks for the suggestion, I will make the change from 1/24 to 1/60.
Some of the examples on the I've found online and on different books only go as far as abstracting the raw data from the sensor, but that's pretty much it. 

Once again thanks for your help.  

Andrei Sima

unread,
Oct 20, 2015, 12:41:19 PM10/20/15
to kivy-...@googlegroups.com
Hi,

As i see this you need to first calibrate the giro.
Like put in on some flat surface and than on a click or after 3-4 sec of 0 - output from giro to pass this as a zero value to a variable (x=0, y=0, z=0)

After that keep adding or subtracting the giro output to that variable. x = x + SensorEvent.values[0] -> here you might have a problem since i do not know if this value can go negative... -0.5 rad/sec ...

The best way to experiment is to create 3 labels and feed them the SensorEvent.values[0], SensorEvent.values[1] and SensorEvent.values[3] and keep playing with those until you figure them out.
Then create another 3 labels and feed the x, y, z in there to see if he updates properly.
Here you might need to use multithreading...

Than from RAD to DEG there is a formula  deg = rad*180/PI, if i remember correctly you might want to search that just to be sure. 
degX = x*180/PI
degY, degZ
Create a label for each and if all ok than you can think about the next step.


Now to animate the line as i see you have 2 options 1 to animate the widget that contains the line, or to constantly update the widget canvas.

This is the way i would try to achieve something like that.

I did not have time today, and i sure that tomorrow will be the same, to investigate this, but you just made me curious about this.

Hope it helps.


____________________
Andrei Sima
0723.223.883

--

Cam J

unread,
Oct 20, 2015, 4:07:33 PM10/20/15
to Kivy users support
Thanks for the pointers Andrei,
   Yes, that's the correct formula for converting Rads to Degrees. 
I'll give it a try and see. I'm still not sure how to do it, but I'll try it.
If you think of something else, please let me know.

Thanks! 
Reply all
Reply to author
Forward
0 new messages