-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #266 from tier4/feature/timesource-options
Feature/timesource options
- Loading branch information
Showing
15 changed files
with
299 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
Assets/AWSIM/Scripts/Clock/Scripts/DotNetSimulationTimeSource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Threading; | ||
using ROS2; | ||
|
||
namespace AWSIM | ||
{ | ||
/// <summary> | ||
/// A thread-safe timesource class that provides simulation time based on the dot net system utc time. | ||
/// This time source takes into account the value of the simulation timescale and | ||
/// starts at zero value when the simulation is started. | ||
/// </summary> | ||
public class DotNetSimulationTimeSource : ITimeSource | ||
{ | ||
private DateTime prevDateTime; | ||
private double time; | ||
private bool hasStarted = false; | ||
|
||
private readonly object lockObject = new object(); | ||
|
||
public DotNetSimulationTimeSource() | ||
{ | ||
hasStarted = false; | ||
} | ||
|
||
public void GetTime(out int seconds, out uint nanoseconds) | ||
{ | ||
lock (lockObject) | ||
{ | ||
DateTime currDateTime = DateTime.UtcNow; | ||
|
||
if(!hasStarted) | ||
{ | ||
hasStarted = true; | ||
time = 0.0; | ||
|
||
prevDateTime = currDateTime; | ||
} | ||
|
||
TimeSpan timeSpan = currDateTime - prevDateTime; | ||
prevDateTime = currDateTime; | ||
|
||
time += timeSpan.TotalMilliseconds * 0.001f * TimeScaleProvider.TimeScale; | ||
TimeUtils.TimeFromTotalSeconds(time, out seconds, out nanoseconds); | ||
} | ||
} | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
Assets/AWSIM/Scripts/Clock/Scripts/DotNetSimulationTimeSource.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
Assets/AWSIM/Scripts/Clock/Scripts/TimeAsDoubleProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using UnityEngine; | ||
|
||
namespace AWSIM | ||
{ | ||
/// <summary> | ||
/// A thread-safe static class to provide the value of the Unity time as double. | ||
/// </summary> | ||
public static class TimeAsDoubleProvider | ||
{ | ||
private static readonly object lockObject = new object(); | ||
|
||
private static double timeAsDouble = 0.0; | ||
public static double TimeAsDouble | ||
{ | ||
get | ||
{ | ||
lock(lockObject) | ||
{ | ||
return timeAsDouble; | ||
} | ||
} | ||
} | ||
|
||
|
||
#region [Public Methods] | ||
|
||
/// <summary> | ||
/// Synchronise the value of the timeAsDouble variable with the Time.timeAsDouble from the Unity thread. | ||
/// </summary> | ||
public static void DoUpdate() | ||
{ | ||
lock(lockObject) | ||
{ | ||
timeAsDouble = Time.timeAsDouble; | ||
} | ||
} | ||
|
||
#endregion | ||
} | ||
} | ||
|
11 changes: 11 additions & 0 deletions
11
Assets/AWSIM/Scripts/Clock/Scripts/TimeAsDoubleProvider.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.