needle from magnetometer

This commit is contained in:
rgarcia-herrera 2020-07-16 19:17:49 -05:00
parent 328d8e3ebb
commit 586f1ebcfd
5 changed files with 104 additions and 23 deletions

View File

@ -27,6 +27,7 @@
on_map_relocated: centermark.lat = mapview.lat; centermark.lon = mapview.lon
MapMarker:
source: 'marker.png'
id: centermark
anchor_x: 0.2
anchor_y: 0.3
@ -74,6 +75,48 @@
Label:
text: str(root.z_calib) + ')'
Label:
text: app.gps_location
Label:
text: app.gps_status
BoxLayout:
size_hint_y: None
height: '48dp'
padding: '4dp'
ToggleButton:
text: 'Start' if self.state == 'normal' else 'Stop'
on_state:
app.start(1000, 0) if self.state == 'down' else \
app.stop()
FloatLayout:
canvas:
Color:
rgb: .98, .98, .98
Rectangle:
size: self.size
Image:
source: 'rose.png'
Image:
source: 'needle.png'
canvas.before:
PushMatrix
Rotate:
angle: root.needle_angle
axis: 0, 0, 1
origin: self.center
canvas.after:
PopMatrix
# BoxLayout:
# orientation: 'vertical'
# canvas:
@ -108,4 +151,3 @@
# Label:
# text: app.gps_status

45
main.py
View File

@ -7,7 +7,9 @@ from kivy_garden.mapview import MapView
from plyer import gps
from kivy.clock import mainthread
from kivy.utils import platform
from kivy.vector import Vector
from kivy.animation import Animation
from math import floor
class MapScreen(Screen):
@ -20,17 +22,39 @@ class CompassScreen(Screen):
x_calib = NumericProperty(0)
y_calib = NumericProperty(0)
z_calib = NumericProperty(0)
needle_angle = NumericProperty(0)
def enable(self):
self.facade.enable()
Clock.schedule_interval(self.get_field, 1 / 20.)
gps.start(1000, 0)
def disable(self):
self.facade.disable()
Clock.unschedule(self.get_field)
def get_field(self, dt):
needle_angle = 7
if self.facade.field != (None, None, None):
self.x_calib, self.y_calib, self.z_calib = self.facade.field
x, y, z = self.facade.field
needle_angle = Vector(x, y).angle((0, 1)) + 90.
# fix animation transition around the unit circle
if (self.needle_angle % 360) - needle_angle > 180:
needle_angle += 360
elif (self.needle_angle % 360) - needle_angle < -180:
needle_angle -= 360
# add the number of revolutions to the result
needle_angle += 360 * floor(self.needle_angle / 360.)
# animate the needle
if self._anim:
self._anim.stop(self)
self._anim = Animation(needle_angle=needle_angle, d=.2, t='out_quad')
self._anim.start(self)
@ -52,6 +76,20 @@ class FlockompassApp(App):
Permission.ACCESS_FINE_LOCATION],
callback)
def start(self, minTime, minDistance):
gps.start(minTime, minDistance)
def stop(self):
gps.stop()
def on_pause(self):
gps.stop()
return True
def on_resume(self):
gps.start(1000, 0)
@mainthread
def on_location(self, **kwargs):
self.gps_location = '\n'.join([
@ -78,7 +116,9 @@ class FlockompassApp(App):
root = ScreenManager(transition=RiseInTransition())
root.add_widget(MapScreen(name='map'))
root.add_widget(CompassScreen(name='compass'))
cs = CompassScreen(name='compass')
cs._anim = None
root.add_widget(cs)
return root
@ -96,4 +136,3 @@ if __name__ == '__main__':
# Button:
# text: 'Use RiseInTransition'
# on_release: root.manager.transition = RiseInTransition()

BIN
marker.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

BIN
needle.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
rose.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB