Skip to content

Commit

Permalink
GPS Time Accuracy
Browse files Browse the repository at this point in the history
Replaced JDToGpsTime with GpsTimeToGpsDate to reduce roundoff error.
  • Loading branch information
ericstoneking committed Jan 20, 2024
1 parent 0019f24 commit 3d4e07b
Show file tree
Hide file tree
Showing 12 changed files with 2,275 additions and 1,956 deletions.
4,121 changes: 2,199 additions & 1,922 deletions Database/42.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Database/JsonToIPC.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,14 @@ def TimeRefreshCode():
outfile.write(" DOY2MD(UTC.Year,UTC.doy,&UTC.Month,&UTC.Day);\n")
outfile.write(" CivilTime = DateToTime(UTC.Year,UTC.Month,UTC.Day,UTC.Hour,UTC.Minute,UTC.Second);\n")
outfile.write(" AtomicTime = CivilTime + LeapSec;\n")
outfile.write(" GpsTime = AtomicTime - 19.0;\n")
outfile.write(" DynTime = AtomicTime + 32.184;\n")
outfile.write(" TT.JulDay = TimeToJD(DynTime);\n")
outfile.write(" TimeToDate(DynTime,&TT.Year,&TT.Month,&TT.Day,\n")
outfile.write(" &TT.Hour,&TT.Minute,&TT.Second,DTSIM);\n")
outfile.write(" TT.doy = MD2DOY(TT.Year,TT.Month,TT.Day);\n")
outfile.write(" UTC.JulDay = TimeToJD(CivilTime);\n")
outfile.write(" JDToGpsTime(TT.JulDay,&GpsRollover,&GpsWeek,&GpsSecond);\n")
outfile.write(" GpsTimeToGpsDate(GpsTime,&GpsRollover,&GpsWeek,&GpsSecond);\n")
outfile.write(" SimTime = DynTime-DynTime0;\n")
outfile.write(" }\n\n")
else:
Expand Down
5 changes: 3 additions & 2 deletions Kit/Include/timekit.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ void TimeToDate(double Time, long *Year, long *Month, long *Day,
long MD2DOY(long Year, long Month, long Day);
void DOY2MD(long Year, long DayOfYear, long *Month, long *Day);
double JD2GMST(double JD);
double GpsTimeToJD(long GpsRollover, long GpsWeek, double GpsSecond);
void JDToGpsTime(double JD, long *GpsRollover, long *GpsWeek, double *GpsSecond);
void GpsTimeToGpsDate(double GpsTime, long *GpsRollover, long *GpsWeek,
double *GpsSecond);
double GpsDateToGpsTime(long GpsRollover, long GpsWeek, double GpsSecond);
double usec(void);
void RealSystemTime(long *Year, long *DOY, long *Month, long *Day,
long *Hour, long *Minute, double *Second, double LSB);
Expand Down
40 changes: 18 additions & 22 deletions Kit/Source/timekit.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,37 +273,33 @@ double JD2GMST(double JD)

}
/**********************************************************************/
/* GPS Epoch is 6 Jan 1980 00:00:00.0 UTC which is JD = 2444244.5 UTC */
/* GPS Time is expressed in weeks and seconds */
/* GPS Time rolls over every 1024 weeks */
/* This function yields JD in TT, so add (32.184+19.0) sec */
double GpsTimeToJD(long GpsRollover, long GpsWeek, double GpsSecond)
{
double DaysSinceWeek,DaysSinceRollover,DaysSinceEpoch,JD;

DaysSinceWeek = GpsSecond/86400.0;
DaysSinceRollover = DaysSinceWeek + 7.0*GpsWeek;
DaysSinceEpoch = DaysSinceRollover + 7168.0*GpsRollover;
JD = DaysSinceEpoch + 2444244.5 + (32.184+19.0)/86400.0;

return(JD);
}
/**********************************************************************/
/* GPS Epoch is 6 Jan 1980 00:00:00.0 UTC which is JD = 2444244.5 UTC */
/* GPS Time is expressed in weeks and seconds */
/* GPS Time rolls over every 1024 weeks */
/* This function requires JD in TT, so subtract (32.184+19.0) sec */
void JDToGpsTime(double JD, long *GpsRollover, long *GpsWeek, double *GpsSecond)
/* GPS Epoch is 6 Jan 1980 00:00:00.0 UTC */
/* which is 6 Jan 1980 00:00:19.0 TAI */
/* J2000 is 1 Jan 2000 12:00:00.0 TT */
/* which is 1 Jan 2000 11:59:27.816 TAI */
/* so J2000-GPS epoch is 7300.5 days minus (19+32.184) sec */
void GpsTimeToGpsDate(double GpsTime, long *GpsRollover, long *GpsWeek,
double *GpsSecond)
{
double DaysSinceEpoch, DaysSinceRollover, DaysSinceWeek;

DaysSinceEpoch = JD - 2444244.5 - (32.184+19.0)/86400.0;
DaysSinceEpoch = 7300.5 + GpsTime/86400.0;
*GpsRollover = (long) (DaysSinceEpoch/7168.0);
DaysSinceRollover = DaysSinceEpoch - 7168.0*((double) *GpsRollover);
*GpsWeek = (long) (DaysSinceRollover/7.0);
DaysSinceWeek = DaysSinceRollover - 7.0*((double) *GpsWeek);
*GpsSecond = DaysSinceWeek*86400.0;
}
/**********************************************************************/
/* GPS Epoch is 6 Jan 1980 00:00:00.0 UTC */
/* which is 6 Jan 1980 00:00:19.0 TAI */
/* J2000 is 1 Jan 2000 12:00:00.0 TT */
/* which is 1 Jan 2000 11:59:27.816 TAI */
/* so J2000-GPS epoch is 7300.5 days minus (19+32.184) sec */
double GpsDateToGpsTime(long GpsRollover, long GpsWeek, double GpsSecond)
{
return(((GpsRollover*1024.0+GpsWeek)*7.0-7300.5)*86400.0+GpsSecond);
}

