forked from topher200/gps_recorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bearing_recorder.py
110 lines (102 loc) · 3.66 KB
/
bearing_recorder.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/python2.6
from __future__ import with_statement
from util import get_last_fileid
import android
import json
import math
VERTICAL_THRESHOLD = 9.2
OUTPUT_FILENAME_TEMPLATE = 'bearing_output_{0}.json'
def record():
# Start location messages
droid = android.Android()
droid.startSensingTimed(1, 1000)
droid.makeToast("Starting Bearing drill")
# Show a dialog box
droid.dialogCreateInput("Bearing Recorder running", "Add message to log")
droid.dialogSetPositiveButtonText("Log message")
droid.dialogSetNegativeButtonText("Exit")
droid.dialogShow()
out_filename = OUTPUT_FILENAME_TEMPLATE.format(get_last_fileid() + 1)
with open(out_filename, 'w') as out_file:
running = True
while running:
# Wait until we get an event
res = droid.eventWait(1000).result
if res == None:
print "SensorListener timeout"
elif res['name'] == "dialog":
# User used the dialog
if (res[u'data'][u'which'] == u'positive'):
droid.makeToast("Saving log message")
message = '# {0}'.format(res[u'data'][u'value'])
print message
out_file.write('{0}\n'.format(message))
droid.dialogShow()
else:
print "User requested exit"
running = False
elif res['name'] == 'sensors':
data = {}
data['time'] = res['data']['time']
xforce = float(res['data']['xforce'])
yforce = float(res['data']['yforce'])
zforce = float(res['data']['zforce'])
xmag = float(res['data']['xMag'])
ymag = float(res['data']['yMag'])
zmag = float(res['data']['zMag'])
if abs(xforce) > abs(yforce) and abs(xforce) > abs(zforce):
if xforce > VERTICAL_THRESHOLD:
angle = math.atan2(-(ymag),zmag)
if angle < 0:
angle = angle + (2 * math.pi)
bearing = int(math.degrees(angle))
data['bearing'] = bearing
elif xforce < -(VERTICAL_THRESHOLD):
angle = math.atan2(ymag,zmag)
if angle < 0:
angle = angle + (2 * math.pi)
bearing = int(math.degrees(angle))
data['bearing'] = bearing
elif xforce > 0:
data['bearing'] = None
else:
data['bearing'] = None
elif abs(yforce) > abs(xforce) and abs(yforce) > abs(zforce):
if yforce > VERTICAL_THRESHOLD:
angle = math.atan2(-(zmag),xmag)
if angle < 0:
angle = angle + (2 * math.pi)
bearing = int(math.degrees(angle))
data['bearing'] = bearing
elif yforce < -(VERTICAL_THRESHOLD):
angle = math.atan2(zmag,xmag)
if angle < 0:
angle = angle + (2 * math.pi)
data['bearing'] = int(math.degrees(angle))
elif yforce > 0:
data['bearing'] = None
else:
data['bearing'] = None
elif abs(zforce) > abs(yforce) and abs(zforce) > abs(xforce):
if zforce > VERTICAL_THRESHOLD:
angle = math.atan2(ymag,xmag)
if angle < 0:
angle = angle + (2 * math.pi)
data['bearing'] = int(math.degrees(angle))
elif zforce < -(VERTICAL_THRESHOLD):
angle = math.atan2(-(ymag),xmag)
if angle < 0:
angle = angle + (2 * math.pi)
data['bearing'] = int(math.degrees(angle))
elif zforce > 0:
data['bearing'] = None
else:
data['bearing'] = None
else:
data['bearing'] = None
print(data)
out_file.write(json.dumps(data) + '\n')
# Shutdown
droid.stopSensing()
if __name__ == '__main__':
record()