upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
nmea_gps.hpp
Go to the documentation of this file.
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2016 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #pragma once
25 
26 #include <atomic>
27 #include <iostream>
28 #include <map>
29 #include <list>
30 #include <mutex>
31 #include <queue>
32 #include <cstdlib>
33 #include <string>
34 #include <thread>
35 #include <unistd.h>
36 #include <utility>
37 #include <vector>
38 
39 #include "nmea_gps.h"
40 
41 namespace upm {
74  class NMEAGPS;
75 
77  struct coord_DD {
79  double latitude = 0.0;
81  double longitude = 0.0;
86  std::string __str__();
87  };
88 
90  struct satellite {
92  std::string prn;
98  int snr;
100  satellite():satellite("", 0, 0, 0){}
109  satellite(const std::string& sprn, int elevation, int azimuth, int snr):
110  prn(sprn), elevation_deg(elevation), azimuth_deg(azimuth), snr(snr) {}
115  std::string __str__();
116  };
117 
119  struct nmeatxt {
121  int severity;
123  std::string message;
125  nmeatxt():nmeatxt(0, "") {}
131  nmeatxt(int severity, const std::string& message):
132  severity(severity), message(message){}
137  std::string __str__();
138  };
139 
141  enum class gps_fix_quality {
143  no_fix = 0,
145  fix_sp,
147  fix_dp,
149  fix_pps,
151  fix_rtk,
153  fix_frtk,
155  fix_dr,
157  fix_manual,
160  };
161 
165  struct gps_fix {
169  std::string time_utc = std::string("");
173  uint8_t satellites = 0;
175  float hdop = 0.0;
177  float altitude_meters = 0.0;
179  float geoid_height_meters = 0.0;
181  float age_seconds = 0.0;
183  std::string station_id = std::string("");
185  bool valid = false;
187  bool chksum_match = false;
192  std::string __str__();
193  };
194 
195  class NMEAGPS {
196  public:
197 
207  NMEAGPS(unsigned int uart, unsigned int baudrate,
208  int enable_pin);
209 
217  NMEAGPS(const std::string& uart, unsigned int baudrate);
218 
226  NMEAGPS(unsigned int bus, uint8_t addr);
227 
231  ~NMEAGPS();
232 
239  std::string readStr(size_t size);
240 
248  int writeStr(const std::string& buffer);
249 
257  void enable(bool enable);
258 
266  void setBaudrate(unsigned int baudrate);
267 
279  bool dataAvailable(unsigned int millis);
280 
285  size_t getMaxQueueDepth();
286 
293  size_t setMaxQueueDepth(size_t depth);
294 
301  void parseStart();
302 
306  void parseStop();
307 
312  bool isRunning() {return _running;}
313 
319  gps_fix getFix();
320 
326  std::string getRawSentence();
327 
333  nmeatxt getTxtMessage();
334 
339  size_t fixQueueSize();
340 
345  size_t rawSentenceQueueSize();
346 
351  size_t txtMessageQueueSize();
352 
359  void parseNMEASentence(const std::string& sentence);
360 
365  std::vector<satellite> satellites();
366 
371  double sentencesPerSecond();
372 
380  double bytesPerSecond();
381 
386  std::string __str__();
387  protected:
390 
391  private:
393  NMEAGPS(const NMEAGPS&) = delete;
394  NMEAGPS &operator=(const NMEAGPS&) = delete;
395 
397  std::thread _parser;
398 
400  void _parse_thread();
401 
403  std::atomic<bool> _running;
404 
406  void _parse_gpgga(const std::string& string);
408  void _parse_gpgsv(const std::string& string);
410  void _parse_gpgll(const std::string& string);
412  void _parse_gptxt(const std::string& string);
413 
415  using fp = void (NMEAGPS::*)(const std::string &);
417  const std::map<std::string, fp> nmea_2_parser =
418  {
419  {"GPGGA", &NMEAGPS::_parse_gpgga},
420  {"GPGSV", &NMEAGPS::_parse_gpgsv},
421  {"GPGLL", &NMEAGPS::_parse_gpgll},
422  {"GPTXT", &NMEAGPS::_parse_gptxt},
423  };
424 
426  std::queue<std::string> _queue_nmea_sentence;
427  std::mutex _mtx_nmea_sentence;
428 
430  std::queue<gps_fix> _queue_fix;
431  std::mutex _mtx_fix;
432 
434  std::queue<nmeatxt> _queue_txt;
435  std::mutex _mtx_txt;
436 
438  std::atomic<size_t> _maxQueueDepth;
439 
441  std::atomic<size_t> _sentences_since_start;
442 
444  std::atomic<size_t> _bytes_since_start;
445 
447  std::atomic<double> _seconds_since_start;
448 
450  std::list<satellite> _satlist;
451  std::mutex _mtx_satlist;
452  };
453 }
bool isRunning()
Definition: nmea_gps.hpp:312
nmea_gps_context m_nmea_gps
Definition: nmea_gps.hpp:389
coord_DD coordinates
Definition: nmea_gps.hpp:167
std::string __str__()
Definition: nmea_gps.cxx:620
Definition: nmea_gps.hpp:77
Definition: nmea_gps.h:51
Definition: nmea_gps.hpp:195
nmeatxt(int severity, const std::string &message)
Definition: nmea_gps.hpp:131
satellite(const std::string &sprn, int elevation, int azimuth, int snr)
Definition: nmea_gps.hpp:109
C++ API wrapper for the bh1749 driver.
Definition: a110x.hpp:29
Definition: nmea_gps.hpp:165
gps_fix_quality
Definition: nmea_gps.hpp:141
std::string message
Definition: nmea_gps.hpp:123
Definition: nmea_gps.hpp:90
Definition: nmea_gps.hpp:119
double longitude
Definition: nmea_gps.hpp:81
int azimuth_deg
Definition: nmea_gps.hpp:96
int snr
Definition: nmea_gps.hpp:98
double latitude
Definition: nmea_gps.hpp:79
int elevation_deg
Definition: nmea_gps.hpp:94
std::string prn
Definition: nmea_gps.hpp:92
satellite()
Definition: nmea_gps.hpp:100
int severity
Definition: nmea_gps.hpp:121
nmeatxt()
Definition: nmea_gps.hpp:125