/**********************************************************************/
/* This function returns the number of microseconds since the Unix */
Expand Down
10 changes: 6 additions & 4 deletions Source/42exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ long AdvanceTime(void)
&UTC.Hour,&UTC.Minute,&UTC.Second,DTSIM);
UTC.doy = MD2DOY(UTC.Year,UTC.Month,UTC.Day);

JDToGpsTime(TT.JulDay,&GpsRollover,&GpsWeek,&GpsSecond);
GpsTimeToGpsDate(GpsTime,&GpsRollover,&GpsWeek,&GpsSecond);

break;
case REAL_TIME :
Expand All @@ -125,7 +125,7 @@ long AdvanceTime(void)
&UTC.Hour,&UTC.Minute,&UTC.Second,DTSIM);
UTC.doy = MD2DOY(UTC.Year,UTC.Month,UTC.Day);

JDToGpsTime(TT.JulDay,&GpsRollover,&GpsWeek,&GpsSecond);
GpsTimeToGpsDate(GpsTime,&GpsRollover,&GpsWeek,&GpsSecond);

break;
case EXTERNAL_TIME :
Expand All @@ -143,6 +143,7 @@ long AdvanceTime(void)
UTC.Hour,UTC.Minute,UTC.Second);
AtomicTime = CivilTime + LeapSec;
DynTime = AtomicTime + 32.184;
GpsTime = AtomicTime - 19.0;

TT.JulDay = TimeToJD(DynTime);
TimeToDate(DynTime,&TT.Year,&TT.Month,&TT.Day,
Expand All @@ -152,7 +153,7 @@ long AdvanceTime(void)
UTC.JulDay = TimeToJD(CivilTime);
UTC.doy = MD2DOY(UTC.Year,UTC.Month,UTC.Day);

JDToGpsTime(TT.JulDay,&GpsRollover,&GpsWeek,&GpsSecond);
GpsTimeToGpsDate(GpsTime,&GpsRollover,&GpsWeek,&GpsSecond);
DynTime0 = DynTime - SimTime;

break;
Expand All @@ -163,6 +164,7 @@ long AdvanceTime(void)
UTC.Hour,UTC.Minute,UTC.Second);
AtomicTime = CivilTime + LeapSec;
DynTime = AtomicTime + 32.184;
GpsTime = AtomicTime - 19.0;

