Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 12 additions & 30 deletions src/rtcm3.c
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ static int decode_type1013(rtcm_t *rtcm)
static int decode_type1019(rtcm_t *rtcm)
{
eph_t eph={0};
double toc,sqrtA,tt;
double toc,sqrtA;
char *msg;
int i=24+12,prn,sat,week,sys=SYS_GPS;

Expand Down Expand Up @@ -788,10 +788,7 @@ static int decode_type1019(rtcm_t *rtcm)
}
eph.sat=sat;
eph.week=adjgpsweek(week);
if (rtcm->time.time==0) rtcm->time=utc2gpst(timeget());
tt=timediff(gpst2time(eph.week,eph.toes),rtcm->time);
if (tt<-302400.0) eph.week++;
else if (tt>=302400.0) eph.week--;
rtcm->time=gpst2time(eph.week,eph.toes);
Copy link

@ourairquality ourairquality Oct 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note sure about setting the rtcm->time to the eph time. adjgpsweek() also uses timeget(). Might be best to leave that.

The further adjustment to the eph.week might be to handle the rtcm->time being different to timeget() used in adjgpsweek(), handling half a week difference here. If this code is not broken then I suggest not changing it as there are a lot of complex code paths here e.g. replying data etc. Could you split these 'simplifications' out and just include the fix to adjgpsweek().

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for changing rtcm->time to use the ephemeris time is that in the original code, when timeget() and the ephemeris time differ by more than two weeks, it causes incorrect behavior. Using the eph time avoids this issue.

Copy link

@ourairquality ourairquality Oct 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With adjgpsweek() improved wouldn't that make eph.weeks consistent with timeget(). Can't see why the other eph.week adjustment was needed, and with that removed it should not be an issue even if the eph time is weeks off timeget() or rtcm->time? Still seems best to initialize the rtcm->time using timeget() as that is used everywhere else. So suggest:

eph.week=adjgpsweek(week);
if (rtcm->time.time==0) rtcm->time=utc2gpst(timeget());

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my situation is a bit special — I’m actually running RTKLIB on an ESP32. On this platform, every time the ESP32 boots, timeget() starts counting from the firmware’s compile time.

Because of that, when rtcm->time is initialized using timeget(), RTKLIB works correctly during the current week and the following week, but by the second week it encounters a week rollover error.

To fix this, I changed the code to set rtcm->time using the ephemeris time (after correction with adjgpsweek()), instead of relying on timeget().

