/
gps.py
82 lines (64 loc) · 1.85 KB
/
gps.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import time
import math
from plyer import gps
from kivy.clock import mainthread
use_gps = True
gps_on = False
lat = 00
lon = 00
bearing = 0
speed = 0
accuracy = 1000
#gps_scale = 100000
last_update=0
def hard_reset():
global lat, lon
lat = 0
lon = 0
@mainthread
def update_location(**kwargs):
global lat, lon, bearing, scale, accuracy
print 'lat: {lat}, lon: {lon}, accuracy:{accuracy}'.format(**kwargs)
lat_frac = kwargs['lat']/90.
#from http://stackoverflow.com/a/19356480
m_per_deg_lat = 111132.954 - 559.822 * math.cos( 2 * lat_frac ) + 1.175 * math.cos( 4 * lat_frac )
m_per_deg_lon = 111132.954 * math.cos ( lat_frac )
bearing = kwargs['bearing']
speed = kwargs['speed']
accuracy = kwargs['accuracy']
lat = kwargs['lat'] * m_per_deg_lat
lon = kwargs['lon'] * m_per_deg_lon
@mainthread
def on_status(self, *args, **kwargs):
print 'GPS status: ',args, kwargs
try:
gps.configure(on_location=update_location, on_status=on_status)
except Exception as ex:
template = "An exception of type {0} occured. Arguments: {1!r}"
message = template.format(type(ex).__name__, ex.args)
print message
use_gps = False
def start():
out = 'Dummy'
if use_gps:
out = gps.start(1000, 0)
gps_on = True
return out
def stop():
if use_gps:
gps.stop()
gps_on = False
def get_location():
import random
global lat, lon, last_update, accuracy
tc = 1 if not last_update else (time.time()-last_update)*10
last_update = time.time()
if not use_gps:
lon += 5*tc*(random.random() ) #* gps_scale/100000
lat += 2.5*tc*(random.random() )
accuracy = 1
return [lon, lat]
def get_bearing():
return bearing
if __name__ == "__main__":
print use_gps, get_location(), gps_on