replacing-nmea.adoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. = Towards a Better GPS Protocol
  2. Eric S. Raymond <esr@thyrsus.com.>
  3. 1.7, 27 January 2020
  4. :date: 25 January 2021
  5. :keywords: gps, gpsd, NMEA
  6. :manmanual: GPSD Documentation
  7. :mansource: The GPSD Project
  8. :robots: index,follow
  9. :sectlinks:
  10. :toc: macro
  11. :type: article
  12. :webfonts!:
  13. include::inc-menu.adoc[]
  14. == Abstract
  15. The NMEA 0183 protocol used by GPS units might best be described as
  16. "layer upon layer of cruft". In this paper, we examine the problems
  17. and consider a cleaner design.
  18. == What's Wrong with NMEA 0183, and Why Fix It?
  19. The protocol used by GPS devices to report to computers is a small
  20. subset of NMEA 0183. NMEA stands for "National Marine Electronics
  21. Association", and the features GPSes use for reporting
  22. time/position/velocity information are a small part of a protocol
  23. originally designed for communication between parts of complex marine
  24. navigation systems. Thus the full protocol includes support for depth
  25. sounders, LORAN, and many other things irrelevant to a modern GPS.
  26. The lowest level of NMEA 0183 is quite sensibly designed. The protocol
  27. consists of sentences, each led by a dollar sign and an identifying text
  28. tag, followed by multiple comma-separated textual fields, ended by an
  29. asterisk, a checksum, and LF/CR. This is a simple, clean format with
  30. good extensibility, easy to parse and generate. It is well adapted to
  31. its job, which is to pass small amounts of numeric and status
  32. information. The textual format makes it easy to log NMEA sessions, edit
  33. them, and play them back -- a substantial advantage in developing
  34. talker and parser software.
  35. Unfortunately, the good news ends there. The design of the upper layers
  36. of NMEA 0183 is patchy, kludgy, and replete with the kind of errors that
  37. arise from growth by accretion rather than forethought. Here are some of
  38. the more obvious problems:
  39. * NMEA timestamps usually (e.g in GPBWC, GPBWR, GPGBS, GPGGA, GPGLL,
  40. GPGXA, GPTRF, GPZTG) report time-of-day only. The exceptions (GPRMC,
  41. GPTRF, GPZDA) report only two digits of year and no century. Time
  42. precision is unspecified, usually to the second though some devices
  43. report a fractional decimal part to millisecond precision.
  44. * It is not possible to get a time/position/velocity report in a single
  45. sentence. Some sentences (GPRMC) report time and 2D position and
  46. velocity, some (GPGGA) report time and 3D position, some (GPVTG) report
  47. velocity only. As a result, the API for a protocol client is complicated
  48. by the necessity of maintaining separate age indications for 2D
  49. position, 3D position, and velocity,
  50. * NMEA sentences have at least three kinds of validity indicators --
  51. mode (GPGSA only), status (GPGLL, GPGGA), and the Active/Void field
  52. (GPRMC, GPGLL). And that's before we get into the FAA extensions in late
  53. revisions of the protocol. Interpreting these status bits is a black art
  54. involving knowledge of undocumented and often vendor-specific quirks.
  55. * There is no standard way of indicating that part of a
  56. time/position/velocity report is invalid (e.g. because the device does
  57. not have a fix of the required quality). Many devices simply report 0
  58. for invalid fields, which doesn't sound bad unless you're near the
  59. zero-zero point in the Bay of Benin -- or at sea level. It is also
  60. not generally possible to distinguish between information a GPS is not
  61. yet reporting but will return on a good fix (such as altitude) from
  62. information it will never report (such as, on many units, local magnetic
  63. variation).
  64. * As least one messy bit in NMEA 0183 was an adaptation to machines with
  65. only small amounts of expensive RAM: the fact that satellite status may
  66. show up in a sequence of as many as three sentences to be processed
  67. serially. On modern machines, RAM buffers are cheap. It makes more sense
  68. to ship a single long sentence and decrease code complexity in the
  69. receiver by not requiring a stateful parser.
  70. * Position accuracy estimates are not easy to compute from NMEA reports.
  71. Reporting a measurement without giving its 95% confidence interval is
  72. bad practice.
  73. * For modern GPS devices, even the small piece of NMEA directly
  74. concerned with GPS capabilities is seriously over-complex. Whereas older
  75. GPS devices included elaborate facilities for waypoint tracking and
  76. navigational computation, newer ones are designed under the assumption
  77. that they are connected to a general-purpose computer that is more
  78. powerful and flexible at these things; thus, the GPS only needs to be a
  79. time/position/velocity oracle.
  80. * NMEA 0183 is in general very loosely specified and poorly documented.
  81. Its problems are compounded by the fact that it is a proprietary
  82. specification, jealously guarded by IP lawyers.
  83. As a result of these problems, implementing NMEA 0183 talker software is
  84. far more complex than need be, and the protocol tends to introduce
  85. latencies that vary in an unpredictable way. The latter is a serious
  86. problem for aviation and other high-precision GPS applications, and
  87. probably provided a technical reason that one major GPS vendor (Garmin)
  88. dropped NMEA 0183 support entirely in 2004 in favor of a tighter binary
  89. protocol (we refrain from speculating on other less creditable motives
  90. for this move).
  91. == How To Do Better
  92. The critique above immediately suggests several ways to improve a
  93. protocol for GPS reports;
  94. * Keep the low-level syntax, because it's not broken. It has all the
  95. advantages of textual protocols. Going to a more tightly-packed binary
  96. format might look attractive at first glance, but the gain in
  97. information would be marginal at best. Textual formats already use 7 out
  98. of 8 bits per byte and encode variable-length numeric fields more
  99. efficiently than binary; also they avoid endianness issues.
  100. * Add to the syntax standard ways of indicating that either (a) the GPS
  101. cannot now ship valid data for the field, or (b) the GPS will _never_
  102. ship data for this field.
  103. * Include a full timestamp, to millisecond precision or better, with
  104. every sentence. Every timestamp should be in the same standard form and
  105. should include a full date with century.
  106. * Report the uncertainty corresponding to a 95% confidence interval on a
  107. standard normal distribution for each measurement field.
  108. * Design the protocol around a single core sentence that reports
  109. time/position/velocity and the uncertainties in same.
  110. * Make it an objective of the design for an informal specification to
  111. fit on a single page.
  112. == Informal specification: SGPS
  113. Here, then, is a proposed informal specification for SGPS, Simple GPS
  114. Protocol.
  115. The protocol consists of sentences, each led by a dollar sign and an
  116. identifying sentence tag, followed by multiple comma-separated textual
  117. fields, ended by an asterisk, a CRC32 checksum, and LF/CR. Sentences are
  118. at most 255 characters long, counting the trailing CR/LF.
  119. A field that is empty indicates that the talker does not have valid
  120. information for this field but promises to report it in the future. A
  121. field consisting of a question mark (?) indicates that the talker does
  122. not expect to ever ship valid information for this field.
  123. The first field of every SGPS report sentence is a full timestamp in the
  124. format of the
  125. https://web.archive.org/web/20150919174330/https://www.w3.org/TR/1998/NOTE-datetime-19980827[W3C
  126. profile of ISO 8601], with the timezone required to be Zulu (UTC) time.
  127. === GPTPV
  128. The core sentence of SGPS has the following layout:
  129. [arabic]
  130. . The sentence tag is GPTPV, standing for Time/Position/Velocity.
  131. . The first field is the required timestamp.
  132. . The second field is the uncertainty of the timestamp in (fractional)
  133. seconds.
  134. . The third field is signed latitude in degrees. Encoding must be
  135. decimal degrees, not degree/minute/second.
  136. . The fourth field is signed longitude in degrees. Encoding must be
  137. decimal degrees, not degree/minute/second.
  138. . The fifth field is horizontal uncertainty in meters (95% confidence).
  139. . The sixth field is altitude in meters.
  140. . The seventh field is vertical uncertainty in meters (95% confidence).
  141. . The eighth field is speed over ground in meters per second.
  142. . The ninth field is speed-over-ground uncertainty in meters per second
  143. (95% confidence).
  144. . The tenth field is course over ground in degrees from true north.
  145. . The eleventh field is uncertainty of course over ground in degrees
  146. (95% confidence).
  147. . The twelfth field is climb/sink in meters per second.
  148. . The thirteenth field is uncertainty of climb/sink in meters per second
  149. (95% confidence).
  150. . The fourteenth field is an FAA mode indicator.
  151. These fourteen fields completely describe the position and velocity of
  152. an object and the associated uncertainties. The FAA mode field is added
  153. to satisfy a U.S. regulator's requirement.
  154. Here is an example:
  155. ....
  156. $GPTPV,2005-02-11T04:40:51.231Z,?,49.45,-123.12,2.3,70.1,52.0,01.0,02.1,23.1,0.6,,,8,A*31
  157. 2005-02-11T04:40:51.231Z, Time (Feb 11 04:40:51 UTC 2005)
  158. ?, Timestamp uncertainty will never be reported
  159. 49.45, Latitude (- sign indicates latitude south)
  160. -123.12, Longitude (- sign indicates longitude west)
  161. 2.3, Meters of horizontal uncertainty of position
  162. 70.1, Altitude, meters above sea level
  163. 52, Uncertainty of altitude
  164. 0.01, Speed, meters/sec
  165. 0.02, Speed uncertainty
  166. 23.1, Course over ground relative to true North
  167. 0.6, Course uncertainty in degrees.
  168. , Climb/sink not reported
  169. , Climb/sink uncertainty not reported
  170. A FAA mode indicator A (Auto).
  171. 31 Checksum.
  172. ....
  173. === GPSVU
  174. A second sentence describes GPS satellite status.
  175. [arabic]
  176. . The sentence tag is GPSVU, standing for Satellite View Update.
  177. . The first field is the required timestamp.
  178. . The second field is a count of satellites.
  179. . The remainder of the sentence fields are groups of four, one for each
  180. predicted position of a visible satellite. Each group has the following
  181. four elements:
  182. [arabic]
  183. .. The PRN or satellite ID.
  184. .. Elevation in degrees
  185. .. Azimuth, degrees
  186. .. Signal-to-noise ratio in decibels. If this satellite was used in the
  187. last fix, suffix this field with a '!'.
  188. Here is an example:
  189. ....
  190. $GPSVU,2005-02-11T04:40:51.231Z,11,03,03,111,00,04,15,270,00,06,01,010,00,13,06,292,00,14,25,170,00,16,57,208,39!,18,67,296,40!,19,40,246,00,22,42,067,42,24,14,311,43!,27,05,244,00*40
  191. ....
  192. === GPVID
  193. A third sentence identifies the device. It is GPVID for Version ID, and
  194. the fields are as follows:
  195. [arabic]
  196. . The sentence tag is GPVID, standing for Vendor ID.
  197. . The first field is the required timestamp.
  198. . The second field is the SGPS revision level.
  199. . The third field is the vendor name.
  200. . The fourth field is the device name or model number.
  201. . The fifth field is a chipset designation.
  202. . The sixth field may be empty or a subtype ID, typically a firmware
  203. revision level.
  204. All fields must consist of US-ASCII text not containing commas. The
  205. total length of the sentence must not exceed the old NMEA maximum of 82.
  206. Here is an example:
  207. ....
  208. $GPVID,2006-11-17T12:29:37Z,1.0,Haicom,H204S,SiRF-II,231.00.00*5C
  209. ....
  210. === GPGSP
  211. With the addition of a fourth sentence, $GPSGP, transition to the new
  212. protocol would be easy. It would have two forms:
  213. $GPGSP,1: directs the receiver to emit GPPVT and GPSVU only, if it is
  214. not already doing so.
  215. $GPGSP,0: directs the receiver to return to NMEA-classic mode, if it is
  216. capable of doing so.
  217. Example:
  218. ....
  219. $GPGSP,1*4E
  220. ....
  221. An SGPS-conformant receiver is required to respond with
  222. $GPSGP,timestamp,x,y where x is 1 or 0 reflecting the command, and y is
  223. 1 or 0 reporting its new mode.
  224. Other listeners can distinguish GPGSP responses from requests by
  225. checking whether field 1 contains an IS8601 timestamp; an easy way to
  226. check this is to look for the trailing Z.
  227. === Other considerations
  228. Finally, SGPS-compliant receivers are required to respond to the
  229. requests $GPPVT, $GPVSU, $GPVID, and $GPGSP (without arguments) with the
  230. corresponding report based on most recent available data.
  231. == Could This Be Adopted?
  232. Astute readers will already have noted that the SGPS sentences might be
  233. sold as a minor extension to NMEA 0183. first supplementing and
  234. eventually obsolescing the half-dozen or so sentences emitted by most
  235. modern GPSes.
  236. The only fields reported in the SGPS set that cannot be trivially
  237. derived from data already computed for NMEA reports are (a) Climb/sink,
  238. and (b) GPSTPV uncertainty fields. None of these should be difficult to
  239. derive.
  240. == Revision History
  241. |===
  242. |Version |Date| Author | Comments
  243. |1.7 |25 January 2020 |gem | Convert from DocBook to AsciiDoc
  244. |1.6 |5 January 2016 |esr
  245. |Corrected timezone in example
  246. W3 datetime document has disapperard, point to Internet Archive
  247. |1.5 |25 February 2009 |esr
  248. |Fixed GPSGP so the response isn't identical to the send,
  249. avoiding problems on multidrop lines. Added SGPS revision
  250. field to $GPVID. Went back to requiring checksums, because
  251. you just know it was going to bite someday otherwise.
  252. Changed sentence length limit to 255.
  253. |1.4 |21 November 2006 |esr
  254. |Fixed timestamp to Zulu time. Specified signed latitude.
  255. |1.3 |16 November 2006 |esr
  256. |Added GPSVID and GPSGP, changed to mandate ISO8601 dates.
  257. |1.2 |25 April 2005 |esr
  258. |Specify UTC. Fix time-uncertainty units. Vertical course
  259. angle changed to climb/sink rate.
  260. |1.1 |11 February 2005 |esr
  261. |Corrected SGPS example, thanks to Kevin Niehage for the bug report.
  262. |1.0 |04 January 2005 |esr |Initial draft.
  263. |===
  264. == COPYING
  265. This file is Copyright 2004 by the GPSD project +
  266. This file is Copyright 2004 by Eric S. Raymond +
  267. SPDX-License-Identifier: BSD-2-clause