Skip to content

Commit

Permalink
Merge pull request #253 from tier4/feature/unscalable-clock-in-thread
Browse files Browse the repository at this point in the history
Clock publisher works in dedeciated thread
  • Loading branch information
mackierx111 authored Jan 31, 2024
2 parents 2ae9703 + 61f360e commit 2f8be74
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 17 deletions.
71 changes: 69 additions & 2 deletions Assets/AWSIM/Scenes/Main/AutowareSimulation.unity
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 1537.125, g: 1904.5333, b: 2520.709, a: 1}
m_IndirectSpecularColor: {r: 1520.0924, g: 1884.674, b: 2491.8425, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down Expand Up @@ -241,7 +241,8 @@ Transform:
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Children:
- {fileID: 1575276245}
m_Father: {fileID: 543989980}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
Expand Down Expand Up @@ -3621,6 +3622,72 @@ MonoBehaviour:
timeScaleText: {fileID: 631930841}
timeScaleSlider: {fileID: 1153401690}
versionText: {fileID: 988705868}
--- !u!1001 &1575276244
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 26523764}
m_Modifications:
- target: {fileID: 2426471684872746624, guid: 27181f17b3e1bb607a4b8fcb8a827e0f, type: 3}
propertyPath: m_Name
value: TimeSourceSelector
objectReference: {fileID: 0}
- target: {fileID: 2426471684872746626, guid: 27181f17b3e1bb607a4b8fcb8a827e0f, type: 3}
propertyPath: m_RootOrder
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2426471684872746626, guid: 27181f17b3e1bb607a4b8fcb8a827e0f, type: 3}
propertyPath: m_LocalPosition.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2426471684872746626, guid: 27181f17b3e1bb607a4b8fcb8a827e0f, type: 3}
propertyPath: m_LocalPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2426471684872746626, guid: 27181f17b3e1bb607a4b8fcb8a827e0f, type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2426471684872746626, guid: 27181f17b3e1bb607a4b8fcb8a827e0f, type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 2426471684872746626, guid: 27181f17b3e1bb607a4b8fcb8a827e0f, type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2426471684872746626, guid: 27181f17b3e1bb607a4b8fcb8a827e0f, type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2426471684872746626, guid: 27181f17b3e1bb607a4b8fcb8a827e0f, type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2426471684872746626, guid: 27181f17b3e1bb607a4b8fcb8a827e0f, type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2426471684872746626, guid: 27181f17b3e1bb607a4b8fcb8a827e0f, type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 2426471684872746626, guid: 27181f17b3e1bb607a4b8fcb8a827e0f, type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 3077555317673241940, guid: 27181f17b3e1bb607a4b8fcb8a827e0f, type: 3}
propertyPath: Type
value: 2
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 27181f17b3e1bb607a4b8fcb8a827e0f, type: 3}
--- !u!4 &1575276245 stripped
Transform:
m_CorrespondingSourceObject: {fileID: 2426471684872746626, guid: 27181f17b3e1bb607a4b8fcb8a827e0f, type: 3}
m_PrefabInstance: {fileID: 1575276244}
m_PrefabAsset: {fileID: 0}
--- !u!114 &1672697011 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 9027057599777832477, guid: 58d73df60b244d146bdf5f5896f78355, type: 3}
Expand Down
37 changes: 37 additions & 0 deletions Assets/AWSIM/Scripts/Clock/Scripts/DotNetSystemTimeSource.cs
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 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.

44 changes: 44 additions & 0 deletions Assets/AWSIM/Scripts/Clock/Scripts/TimeScaleProvider.cs
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 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.

15 changes: 14 additions & 1 deletion Assets/AWSIM/Scripts/Clock/Scripts/TimeSourceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public static class TimeSourceProvider
public enum TimeSourceType
{
UNITY,
SS2
SS2,
DOTNET
}

#region [Event]
Expand Down Expand Up @@ -94,6 +95,18 @@ public static void SetTimeSource(TimeSourceType type)
return;
}

// dot net system time source
if(type == TimeSourceType.DOTNET)
{
if(currentTimeSource == null || !(currentTimeSource is DotNetSystemTimeSource))
{
currentTimeSource = new DotNetSystemTimeSource();
onTimeSourceChanged?.Invoke();
}

return;
}

// default time source
if(currentTimeSource == null || !(currentTimeSource is UnityTimeSource))
{
Expand Down
45 changes: 32 additions & 13 deletions Assets/AWSIM/Scripts/ROS/ClockPublisher.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ROS2;
using System.Threading;

namespace AWSIM
{
Expand All @@ -16,7 +15,9 @@ public class ClockPublisher : MonoBehaviour

IPublisher<rosgraph_msgs.msg.Clock> clockPublisher;
rosgraph_msgs.msg.Clock clockMsg;
float timeScale = 1.0f;

Thread clockThread;
bool isRunning = false;


#region [Life Cycle]
Expand All @@ -28,38 +29,56 @@ void Awake()
clockMsg = new rosgraph_msgs.msg.Clock();
}

void Start()
{
TimeScaleProvider.DoUpdate();
StartClockThread();
}

void OnDestroy()
{
StopClockThread();
SimulatorROS2Node.RemovePublisher<rosgraph_msgs.msg.Clock>(clockPublisher);
}

#endregion

void Start()
#region [Clock Thread]

void StartClockThread()
{
timeScale = Time.timeScale;
InvokeRepeating("PublishClock", 1.0f, timeScale/publishHz);
clockThread = new Thread(UpdateClock)
{
Name = "Clock"
};
isRunning = true;
clockThread.Start();
}

void Update()
void StopClockThread()
{
if (Mathf.Abs(Time.timeScale - timeScale) > 0.01f)
isRunning = false;
if (clockThread != null && clockThread.IsAlive)
{
timeScale = Time.timeScale;
OnTimeScaleChanged();
clockThread.Join();
}
}

void OnTimeScaleChanged()
void UpdateClock()
{
CancelInvoke();
InvokeRepeating("PublishClock", 0.0f, timeScale/publishHz);
while(isRunning)
{
Thread.Sleep(1000 / publishHz);
PublishClock();
}
}

void PublishClock()
{
SimulatorROS2Node.UpdateROSClockTime(clockMsg.Clock_);
clockPublisher.Publish(clockMsg);
}

#endregion
}
}
5 changes: 4 additions & 1 deletion Assets/AWSIM/Scripts/UI/DemoUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public void SetTimeScale(float timeScale)
{
Time.timeScale = timeScale;
timeScaleText.text = "x " + timeScale.ToString("F2");

// synchronisation of new timescale value with TimeScaleProvider
TimeScaleProvider.DoUpdate();
}
}
}
}

0 comments on commit 2f8be74

Please sign in to comment.