//////////////////////////////////////////////////////////////////////////
// Homemade GPS Receiver
// Copyright (C) 2013 Andrew Holme
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//
// http://www.aholme.co.uk/GPS/Main.htm
//////////////////////////////////////////////////////////////////////////
#include
#include
#include
#include "gps.h"
#include "ephemeris.h"
#include "spi.h"
#define MAX_ITER 20
#define WGS84_A (6378137.0)
#define WGS84_F_INV (298.257223563)
#define WGS84_B (6356752.31424518)
#define WGS84_E2 (0.00669437999014132)
///////////////////////////////////////////////////////////////////////////////////////////////
struct SNAPSHOT {
EPHEM eph;
float power;
int ch, sv, ms, bits, g1, ca_phase;
bool LoadAtomic(int ch, uint16_t *up, uint16_t *dn);
double GetClock();
};
static SNAPSHOT Replicas[NUM_CHANS];
///////////////////////////////////////////////////////////////////////////////////////////////
// Gather channel data and consistent ephemerides
bool SNAPSHOT::LoadAtomic(int ch_, uint16_t *up, uint16_t *dn) {
/* Called inside atomic section - yielding not allowed */
if (ChanSnapshot(
ch_, // in: channel id
up[1], // in: FPGA circular buffer pointer
&sv, // out: satellite id
&bits, // out: total bits held locally (CHANNEL struct) + remotely (FPGA)
&power) // out: received signal strength ^ 2
&& Ephemeris[sv].Valid()) {
ms = up[0];
g1 = dn[0] & 0x3FF;
ca_phase = dn[0] >> 10;
memcpy(&eph, Ephemeris+sv, sizeof eph);
return true;
}
else
return false; // channel not ready
}
///////////////////////////////////////////////////////////////////////////////////////////////
static int LoadAtomic() {
const int WPC=3;
SPI_MISO clocks;
int chans=0;
// Yielding to other tasks not allowed after spi_hog returns
spi_hog(CmdGetClocks, &clocks, 2+NUM_CHANS*WPC*2);
uint16_t srq = clocks.word[0]; // un-serviced epochs
uint16_t *up = clocks.word+1; // Embedded CPU memory
uint16_t *dn = clocks.word+WPC*NUM_CHANS; // FPGA clocks (in reverse order)
for (int ch=0; ch>=1, up+=WPC, dn-=WPC) {
up[0] += (srq&1); // add 1ms for un-serviced epochs
if (Replicas[chans].LoadAtomic(ch,up,dn))
Replicas[chans++].ch=ch;
}
// Safe to yield again ...
return chans;
}
///////////////////////////////////////////////////////////////////////////////////////////////
static int LoadReplicas() {
const int GLITCH_GUARD=500;
SPI_MISO glitches[2];
// Get glitch counters "before"
spi_get(CmdGetGlitches, glitches+0, NUM_CHANS*2);
TimerWait(GLITCH_GUARD);
// Gather consistent snapshot of all channels
int pass1 = LoadAtomic();
int pass2 = 0;
// Get glitch counters "after"
TimerWait(GLITCH_GUARD);
spi_get(CmdGetGlitches, glitches+1, NUM_CHANS*2);
// Strip noisy channels
for (int i=0; ipass2) memcpy(Replicas+pass2, Replicas+i, sizeof(SNAPSHOT));
pass2++;
}
return pass2;
}
///////////////////////////////////////////////////////////////////////////////////////////////
double SNAPSHOT::GetClock() {
// Find 10-bit shift register in 1023 state sequence
int chips = SearchCode(sv, g1);
// TOW refers to leading edge of next (un-processed) subframe.
// Channel.cpp processes NAV data up to the subframe boundary.
// Un-processed bits remain in holding buffers.
return // Un-corrected satellite clock
eph.tow * 6 + // Time of week in seconds
bits / BPS + // NAV data bits buffered
ms * 1e-3 + // Milliseconds since last bit (0...20)
chips / CPS + // Code chips (0...1022)
ca_phase * pow(2, -6) / CPS; // Code NCO phase
}
///////////////////////////////////////////////////////////////////////////////////////////////
static int Solve(int chans, double *x_n, double *y_n, double *z_n, double *t_bias) {
int i, j, r, c;
double t_tx[NUM_CHANS]; // Clock replicas in seconds since start of week
double x_sv[NUM_CHANS],
y_sv[NUM_CHANS],
z_sv[NUM_CHANS];
double t_pc; // Uncorrected system time when clock replica snapshots taken
double t_rx; // Corrected GPS time
double dPR[NUM_CHANS]; // Pseudo range error
double jac[NUM_CHANS][4], ma[4][4], mb[4][4], mc[4][NUM_CHANS], md[4];
double weight[NUM_CHANS];
*x_n = *y_n = *z_n = *t_bias = t_pc = 0;
for (i=0; i