forked from isohl/FlightPrediction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SayAltitude.py
146 lines (133 loc) · 5.1 KB
/
SayAltitude.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import decodeTNC
import pyttsx
import fap
import os
from math import *
import time
earth_radius = 3960.0
degrees_to_radians = pi/180.0
radians_to_degrees = 180.0/pi
def milechangelatitude(change):
return radians(change)*earth_radius
def milechangelongitude(latitude, change):
r = earth_radius*cos(radians(latitude))
return radians(change)*r
def getNewPacket(savedtime):
global filepath
for i in range(100):
time = os.stat(filepath)
if time == savedtime:
continue
savedtime = time
f = open(filepath)
#print "Retrieving data"
allLines = f.readlines()
while allLines == []:
allLines = f.readlines()
lastline = allLines[len(allLines)-1]
f.close()
return lastline, savedtime
return None,savedtime
def checkCompatibility(packet):
#acceptedSigns = ["KE7ROS","WB1SAR","KF7WII","KF7WIG","KF7WIJ"]
acceptedSigns = ["KF7WIG-11", "WB1SAR-11"]
#src = packet.src_callsign.strip("-11")
src = packet.src_callsign
#src = src.strip("-7")
if src in acceptedSigns:
return True
else:
return False
def sayAltitude(packet,ascentrate=0,bearing = (0,0), calledlast = False):
alt = packet.altitude
src = packet.src_callsign
if alt is not None:
alt = int(floor(alt/1000*3.28084)*1000)
text = "Balloon currently at %s feet" % (int(alt))
print text
if ascentrate is not None and int(ascentrate) is not 0:
text2 = "Ascent Rate is %s Feet per minute" % ascentrate
print text2
engine.say(text2)
if not bearing == (0,0) and not calledlast:
text3 = "Bearing is %s degrees azimuth and %s degrees altitude" % bearing
print text3
engine.say(text3)
engine.say(text)
engine.runAndWait()
print "Said"
def sayBearing():
pass
if __name__=="__main__":
engine = pyttsx.init()
#engine.say('Altitude speech engine starting')
#engine.runAndWait()
fap.init()
if os.name=="nt":
filepath = os.path.expanduser('~')+"\\tnclogs\\tnc.log"
elif os.name=="posix":
filepath = os.path.expanduser("~/Documents/tnc.log")
else:
print "UNKNOWN OPERATING SYSTEM. GET A LIFE."
savedtime=os.stat(filepath).st_mtime
ascentrate = 0
ascentaverage = None
lasttime = time.time()
lastpacket = None
lastpackettime = None
bearing = (0,0)
calledlast = True
try:
while 1:
newpacket,savedtime = getNewPacket(savedtime)
if not newpacket==None:
try:
p = fap.Packet(newpacket)
compatible = checkCompatibility(p)
#compatible = True
if compatible:
if lastpacket is not None:
lastalt = lastpacket.altitude
if not lastalt==p.altitude:
currentpackettime = time.time()
ascentrate = floor(((p.altitude-lastalt)*3.28084)/((currentpackettime-lastpackettime)/60)/100)*100
if ascentaverage is None:
ascentaverage = ascentrate
else:
ascentaverage = int(floor((ascentaverage+ascentrate)/2/100)*100)
lastpacket = p
lastpackettime = time.time()
except fap.DecodeError:
#newpacket = str("TEST1>TEST:")+newpacket
#try:
#p = fap.Packet(newpacket)
#except fap.DecodeError:
#continue
if lastpacket is not None and newpacket.startswith("$PKWDPOS"):
latitude, longitude = decodeTNC.latlong(newpacket)
longitudediff = float(longitude) - lastpacket.longitude
latitudediff = float(latitude) - lastpacket.latitude
groundalt = 5800
packagealt = lastpacket.altitude
xdiff = milechangelongitude(float(latitude),longitudediff)
ydiff = milechangelatitude(latitudediff)
angle = 360 + int(degrees(atan2(ydiff,xdiff)))
distance = hypot(xdiff,ydiff)
fromhorizon = int(degrees(atan(packagealt/distance)))
bearing = (angle,fromhorizon)
continue
curtime = time.time()
if curtime-lasttime>30:
if not lastpacket == None:
sayAltitude(lastpacket,ascentaverage,bearing,calledlast)
if calledlast:
calledlast = False
pass
else:
calledlast = True
lasttime = curtime
except KeyboardInterrupt:
print "KeyBoardInterrupt"
pass
finally:
fap.cleanup()