source: ntrip/trunk/BNC/RTCM/RTCM2.cpp@ 725

Last change on this file since 725 was 725, checked in by weber, 16 years ago

* empty log message *

File size: 33.0 KB
Line 
1//------------------------------------------------------------------------------
2//
3// RTCM2.cpp
4//
5// Purpose:
6//
7// Module for extraction of RTCM2 messages
8//
9// References:
10//
11// RTCM 10402.3 Recommended Standards for Differential GNSS (Global
12// Navigation Satellite Systems) Service; RTCM Paper 136-2001/SC104-STD,
13// Version 2.3, 20 Aug. 2001; Radio Technical Commission For Maritime
14// Services, Alexandria, Virgina (2001).
15// ICD-GPS-200; Navstar GPS Space Segment / Navigation User Interfaces;
16// Revison C; 25 Sept. 1997; Arinc Research Corp., El Segundo (1997).
17// Jensen M.; RTCM2ASC Documentation;
18// URL http://kom.aau.dk/~borre/masters/receiver/rtcm2asc.htm;
19// last accessed 17 Sep. 2006
20// Sager J.; Decoder for RTCM SC-104 data from a DGPS beacon receiver;
21// URL http://www.wsrcc.com/wolfgang/ftp/rtcm-0.3.tar.gz;
22// last accessed 17 Sep. 2006
23//
24// Notes:
25//
26// - The host computer is assumed to use little endian (Intel) byte order
27//
28// Last modified:
29//
30// 2006/09/17 OMO Created
31// 2006/09/19 OMO Fixed getHeader() methods
32// 2006/09/21 OMO Reduced phase ambiguity to 2^23 cycles
33// 2006/10/05 OMO Specified const'ness of various member functions
34// 2006/10/13 LMV Fixed resolvedPhase to handle missing C1 range
35// 2006/10/14 LMV Fixed loop cunter in ThirtyBitWord
36// 2006/10/14 LMV Exception handling
37// 2006/10/17 OMO Removed obsolete check of multiple message indicator
38// 2006/10/17 OMO Fixed parity handling
39// 2006/10/18 OMO Improved screening of bad data in RTCM2_Obs::extract
40// 2006/11/25 OMO Revised check for presence of GLONASS data
41// 2007/05/25 GW Round time tag to 100 ms
42// 2007/12/11 AHA Changed handling of C/A- and P-Code on L1
43// 2007/12/13 AHA Changed epoch comparison in packet extraction
44// 2008/03/01 OMO Compilation flag for epoch rounding
45// 2008/03/04 AHA Fixed problems with PRN 32
46// 2008/03/05 AHA Implemeted fix for Trimble 4000SSI receivers
47// 2008/03/07 AHA Major revision of input buffer handling
48// 2008/03/07 AHA Removed unnecessary failure flag
49// 2008/03/10 AHA Corrected extraction of antenna serial number
50// 2008/03/10 AHA Corrected buffer length check in getPacket()
51// 2008/03/11 AHA Added checks for data consistency in extraction routines
52// 2008/03/11 AHA isGPS-flag in RTCM2_Obs is now set to false on clear()
53//
54// (c) DLR/GSOC
55//
56//------------------------------------------------------------------------------
57
58#include <bitset>
59#include <cmath>
60#include <fstream>
61#include <iomanip>
62#include <iostream>
63#include <string>
64#include <vector>
65
66#include "RTCM2.h"
67
68// Activate (1) or deactivate (0) debug output for tracing parity errors and
69// undersized packets in get(Unsigned)Bits
70
71#define DEBUG 0
72
73// Activate (1) or deactivate (0) rounding of measurement epochs to 100ms
74//
75// Note: A need to round the measurement epoch to integer tenths of a second was
76// noted by BKG in the processing of RTCM2 data from various receivers in NTRIP
77// real-time networks. It is unclear at present, whether this is due to an
78// improper implementation of the RTCM2 standard in the respective receivers
79// or an unclear formulation of the standard.
80
81#define ROUND_EPOCH 1
82
83// Fix for data streams originating from TRIMBLE_4000SSI receivers.
84// GPS PRN32 is erroneously flagged as GLONASS satellite in the C/A
85// pseudorange messages. We therefore use a majority voting to
86// determine the true constellation for this message.
87// This fix is only required for Trimble4000SSI receivers but can also
88// be used with all other known receivers.
89
90#define FIX_TRIMBLE_4000SSI 1
91
92using namespace std;
93
94
95// GPS constants
96
97const double c_light = 299792458.0; // Speed of light [m/s]; IAU 1976
98const double f_L1 = 1575.42e6; // L1 frequency [Hz] (10.23MHz*154)
99const double f_L2 = 1227.60e6; // L2 frequency [Hz] (10.23MHz*120)
100
101const double lambda_L1 = c_light/f_L1; // L1 wavelength [m] (0.1903m)
102const double lambda_L2 = c_light/f_L2; // L2 wavelength [m]
103
104//
105// Bits for message availability checks
106//
107
108const int bit_L1rngGPS = 0;
109const int bit_L2rngGPS = 1;
110const int bit_L1cphGPS = 2;
111const int bit_L2cphGPS = 3;
112const int bit_L1rngGLO = 4;
113const int bit_L2rngGLO = 5;
114const int bit_L1cphGLO = 6;
115const int bit_L2cphGLO = 7;
116
117
118//
119// namespace rtcm2
120//
121
122namespace rtcm2 {
123
124//------------------------------------------------------------------------------
125//
126// class ThirtyBitWord (implementation)
127//
128// Purpose:
129//
130// Handling of RTCM2 30bit words
131//
132//------------------------------------------------------------------------------
133
134// Constructor
135
136ThirtyBitWord::ThirtyBitWord() : W(0) {
137};
138
139// Clear entire 30-bit word and 2-bit parity from previous word
140
141void ThirtyBitWord::clear() {
142 W = 0;
143};
144
145// Parity check
146
147bool ThirtyBitWord::validParity() const {
148
149 // Parity stuff
150
151 static const unsigned int PARITY_25 = 0xBB1F3480;
152 static const unsigned int PARITY_26 = 0x5D8F9A40;
153 static const unsigned int PARITY_27 = 0xAEC7CD00;
154 static const unsigned int PARITY_28 = 0x5763E680;
155 static const unsigned int PARITY_29 = 0x6BB1F340;
156 static const unsigned int PARITY_30 = 0x8B7A89C0;
157
158 // Look-up table for parity of eight bit bytes
159 // (parity=0 if the number of 0s and 1s is equal, else parity=1)
160 static unsigned char byteParity[] = {
161 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
162 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,
163 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,
164 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
165 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,
166 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
167 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
168 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0
169 };
170
171 // Local variables
172
173 unsigned int t, w, p;
174
175 // The sign of the data is determined by the D30* parity bit
176 // of the previous data word. If D30* is set, invert the data
177 // bits D01..D24 to obtain the d01..d24 (but leave all other
178 // bits untouched).
179
180 w = W;
181 if ( w & 0x40000000 ) w ^= 0x3FFFFFC0;
182
183 // Compute the parity of the sign corrected data bits d01..d24
184 // as described in the ICD-GPS-200
185
186 t = w & PARITY_25;
187 p = ( byteParity[t &0xff] ^ byteParity[(t>> 8)&0xff] ^
188 byteParity[(t>>16)&0xff] ^ byteParity[(t>>24) ] );
189
190 t = w & PARITY_26;
191 p = (p<<1) |
192 ( byteParity[t &0xff] ^ byteParity[(t>> 8)&0xff] ^
193 byteParity[(t>>16)&0xff] ^ byteParity[(t>>24) ] );
194
195 t = w & PARITY_27;
196 p = (p<<1) |
197 ( byteParity[t &0xff] ^ byteParity[(t>> 8)&0xff] ^
198 byteParity[(t>>16)&0xff] ^ byteParity[(t>>24) ] );
199
200 t = w & PARITY_28;
201 p = (p<<1) |
202 ( byteParity[t &0xff] ^ byteParity[(t>> 8)&0xff] ^
203 byteParity[(t>>16)&0xff] ^ byteParity[(t>>24) ] );
204
205 t = w & PARITY_29;
206 p = (p<<1) |
207 ( byteParity[t &0xff] ^ byteParity[(t>> 8)&0xff] ^
208 byteParity[(t>>16)&0xff] ^ byteParity[(t>>24) ] );
209
210 t = w & PARITY_30;
211 p = (p<<1) |
212 ( byteParity[t &0xff] ^ byteParity[(t>> 8)&0xff] ^
213 byteParity[(t>>16)&0xff] ^ byteParity[(t>>24) ] );
214
215 return ( (W & 0x3f) == p);
216
217};
218
219
220// Check preamble
221
222bool ThirtyBitWord::isHeader() const {
223
224 const unsigned char Preamble = 0x66;
225
226 unsigned char b = (value()>>22) & 0xFF;
227
228 return ( b==Preamble );
229
230};
231
232
233// Return entire 32-bit (current word and previous parity)
234
235unsigned int ThirtyBitWord::all() const {
236 return W;
237};
238
239
240// Return sign-corrected 30-bit (or zero if parity mismatch)
241
242unsigned int ThirtyBitWord::value() const {
243
244 unsigned int w = W;
245
246 if (validParity()) {
247 // Return data and current parity bits. Invert data bits if D30*
248 // is set and discard old parity bits.
249 if ( w & 0x40000000 ) w ^= 0x3FFFFFC0;
250 return (w & 0x3FFFFFFF);
251 }
252 else {
253 // Error; invalid parity
254 return 0;
255 };
256
257};
258
259
260// Append a byte with six data bits
261
262void ThirtyBitWord::append(unsigned char b) {
263
264 // Look up table for swap (left-right) of 6 data bits
265 static const unsigned char
266 swap[] = {
267 0,32,16,48, 8,40,24,56, 4,36,20,52,12,44,28,60,
268 2,34,18,50,10,42,26,58, 6,38,22,54,14,46,30,62,
269 1,33,17,49, 9,41,25,57, 5,37,21,53,13,45,29,61,
270 3,35,19,51,11,43,27,59, 7,39,23,55,15,47,31,63
271 };
272
273 // Bits 7 and 6 (of 0..7) must be "01" for valid data bytes
274 if ( (b & 0x40) != 0x40 ) {
275 // We simply skip the invalid input byte and leave the word unchanged
276#if (DEBUG>0)
277 cerr << "Error in append()" << bitset<32>(all()) << endl;
278#endif
279 return;
280 };
281
282 // Swap bits 0..5 to restore proper bit order for 30bit words
283 b = swap[ b & 0x3f];
284
285 // Fill word
286 W = ( (W <<6) | (b & 0x3f) ) ;
287
288};
289
290
291// Get next 30bit word from string
292
293void ThirtyBitWord::get(const string& buf) {
294
295 // Check if string is long enough
296
297 if (buf.size()<5) {
298 // Ignore; users should avoid this case prior to calling get()
299#if ( DEBUG > 0 )
300 cerr << "Error in get(): packet too short (" << buf.size() <<")" << endl;
301#endif
302 return;
303 };
304
305 // Process 5 bytes
306
307 for (int i=0; i<5; i++) append(buf[i]);
308
309#if (DEBUG>0)
310 if (!validParity()) {
311 cerr << "Parity error in get()"
312 << bitset<32>(all()) << endl;
313 };
314#endif
315
316};
317
318// Get next 30bit word from file
319
320void ThirtyBitWord::get(istream& inp) {
321
322 unsigned char b;
323
324 for (int i=0; i<5; i++) {
325 inp >> b;
326 if (inp.fail()) { clear(); return; };
327 append(b);
328 };
329
330#if (DEBUG>0)
331 if (!validParity()) {
332 cerr << "Parity error in get()"
333 << bitset<32>(all()) << endl;
334 };
335#endif
336
337};
338
339// Get next header word from string
340
341void ThirtyBitWord::getHeader(string& buf) {
342
343 const int wordLen = 5; // Number of bytes representing a 30-bit word
344 const int spare = 1; // Number of spare words for resync of parity
345 // (same value as inRTCM2packet::getPacket())
346 unsigned int i;
347
348 i=0;
349 while (!isHeader() && i<buf.size() ) {
350 // Process byte
351 append(buf[i]);
352 // Increment count
353 i++;
354 };
355
356 // Remove processed bytes from buffer. Retain also the previous word to
357 // allow a resync if getHeader() is called repeatedly on the same buffer.
358 if (i>=(1+spare)*wordLen) buf.erase(0,i-(1+spare)*wordLen);
359
360#if (DEBUG>0)
361 if (!validParity()) {
362 cerr << "Parity error in getHeader()"
363 << bitset<32>(all()) << endl;
364 };
365#endif
366
367};
368
369// Get next header word from file
370
371void ThirtyBitWord::getHeader(istream& inp) {
372
373 unsigned char b;
374 unsigned int i;
375
376 i=0;
377 while ( !isHeader() || i<5 ) {
378 inp >> b;
379 if (inp.fail()) { clear(); return; };
380 append(b); i++;
381 };
382
383#if (DEBUG>0)
384 if (!validParity()) {
385 cerr << "Parity error in getHeader()"
386 << bitset<32>(all()) << endl;
387 };
388#endif
389
390};
391
392
393//------------------------------------------------------------------------------
394//
395// RTCM2packet (class implementation)
396//
397// Purpose:
398//
399// A class for handling RTCM2 data packets
400//
401//------------------------------------------------------------------------------
402
403// Constructor
404
405RTCM2packet::RTCM2packet() {
406 clear();
407};
408
409// Initialization
410
411void RTCM2packet::clear() {
412
413 W.clear();
414
415 H1=0;
416 H2=0;
417
418 DW.resize(0,0);
419
420};
421
422// Complete packet, valid parity
423
424bool RTCM2packet::valid() const {
425
426 // The methods for creating a packet (get,">>") ensure
427 // that a packet has a consistent number of data words
428 // and a valid parity in all header and data words.
429 // Therefore a packet is either empty or valid.
430
431 return (H1!=0);
432
433};
434
435
436//
437// Gets the next packet from the buffer
438//
439
440void RTCM2packet::getPacket(std::string& buf) {
441
442 const int wordLen = 5; // Number of bytes representing a 30-bit word
443 const int spare = 1; // Number of spare words for resync of parity
444 // (same value as used in ThirtyBitWord::getHeader)
445 unsigned int n;
446
447 // Try to read a full packet. Processed bytes are removed from the input
448 // buffer except for the latest spare*wordLen bytes to restore the parity
449 // bytes upon subseqeunt calls of getPAcket().
450
451 // Locate and read the first header word
452 W.getHeader(buf);
453 if (!W.isHeader()) {
454 // No header found; try again next time. buf retains only the spare
455 // words. The packet contents is cleared to indicate an unsuccessful
456 // termination of getPacket().
457 clear();
458#if ( DEBUG > 0 )
459 cerr << "Error in getPacket(): W.isHeader() = false for H1" << endl;
460#endif
461 return;
462 };
463 H1 = W.value();
464
465 // Do we have enough bytes to read the next word? If not, the packet
466 // contents is cleared to indicate an unsuccessful termination. The
467 // previously read spare and header bytes are retained in the buffer
468 // for use in the next call of getPacket().
469 if (buf.size()<(spare+2)*wordLen) {
470 clear();
471#if ( DEBUG > 0 )
472 cerr << "Error in getPacket(): buffer too short for complete H2" << endl;
473#endif
474 return;
475 };
476
477 // Read the second header word
478 W.get(buf.substr((spare+1)*wordLen,buf.size()-(spare+1)*wordLen));
479 H2 = W.value();
480 if (!W.validParity()) {
481 // Invalid H2 word; delete first buffer byte and try to resynch next time.
482 // The packet contents is cleared to indicate an unsuccessful termination.
483 clear();
484 buf.erase(0,1);
485#if ( DEBUG > 0 )
486 cerr << "Error in getPacket(): W.validParity() = false for H2" << endl;
487#endif
488 return;
489 };
490
491 n = nDataWords();
492
493 // Do we have enough bytes to read the next word? If not, the packet
494 // contents is cleared to indicate an unsuccessful termination. The
495 // previously read spare and header bytes are retained in the buffer
496 // for use in the next call of getPacket().
497 if (buf.size()<(spare+2+n)*wordLen) {
498 clear();
499#if ( DEBUG > 0 )
500 cerr << "Error in getPacket(): buffer too short for complete " << n
501 << " DWs" << endl;
502#endif
503 return;
504 };
505
506 DW.resize(n);
507 for (unsigned int i=0; i<n; i++) {
508 W.get(buf.substr((spare+2+i)*wordLen,buf.size()-(spare+2+i)*wordLen));
509 DW[i] = W.value();
510 if (!W.validParity()) {
511 // Invalid data word; delete first byte and try to resynch next time.
512 // The packet contents is cleared to indicate an unsuccessful termination.
513 clear();
514 buf.erase(0,1);
515#if ( DEBUG > 0 )
516 cerr << "Error in getPacket(): W.validParity() = false for DW"
517 << i << endl;
518#endif
519 return;
520 };
521 };
522
523 // Successful packet extraction; delete total number of message bytes
524 // from buffer.
525 // Note: a total of "spare" words remain in the buffer to enable a
526 // parity resynchronization when searching the next header.
527
528 buf.erase(0,(n+2)*wordLen);
529
530 return;
531
532};
533
534
535//
536// Gets the next packet from the input stream
537//
538
539void RTCM2packet::getPacket(std::istream& inp) {
540
541 int n;
542
543 W.getHeader(inp);
544 H1 = W.value();
545 if (inp.fail() || !W.isHeader()) { clear(); return; }
546
547 W.get(inp);
548 H2 = W.value();
549 if (inp.fail() || !W.validParity()) { clear(); return; }
550
551 n = nDataWords();
552 DW.resize(n);
553 for (int i=0; i<n; i++) {
554 W.get(inp);
555 DW[i] = W.value();
556 if (inp.fail() || !W.validParity()) { clear(); return; }
557 };
558
559 return;
560
561};
562
563//
564// Input operator
565//
566// Reads an RTCM2 packet from the input stream.
567//
568
569istream& operator >> (istream& is, RTCM2packet& p) {
570
571 p.getPacket(is);
572
573 return is;
574
575};
576
577// Access methods
578
579unsigned int RTCM2packet::header1() const {
580 return H1;
581};
582
583unsigned int RTCM2packet::header2() const {
584 return H2;
585};
586
587unsigned int RTCM2packet::dataWord(int i) const {
588 if ( (unsigned int)i < DW.size() ) {
589 return DW[i];
590 }
591 else {
592 return 0;
593 }
594};
595
596unsigned int RTCM2packet::msgType() const {
597 return ( H1>>16 & 0x003F );
598};
599
600unsigned int RTCM2packet::stationID() const {
601 return ( H1>> 6 & 0x03FF );
602};
603
604unsigned int RTCM2packet::modZCount() const {
605 return ( H2>>17 & 0x01FFF );
606};
607
608unsigned int RTCM2packet::seqNumber() const {
609 return ( H2>>14 & 0x0007 );
610};
611
612unsigned int RTCM2packet::nDataWords() const {
613 return ( H2>> 9 & 0x001F );
614};
615
616unsigned int RTCM2packet::staHealth() const {
617 return ( H2>> 6 & 0x0003 );
618};
619
620
621//
622// Get unsigned bit field
623//
624// Bits are numbered from left (msb) to right (lsb) starting at bit 0
625//
626
627unsigned int RTCM2packet::getUnsignedBits ( unsigned int start,
628 unsigned int n ) const {
629
630 unsigned int iFirst = start/24; // Index of first data word
631 unsigned int iLast = (start+n-1)/24; // Index of last data word
632 unsigned int bitField = 0;
633 unsigned int tmp;
634
635 // Checks
636
637 if (n>32) {
638 throw("Error: can't handle >32 bits in RTCM2packet::getUnsignedBits");
639 };
640
641 if ( 24*DW.size() < start+n-1 ) {
642#if (DEBUG>0)
643 cerr << "Debug output RTCM2packet::getUnsignedBits" << endl
644 << " P.msgType: " << setw(5) << msgType() << endl
645 << " P.nDataWords: " << setw(5) << nDataWords() << endl
646 << " start: " << setw(5) << start << endl
647 << " n: " << setw(5) << n << endl
648 << " P.H1: " << setw(5) << bitset<32>(H1) << endl
649 << " P.H2: " << setw(5) << bitset<32>(H2) << endl
650 << endl
651 << flush;
652#endif
653 throw("Error: Packet too short in RTCM2packet::getUnsignedBits");
654 }
655
656 // Handle initial data word
657 // Get all data bits. Strip parity and unwanted leading bits.
658 // Store result in 24 lsb bits of tmp.
659
660 tmp = (DW[iFirst]>>6) & 0xFFFFFF;
661 tmp = ( ( tmp << start%24) & 0xFFFFFF ) >> start%24 ;
662
663 // Handle central data word
664
665 if ( iFirst<iLast ) {
666 bitField = tmp;
667 for (unsigned int iWord=iFirst+1; iWord<iLast; iWord++) {
668 tmp = (DW[iWord]>>6) & 0xFFFFFF;
669 bitField = (bitField << 24) | tmp;
670 };
671 tmp = (DW[iLast]>>6) & 0xFFFFFF;
672 };
673
674 // Handle last data word
675
676 tmp = tmp >> (23-(start+n-1)%24);
677 bitField = (bitField << ((start+n-1)%24+1)) | tmp;
678
679 // Done
680
681 return bitField;
682
683};
684
685//
686// Get signed bit field
687//
688// Bits are numbered from left (msb) to right (lsb) starting at bit 0
689//
690
691int RTCM2packet::getBits ( unsigned int start,
692 unsigned int n ) const {
693
694
695 // Checks
696
697 if (n>32) {
698 throw("Error: can't handle >32 bits in RTCM2packet::getBits");
699 };
700
701 if ( 24*DW.size() < start+n-1 ) {
702#if (DEBUG>0)
703 cerr << "Debug output RTCM2packet::getUnsignedBits" << endl
704 << " P.msgType: " << setw(5) << msgType() << endl
705 << " P.nDataWords: " << setw(5) << nDataWords() << endl
706 << " start: " << setw(5) << start << endl
707 << " n: " << setw(5) << n << endl
708 << " P.H1: " << setw(5) << bitset<32>(H1) << endl
709 << " P.H2: " << setw(5) << bitset<32>(H2) << endl
710 << endl
711 << flush;
712#endif
713 throw("Error: Packet too short in RTCM2packet::getBits");
714 }
715
716 return ((int)(getUnsignedBits(start,n)<<(32-n))>>(32-n));
717
718};
719
720
721//------------------------------------------------------------------------------
722//
723// RTCM2_03 (class implementation)
724//
725// Purpose:
726//
727// A class for handling RTCM 2 GPS Reference Station Parameters messages
728//
729//------------------------------------------------------------------------------
730
731void RTCM2_03::extract(const RTCM2packet& P) {
732
733 // Check validity, packet type and number of data words
734
735 validMsg = (P.valid());
736 if (!validMsg) return;
737
738 validMsg = (P.ID()==03);
739 if (!validMsg) return;
740
741 validMsg = (P.nDataWords()==4);
742 if (!validMsg) return;
743
744 // Antenna reference point coordinates
745
746 x = P.getBits( 0,32)*0.01; // X [m]
747 y = P.getBits(32,32)*0.01; // Y [m]
748 z = P.getBits(64,32)*0.01; // Z [m]
749
750};
751
752//------------------------------------------------------------------------------
753//
754// RTCM2_23 (class implementation)
755//
756// Purpose:
757//
758// A class for handling RTCM 2 Antenna Type Definition messages
759//
760//------------------------------------------------------------------------------
761
762void RTCM2_23::extract(const RTCM2packet& P) {
763
764 int nad, nas;
765
766 // Check validity and packet type
767
768 validMsg = (P.valid());
769 if (!validMsg) return;
770
771 validMsg = (P.ID()==23);
772 if (!validMsg) return;
773
774 // Antenna descriptor
775 antType = "";
776 nad = P.getUnsignedBits(3,5);
777 for (int i=0;i<nad;i++)
778 antType += (char)P.getUnsignedBits(8+i*8,8);
779
780 // Optional antenna serial numbers
781 if (P.getUnsignedBits(2,1)==1) {
782 nas = P.getUnsignedBits(19+8*nad,5);
783 antSN = "";
784 for (int i=0;i<nas;i++)
785 antSN += (char)P.getUnsignedBits(24+8*nad+i*8,8);
786 };
787
788};
789
790
791//------------------------------------------------------------------------------
792//
793// RTCM2_24 (class implementation)
794//
795// Purpose:
796//
797// A class for handling RTCM 2 Reference Station Antenna
798// Reference Point Parameter messages
799//
800//------------------------------------------------------------------------------
801
802void RTCM2_24::extract(const RTCM2packet& P) {
803
804 double dx,dy,dz;
805
806 // Check validity, packet type and number of data words
807
808 validMsg = (P.valid());
809 if (!validMsg) return;
810
811 validMsg = (P.ID()==24);
812 if (!validMsg) return;
813
814 validMsg = (P.nDataWords()==6);
815 if (!validMsg) return;
816
817 // System indicator
818
819 isGPS = (P.getUnsignedBits(118,1)==0);
820 isGLONASS = (P.getUnsignedBits(118,1)==1);
821
822 // Antenna reference point coordinates
823
824 x = 64.0*P.getBits( 0,32);
825 y = 64.0*P.getBits(40,32);
826 z = 64.0*P.getBits(80,32);
827 dx = P.getUnsignedBits( 32,6);
828 dy = P.getUnsignedBits( 72,6);
829 dz = P.getUnsignedBits(112,6);
830 x = 0.0001*( x + (x<0? -dx:+dx) );
831 y = 0.0001*( y + (y<0? -dy:+dy) );
832 z = 0.0001*( z + (z<0? -dz:+dz) );
833
834 // Antenna Height
835
836 if (P.getUnsignedBits(119,1)==1) {
837 h= P.getUnsignedBits(120,18)*0.0001;
838 };
839
840
841};
842
843
844//------------------------------------------------------------------------------
845//
846// RTCM2_Obs (class definition)
847//
848// Purpose:
849//
850// A class for handling blocks of RTCM2 18 & 19 packets that need to be
851// combined to get a complete set of measurements
852//
853// Notes:
854//
855// The class collects L1/L2 code and phase measurements for GPS and GLONASS.
856// Since the Multiple Message Indicator is inconsistently handled by various
857// receivers we simply require code and phase on L1 and L2 for a complete
858// set ob observations at a given epoch. GLONASS observations are optional,
859// but all four types (code+phase,L1+L2) must be provided, if at least one
860// is given. Also, the GLONASS message must follow the corresponding GPS
861// message.
862//
863//------------------------------------------------------------------------------
864
865// Constructor
866
867RTCM2_Obs::RTCM2_Obs() {
868
869 clear();
870
871};
872
873// Reset entire block
874
875void RTCM2_Obs::clear() {
876
877 GPSonly = true;
878
879 secs=0.0; // Seconds of hour (GPS time)
880 nSat=0; // Number of space vehicles
881 PRN.resize(0); // space vehicles
882 rng_C1.resize(0); // Pseudorange [m]
883 rng_P1.resize(0); // Pseudorange [m]
884 rng_P2.resize(0); // Pseudorange [m]
885 cph_L1.resize(0); // Carrier phase [m]
886 cph_L2.resize(0); // Carrier phase [m]
887
888 availability.reset(); // Message status flags
889
890};
891
892// Availability checks
893
894bool RTCM2_Obs::anyGPS() const {
895
896 return availability.test(bit_L1rngGPS) ||
897 availability.test(bit_L2rngGPS) ||
898 availability.test(bit_L1cphGPS) ||
899 availability.test(bit_L2cphGPS);
900
901};
902
903bool RTCM2_Obs::anyGLONASS() const {
904
905 return availability.test(bit_L1rngGLO) ||
906 availability.test(bit_L2rngGLO) ||
907 availability.test(bit_L1cphGLO) ||
908 availability.test(bit_L2cphGLO);
909
910};
911
912bool RTCM2_Obs::allGPS() const {
913
914 return availability.test(bit_L1rngGPS) &&
915 availability.test(bit_L2rngGPS) &&
916 availability.test(bit_L1cphGPS) &&
917 availability.test(bit_L2cphGPS);
918
919};
920
921bool RTCM2_Obs::allGLONASS() const {
922
923 return availability.test(bit_L1rngGLO) &&
924 availability.test(bit_L2rngGLO) &&
925 availability.test(bit_L1cphGLO) &&
926 availability.test(bit_L2cphGLO);
927
928};
929
930// Validity
931
932bool RTCM2_Obs::valid() const {
933
934 return ( allGPS() && ( GPSonly || allGLONASS() ) );
935
936};
937
938
939//
940// Extract RTCM2 18 & 19 messages and store relevant data for future use
941//
942
943void RTCM2_Obs::extract(const RTCM2packet& P) {
944
945 bool isGPS,isCAcode,isL1,isOth;
946 int NSat,idx;
947 int sid,prn;
948 double t,rng,cph;
949
950 // Check validity and packet type
951
952 if ( ! ( P.valid() &&
953 (P.ID()==18 || P.ID()==19) ) ) return;
954
955 // Check number of data words, message starts with 1 DW for epoch, then each
956 // satellite brings 2 DW,
957 // Do not start decoding if less than 3 DW are in package
958
959 if ( P.nDataWords()<3 ) {
960#if ( DEBUG > 0 )
961 cerr << "Error in RTCM2_Obs::extract(): less than 3 DW ("
962 << P.nDataWords() << ") detected" << endl;
963#endif
964 return;
965 };
966
967 // Check if number of data words is odd number
968
969 if ( P.nDataWords()%2==0 ){
970#if ( DEBUG > 0 )
971 cerr << "Error in RTCM2_Obs::extract(): odd number of DW ("
972 << P.nDataWords() << ") detected" << endl;
973#endif
974 return;
975 };
976
977 // Clear previous data if block was already complete
978
979 if (valid()) clear();
980
981 // Process carrier phase message
982
983 if ( P.ID()==18 ) {
984
985 // Number of satellites in current message
986 NSat = (P.nDataWords()-1)/2;
987
988 // Current epoch (mod 3600 sec)
989 t = 0.6*P.modZCount()
990 + P.getUnsignedBits(4,20)*1.0e-6;
991
992#if (ROUND_EPOCH==1)
993 // SC-104 V2.3 4-42 Note 1 4. Assume measurements at hard edges
994 // of receiver clock with minimum divisions of 10ms
995 // and clock error less then recommended 1.1ms
996 // Hence, round time tag to 100 ms
997 t = floor(t*100.0+0.5)/100.0;
998#endif
999
1000 // Frequency (exit if neither L1 nor L2)
1001 isL1 = ( P.getUnsignedBits(0,1)==0 );
1002 isOth = ( P.getUnsignedBits(1,1)==1 );
1003 if (isOth) return;
1004
1005 // Constellation (for first satellite in message)
1006 isGPS = ( P.getUnsignedBits(26,1)==0 );
1007 GPSonly = GPSonly && isGPS;
1008
1009 // Multiple Message Indicator (only checked for first satellite)
1010 // pendingMsg = ( P.getUnsignedBits(24,1)==1 );
1011
1012 // Handle epoch: store epoch of first GPS message and
1013 // check consistency of subsequent messages. GLONASS time tags
1014 // are different and have to be ignored
1015 if (isGPS) {
1016 if ( nSat==0 ) {
1017 secs = t; // Store epoch
1018 }
1019// else if (t!=secs) {
1020 else if (abs(t-secs)>1e-6) {
1021 clear(); secs = t; // Clear all data, then store epoch
1022 };
1023 };
1024
1025 // Discard GLONASS observations if no prior GPS observations
1026 // are available
1027 if (!isGPS && !anyGPS() ) return;
1028
1029 // Set availability flags
1030
1031 if ( isL1 && isGPS) availability.set(bit_L1cphGPS);
1032 if (!isL1 && isGPS) availability.set(bit_L2cphGPS);
1033 if ( isL1 && !isGPS) availability.set(bit_L1cphGLO);
1034 if (!isL1 && !isGPS) availability.set(bit_L2cphGLO);
1035
1036 // Process all satellites
1037
1038 for (int iSat=0;iSat<NSat;iSat++){
1039
1040 // Code type
1041 isCAcode = ( P.getUnsignedBits(iSat*48+25,1)==0 );
1042
1043 // Satellite
1044 sid = P.getUnsignedBits(iSat*48+27,5);
1045 if (sid==0) sid=32;
1046
1047 prn = (isGPS? sid : sid+200 );
1048
1049 // Carrier phase measurement (mod 2^23 [cy]; sign matched to range)
1050 cph = -P.getBits(iSat*48+40,32)/256.0;
1051
1052 // Is this a new PRN?
1053 idx=-1;
1054 for (unsigned int i=0;i<PRN.size();i++) {
1055 if (PRN[i]==prn) { idx=i; break; };
1056 };
1057 if (idx==-1) {
1058 // Insert new sat at end of list
1059 nSat++; idx = nSat-1;
1060 PRN.push_back(prn);
1061 rng_C1.push_back(0.0);
1062 rng_P1.push_back(0.0);
1063 rng_P2.push_back(0.0);
1064 cph_L1.push_back(0.0);
1065 cph_L2.push_back(0.0);
1066 };
1067
1068 // Store measurement
1069 if (isL1) {
1070 cph_L1[idx] = cph;
1071 }
1072 else {
1073 cph_L2[idx] = cph;
1074 };
1075
1076 };
1077
1078 };
1079
1080
1081 // Process pseudorange message
1082
1083 if ( P.ID()==19 ) {
1084
1085 // Number of satellites in current message
1086 NSat = (P.nDataWords()-1)/2;
1087
1088 // Current epoch (mod 3600 sec)
1089 t = 0.6*P.modZCount()
1090 + P.getUnsignedBits(4,20)*1.0e-6;
1091
1092#if (ROUND_EPOCH==1)
1093 // SC-104 V2.3 4-42 Note 1 4. Assume measurements at hard edges
1094 // of receiver clock with minimum divisions of 10ms
1095 // and clock error less then recommended 1.1ms
1096 // Hence, round time tag to 100 ms
1097 t = floor(t*100.0+0.5)/100.0;
1098#endif
1099
1100 // Frequency (exit if neither L1 nor L2)
1101 isL1 = ( P.getUnsignedBits(0,1)==0 );
1102 isOth = ( P.getUnsignedBits(1,1)==1 );
1103 if (isOth) return;
1104
1105#if (FIX_TRIMBLE_4000SSI==1)
1106 // Fix for data streams originating from TRIMBLE_4000SSI receivers.
1107 // GPS PRN32 is erroneously flagged as GLONASS satellite in the C/A
1108 // pseudorange messages. We therefore use a majority voting to
1109 // determine the true constellation for this message.
1110 // This fix is only required for Trimble4000SSI receivers but can also
1111 // be used with all other known receivers.
1112 int nGPS=0;
1113 for(int iSat=0; iSat<NSat; iSat++){
1114 // Constellation (for each satellite in message)
1115 isGPS = ( P.getUnsignedBits(iSat*48+26,1)==0 );
1116 if(isGPS) nGPS++;
1117 };
1118 isGPS = (2*nGPS>NSat);
1119#else
1120 // Constellation (for first satellite in message)
1121 isGPS = ( P.getUnsignedBits(26,1)==0 );
1122#endif
1123 GPSonly = GPSonly && isGPS;
1124
1125 // Multiple Message Indicator (only checked for first satellite)
1126 // pendingMsg = ( P.getUnsignedBits(24,1)==1 );
1127
1128 // Handle epoch: store epoch of first GPS message and
1129 // check consistency of subsequent messages. GLONASS time tags
1130 // are different and have to be ignored
1131 if (isGPS) {
1132 if ( nSat==0 ) {
1133 secs = t; // Store epoch
1134 }
1135// else if (t!=secs) {
1136 else if (abs(t-secs)>1e-6) {
1137 clear(); secs = t; // Clear all data, then store epoch
1138 };
1139 };
1140
1141 // Discard GLONASS observations if no prior GPS observations
1142 // are available
1143 if (!isGPS && !anyGPS() ) return;
1144
1145 // Set availability flags
1146 if ( isL1 && isGPS) availability.set(bit_L1rngGPS);
1147 if (!isL1 && isGPS) availability.set(bit_L2rngGPS);
1148 if ( isL1 && !isGPS) availability.set(bit_L1rngGLO);
1149 if (!isL1 && !isGPS) availability.set(bit_L2rngGLO);
1150
1151 // Process all satellites
1152
1153 for (int iSat=0;iSat<NSat;iSat++){
1154
1155 // Code type
1156 isCAcode = ( P.getUnsignedBits(iSat*48+25,1)==0 );
1157
1158 // Satellite
1159 sid = P.getUnsignedBits(iSat*48+27,5);
1160 if (sid==0) sid=32;
1161 prn = (isGPS? sid : sid+200 );
1162
1163 // Pseudorange measurement [m]
1164 rng = P.getUnsignedBits(iSat*48+40,32)*0.02;
1165
1166 // Is this a new PRN?
1167 idx=-1;
1168 for (unsigned int i=0;i<PRN.size();i++) {
1169 if (PRN[i]==prn) { idx=i; break; };
1170 };
1171 if (idx==-1) {
1172 // Insert new sat at end of list
1173 nSat++; idx = nSat-1;
1174 PRN.push_back(prn);
1175 rng_C1.push_back(0.0);
1176 rng_P1.push_back(0.0);
1177 rng_P2.push_back(0.0);
1178 cph_L1.push_back(0.0);
1179 cph_L2.push_back(0.0);
1180 };
1181
1182 // Store measurement
1183 if (isL1) {
1184 if (isCAcode) {
1185 rng_C1[idx] = rng;
1186 }
1187 else {
1188 rng_P1[idx] = rng;
1189 }
1190 }
1191 else {
1192 rng_P2[idx] = rng;
1193 };
1194
1195 };
1196
1197 };
1198
1199};
1200
1201//
1202// Resolution of 2^24 cy carrier phase ambiguity
1203// caused by 32-bit data field restrictions
1204//
1205// Note: the RTCM standard specifies an ambiguity of +/-2^23 cy.
1206// However, numerous receivers generate data in the +/-2^22 cy range.
1207// A reduced ambiguity of 2^23 cy appears compatible with both cases.
1208//
1209
1210double RTCM2_Obs::resolvedPhase_L1(int i) const {
1211
1212//const double ambig = pow(2.0,24); // as per RTCM2 spec
1213 const double ambig = pow(2.0,23); // used by many receivers
1214
1215 double rng;
1216 double n;
1217
1218 if (!valid() || i<0 || i>nSat-1) return 0.0;
1219
1220 rng = rng_C1[i];
1221 if (rng==0.0) rng = rng_P1[i];
1222 if (rng==0.0) return 0.0;
1223
1224 n = floor( (rng/lambda_L1-cph_L1[i]) / ambig + 0.5 );
1225
1226 return cph_L1[i] + n*ambig;
1227
1228};
1229
1230double RTCM2_Obs::resolvedPhase_L2(int i) const {
1231
1232//const double ambig = pow(2.0,24); // as per RTCM2 spec
1233 const double ambig = pow(2.0,23); // used by many receivers
1234
1235 double rng;
1236 double n;
1237
1238 if (!valid() || i<0 || i>nSat-1) return 0.0;
1239
1240 rng = rng_C1[i];
1241 if (rng==0.0) rng = rng_P1[i];
1242 if (rng==0.0) return 0.0;
1243
1244 n = floor( (rng/lambda_L2-cph_L2[i]) / ambig + 0.5 );
1245
1246 return cph_L2[i] + n*ambig;
1247
1248};
1249
1250//
1251// Resolution of epoch using reference date (GPS week and secs)
1252//
1253
1254void RTCM2_Obs::resolveEpoch (int refWeek, double refSecs,
1255 int& epochWeek, double& epochSecs ) const {
1256
1257 const double secsPerWeek = 604800.0;
1258
1259 epochWeek = refWeek;
1260 epochSecs = secs + 3600.0*(floor((refSecs-secs)/3600.0+0.5));
1261
1262 if (epochSecs<0 ) { epochWeek--; epochSecs+=secsPerWeek; };
1263 if (epochSecs>secsPerWeek) { epochWeek++; epochSecs-=secsPerWeek; };
1264
1265};
1266
1267}; // End of namespace rtcm2
Note: See TracBrowser for help on using the repository browser.