TT.JulDay = TimeToJD(DynTime);
TimeToDate(DynTime,&TT.Year,&TT.Month,&TT.Day,
Expand All @@ -172,7 +174,7 @@ long AdvanceTime(void)
UTC.JulDay = TimeToJD(CivilTime);
UTC.doy = MD2DOY(UTC.Year,UTC.Month,UTC.Day);

JDToGpsTime(TT.JulDay,&GpsRollover,&GpsWeek,&GpsSecond);
GpsTimeToGpsDate(GpsTime,&GpsRollover,&GpsWeek,&GpsSecond);
SimTime = DynTime - DynTime0;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion Source/42init.c
Original file line number Diff line number Diff line change
Expand Up @@ -4549,7 +4549,7 @@ void InitSim(int argc, char **argv)
UTC.JulDay = TimeToJD(CivilTime);
UTC.doy = MD2DOY(UTC.Year,UTC.Month,UTC.Day);

JDToGpsTime(TT.JulDay,&GpsRollover,&GpsWeek,&GpsSecond);
GpsTimeToGpsDate(GpsTime,&GpsRollover,&GpsWeek,&GpsSecond);

/* .. Load Sun and Planets */
LoadSun();
Expand Down
6 changes: 6 additions & 0 deletions Source/IPC/AppWriteToFile.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ void WriteToFile(FILE *StateFile, struct AcType *AC)
fprintf(StateFile,"%s",line);
if (AC->EchoEnabled) printf("%s",line);

sprintf(line,"SC[%ld].AC.Thr[%ld].ThrustLevelCmd = %18.12le\n",
Isc,i,
AC->Thr[i].ThrustLevelCmd);
fprintf(StateFile,"%s",line);
if (AC->EchoEnabled) printf("%s",line);

}

sprintf(line,"SC[%ld].AC.Cmd.AngRate = %18.12le %18.12le %18.12le\n",
Expand Down
8 changes: 8 additions & 0 deletions Source/IPC/AppWriteToSocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ void WriteToSocket(SOCKET Socket, struct AcType *AC)
MsgLen += LineLen;
if (AC->EchoEnabled) printf("%s",line);

sprintf(line,"SC[%ld].AC.Thr[%ld].ThrustLevelCmd = %18.12le\n",
Isc,i,
AC->Thr[i].ThrustLevelCmd);
LineLen = strlen(line);
memcpy(&Msg[MsgLen],line,LineLen);
MsgLen += LineLen;
if (AC->EchoEnabled) printf("%s",line);

}

sprintf(line,"SC[%ld].AC.Cmd.AngRate = %18.12le %18.12le %18.12le\n",
Expand Down
9 changes: 8 additions & 1 deletion Source/IPC/SimReadFromCmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ void ReadFromCmd(void)
SC[Isc].AC.Thr[i].PulseWidthCmd = DbleVal[0];
}

if (sscanf(line,"SC[%ld].AC.Thr[%ld].ThrustLevelCmd = %le",
&Isc,&i,
&DbleVal[0]) == 3) {
SC[Isc].AC.Thr[i].ThrustLevelCmd = DbleVal[0];
}

if (sscanf(line,"SC[%ld].AC.Cmd.AngRate = %le %le %le",
&Isc,
&DbleVal[0],
Expand Down Expand Up @@ -1224,13 +1230,14 @@ void ReadFromCmd(void)
DOY2MD(UTC.Year,UTC.doy,&UTC.Month,&UTC.Day);
CivilTime = DateToTime(UTC.Year,UTC.Month,UTC.Day,UTC.Hour,UTC.Minute,UTC.Second);
AtomicTime = CivilTime + LeapSec;
GpsTime = AtomicTime - 19.0;
DynTime = AtomicTime + 32.184;
TT.JulDay = TimeToJD(DynTime);
TimeToDate(DynTime,&TT.Year,&TT.Month,&TT.Day,
&TT.Hour,&TT.Minute,&TT.Second,DTSIM);
TT.doy = MD2DOY(TT.Year,TT.Month,TT.Day);
UTC.JulDay = TimeToJD(CivilTime);
JDToGpsTime(TT.JulDay,&GpsRollover,&GpsWeek,&GpsSecond);
GpsTimeToGpsDate(GpsTime,&GpsRollover,&GpsWeek,&GpsSecond);
SimTime = DynTime-DynTime0;
}

Expand Down
9 changes: 8 additions & 1 deletion Source/IPC/SimReadFromFile.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ void ReadFromFile(FILE *StateFile, long EchoEnabled)
SC[Isc].AC.Thr[i].PulseWidthCmd = DbleVal[0];
}

if (sscanf(line,"SC[%ld].AC.Thr[%ld].ThrustLevelCmd = %le",
&Isc,&i,
&DbleVal[0]) == 3) {
SC[Isc].AC.Thr[i].ThrustLevelCmd = DbleVal[0];
}

