-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPS.lua
196 lines (151 loc) · 4.64 KB
/
GPS.lua
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
--[[#############################################################################
Telemetry Widget script for Taranis x7
Copyright (C) by mosch
License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
"GPS last known postions v1.1"
Desrciption:
stores GPS coordinates every second into /SCRIPTS/TELEMETRY/GPSpositions.txt and displays the last 4 GPS positions
and GPS sattelite count on the telemetry screen. In case that telemetry is not available anymore (crash, power loss etc.)
the screen won't be updated but still shows the last 4 postions. If 50 postions are stored, the log will be reset and
starts at 0.
if the sattelite count always shows 0 or no value change the following line in the script:
from:
gpssatId = getTelemetryId("Tmp2")
to:
gpssatId = getTelemetryId("Sats")
################################################################################]]
log_filename = "/SCRIPTS/TELEMETRY/GPSpositions.txt"
local gpsValue = 0
local gpsLAT = 0
local gpsLON = 0
local gpsSATS = 0
local wait_time = 100
local wait_time_read = 100
local old_time_read = 0
local old_time_write = 0
local coordinates = {}
local coordinates_tmp = {}
local update = true
local string_gmatch = string.gmatch
local now = 0
local ctr = 0
coordinates[1] = "0"
coordinates[2] = "0"
coordinates[3] = "0"
coordinates[4] = "0"
local function rnd(v,d)
if d then
return math.floor((v*10^d)+0.5)/(10^d)
else
return math.floor(v+0.5)
end
end
local function write_log()
now = getTime()
if old_time_write + wait_time < now then
if string.len(gpsValue) <= 8 then
gpsLAT = "not available"
gpsLON = "not available"
else
local f = io.open(log_filename, "a")
io.write(f, gpsValue ,"\r\n")
io.close(f)
end
old_time_write = now
end
end
local function read_log()
ctr = 0
now = getTime()
if old_time_read + wait_time_read < now then
local f2 = io.open(log_filename, "r")
buffer = io.read(f2, 2048)
io.close(f2)
for line in string_gmatch(buffer, "([^\n]+)\n") do
ctr = ctr + 1
coordinates_tmp[ctr] = line
end
if ctr > 49 then
local f2 = io.open(log_filename, "w")
io.close(f2)
end
if ctr == 1 then
coordinates[1] = "1: " .. coordinates_tmp[ctr]
coordinates[2] = "2: 0"
coordinates[3] = "3: 0"
coordinates[4] = "4: 0"
end
if ctr == 2 then
coordinates[1] = "1: " .. coordinates_tmp[ctr-1]
coordinates[2] = "2: " .. coordinates_tmp[ctr]
coordinates[3] = "3: 0"
coordinates[4] = "4: 0"
end
if ctr == 3 then
coordinates[1] = "1: " .. coordinates_tmp[ctr-2]
coordinates[2] = "2: " .. coordinates_tmp[ctr-1]
coordinates[3] = "3: " .. coordinates_tmp[ctr]
coordinates[4] = "4: 0"
end
if ctr > 3 then
coordinates[1] = ctr-3 .. ": " .. coordinates_tmp[ctr-3]
coordinates[2] = ctr-2 .. ": " .. coordinates_tmp[ctr-2]
coordinates[3] = ctr-1 .. ": " .. coordinates_tmp[ctr-1]
coordinates[4] = ctr .. ": " .. coordinates_tmp[ctr]
end
old_time_read = now
end
end
local function getTelemetryId(name)
field = getFieldInfo(name)
if field then
return field.id
else
return-1
end
end
local function init()
gpsId = getTelemetryId("GPS")
gpssatId = getTelemetryId("Tmp2")
--gpssatId = getTelemetryId("Sats")]]
end
local function background()
gpsLatLon = getValue(gpsId)
gpsSATS = getValue(gpssatId)
if (type(gpsLatLon) == "table") then
gpsValue = rnd(gpsLatLon["lat"],6) .. ", " .. rnd(gpsLatLon["lon"],6)
gpsLAT = rnd(gpsLatLon["lat"],6)
gpsLON = rnd(gpsLatLon["lon"],6)
update = true
else
gpsValue = ""
end
if string.len(gpsValue) > 8 then
write_log()
update = true
else
gpsValue = " no gps data available"
update = false
end
read_log()
if string.len(gpsSATS) > 2 then
gpsSATS = string.sub (gpsSATS, 3,6)
gpsSATS = "Sat:" .. gpsSATS
end
end
local function run(event)
lcd.clear()
background()
lcd.drawText(2,2,"Last GPS positions",SMLSIZE)
lcd.drawLine(0,10, 128, 10, SOLID, FORCE)
lcd.drawText(2,14, coordinates[1],SMLSIZE)
lcd.drawText(2,26, coordinates[2],SMLSIZE)
lcd.drawText(2,38, coordinates[3],SMLSIZE)
lcd.drawText(2,50, coordinates[4],SMLSIZE)
if update == true then
lcd.drawText(100,2, gpsSATS, SMLSIZE + INVERS + BLINK)
elseif update == false then
lcd.drawText(100,2,"Sat:--", SMLSIZE)
end
end
return {init=init, run=run, background=background}