1
- package com .hoho .android .usbserial .examples ;
1
+ package com .mikey0000 .android .usbserial .examples ;
2
2
3
3
/**
4
4
* Created by michaelarthur on 2/09/15.
5
5
*/
6
6
7
7
import android .location .Location ;
8
8
import android .os .SystemClock ;
9
- import android .util .Log ;
10
9
11
- import java .lang .reflect .InvocationTargetException ;
12
- import java .lang .reflect .Method ;
13
10
import java .util .HashMap ;
14
11
import java .util .Map ;
15
12
16
13
17
14
public class NMEAParser {
18
15
19
- // fucking java interfaces
16
+ // java interfaces
20
17
interface SentenceParser {
21
18
public boolean parse (String [] tokens , GPSPosition position );
22
19
}
@@ -30,8 +27,6 @@ static float Latitude2Decimal(String lat, String NS) {
30
27
med = -med ;
31
28
}
32
29
return med ;
33
-
34
-
35
30
}
36
31
37
32
static float Longitude2Decimal (String lon , String WE ) {
@@ -41,7 +36,6 @@ static float Longitude2Decimal(String lon, String WE) {
41
36
med = -med ;
42
37
}
43
38
return med ;
44
-
45
39
}
46
40
47
41
// parsers
@@ -51,6 +45,8 @@ public boolean parse(String[] tokens, GPSPosition position) {
51
45
position .lat = Latitude2Decimal (tokens [2 ], tokens [3 ]);
52
46
position .lon = Longitude2Decimal (tokens [4 ], tokens [5 ]);
53
47
position .quality = Integer .parseInt (tokens [6 ]);
48
+ position .numberOfSatellites = Integer .parseInt (tokens [7 ]);
49
+ position .hdop = Float .parseFloat (tokens [8 ]);
54
50
position .altitude = Float .parseFloat (tokens [9 ]);
55
51
return true ;
56
52
}
@@ -94,32 +90,52 @@ public class GPSPosition {
94
90
public float time = 0.0f ;
95
91
public float lat = 0.0f ;
96
92
public float lon = 0.0f ;
97
- public boolean fixed = false ;
98
93
public int quality = 0 ;
99
94
public float dir = 0.0f ;
100
95
public float altitude = 0.0f ;
101
96
public float velocity = 0.0f ;
97
+ public int numberOfSatellites = 0 ;
98
+ // See: https://en.wikipedia.org/wiki/Dilution_of_precision_(navigation)
99
+ public float hdop = 0.0f ;
100
+
101
+ public boolean fixed = false ;
102
102
103
103
public void updatefix () {
104
104
fixed = quality > 0 ;
105
105
}
106
106
107
107
public String toString () {
108
- return String .format ("POSITION: lat: %f, lon: %f, time: %f, Q: %d, dir: %f, alt: %f, vel: %f" , lat , lon , time , quality , dir , altitude , velocity );
108
+ return String .format (
109
+ "POSITION: lat: %f, lon: %f, time: %f, Q: %d, hdop: %f, dir: %f, alt: %f, vel: %f, # of sat: %d, fixed: %b" ,
110
+ lat , lon , time , quality , hdop , dir , altitude , velocity , numberOfSatellites , fixed
111
+ );
109
112
}
110
113
}
111
114
112
115
GPSPosition position = new GPSPosition ();
113
116
114
117
private static final Map <String , SentenceParser > sentenceParsers = new HashMap <String , SentenceParser >();
115
118
119
+ /**
120
+ * Maps a NMEA quality identifier to the best possible accuracy value (in meters)
121
+ */
122
+ protected static final Map <Integer ,Float > qualityAccuracy = new HashMap <Integer , Float >();
123
+
116
124
public NMEAParser () {
117
125
sentenceParsers .put ("GPGGA" , new GPGGA ());
118
126
sentenceParsers .put ("GPGGL" , new GPGGL ());
119
127
sentenceParsers .put ("GPRMC" , new GPRMC ());
120
128
sentenceParsers .put ("GPRMZ" , new GPRMZ ());
121
129
//only really good GPS devices have this sentence but ...
122
- sentenceParsers .put ("GPVTG" , new GPVTG ());
130
+ //sentenceParsers.put("GPVTG", new GPVTG());
131
+
132
+ // concrete accuracy values (in meters) per NMEA quality level
133
+ // this is used to calculate the position accuracy in NMEAParser.calculateAccuracy
134
+ qualityAccuracy .put ( 0 , 0.0f ); // invalid
135
+ qualityAccuracy .put ( 1 , 2.0f ); // GPS 2d/3d
136
+ qualityAccuracy .put ( 2 , 0.7f ); // DGNSS
137
+ qualityAccuracy .put ( 4 , 0.04f ); // RTK fixed
138
+ qualityAccuracy .put ( 5 , 0.04f ); // RTK fixed
123
139
}
124
140
125
141
public GPSPosition parse (String line ) {
@@ -149,24 +165,28 @@ public GPSPosition parse(String line) {
149
165
}
150
166
151
167
public Location location (String str ) {
152
- Location localLocation ;
153
- GPSPosition GPSPos = parse (str );
154
-
155
- if (!GPSPos .fixed ) {
156
- GPSPos = null ;
157
- return null ;
168
+ Location localLocation = null ;
169
+ parse ( str );
170
+
171
+ if ( position .quality > 0 ) { // quality 0 is an invalid entry
172
+ localLocation = new Location ("gps" );
173
+ localLocation .setLongitude (position .lon );
174
+ localLocation .setLatitude (position .lat );
175
+ localLocation .setAltitude (position .altitude );
176
+ localLocation .setSpeed (position .velocity );
177
+ localLocation .setBearing (position .dir );
178
+ localLocation .setTime (System .currentTimeMillis ());
179
+ localLocation .setElapsedRealtimeNanos (SystemClock .elapsedRealtimeNanos ());
180
+ localLocation .setAccuracy (calculateAccuracy ());
181
+
182
+ position = new GPSPosition ();
158
183
}
159
184
160
- localLocation = new Location ("gps" );
161
- localLocation .setLongitude (GPSPos .lon );
162
- localLocation .setLatitude (GPSPos .lat );
163
- localLocation .setAltitude (GPSPos .altitude );
164
- localLocation .setSpeed (GPSPos .velocity );
165
- localLocation .setBearing (GPSPos .dir );
166
- localLocation .setTime (System .currentTimeMillis ());
167
- localLocation .setElapsedRealtimeNanos (SystemClock .elapsedRealtimeNanos ());
168
- localLocation .setAccuracy (GPSPos .quality );
169
-
170
185
return localLocation ;
171
186
}
187
+
188
+ protected float calculateAccuracy () {
189
+
190
+ return qualityAccuracy .get ( position .quality ) * position .hdop ;
191
+ }
172
192
}
0 commit comments