Created
November 2, 2019 22:39
-
-
Save theely/8cc725cdb9b33811852f6d5acbceb917 to your computer and use it in GitHub Desktop.
Rolling GPS latlong logging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Rolling GPS latlong logging | |
-- Stores the latest 100 distinct latlong coordinates (redundant coordinates are ignored). | |
-- If the number of sats is smaller than 4 coordinates are not recorded. | |
-- requires GPS telemetry | |
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 getGPSLatLon() | |
gpsLatLon = getValue("GPS") | |
if (type(gpsLatLon) == "table") then | |
return rnd(gpsLatLon["lat"],5) .. ", " .. rnd(gpsLatLon["lon"],5) | |
else | |
return "not currently available" | |
end | |
end | |
local function init_func() | |
end | |
local function log_position(latlog,alt,sats) | |
local logFile = "/LOGS/GPS_rolling_positions.log" | |
local logRead = io.open(logFile,"r") | |
local previous_positions = "" | |
if logRead ~= nil then | |
-- every log entry is exactly 92chars, read 99 line, equal to 9108 chars | |
previous_positions = io.read(logRead,9108) | |
previous_position = "" | |
for match in string.gmatch(previous_positions, 'LatLong: +"([0-9 ,.-]+)"') do | |
previous_position = match | |
break | |
end | |
io.close(logRead) | |
end | |
if latlog == previous_position then | |
return | |
end | |
local logWrite = io.open(logFile,'w') | |
if logWrite ~= nil then | |
io.write(logWrite,string.format("%-13s%-22s","LatLong:",'"'..latlog..'"')) | |
io.write(logWrite,string.format("%13s%07d","Altitude:",alt)) | |
io.write(logWrite,string.format("%13s%02d","Satelites:",sats)) | |
local now = getDateTime() | |
io.write(logWrite,string.format("%21s", now.year.."-"..now.mon.."-"..now.day.." "..now.hour..":"..now.min..":"..now.sec)) | |
io.write(logWrite,"\n") | |
io.write(logWrite,previous_positions) | |
io.close(logWrite) | |
end | |
end | |
local function run_func() | |
latlog = getGPSLatLon() | |
alt = getValue("Alt") | |
sats = getValue("Sats") | |
if sats > 3 then | |
log_position(latlog,alt,sats) | |
end | |
end | |
return { run=run_func, init=init_func } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment