-
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 #253 from tier4/feature/unscalable-clock-in-thread
Clock publisher works in dedeciated thread
- Loading branch information
Showing
8 changed files
with
222 additions
and
17 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
37 changes: 37 additions & 0 deletions
37
Assets/AWSIM/Scripts/Clock/Scripts/DotNetSystemTimeSource.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,37 @@ | ||
using System; | ||
using System.Threading; | ||
using ROS2; | ||
|
||
namespace AWSIM | ||
{ | ||
/// <summary> | ||
/// A thread-safe timesource class that provides the dot net system utc time. | ||
/// </summary> | ||
public class DotNetSystemTimeSource : ITimeSource | ||
{ | ||
private DateTime prevDateTime; | ||
private double time; | ||
|
||
private readonly object lockObject = new object(); | ||
|
||
public DotNetSystemTimeSource() | ||
{ | ||
prevDateTime = DateTime.UtcNow; | ||
time = 0.0; | ||
} | ||
|
||
public void GetTime(out int seconds, out uint nanoseconds) | ||
{ | ||
lock (lockObject) | ||
{ | ||
DateTime currDateTime = DateTime.UtcNow; | ||
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/DotNetSystemTimeSource.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using UnityEngine; | ||
|
||
namespace AWSIM | ||
{ | ||
/// <summary> | ||
/// A thread-safe static class to provide the value of the time scale of the simulation. | ||
/// </summary> | ||
public static class TimeScaleProvider | ||
{ | ||
private static readonly object lockObject = new object(); | ||
|
||
private static float timeScale = 1.0f; | ||
public static float TimeScale | ||
{ | ||
get | ||
{ | ||
lock (lockObject) | ||
{ | ||
return timeScale; | ||
} | ||
} | ||
} | ||
|
||
|
||
#region [Public Methods] | ||
|
||
/// <summary> | ||
/// Synchronise the value of the timeScale variable with the Time.timeScale from the Unity thread. | ||
/// </summary> | ||
public static void DoUpdate() | ||
{ | ||
lock(lockObject) | ||
{ | ||
if (Mathf.Abs(Time.timeScale - timeScale) > 0.01f) | ||
{ | ||
timeScale = Time.timeScale; | ||
} | ||
} | ||
} | ||
|
||
#endregion | ||
} | ||
} | ||
|
11 changes: 11 additions & 0 deletions
11
Assets/AWSIM/Scripts/Clock/Scripts/TimeScaleProvider.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
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