eph.toe=gpst2time(eph.week,eph.toes);
eph.toc=gpst2time(eph.week,toc);
eph.ttr=rtcm->time;
Expand Down Expand Up @@ -1081,7 +1078,7 @@ static int decode_type1039(rtcm_t *rtcm)
static int decode_type1041(rtcm_t *rtcm)
{
eph_t eph={0};
double toc,sqrtA,tt;
double toc,sqrtA;
char *msg;
int i=24+12,prn,sat,week,sys=SYS_IRN;

Expand Down Expand Up @@ -1130,10 +1127,7 @@ static int decode_type1041(rtcm_t *rtcm)
}
eph.sat=sat;
eph.week=adjgpsweek(week);
if (rtcm->time.time==0) rtcm->time=utc2gpst(timeget());
tt=timediff(gpst2time(eph.week,eph.toes),rtcm->time);
if (tt<-302400.0) eph.week++;
else if (tt>=302400.0) eph.week--;
rtcm->time=gpst2time(eph.week,eph.toes);
eph.toe=gpst2time(eph.week,eph.toes);
eph.toc=gpst2time(eph.week,toc);
eph.ttr=rtcm->time;
Expand All @@ -1151,7 +1145,7 @@ static int decode_type1041(rtcm_t *rtcm)
static int decode_type1044(rtcm_t *rtcm)
{
eph_t eph={0};
double toc,sqrtA,tt;
double toc,sqrtA;
char *msg;
int i=24+12,prn,sat,week,sys=SYS_QZS;

Expand Down Expand Up @@ -1203,10 +1197,7 @@ static int decode_type1044(rtcm_t *rtcm)
}
eph.sat=sat;
eph.week=adjgpsweek(week);
if (rtcm->time.time==0) rtcm->time=utc2gpst(timeget());
tt=timediff(gpst2time(eph.week,eph.toes),rtcm->time);
if (tt<-302400.0) eph.week++;
else if (tt>=302400.0) eph.week--;
rtcm->time=gpst2time(eph.week,eph.toes);
eph.toe=gpst2time(eph.week,eph.toes);
eph.toc=gpst2time(eph.week,toc);
eph.ttr=rtcm->time;
Expand All @@ -1225,7 +1216,7 @@ static int decode_type1044(rtcm_t *rtcm)
static int decode_type1045(rtcm_t *rtcm)
{
eph_t eph={0};
double toc,sqrtA,tt;
double toc,sqrtA;
char *msg;
int i=24+12,prn,sat,week,e5a_hs,e5a_dvs,rsv,sys=SYS_GAL;

Expand Down Expand Up @@ -1281,10 +1272,7 @@ static int decode_type1045(rtcm_t *rtcm)
}
eph.sat=sat;
eph.week=week+1024; /* gal-week = gst-week + 1024 */
if (rtcm->time.time==0) rtcm->time=utc2gpst(timeget());
tt=timediff(gpst2time(eph.week,eph.toes),rtcm->time);
if (tt<-302400.0) eph.week++;
else if (tt>=302400.0) eph.week--;
rtcm->time=gpst2time(eph.week,eph.toes);
eph.toe=gpst2time(eph.week,eph.toes);
eph.toc=gpst2time(eph.week,toc);
eph.ttr=rtcm->time;
Expand All @@ -1304,7 +1292,7 @@ static int decode_type1045(rtcm_t *rtcm)
static int decode_type1046(rtcm_t *rtcm)
{
eph_t eph={0};
double toc,sqrtA,tt;
double toc,sqrtA;
char *msg;
int i=24+12,prn,sat,week,e5b_hs,e5b_dvs,e1_hs,e1_dvs,sys=SYS_GAL;

Expand Down Expand Up @@ -1362,10 +1350,7 @@ static int decode_type1046(rtcm_t *rtcm)
}
eph.sat=sat;
eph.week=week+1024; /* gal-week = gst-week + 1024 */
if (rtcm->time.time==0) rtcm->time=utc2gpst(timeget());
tt=timediff(gpst2time(eph.week,eph.toes),rtcm->time);
if (tt<-302400.0) eph.week++;
else if (tt>=302400.0) eph.week--;
rtcm->time=gpst2time(eph.week,eph.toes);
eph.toe=gpst2time(eph.week,eph.toes);
eph.toc=gpst2time(eph.week,toc);
eph.ttr=rtcm->time;
Expand All @@ -1385,7 +1370,7 @@ static int decode_type1046(rtcm_t *rtcm)
static int decode_type1042(rtcm_t *rtcm)
{
eph_t eph={0};
double toc,sqrtA,tt;
double toc,sqrtA;
char *msg;
int i=24+12,prn,sat,week,sys=SYS_CMP;

Expand Down Expand Up @@ -1436,10 +1421,7 @@ static int decode_type1042(rtcm_t *rtcm)
}
eph.sat=sat;
eph.week=adjbdtweek(week);
if (rtcm->time.time==0) rtcm->time=utc2gpst(timeget());
tt=timediff(bdt2gpst(bdt2time(eph.week,eph.toes)),rtcm->time);
if (tt<-302400.0) eph.week++;
else if (tt>=302400.0) eph.week--;
rtcm->time=bdt2time(eph.week,eph.toes);
eph.toe=bdt2gpst(bdt2time(eph.week,eph.toes)); /* bdt -> gpst */
eph.toc=bdt2gpst(bdt2time(eph.week,toc)); /* bdt -> gpst */
eph.ttr=rtcm->time;
Expand Down
2 changes: 1 addition & 1 deletion src/rtkcmn.c
Original file line number Diff line number Diff line change
Expand Up @@ -2031,7 +2031,7 @@ extern int adjgpsweek(int week)
int w;
(void)time2gpst(utc2gpst(timeget()),&w);
if (w<1560) w=1560; /* use 2009/12/1 if time is earlier than 2009/12/1 */
return week+(w-week+1)/1024*1024;
return week+(w-week+511)/1024*1024;
}
/* get tick time ---------------------------------------------------------------
* get current tick in ms
Expand Down