Skip to content

Commit

Permalink
Move GPS leap second and UTC-to-GPS conversion
Browse files Browse the repository at this point in the history
to a new GpsUtils class.
  • Loading branch information
Guodong Rong authored and EricBoiseLGSVL committed May 5, 2020
1 parent fe4a1d8 commit 05f7db1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
6 changes: 2 additions & 4 deletions Assets/Scripts/Bridge/Cyber/CyberConversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ namespace Simulator.Bridge.Cyber
{
static class Conversions
{
static readonly DateTime GpsEpoch = new DateTime(1980, 1, 6, 0, 0, 0, DateTimeKind.Utc);

static byte[] ActualBytes(byte[] array, int length)
{
byte[] result = new byte[length];
Expand Down Expand Up @@ -236,7 +234,7 @@ public static apollo.canbus.Chassis ConvertFrom(CanBusData data)
else dir = 45 * Mathf.Round((eul.y % 360 + 360) / 45.0f);

var dt = DateTimeOffset.FromUnixTimeMilliseconds((long)(data.Time * 1000.0)).UtcDateTime;
var measurement_time = (dt - GpsEpoch).TotalSeconds + 18.0;
var measurement_time = GpsUtils.UtcToGpsSeconds(dt);
var gpsTime = DateTimeOffset.FromUnixTimeSeconds((long)measurement_time).DateTime.ToLocalTime();

return new apollo.canbus.Chassis()
Expand Down Expand Up @@ -297,7 +295,7 @@ public static apollo.drivers.gnss.GnssBestPose ConvertFrom(GpsData data)
double Height = 0; // sea level to WGS84 ellipsoid

var dt = DateTimeOffset.FromUnixTimeMilliseconds((long)(data.Time * 1000.0)).UtcDateTime;
var measurement_time = (dt - GpsEpoch).TotalSeconds + 18.0;
var measurement_time = GpsUtils.UtcToGpsSeconds(dt);

return new apollo.drivers.gnss.GnssBestPose()
{
Expand Down
25 changes: 25 additions & 0 deletions Assets/Scripts/Bridge/GpsUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2020 LG Electronics, Inc.
*
* This software contains code licensed as described in LICENSE.
*
*/

using System;

namespace Simulator.Bridge
{
static public class GpsUtils
{
static readonly public DateTime GpsEpoch = new DateTime(1980, 1, 6, 0, 0, 0, DateTimeKind.Utc);
// GPS Time requires leap seconds which is updated every several years.
// https://racelogic.support/01VBOX_Automotive/01General_Information/Knowledge_Base/What_are_GPS_Leap_Seconds%3F
// We keep a single copy of this value here for easy update later.
static readonly public float GpsLeapSeconds = 18.0f;

static public double UtcToGpsSeconds(DateTime utc)
{
return (utc - GpsEpoch).TotalSeconds + GpsLeapSeconds;
}
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Bridge/GpsUtils.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions Assets/Scripts/Bridge/Ros/RosConversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ namespace Simulator.Bridge.Ros
{
static class Conversions
{
static readonly DateTime GpsEpoch = new DateTime(1980, 1, 6, 0, 0, 0, DateTimeKind.Utc);

public static CompressedImage ConvertFrom(ImageData data)
{
return new CompressedImage()
Expand Down Expand Up @@ -210,7 +208,7 @@ public static Apollo.ChassisMsg ConvertFrom(CanBusData data)
else dir = 45 * UnityEngine.Mathf.Round((eul.y % 360 + 360) / 45.0f);

var dt = DateTimeOffset.FromUnixTimeMilliseconds((long)(data.Time * 1000.0)).UtcDateTime;
var measurement_time = (dt - GpsEpoch).TotalSeconds + 18.0;
var measurement_time = GpsUtils.UtcToGpsSeconds(dt);
var gpsTime = DateTimeOffset.FromUnixTimeSeconds((long)measurement_time).DateTime.ToLocalTime();

return new Apollo.ChassisMsg()
Expand Down Expand Up @@ -338,7 +336,7 @@ public static Apollo.GnssBestPose ConvertFrom(GpsData data)
double Height = 0; // sea level to WGS84 ellipsoid

var dt = DateTimeOffset.FromUnixTimeMilliseconds((long)(data.Time * 1000.0)).UtcDateTime;
var measurement_time = (dt - GpsEpoch).TotalSeconds + 18.0;
var measurement_time = GpsUtils.UtcToGpsSeconds(dt);

return new Apollo.GnssBestPose()
{
Expand Down Expand Up @@ -462,7 +460,7 @@ public static Apollo.Gps ApolloConvertFrom(GpsOdometryData data)
{
header = new Apollo.Header()
{
timestamp_sec = (dt - GpsEpoch).TotalSeconds + 18.0,
timestamp_sec = GpsUtils.UtcToGpsSeconds(dt),
sequence_num = data.Sequence,
},

Expand Down

0 comments on commit 05f7db1

Please sign in to comment.