Skip to content
This repository was archived by the owner on Apr 13, 2021. It is now read-only.

Commit 846b7c3

Browse files
committed
Merge pull request #572 from jacobmcnamee/generalize
Generalize data structures PRN -> SID, SBAS support
2 parents 21e3542 + d1e5b84 commit 846b7c3

File tree

17 files changed

+870
-641
lines changed

17 files changed

+870
-641
lines changed

libswiftnav

src/acq.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@
2929

3030
static BinarySemaphore load_wait_sem;
3131

32-
void acq_set_prn(u8 prn)
32+
void acq_set_sid(gnss_signal_t sid)
3333
{
3434
chBSemInit(&load_wait_sem, TRUE);
35-
nap_acq_code_wr_blocking(prn);
35+
nap_acq_code_wr_blocking(sid);
3636
if (chBSemWaitTimeout(&load_wait_sem, 1000) == RDY_TIMEOUT) {
3737
log_error("acq: Timeout waiting for code load!");
3838
}
3939
}
4040

4141
/** Send results of an acquisition to the host.
4242
*
43-
* \param prn PRN (0-31) of the acquisition
43+
* \param sid SID of the acquisition
4444
* \param snr Signal to noise ratio of best point from acquisition.
4545
* \param cp Code phase of best point.
4646
* \param cf Carrier frequency of best point.

src/acq.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include <libswiftnav/common.h>
1717
#include <libswiftnav/signal.h>
1818

19-
void acq_set_prn(u8 prn);
19+
void acq_set_sid(gnss_signal_t sid);
2020

2121
bool acq_load(u32 count);
2222
void acq_service_load_done(void);

src/base_obs.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <libswiftnav/ephemeris.h>
2121
#include <libswiftnav/coord_system.h>
2222
#include <libswiftnav/linear_algebra.h>
23+
#include <libswiftnav/signal.h>
2324

2425
#include "board/leds.h"
2526
#include "position.h"
@@ -289,19 +290,18 @@ static void obs_callback(u16 sender_id, u8 len, u8 msg[], void* context)
289290
/* Pull out the contents of the message. */
290291
packed_obs_content_t *obs = (packed_obs_content_t *)(msg + sizeof(observation_header_t));
291292
for (u8 i=0; i<obs_in_msg; i++) {
292-
/* Check the PRN is valid. e.g. simulation mode outputs test observations
293-
* with PRNs >200. */
294-
if (obs[i].sid.sat > 31) { /* TODO prn - sid; assume everything below is 0x1F masked! */
293+
gnss_signal_t sid = sid_from_sbp(obs[i].sid);
294+
if (!sid_valid(sid))
295295
continue;
296-
}
297296

298297
/* Flag this as visible/viable to acquisition/search */
299-
manage_set_obs_hint(sid_from_sbp(obs[i].sid));
298+
manage_set_obs_hint(sid);
300299

301300
/* Check if we have an ephemeris for this satellite, we will need this to
302301
* fill in satellite position etc. parameters. */
303-
chMtxLock(&es_mutex);
304-
if (ephemeris_good(&es[obs[i].sid.sat], t)) {
302+
ephemeris_lock();
303+
ephemeris_t *e = ephemeris_get(sid);
304+
if (ephemeris_good(e, t)) {
305305
/* Unpack the observation into a navigation_measurement_t. */
306306
unpack_obs_content(
307307
&obs[i],
@@ -314,7 +314,7 @@ static void obs_callback(u16 sender_id, u8 len, u8 msg[], void* context)
314314
double clock_err;
315315
double clock_rate_err;
316316
/* Calculate satellite parameters using the ephemeris. */
317-
calc_sat_state(&es[obs[i].sid.sat], t,
317+
calc_sat_state(e, t,
318318
base_obss_rx.nm[base_obss_rx.n].sat_pos,
319319
base_obss_rx.nm[base_obss_rx.n].sat_vel,
320320
&clock_err, &clock_rate_err);
@@ -327,7 +327,7 @@ static void obs_callback(u16 sender_id, u8 len, u8 msg[], void* context)
327327
base_obss_rx.nm[base_obss_rx.n].tot = t;
328328
base_obss_rx.n++;
329329
}
330-
chMtxUnlock();
330+
ephemeris_unlock();
331331
}
332332

333333
/* If we can, and all the obs have been received, update to using the new

src/board/nap/acq_channel.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ void nap_acq_corr_rd_blocking(u16 *index, u16 *max, float *ave)
127127
* CA Code for SV to be searched for must be written into channel's code ram
128128
* before acquisitions are started.
129129
*
130-
* \param prn PRN number (0-31) of CA code to be written.
130+
* \param sid Signal identifier corresponding to CA code to be written.
131131
*/
132-
void nap_acq_code_wr_blocking(u8 prn)
132+
void nap_acq_code_wr_blocking(gnss_signal_t sid)
133133
{
134-
nap_xfer_blocking(NAP_REG_ACQ_CODE, 128, 0, ca_code(prn));
134+
nap_xfer_blocking(NAP_REG_ACQ_CODE, 128, 0, ca_code(sid));
135135
}
136136

137137
/** \} */

src/board/nap/acq_channel.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define SWIFTNAV_ACQ_CHANNEL_H
1616

1717
#include <libswiftnav/common.h>
18+
#include <libswiftnav/signal.h>
1819

1920
#include "../../main.h"
2021
#include "nap_common.h"
@@ -51,7 +52,7 @@ void nap_acq_load_wr_disable_blocking(void);
5152
void nap_acq_init_wr_params_blocking(s16 carrier_freq);
5253
void nap_acq_init_wr_disable_blocking(void);
5354
void nap_acq_corr_rd_blocking(u16 *index, u16 *max, float *ave);
54-
void nap_acq_code_wr_blocking(u8 prn);
55+
void nap_acq_code_wr_blocking(gnss_signal_t sid);
5556

5657
#endif /* SWIFTNAV_ACQ_CHANNEL_H */
5758

src/board/nap/track_channel.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,10 @@ void nap_track_phase_rd_blocking(u8 channel, s32* carrier_phase,
221221
*
222222
* \param prn PRN number (0-31) of CA code to be written.
223223
*/
224-
void nap_track_code_wr_blocking(u8 channel, u8 prn)
224+
void nap_track_code_wr_blocking(u8 channel, gnss_signal_t sid)
225225
{
226226
nap_xfer_blocking(NAP_REG_TRACK_BASE + channel * NAP_TRACK_N_REGS
227-
+ NAP_REG_TRACK_CODE_OFFSET, 128, 0, ca_code(prn));
227+
+ NAP_REG_TRACK_CODE_OFFSET, 128, 0, ca_code(sid));
228228
}
229229

230230
/** \} */

src/board/nap/track_channel.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define SWIFTNAV_TRACK_CHANNEL_H
1616

1717
#include <libswiftnav/common.h>
18+
#include <libswiftnav/signal.h>
1819

1920
#include "../../main.h"
2021
#include "nap_common.h"
@@ -76,7 +77,7 @@ void nap_track_corr_rd_blocking(u8 channel, u32* sample_count, corr_t corrs[]);
7677
void nap_track_phase_unpack(u8 packed[], s32* carrier_phase, u64* code_phase);
7778
void nap_track_phase_rd_blocking(u8 channel, s32* carrier_phase,
7879
u64* code_phase);
79-
void nap_track_code_wr_blocking(u8 channel, u8 prn);
80+
void nap_track_code_wr_blocking(u8 channel, gnss_signal_t sid);
8081

8182
#endif /* SWIFTNAV_TRACK_CHANNEL_H */
8283

src/ephemeris.c

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <libswiftnav/ephemeris.h>
1616
#include <libswiftnav/logging.h>
1717
#include <ch.h>
18+
#include <assert.h>
1819

1920
#include "sbp.h"
2021
#include "sbp_utils.h"
@@ -23,32 +24,35 @@
2324
#include "ephemeris.h"
2425

2526
MUTEX_DECL(es_mutex);
26-
ephemeris_t es[MAX_SATS] _CCM;
27-
static ephemeris_t es_candidate[MAX_SATS] _CCM;
27+
static ephemeris_t es[NUM_SATS] _CCM;
28+
static ephemeris_t es_candidate[NUM_SATS] _CCM;
2829

2930
static void ephemeris_new(ephemeris_t *e)
3031
{
32+
assert(sid_valid(e->sid));
33+
3134
gps_time_t t = get_current_time();
32-
if (!ephemeris_good(&es[e->sid.sat], t)) {
35+
u32 index = sid_to_index(e->sid);
36+
if (!ephemeris_good(&es[index], t)) {
3337
/* Our currently used ephemeris is bad, so we assume this is better. */
3438
log_info("New untrusted ephemeris for PRN %02d", e->sid.sat+1);
35-
chMtxLock(&es_mutex);
36-
es[e->sid.sat] = es_candidate[e->sid.sat] = *e;
37-
chMtxUnlock();
39+
ephemeris_lock();
40+
es[index] = es_candidate[index] = *e;
41+
ephemeris_unlock();
3842

39-
} else if (ephemeris_equal(&es_candidate[e->sid.sat], e)) {
43+
} else if (ephemeris_equal(&es_candidate[index], e)) {
4044
/* The received ephemeris matches our candidate, so we trust it. */
4145
log_info("New trusted ephemeris for PRN %02d", e->sid.sat+1);
42-
chMtxLock(&es_mutex);
43-
es[e->sid.sat] = *e;
44-
chMtxUnlock();
46+
ephemeris_lock();
47+
es[index] = *e;
48+
ephemeris_unlock();
4549
} else {
4650
/* This is our first reception of this new ephemeris, so treat it with
4751
* suspicion and call it the new candidate. */
4852
log_info("New ephemeris candidate for PRN %02d", e->sid.sat+1);
49-
chMtxLock(&es_mutex);
50-
es_candidate[e->sid.sat] = *e;
51-
chMtxUnlock();
53+
ephemeris_lock();
54+
es_candidate[index] = *e;
55+
ephemeris_unlock();
5256
}
5357
}
5458

@@ -86,11 +90,12 @@ static msg_t nav_msg_thread(void *arg)
8690
/* Decoded a new ephemeris. */
8791
ephemeris_new(&e);
8892

89-
if (!es[ch->sid.sat].healthy) {
93+
ephemeris_t *eph = ephemeris_get(ch->sid);
94+
if (!eph->healthy) {
9095
log_info("PRN %02d unhealthy", ch->sid.sat+1);
9196
} else {
9297
msg_ephemeris_t msg;
93-
pack_ephemeris(&es[ch->sid.sat], &msg);
98+
pack_ephemeris(eph, &msg);
9499
sbp_send_msg(SBP_MSG_EPHEMERIS, sizeof(msg_ephemeris_t), (u8 *)&msg);
95100
}
96101
}
@@ -110,7 +115,7 @@ static void ephemeris_msg_callback(u16 sender_id, u8 len, u8 msg[], void* contex
110115

111116
ephemeris_t e;
112117
unpack_ephemeris((msg_ephemeris_t *)msg, &e);
113-
if (e.sid.sat >= MAX_SATS) {
118+
if (!sid_valid(e.sid)) {
114119
log_warn("Ignoring ephemeris for invalid sat");
115120
return;
116121
}
@@ -122,8 +127,8 @@ void ephemeris_setup(void)
122127
{
123128
memset(es_candidate, 0, sizeof(es_candidate));
124129
memset(es, 0, sizeof(es));
125-
for (u8 i=0; i<32; i++) {
126-
es[i].sid.sat = i;
130+
for (u32 i=0; i<NUM_SATS; i++) {
131+
es[i].sid = sid_from_index(i);
127132
}
128133

129134
static sbp_msg_callbacks_node_t ephemeris_msg_node;
@@ -137,3 +142,19 @@ void ephemeris_setup(void)
137142
NORMALPRIO-1, nav_msg_thread, NULL);
138143
}
139144

145+
void ephemeris_lock(void)
146+
{
147+
chMtxLock(&es_mutex);
148+
}
149+
150+
void ephemeris_unlock(void)
151+
{
152+
Mutex *m = chMtxUnlock();
153+
assert(m == &es_mutex);
154+
}
155+
156+
ephemeris_t *ephemeris_get(gnss_signal_t sid)
157+
{
158+
assert(sid_valid(sid));
159+
return &es[sid_to_index(sid)];
160+
}

src/ephemeris.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515

1616
#include <libswiftnav/constants.h>
1717
#include <libswiftnav/ephemeris.h>
18-
19-
extern Mutex es_mutex;
20-
extern ephemeris_t es[MAX_SATS];
18+
#include <libswiftnav/signal.h>
2119

2220
void ephemeris_setup(void);
21+
void ephemeris_lock(void);
22+
void ephemeris_unlock(void);
23+
ephemeris_t *ephemeris_get(gnss_signal_t sid);
2324

2425
#endif
2526

0 commit comments

Comments
 (0)