Project

General

Profile

Statistics
| Branch: | Revision:

root / quad1 / AeroQuad / TinyGPS.h @ 9240aaa3

History | View | Annotate | Download (5.5 KB)

1
/*
2
  TinyGPS - a small GPS library for Arduino providing basic NMEA parsing
3
  Copyright (C) 2008-9 Mikal Hart
4
  All rights reserved.
5

6
  This library is free software; you can redistribute it and/or
7
  modify it under the terms of the GNU Lesser General Public
8
  License as published by the Free Software Foundation; either
9
  version 2.1 of the License, or (at your option) any later version.
10

11
  This library is distributed in the hope that it will be useful,
12
  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
  Lesser General Public License for more details.
15

16
  You should have received a copy of the GNU Lesser General Public
17
  License along with this library; if not, write to the Free Software
18
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
*/
20

    
21
#ifndef TinyGPS_h
22
#define TinyGPS_h
23

    
24
#include "WProgram.h"
25

    
26
#define _GPS_VERSION 9 // software version of this library
27
#define _GPS_MPH_PER_KNOT 1.15077945
28
#define _GPS_MPS_PER_KNOT 0.51444444
29
#define _GPS_KMPH_PER_KNOT 1.852
30
#define _GPS_MILES_PER_METER 0.00062137112
31
#define _GPS_KM_PER_METER 0.001
32
//#define _GPS_NO_STATS
33

    
34
class TinyGPS
35
{
36
  public:
37
    TinyGPS();
38
    bool encode(char c); // process one character received from GPS
39
    TinyGPS &operator << (char c) {encode(c); return *this;}
40
    
41
    // lat/long in hundred thousandths of a degree and age of fix in milliseconds
42
    inline void get_position(long *latitude, long *longitude, unsigned long *fix_age = 0)
43
    {
44
      if (latitude) *latitude = _latitude;
45
      if (longitude) *longitude = _longitude;
46
      if (fix_age) *fix_age = _last_position_fix == GPS_INVALID_FIX_TIME ? 
47
        GPS_INVALID_AGE : millis() - _last_position_fix;
48
    }
49

    
50
    // date as ddmmyy, time as hhmmsscc, and age in milliseconds
51
    inline void get_datetime(unsigned long *date, unsigned long *time, unsigned long *fix_age = 0)
52
    {
53
      if (date) *date = _date;
54
      if (time) *time = _time;
55
      if (fix_age) *fix_age = _last_time_fix == GPS_INVALID_FIX_TIME ? 
56
        GPS_INVALID_AGE : millis() - _last_time_fix;
57
    }
58

    
59
    // signed altitude in centimeters (from GPGGA sentence)
60
    inline long altitude() { return _altitude; }
61

    
62
    // course in last full GPRMC sentence in 100th of a degree
63
    inline unsigned long course() { return _course; }
64
    
65
    // speed in last full GPRMC sentence in 100ths of a knot
66
    unsigned long speed() { return _speed; }
67

    
68
#ifndef _GPS_NO_STATS
69
    void stats(unsigned long *chars, unsigned short *good_sentences, unsigned short *failed_cs);
70
#endif
71

    
72
    inline void f_get_position(float *latitude, float *longitude, unsigned long *fix_age = 0)
73
    {
74
      long lat, lon;
75
      get_position(&lat, &lon, fix_age);
76
      *latitude = lat / 100000.0;
77
      *longitude = lon / 100000.0;
78
    }
79

    
80
    inline void crack_datetime(int *year, byte *month, byte *day, 
81
      byte *hour, byte *minute, byte *second, byte *hundredths = 0, unsigned long *fix_age = 0)
82
    {
83
      unsigned long date, time;
84
      get_datetime(&date, &time, fix_age);
85
      if (year) 
86
      {
87
        *year = date % 100;
88
        *year += *year > 80 ? 1900 : 2000;
89
      }
90
      if (month) *month = (date / 100) % 100;
91
      if (day) *day = date / 10000;
92
      if (hour) *hour = time / 1000000;
93
      if (minute) *minute = (time / 10000) % 100;
94
      if (second) *second = (time / 100) % 100;
95
      if (hundredths) *hundredths = time % 100;
96
    }
97

    
98
    inline float f_altitude()    { return altitude() / 100.0; }
99
    inline float f_course()      { return course() / 100.0; }
100
    inline float f_speed_knots() { return speed() / 100.0; }
101
    inline float f_speed_mph()   { return _GPS_MPH_PER_KNOT * f_speed_knots(); }
102
    inline float f_speed_mps()   { return _GPS_MPS_PER_KNOT * f_speed_knots(); }
103
    inline float f_speed_kmph()  { return _GPS_KMPH_PER_KNOT * f_speed_knots(); }
104

    
105
    static int library_version() { return _GPS_VERSION; }
106

    
107
    enum {GPS_INVALID_AGE = 0xFFFFFFFF, GPS_INVALID_ANGLE = 999999999, GPS_INVALID_ALTITUDE = 999999999, GPS_INVALID_DATE = 0,
108
      GPS_INVALID_TIME = 0xFFFFFFFF, GPS_INVALID_SPEED = 999999999, GPS_INVALID_FIX_TIME = 0xFFFFFFFF};
109

    
110
private:
111
    enum {_GPS_SENTENCE_GPGGA, _GPS_SENTENCE_GPRMC, _GPS_SENTENCE_OTHER};
112
    
113
    // properties
114
    unsigned long _time, _new_time;
115
    unsigned long _date, _new_date;
116
    long _latitude, _new_latitude;
117
    long _longitude, _new_longitude;
118
    long _altitude, _new_altitude;
119
    unsigned long  _speed, _new_speed;
120
    unsigned long  _course, _new_course;
121

    
122
    unsigned long _last_time_fix, _new_time_fix;
123
    unsigned long _last_position_fix, _new_position_fix;
124

    
125
    // parsing state variables
126
    byte _parity;
127
    bool _is_checksum_term;
128
    char _term[15];
129
    byte _sentence_type;
130
    byte _term_number;
131
    byte _term_offset;
132
    bool _gps_data_good;
133

    
134
#ifndef _GPS_NO_STATS
135
    // statistics
136
    unsigned long _encoded_characters;
137
    unsigned short _good_sentences;
138
    unsigned short _failed_checksum;
139
    unsigned short _passed_checksum;
140
#endif
141

    
142
    // internal utilities
143
    int from_hex(char a);
144
    unsigned long parse_decimal();
145
    unsigned long parse_degrees();
146
    bool term_complete();
147
    bool gpsisdigit(char c) { return c >= '0' && c <= '9'; }
148
    long gpsatol(const char *str);
149
    int gpsstrcmp(const char *str1, const char *str2);
150
};
151

    
152
// Arduino 0012 workaround
153
#undef int
154
#undef char
155
#undef long
156
#undef byte
157
#undef float
158
#undef abs
159
#undef round 
160

    
161
#endif