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

Commit

Permalink
verify that loaded almanac is valid (within a certain week number range)
Browse files Browse the repository at this point in the history
Invalidate almanac in memory if invalid for a particular PRN
  • Loading branch information
denniszollo committed Jul 2, 2015
1 parent ec4be56 commit e23265c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
44 changes: 32 additions & 12 deletions src/manage.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <math.h>
#include <string.h>
#include <stdlib.h>

#include <ch.h>

Expand Down Expand Up @@ -88,7 +89,8 @@ static void almanac_callback(u16 sender_id, u8 len, u8 msg[], void* context)

almanac_t *new_almanac = (almanac_t*)msg;

log_info("Received alamanc for PRN %02d\n", new_almanac->prn);
log_info("Received alamanc for PRN %02d with week %03d\n", new_almanac->prn,
new_almanac->week);
memcpy(&almanac[new_almanac->prn-1], new_almanac, sizeof(almanac_t));

int fd = cfs_open("almanac", CFS_WRITE);
Expand Down Expand Up @@ -129,26 +131,36 @@ void manage_acq_setup()
acq_prn_param[prn].dopp_hint_low = ACQ_FULL_CF_MIN;
acq_prn_param[prn].dopp_hint_high = ACQ_FULL_CF_MAX;
}

int fd = cfs_open("almanac", CFS_READ);
if (fd != -1) {
cfs_read(fd, almanac, 32*sizeof(almanac_t));
log_info("Loaded almanac from flash\n");
u16 wn = 0;
for (u8 prn=0; prn<32; prn++){
/* get the week number for the first valid satellite to report */
if(almanac[prn].valid) {
wn=almanac[prn].week;
break;
}
}
/* we only print that the almanac was loaded if there was a valid sat within */
if (wn != 0) {
log_info("Loaded almanac from flash with week number %03d\n", wn);
}
cfs_close(fd);
} else {
}
else {
log_info("No almanac file present in flash, create an empty one\n");
cfs_coffee_reserve("almanac", 32*sizeof(almanac_t));
cfs_coffee_configure_log("almanac", 256, sizeof(almanac_t));

/* set all satellites to invalid when creating a placeholder */
for (u8 prn=0; prn<32; prn++) {
almanac[prn].valid = 0;
almanac[prn].valid = 0;
}
}

sbp_register_cbk(
SBP_MSG_ALMANAC,
&almanac_callback,
&almanac_callback_node
sbp_register_cbk(
SBP_MSG_ALMANAC,
&almanac_callback,
&almanac_callback_node
);

chThdCreateStatic(
Expand All @@ -171,8 +183,16 @@ static void manage_calc_almanac_scores(void)
/* No almanac or position/time information, give it the benefit of the
* doubt. */
acq_prn_param[prn].score[ACQ_HINT_ALMANAC] = 0;
} else if (almanac[prn].valid && time_quality > TIME_COARSE &&
(abs(almanac[prn].week - t.wn%1024) > MAX_ALM_WEEK_DIFF)) {
/* If our almanac is out of date with respect to a time estimate of
* coarse or better, we invalidate it */
log_info("Almanac for prn %i and week %i is out of date, invalidating it.",
prn, almanac[prn].week);
almanac[prn].valid = 0;
} else {
calc_sat_az_el_almanac(&almanac[prn], t.tow, t.wn-1024, position_solution.pos_ecef, &az, &el);
calc_sat_az_el_almanac(&almanac[prn], t.tow, t.wn-1024,
position_solution.pos_ecef, &az, &el);
float dopp = -calc_sat_doppler_almanac(&almanac[prn], t.tow, t.wn,
position_solution.pos_ecef);
acq_prn_param[prn].score[ACQ_HINT_ALMANAC] =
Expand Down
1 change: 1 addition & 0 deletions src/manage.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#define MANAGE_TRACK_THREAD_PRIORITY (NORMALPRIO-2)
#define MANAGE_TRACK_THREAD_STACK 3000

#define MAX_ALM_WEEK_DIFF 6
/** \} */

void manage_acq_setup(void);
Expand Down

0 comments on commit e23265c

Please sign in to comment.