if (sscanf(line,"SC[%ld].AC.Cmd.AngRate = %le %le %le",
&Isc,
&DbleVal[0],
Expand Down Expand Up @@ -1177,13 +1183,14 @@ void ReadFromFile(FILE *StateFile, long EchoEnabled)
DOY2MD(UTC.Year,UTC.doy,&UTC.Month,&UTC.Day);
CivilTime = DateToTime(UTC.Year,UTC.Month,UTC.Day,UTC.Hour,UTC.Minute,UTC.Second);
AtomicTime = CivilTime + LeapSec;
GpsTime = AtomicTime - 19.0;
DynTime = AtomicTime + 32.184;
TT.JulDay = TimeToJD(DynTime);
TimeToDate(DynTime,&TT.Year,&TT.Month,&TT.Day,
&TT.Hour,&TT.Minute,&TT.Second,DTSIM);
TT.doy = MD2DOY(TT.Year,TT.Month,TT.Day);
UTC.JulDay = TimeToJD(CivilTime);
JDToGpsTime(TT.JulDay,&GpsRollover,&GpsWeek,&GpsSecond);
GpsTimeToGpsDate(GpsTime,&GpsRollover,&GpsWeek,&GpsSecond);
SimTime = DynTime-DynTime0;
}

Expand Down
9 changes: 8 additions & 1 deletion Source/IPC/SimReadFromGmsec.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ void ReadFromGmsec(GMSEC_ConnectionMgr ConnMgr,GMSEC_Status status, long EchoEna
SC[Isc].AC.Thr[i].PulseWidthCmd = DbleVal[0];
}

if (sscanf(line,"SC[%ld].AC.Thr[%ld].ThrustLevelCmd = %le",
&Isc,&i,
&DbleVal[0]) == 3) {
SC[Isc].AC.Thr[i].ThrustLevelCmd = DbleVal[0];
}

if (sscanf(line,"SC[%ld].AC.Cmd.AngRate = %le %le %le",
&Isc,
&DbleVal[0],
Expand Down Expand Up @@ -1201,13 +1207,14 @@ void ReadFromGmsec(GMSEC_ConnectionMgr ConnMgr,GMSEC_Status status, long EchoEna
DOY2MD(UTC.Year,UTC.doy,&UTC.Month,&UTC.Day);
CivilTime = DateToTime(UTC.Year,UTC.Month,UTC.Day,UTC.Hour,UTC.Minute,UTC.Second);
AtomicTime = CivilTime + LeapSec;
GpsTime = AtomicTime - 19.0;
DynTime = AtomicTime + 32.184;
TT.JulDay = TimeToJD(DynTime);
TimeToDate(DynTime,&TT.Year,&TT.Month,&TT.Day,
&TT.Hour,&TT.Minute,&TT.Second,DTSIM);
TT.doy = MD2DOY(TT.Year,TT.Month,TT.Day);
UTC.JulDay = TimeToJD(CivilTime);
JDToGpsTime(TT.JulDay,&GpsRollover,&GpsWeek,&GpsSecond);
GpsTimeToGpsDate(GpsTime,&GpsRollover,&GpsWeek,&GpsSecond);
SimTime = DynTime-DynTime0;
}

Expand Down
9 changes: 8 additions & 1 deletion Source/IPC/SimReadFromSocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ void ReadFromSocket(SOCKET Socket, long EchoEnabled)
SC[Isc].AC.Thr[i].PulseWidthCmd = DbleVal[0];
}

if (sscanf(line,"SC[%ld].AC.Thr[%ld].ThrustLevelCmd = %le",
&Isc,&i,
&DbleVal[0]) == 3) {
SC[Isc].AC.Thr[i].ThrustLevelCmd = DbleVal[0];
}

if (sscanf(line,"SC[%ld].AC.Cmd.AngRate = %le %le %le",
&Isc,
&DbleVal[0],
Expand Down Expand Up @@ -1200,13 +1206,14 @@ void ReadFromSocket(SOCKET Socket, long EchoEnabled)
DOY2MD(UTC.Year,UTC.doy,&UTC.Month,&UTC.Day);
CivilTime = DateToTime(UTC.Year,UTC.Month,UTC.Day,UTC.Hour,UTC.Minute,UTC.Second);
AtomicTime = CivilTime + LeapSec;
GpsTime = AtomicTime - 19.0;
DynTime = AtomicTime + 32.184;
TT.JulDay = TimeToJD(DynTime);
TimeToDate(DynTime,&TT.Year,&TT.Month,&TT.Day,
&TT.Hour,&TT.Minute,&TT.Second,DTSIM);
TT.doy = MD2DOY(TT.Year,TT.Month,TT.Day);
UTC.JulDay = TimeToJD(CivilTime);
JDToGpsTime(TT.JulDay,&GpsRollover,&GpsWeek,&GpsSecond);
GpsTimeToGpsDate(GpsTime,&GpsRollover,&GpsWeek,&GpsSecond);
SimTime = DynTime-DynTime0;
}

Expand Down

0 comments on commit 3d4e07b

Please sign in to comment.