add beginnings of an nmea parser

This commit is contained in:
Daniel Barlow 2025-05-14 18:30:14 +01:00
parent 71012d5038
commit 3aa64e8fac
3 changed files with 6692 additions and 0 deletions

24
pkgs/maps/assert.fnl Normal file
View File

@ -0,0 +1,24 @@
;; these are macros; this module should be imported
;; using import-macros
;; e.g. (import-macros { : expect= } :anoia.assert)
(fn expect [assertion]
(let [msg (.. "expectation failed: " (view assertion))]
`(when (not ,assertion)
(assert false ,msg))))
(fn expect= [actual expected]
`(let [view# (. (require :fennel) :view)
ve# (view# ,expected)
va# (view# ,actual)]
(when (not (= ve# va#))
(assert false
(.. "\nexpected " ve# "\ngot " va#)
))))
(fn define-tests [& body]
(when _G.RUNNING_TESTS
`(do ,(unpack body))))
{ : define-tests : expect : expect= }

6549
pkgs/maps/foo.nmea Normal file

File diff suppressed because it is too large Load Diff

119
pkgs/maps/nmea.fnl Normal file
View File

@ -0,0 +1,119 @@
;; each sentence is introduced by $ and terminated by *
;; first two chars are "talker id"
;; 3-5 are the message type
;; fields are comma-delimited
;; we assume that we can parse the message based only on type
;; (i.e. that the same type should always be parsed the same way no
;; matter who the talker)
;; outline: split message into lines, extract values between
;; $ and * as talker, type, fields
;; then call the parser for the type
(import-macros { : define-tests : expect : expect= } :assert)
(fn split-line [line]
(let [(talker msgtype rest) (string.match line "$(..)(...)(.+)*")
fields (icollect [k _ (string.gmatch rest ",([^,]*)")] k)]
{ : talker :message-type msgtype :fields fields }))
(expect=
(split-line "$GLGSV,2,1,07,78,47,295,34,77,35,208,19,88,36,267,30,87,67,025,27,1*74\n")
{
:talker "GL"
:message-type "GSV"
:fields ["2" "1" "07" "78" "47" "295" "34" "77" "35" "208" "19" "88"
"36" "267" "30" "87" "67" "025" "27" "1"
]
}
)
(expect=
(split-line "$GNGNS,111134.00,5131.348976,N,00005.551003,W,AAAANN,19,0.7,20.3,47.0,,,V*0C\n")
{
:talker "GN"
:message-type "GNS"
:fields ["111134.00" ; utc
"5131.348976" "N"
"00005.551003" "W"
"AAAANN"
"19" ; number of satellites
"0.7" ; hdop
"20.3" ; height
"47.0" ; geoidal separation
"" "" ; differential data age, reference station
"V" ; not valid for navigation (?)
]
}
)
(local latlon-signs { :N 1 :S -1 :W -1 :E 1 } )
(local
msg-types
{
:GNS
(fn [fields]
(let [[utc lat latsign lon lonsign mode total-space-vehicles
hdop altitude geoidal-separation _ _ nav-status] fields]
{ :lat (* (/ (tonumber lat) 100) (. latlon-signs latsign))
:lon (* (/ (tonumber lon) 100) (. latlon-signs lonsign))
: mode
:total-space-vehicles (tonumber total-space-vehicles)
:hdop (tonumber hdop)
:altitude (tonumber altitude)
:geoidal-separation (tonumber geoidal-separation)
: nav-status
}
))
:VTG
(fn [[bearing-true _ bearing-mag _ knots _ kmh _ mode]]
{
:bearing-true (tonumber bearing-true)
:bearing-magnetic (tonumber bearing-mag)
:speed-knots (tonumber knots)
:speed-kmh (tonumber kmh)
: mode
}
)
})
(fn parse-line [line]
(let [{ : message-type : talker : fields &as split} (split-line line)
parser (. msg-types message-type)]
(if parser
(doto (parser fields)
(tset :message-type message-type)
(tset :talker talker))
split)))
(expect=
(parse-line
"$GNGNS,111134.00,5131.348976,N,00005.551003,W,AAAANN,19,0.7,20.3,47.0,,,V*0C\n")
{:altitude 20.3
:geoidal-separation 47
:hdop 0.7
:lat 51.31348976
:lon -0.05551003
:message-type "GNS"
:mode "AAAANN"
:nav-status "V"
:talker "GN"
:total-space-vehicles 19})
(expect=
(parse-line
"$GNVTG,263.3,T,266.5,M,1.6,N,2.9,K,A*32\n")
{
:talker :GN
:message-type :VTG
:bearing-true 263.3
:bearing-magnetic 266.5
:speed-knots 1.6
:speed-kmh 2.9
:mode :A
})