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()