This repository was archived by the owner on May 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathRX.cpp
More file actions
675 lines (579 loc) · 20.6 KB
/
RX.cpp
File metadata and controls
675 lines (579 loc) · 20.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
//============================================================================
//
// This file is part of GPSTk, the GPS Toolkit.
//
// The GPSTk is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 3.0 of the License, or
// any later version.
//
// The GPSTk is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with GPSTk; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
//
// Copyright 2004, The University of Texas at Austin
//
//============================================================================
//============================================================================
//
//This software developed by Applied Research Laboratories at the University of
//Texas at Austin, under contract to an agency or agencies within the U.S.
//Department of Defense. The U.S. Government retains all rights to use,
//duplicate, distribute, disclose, or release this software.
//
//Pursuant to DoD Directive 523024
//
// DISTRIBUTION STATEMENT A: This software has been approved for public
// release, distribution is unlimited.
//
//=============================================================================
/*
First shot at integrating position solutions into the tracking algorithm.
*/
/*
COMPILE:
g++ -c -o RX.o -O -I. -I/.../gpstk/dev/apps/swrx -I/.../gpstk/dev/src RX.cpp
g++ -o RX RX.o /.../gpstk/dev/apps/swrx/simlib.a /.../gpstk/dev/src/libgpstk.a -lm -lstdc++ -lfftw3 -lm -lpthread
SAMPLE USAGE:
hilbert -i data.bin | ./RX -b 1 -q 2 -x 4.13 -r 8.184 -c c:1:30:416.789:-8800 -c c:1:2:410.191:-8300 -c c:1:10:836.144:-9000 -c c:1:15:174.609:-4000 -c c:1:18:255.254:-3500 -c c:1:24:183.284:-5700 -c c:1:26:907.747:-4300 -c c:1:29:355.327:-5400 -p 1 -e rin269.08n -w 1498
*/
#include <math.h>
#include <complex>
#include <iostream>
#include <list>
#include <pthread.h>
#include "BasicFramework.hpp"
#include "CommandOption.hpp"
#include "StringUtils.hpp"
#include "GNSSconstants.hpp"
#include "EngNav.hpp"
#include "EMLTracker.hpp"
#include "CCReplica.hpp"
#include "CACodeGenerator.hpp"
#include "PCodeGenerator.hpp"
#include "complex_math.h"
#include "IQStream.hpp"
#include "NavFramer.hpp"
#include "CommonTime.hpp"
#include "GPSEphemerisStore.hpp"
#include "RinexNavStream.hpp"
#include "RinexNavData.hpp"
#include "GGTropModel.hpp"
#include "IonoModel.hpp"
#include "GPSEllipsoid.hpp"
#include "PRSolution2.hpp"
using namespace gpstk;
using namespace std;
/* exp10() is a gnu specific extensions */
#ifndef exp10
#define exp10(x) (exp((x)*log(10.)))
#endif
struct Buffer // input buffer
{
vector< complex<float> > arr;
};
struct Par // Parameters to pass to Pthread function.
{
int dp;
int bufferSize;
EMLTracker *tr;
Buffer *s;
int *count;
NavFramer *nf;
bool v; // verbose
int prn;
bool solvePos;
};
extern "C" void *Cfunction(void*); // C-style function to be called with pthreads
pthread_mutex_t mutexVec = PTHREAD_MUTEX_INITIALIZER;
int dataPoints[32]; // variables needed by position algorithm
bool edgeFound;
int ZCount;
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
class RxSim : public BasicFramework
{
public:
RxSim() throw();
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Woverloaded-virtual"
bool initialize(int argc, char *argv[]) throw();
#pragma clang diagnostic pop
void function(EMLTracker *tr, int dp, int *count,
complex<double> s, NavFramer *nf);
protected:
virtual void process();
private:
CCReplica* cc;
vector<EMLTracker*> tr;
int band;
double gain;
bool fakeL2;
int sat;
double timeStep; // Time between samples
double interFreq; // Intermediate frequency from receive
double timeLimit;
IQStream *input;
unsigned iadMax;
int numTrackers;
bool solvePos; // If false, no position solutions will be performed (default)
string ephFile; // Parameters for position solution.
int gpsWeek; // (we don't currently generate our own eph file).
};
//-----------------------------------------------------------------------------
RxSim::RxSim() throw() :
BasicFramework("rxSim", "A simulation of a gps receiver."),
cc(NULL), tr(0), band(1), gain(1), fakeL2(false),
timeStep(50e-9), interFreq(0.42e6),
timeLimit(9e99), input(NULL), iadMax(20460), solvePos(false)
{}
bool RxSim::initialize(int argc, char *argv[]) throw()
{
using namespace gpstk::StringUtils;
CommandOptionWithAnyArg
codeOpt('c', "code",
"The code/carrier to track. ARG takes the form of "
"code:carrier:prn:offset:doppler. Code is either c or p. "
"Carrier is either 1 or 2. Prn is an integer between 1 and 32. "
"Offset is a number in us, Doppler is a number in Hz. Currently, "
"only one signal can be specified. For example, to track P code "
"on L2 for PRN 3, with no initial time or doppler offset, "
"specify -c p:2:3:0:0", true),
dllAlphaOpt('\0', "dllAlpha",
"The gain on the phase update for the code tracker. The "
"default is 1e-5 chips/tick"),
dllBetaOpt('\0', "dllBeta",
"The gain on the frequency update for the code tracker. The "
"default is 1e-12 chips/tick"),
pllAlphaOpt('\0', "pllAlpha",
"The gain on the phase update for the carrier tracker. The "
"default is 0.4 cycles/tick"),
pllBetaOpt('\0', "pllBeta",
"The gain on the frequency update for the carrier tracker. "
"The default is 0.1 cycles / iad_period"),
sampleRateOpt('r',"sample-rate",
"Specifies the nominal sample rate, in MHz. The "
"default is 20 MHz."),
interFreqOpt('x',"inter-freq",
"Specifies the intermediate frequency of the receiver,"
" in MHz. Default is 0.42 MHz. If there is no down-"
"conversion, the IF should be the L1 or L2 carrier"
" frequency" ),
quantizationOpt('q', "quantization",
"They quantization applied to the data. 1, 2 or f. "
"The default is f."),
gainOpt('g', "gain",
"Gain to apply to the if prior to digitization, in dB. Default is 0."),
timeLimitOpt('t', "time-limit",
"Limit the amount of data to process. Specify time in ms. Defaults to all data."),
inputOpt('i', "input",
"Where to get the IQ samples from. The default is to use stdin."),
posOpt('p',"prsolve", "Solve for position solution every subframe."
" An ephemeris file and gpsWeek number must also be "
"supplied if this option is selected."
" Any value can be entered, e.g. '-p 7'"),
ephFileOption('e', "ephemeris", "Rinex Ephemeris data file name "
"for position solution."),
gpsWeekOpt('w',"gps-week",
"The GPSWeek, used for position solution.")
;
CommandOptionWithNumberArg
bandsOpt('b', "bands",
"The number of complex samples per epoch. The default is 2.");
if (!BasicFramework::initialize(argc,argv))
return false;
if (timeLimitOpt.getCount())
timeLimit = asDouble(timeLimitOpt.getValue()[0]) * 1e-3;
if (!codeOpt.getCount())
{
cout << "Must specify a code/carrier to track. Bye." << endl;
return false;
}
if (sampleRateOpt.getCount())
timeStep = 1/(asDouble(sampleRateOpt.getValue().front()) * 1e6 );
if (interFreqOpt.getCount())
interFreq = asDouble(interFreqOpt.getValue().front()) * 1e6;
if (posOpt.getCount())
{
solvePos = true;
if(!gpsWeekOpt.getCount() || !ephFileOption.getCount())
{
cout << "If solving for position (-p option), you must specify both "
<< "an ephemeris file and the gpsWeek (-e and -w options). "
<< "Exiting." << endl;
return false;
}
}
if(gpsWeekOpt.getCount())
{
gpsWeek = asInt(gpsWeekOpt.getValue().front());
}
if(ephFileOption.getCount())
{
ephFile = ephFileOption.getValue()[0];
}
numTrackers = codeOpt.getCount();
for (int i=0; i < (int)codeOpt.getCount(); i++)
{
string val=codeOpt.getValue()[i];
const char delim(':');
if (numWords(val, delim) != 5)
{
cout << "Error in code parameter:" << val << endl;
return false;
}
string code = lowerCase(word(val, 0, delim));
band = asInt(word(val, 1, delim));
int prn = asInt(word(val, 2, delim));
double offset = asDouble(word(val, 3, delim)) * 1e-6;
double doppler = asDouble(word(val, 4, delim));
CodeGenerator* codeGenPtr;
double chipFreq;
switch (code[0])
{
case 'c':
codeGenPtr = new CACodeGenerator(prn);
chipFreq = CA_CHIP_FREQ_GPS;
break;
case 'p':
codeGenPtr = new PCodeGenerator(prn);
chipFreq = PY_CHIP_FREQ_GPS;
break;
default:
cout << "Unsupported code: " << code << endl;
return false;
}
// Note that this object is responsible for destroying
// the codeGenPtr object
cc = new CCReplica(timeStep, chipFreq, interFreq, codeGenPtr);
double chips = offset / cc->codeChipLen;
cc->moveCodePhase(chips);
cc->setCodeFreqOffsetHz(doppler);
cc->setCarrierFreqOffsetHz(doppler);
double spacing = 0.5 * cc->codeChipLen;
if (spacing < timeStep)
spacing = timeStep;
tr[i] = new EMLTracker(*cc, spacing);
if (dllAlphaOpt.getCount())
tr[i]->dllAlpha = asDouble(dllAlphaOpt.getValue()[0]);
if (dllBetaOpt.getCount())
tr[i]->dllBeta = asDouble(dllBetaOpt.getValue()[0]);
if (pllAlphaOpt.getCount())
tr[i]->pllAlpha = asDouble(pllAlphaOpt.getValue()[0]);
if (pllBetaOpt.getCount())
tr[i]->pllBeta = asDouble(pllBetaOpt.getValue()[0]);
tr[i]->prn = prn;
tr[i]->debugLevel = debugLevel;
if(verboseLevel)
tr[i]->dump(cout, 1);
}
char quantization='f';
if (quantizationOpt.getCount())
quantization = quantizationOpt.getValue()[0][0];
switch (quantization)
{
case '1': input = new IQ1Stream(); break;
case '2': input = new IQ2Stream(); break;
case 'f':
default: input = new IQFloatStream(); break;
}
if (inputOpt.getCount())
{
input->open(inputOpt.getValue()[0].c_str());
}
else
{
using std::basic_ios;
input->copyfmt(std::cin);
input->clear(std::cin.rdstate());
input->basic_ios<char>::rdbuf(std::cin.rdbuf());
input->filename = "<stdin>";
}
if (bandsOpt.getCount())
input->bands = asInt(bandsOpt.getValue()[0]);
if (gainOpt.getCount())
{
double gainDb = StringUtils::asDouble(gainOpt.getValue()[0]);
gain = exp10(gainDb/10.);
}
if (verboseLevel)
{
cout << "# Taking input from " << input->filename
<< " (" << input->bands << " samples/epoch)" << endl
<< "# Rx gain level: " << gain << endl;
}
return true;
}
//-----------------------------------------------------------------------------
void RxSim::process()
{
pthread_t *thread_id = new pthread_t[numTrackers];
pthread_attr_t attr;
int rc;
void *status;
vector<Par> p(numTrackers);
vector<NavFramer> nf(numTrackers);
long int dataPoint =0;
vector<int> count(numTrackers);
for(int i=0;i<numTrackers;i++)
{
nf[i].debugLevel = debugLevel;
nf[i].dump(cout);
count[i]=0;
}
complex<float> s;
while (*input >> s)
{
Buffer b;
b.arr.push_back(s);
dataPoint++;
int index = 0;
int bufferSize = 40*16367;
while(index < bufferSize) // Fill input buffer
{
*input >> s;
b.arr.push_back(s);
index++;
dataPoint++;
}
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
edgeFound = false;
for(int i = 0; i < numTrackers; i++)
{
p[i].dp = dataPoint; // Set parameters for each tracker.
p[i].bufferSize = bufferSize;
p[i].s = &b;
p[i].count = &count[i];
p[i].tr = tr[i];
p[i].nf = &nf[i];
p[i].v = (verboseLevel);
p[i].prn = tr[i]->prn;
p[i].solvePos = solvePos;
// Split
rc = pthread_create( &thread_id[i], &attr, Cfunction, &p[i] ) ;
if (rc)
{
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
// Join
for(int i = 0; i < numTrackers; i++)
{
rc = pthread_join( thread_id[i], &status) ;
if (rc)
{
printf("ERROR; return code from pthread_join() is %d\n", rc);
exit(-1);
}
}
if(edgeFound == true)
{
//---------------------------------------------------------------------------
// Position Solution
if(solvePos == true)
{
GPSEphemerisStore bce;
IonoModel iono;
CommonTime time;
double zCount = (double)ZCount - 6.0;
double sampleRate = 1/timeStep;
GPSEllipsoid gm;
vector<SatID> svVec;
vector<double> ionoVec;
Triple antennaPos;
time = GPSWeekZcount(gpsWeek, zCount);
RinexNavStream rns(ephFile.c_str(), ios::in);
rns.exceptions(ifstream::failbit);
RinexNavHeader hdr;
rns >> hdr;
iono = IonoModel(hdr.ionAlpha, hdr.ionBeta);
RinexNavData rnd;
while (rns >> rnd)
bce.addEphemeris(rnd);
if (time < bce.getInitialTime() || time > bce.getFinalTime())
cout << "Warning: Initial time does not appear to be "
<< "within the provided ephemeris data." << endl;
for (int i=1; i < 33; i++)
{
SatID sv(i, SatID::systemGPS);
svVec.push_back(sv);
}
float refDataPoint;
long int total = 0;
int numberSVs = 0;
for(int i=0; i<32;i++)
{
total += dataPoints[i];
if(dataPoints[i] != 0)
numberSVs++;
}
refDataPoint = total/numberSVs;
vector<double> obsVec(32);
for(int i=0; i<32; i++)
{
if(dataPoints[i] != 0)
{
// 0.073 is an arbitrary guessed time of flight
obsVec[i] = gpstk::C_MPS*(0.073 - (refDataPoint -
dataPoints[i])/(sampleRate)); //*2 because of hilbert
}
else
{
SatID temp(0, SatID::systemGPS);
svVec[i] = temp; // set SatID equal to 0,
//the SV won't be considered
}
}
// Calculate initial position solution.
GGTropModel gg;
gg.setWeather(30., 1000., 50.);
PRSolution2 prSolver;
prSolver.RMSLimit = 400;
prSolver.RAIMCompute(time, svVec, obsVec, bce, &gg);
Vector<double> sol = prSolver.Solution;
cout << endl << "Position (ECEF): " << fixed << sol[0]
<< " " << sol[1]
<< " " << sol[2] << endl;
time -= (sol[3] / gpstk::C_MPS);
cout << "Time: " << time << endl;
//cout << "Clock Error (includes that caused by guess): "
//<< sol[3]*1000/gpstk::C_MPS << " ms" << endl;
cout << "# good SV's: " << prSolver.Nsvs << endl
<< "RMSResidual: " << prSolver.RMSResidual << " meters"
<< endl;
// If we wanted to just output ranges, we can correct the obsVector
// using the clock error and have the range to each sat.
for(int i = 0; i < 32; i++)
dataPoints[i] = 0;
// Calculate Ionosphere correction.
/* antennaPos[0] = sol[0];
antennaPos[1] = sol[1];
antennaPos[2] = sol[2];
ECEF ecef(antennaPos);
for (int i=1; i<=32; i++)
{
SatID sv(i, SatID::systemGPS);
try
{
Xvt svpos = bce.getXvt(sv, time);
double el = antennaPos.elvAngle(svpos.x);
double az = antennaPos.azAngle(svpos.x);
double ic = iono.getCorrection(time, ecef, el, az); // in meters
ionoVec.push_back(ic);
}
catch (Exception& e)
{}
}
if(verboseLevel)
{
for(int i = 0; i < 32; i++)
{
cout << svVec[i] << " " << obsVec[i] << " " << ionoVec[i] << endl;
}
}
for(int i=0;i<32;i++)
{
obsVec[i] -= sol[3]; // convert pseudoranges to ranges
obsVec[i] += ionoVec[i]; // make iono correction to ranges.
}*/
// Then plug back into RAIMCompute...
}
}
//---------------------------------------------------------------------------
if (cc->localTime > timeLimit)
break;
}
delete thread_id;
pthread_attr_destroy(&attr);
pthread_exit(NULL);
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
int main(int argc, char *argv[])
{
try
{
RxSim crap;
if (!crap.initialize(argc, argv))
exit(0);
crap.run();
}
catch (gpstk::Exception &exc)
{ cerr << exc << endl; }
catch (std::exception &exc)
{ cerr << "Caught std::exception " << exc.what() << endl; }
catch (...)
{ cerr << "Caught unknown exception" << endl; }
}
void *Cfunction(void* p)
{
Par *par = (Par*)p;
EMLTracker *tr = par->tr;
int *count = par->count;
NavFramer *nf = par->nf;
int bufferSize = par->bufferSize;
int dp = par->dp - bufferSize;
Buffer *b = par->s;
bool v = par->v;
int prn = par->prn;
bool solvePos = par->solvePos;
int index = 0;
pthread_mutex_lock (&mutexVec);
edgeFound = false;
pthread_mutex_unlock (&mutexVec);
while(index < bufferSize + 1) // number of data points to track before join.
{
if (tr->process(b->arr[index]))
{
if(v)
tr->dump(cout);
if(tr->navChange)
{
if(nf->process(*tr, dp,
(float)tr->localReplica.getCodePhaseOffsetSec()*1e6))
{
pthread_mutex_lock (&mutexVec);
dataPoints[prn-1] = nf->subframes.back().dataPoint;
edgeFound = true;
ZCount = EngNav::getHOWTime(nf->subframes.back().words[1]);
pthread_mutex_unlock (&mutexVec);
if(!solvePos)
{nf->subframes.back().dump(cout,1);}
/*cout << "DataPoint: " << nf->subframes.back().dataPoint
<< "Zcount: "
<< EngNav::getHOWTime(nf->subframes.back().words[1])
<< */
}
*count = 0;
}
if(*count == 20)
// The *20* depends on the EMLtracker updating every C/A period.
{
*count = 0;
if(nf->process(*tr, dp,
(float)tr->localReplica.getCodePhaseOffsetSec()*1e6))
{
pthread_mutex_lock (&mutexVec);
dataPoints[prn-1] = nf->subframes.back().dataPoint;
edgeFound = true;
ZCount = EngNav::getHOWTime(nf->subframes.back().words[1]);
pthread_mutex_unlock (&mutexVec);
if(!solvePos)
{nf->subframes.back().dump(cout,0);}
}
}
*count = *count + 1;
}
index++;
dp++;
}
pthread_exit((void*) 0);
return NULL;
}