-
Notifications
You must be signed in to change notification settings - Fork 0
/
ground_station_parser_simulation.py
177 lines (124 loc) · 5.42 KB
/
ground_station_parser_simulation.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import gpxpy
import gpxpy.gpx
inputFile = "meldingFraNyttelast.txt";
def extractMessages(values):
messages=[]; # one message is the same as 13 uint8_t's
words = []; #one word is the same as one uint8_t. (one element of the values variable)
remainingWords = 0;
# This will extract the messages
for i in range(0, len(values)):
if remainingWords > 0:
#We are currently reading a message
words.append(values[i]);
remainingWords-=1;
if(remainingWords == 0):
#We have read the entire message
messages.append(words);
words = [];
else:
#we are looking for the start of a new message
if values[i] == 255:
remainingWords = 13;
return messages;
def validateValue(a, b):
if(a >= 0 and a <= 9):
return a;
elif (a == 13):
return -3; # a minus sign
elif(a == 14):
#print ("Error indicator found.");
return None;
else:
#print(str(b) + " Unexpected error. Value is not 0-9 or 14 or 13.");
return None;
def ConvertMessagesBackToChars(messages):
messagesAsChars=[]
errorIndices = []
messagesWithErrors = []
# Here we are expanding one word(=uint8_t) into 2 chars.
for i in range(0, len(messages)):
tmpMessage = [];
for j in range(0, len(messages[i])):
word = messages[i][j];
tmp1 = validateValue((word >> 4), i);
tmp2 = validateValue((word & int('00001111',2)), i)
if tmp1 == None or tmp2 == None:
# Something unexpected has happen.
errorIndices.append(i);
messagesWithErrors.append(messages[i]);
tmpMessage = []
#print(str(i) + "tmp1 or tmp2 is None")
break; # No need to keep iterating through the rest of the message.
else:
char1 = chr(tmp1 + 48);
char2 = chr(tmp2 + 48);
tmpMessage.append(char1);
tmpMessage.append(char2);
if (len(tmpMessage) != 0):
messagesAsChars.append(tmpMessage);
return messagesAsChars, errorIndices, messagesWithErrors;
def interpretMessages(messagesAsChars):
# latitude, longitude, quality ind., numofsat, altitude
#field_sizes: 8 9 1 2 6
interpretedMessages = [];
tmpmsg = [];
# Reconstruct the information.
for i in range(0, len(messagesAsChars)):
msg = messagesAsChars[i];
#print(str(i) + " of " + str(len(messagesAsChars)))
latitude = float(msg[0]+msg[1]) + float(msg[2]+msg[3]+'.'+msg[4]+msg[5]+msg[6]+msg[7] )/60;
longitude = float(msg[8] + msg[9] + msg[10]) + float(msg[11] + msg[12] + '.' + msg[13]+msg[14]+msg[15]+msg[16])/60;
qualityIndicator = int(msg[17]);
numberOfSats = int(msg[18] + msg[19]);
altitude = float(msg[20]+msg[21]+msg[22]+msg[23]+'.'+msg[24]+msg[25])
tmpmsg.append(latitude);
tmpmsg.append(longitude);
tmpmsg.append(qualityIndicator);
tmpmsg.append(numberOfSats);
tmpmsg.append(altitude);
interpretedMessages.append(tmpmsg)
tmpmsg = [];
return interpretedMessages;
def deleteMessagesWithBadQualityIndicator(interpretedMessages):
indicesToBeDeleted = [];
# Find all messages that have an error at the quality indicator
# TODO: what is an unwanted quality indicator value??
for i in range(0, len(interpretedMessages)):
if (interpretedMessages[i][2] == 88):
indicesToBeDeleted.append(i);
# Delete the found messages.
for i in range(0, len(indicesToBeDeleted)):
del interpretedMessages[indicesToBeDeleted[i]];
return interpretedMessages;
def createGPX(interpretedMessages, filename):
gpx = gpxpy.gpx.GPX()
# Create first track in our GPX:
gpx_track = gpxpy.gpx.GPXTrack()
gpx.tracks.append(gpx_track)
# Create first segment in our GPX track:
gpx_segment = gpxpy.gpx.GPXTrackSegment()
gpx_track.segments.append(gpx_segment)
#lat lon elevation
for i in range(0, len(interpretedMessages)):
gpx_segment.points.append(gpxpy.gpx.GPXTrackPoint(interpretedMessages[i][0], interpretedMessages[i][1], elevation=interpretedMessages[i][4]))
with open(filename, "w") as f:
f.write(gpx.to_xml())
def main():
# Read the raw data.
rawData = "";
with open(inputFile, "r") as f:
rawData = f.readlines();
# Convert each element into an integer and also strip the \n at the end
values = [];
for i in range(0,len(rawData)):
values.append(int((rawData[i].rstrip("\n"))));
messagesAsUint8 = extractMessages(values);
messagesAsChars, errorIndices, messagesWithErrors = ConvertMessagesBackToChars(messagesAsUint8);
interpretedMessages = interpretMessages(messagesAsChars);
interpretedMessages = deleteMessagesWithBadQualityIndicator(interpretedMessages);
for elem in interpretedMessages:
print (elem)
#TODO: Handle the errorIndices and messagesWithErrors. Not sure what you want to do with them.
#TODO: Check if the values make sence (i.e. not travelled insanely far). Remember that we might lose and regain "fix"...
createGPX(interpretedMessages,"locations.gpx")
main();