1 -- Swamp Bike Opera embedded system for Kaffe Matthews
2 -- Copyright (C) 2012 Wolfgang Hauptfleisch, Dave Griffiths
4 -- This program is free software: you can redistribute it and/or modify
5 -- it under the terms of the GNU General Public License as published by
6 -- the Free Software Foundation, either version 3 of the License, or
7 -- (at your option) any later version.
9 -- This program is distributed in the hope that it will be useful,
10 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
11 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 -- GNU General Public License for more details.
14 -- You should have received a copy of the GNU General Public License
15 -- along with this program. If not, see <http://www.gnu.org/licenses/>.
17 module("gps", package.seeall)
24 local testing_pos={lat=41.147387819527,lng=-8.6317899816589}
25 --{lat=41.140157986104,lng=-8.6173141712656}
26 --{lat=51.04790318580878,lng=3.7287747859954834}
27 --{lat=51.04803133876788,lng=3.730437755584717}
28 -- {lat=50.1729952601643,lng=-5.106239318847656}
29 -- {lat=50.16647031001818,lng=-5.097473859786987}
34 -- convert wgs data to location
35 function wgs_to_dec(lat, sign1, lon, sign2)
37 local d,m,r = string.match(lat, "(%d%d)(%d%d).(%d%d%d%d)")
38 local s = ( 60 / 10000 ) * r
39 local lat_deg = d + (m/60) + (s/3600)
41 local d,m,r = string.match(lon, "(%d%d)(%d%d).(%d%d%d%d)")
42 local s = ( 60 / 10000 ) * r
43 local lon_deg = d + (m/60) + (s/3600)
46 lon_deg = lon_deg * - 1
49 local file = io.open("/tmp/swamp_gps", "w")
51 file:write(std.round(testing_pos.lat, 10).." "..
52 std.round(testing_pos.lng, 10))
54 testing_pos.lat=testing_pos.lat+0.0001*math.cos(test_time)
55 --(math.random()-0.5)*0.0001
56 testing_pos.lng=testing_pos.lng+0.0001*math.sin(test_time)
57 --(math.random()-0.5)*0.0001
59 test_time=test_time+0.1
61 file:write(std.round(lat_deg, 10).." "..std.round(lon_deg, 10))
68 --@param device number
70 for line in io.open("/dev/ttyACM"..device):lines() do
71 local no, lat, sign1, lon, sign2 =
72 string.match(line, "GPGGA,(.-),(.-),(%w),(.-),(%w)")
74 wgs_to_dec(lat, sign1, lon, sign2)
81 --@return device number or false
82 function detect_device()
84 if io.open("/dev/ttyACM"..i) then
94 posix.mkfifo("/tmp/swamp_gps")
96 local dev = detect_device()
98 print("found gps device "..dev)
101 print("no gps device found")
102 os.execute("sleep 5")