Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/timesource options #266

Merged
merged 13 commits into from
Feb 27, 2024
110 changes: 61 additions & 49 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: 1519.6069, g: 1883.9946, b: 2490.9849, 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 @@ -536,6 +536,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
trafficManager: {fileID: 723377035}
egoTransform: {fileID: 7808269042169720464}
timeSourceSelector: {fileID: 1259688278}
commandLineConfigParam: --json_path
useJsonConfig: 0
jsonPath:
Expand Down Expand Up @@ -3239,6 +3240,17 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 830ab73cebda3db4580ebd3ece935931, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &1259688278 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 3077555317673241940, guid: 27181f17b3e1bb607a4b8fcb8a827e0f, type: 3}
m_PrefabInstance: {fileID: 1575276244}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 0dde57169074a9c5c9780c80294b4427, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1001 &1381324630
PrefabInstance:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -4046,7 +4058,7 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 3077555317673241940, guid: 27181f17b3e1bb607a4b8fcb8a827e0f, type: 3}
propertyPath: Type
value: 2
value: 3
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 27181f17b3e1bb607a4b8fcb8a827e0f, type: 3}
Expand Down Expand Up @@ -4530,6 +4542,53 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 23c1ce4fb46143f46bc5cb5224c934f6, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Version: 7
m_ObsoleteRenderingPath: 0
m_ObsoleteFrameSettings:
overrides: 0
enableShadow: 0
enableContactShadows: 0
enableShadowMask: 0
enableSSR: 0
enableSSAO: 0
enableSubsurfaceScattering: 0
enableTransmission: 0
enableAtmosphericScattering: 0
enableVolumetrics: 0
enableReprojectionForVolumetrics: 0
enableLightLayers: 0
enableExposureControl: 1
diffuseGlobalDimmer: 0
specularGlobalDimmer: 0
shaderLitMode: 0
enableDepthPrepassWithDeferredRendering: 0
enableTransparentPrepass: 0
enableMotionVectors: 0
enableObjectMotionVectors: 0
enableDecals: 0
enableRoughRefraction: 0
enableTransparentPostpass: 0
enableDistortion: 0
enablePostprocess: 0
enableOpaqueObjects: 0
enableTransparentObjects: 0
enableRealtimePlanarReflection: 0
enableMSAA: 0
enableAsyncCompute: 0
runLightListAsync: 0
runSSRAsync: 0
runSSAOAsync: 0
runContactShadowsAsync: 0
runVolumeVoxelizationAsync: 0
lightLoopSettings:
overrides: 0
enableDeferredTileAndCluster: 0
enableComputeLightEvaluation: 0
enableComputeLightVariants: 0
enableComputeMaterialVariants: 0
enableFptlForForwardOpaque: 0
enableBigTilePrepass: 0
isFptlEnabled: 0
clearColorMode: 0
backgroundColorHDR: {r: 0.025, g: 0.07, b: 0.19, a: 0}
clearDepth: 1
Expand Down Expand Up @@ -4585,53 +4644,6 @@ MonoBehaviour:
data1: 0
data2: 0
defaultFrameSettings: 0
m_Version: 7
m_ObsoleteRenderingPath: 0
m_ObsoleteFrameSettings:
overrides: 0
enableShadow: 0
enableContactShadows: 0
enableShadowMask: 0
enableSSR: 0
enableSSAO: 0
enableSubsurfaceScattering: 0
enableTransmission: 0
enableAtmosphericScattering: 0
enableVolumetrics: 0
enableReprojectionForVolumetrics: 0
enableLightLayers: 0
enableExposureControl: 1
diffuseGlobalDimmer: 0
specularGlobalDimmer: 0
shaderLitMode: 0
enableDepthPrepassWithDeferredRendering: 0
enableTransparentPrepass: 0
enableMotionVectors: 0
enableObjectMotionVectors: 0
enableDecals: 0
enableRoughRefraction: 0
enableTransparentPostpass: 0
enableDistortion: 0
enablePostprocess: 0
enableOpaqueObjects: 0
enableTransparentObjects: 0
enableRealtimePlanarReflection: 0
enableMSAA: 0
enableAsyncCompute: 0
runLightListAsync: 0
runSSRAsync: 0
runSSAOAsync: 0
runContactShadowsAsync: 0
runVolumeVoxelizationAsync: 0
lightLoopSettings:
overrides: 0
enableDeferredTileAndCluster: 0
enableComputeLightEvaluation: 0
enableComputeLightVariants: 0
enableComputeMaterialVariants: 0
enableFptlForForwardOpaque: 0
enableBigTilePrepass: 0
isFptlEnabled: 0
--- !u!81 &2122627085
AudioListener:
m_ObjectHideFlags: 0
Expand Down
10 changes: 10 additions & 0 deletions Assets/AWSIM/Scenes/Main/AutowareSimulation/AutowareSimulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class AutowareSimulation : MonoBehaviour
{
[SerializeField] TrafficManager trafficManager;
[SerializeField] Transform egoTransform;
[SerializeField] TimeSourceSelector timeSourceSelector;

[Header("Player Config")]
[SerializeField] string commandLineConfigParam = "--json_path";
Expand All @@ -33,13 +34,19 @@ public class EgoConfiguration
public class Configuration
{
public float TimeScale; // Reflected in Time.timeScale
public string TimeSource; // Reflected in TimeSourceSelector
public int RandomTrafficSeed; // Reflected in TrafficManager.seed
public int MaxVehicleCount; // Reflected in TrafficManager.maxVehicleCount
public EgoConfiguration Ego = new EgoConfiguration();
}

void Awake()
{
// check if time source selector is present
if(timeSourceSelector == null)
{
Debug.LogWarning("TimeSource: There is no TimeSourceSelector object assigned in the inspector. The default time source will be used.");
}

#if !UNITY_EDITOR
// initialize
Expand All @@ -62,6 +69,9 @@ void Awake()

var rotation = Quaternion.Euler(config.Ego.EulerAngles);
egoTransform.rotation = ROS2Utility.RosToUnityRotation(rotation);

// set time source
timeSourceSelector?.SetType(config.TimeSource);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion Assets/AWSIM/Scenes/Main/AutowareSimulation/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"TimeScale": 0.2,
"TimeSource": "simulation",
"RandomTrafficSeed": 33,
"MaxVehicleCount": 2,
"Ego": {
Expand All @@ -14,4 +15,4 @@
"z": 35.0
}
}
}
}
48 changes: 48 additions & 0 deletions Assets/AWSIM/Scripts/Clock/Scripts/DotNetSimulationTimeSource.cs
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);
}
}
}

}

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

21 changes: 17 additions & 4 deletions Assets/AWSIM/Scripts/Clock/Scripts/DotNetSystemTimeSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,43 @@
namespace AWSIM
{
/// <summary>
/// A thread-safe timesource class that provides the dot net system utc time.
/// A thread-safe timesource class that provides the dot net system utc time since epoch.
/// This timesource takes into account the value of the simulation timescale.
/// </summary>
public class DotNetSystemTimeSource : ITimeSource
{
private DateTime prevDateTime;
private double time;
private bool hasStarted = false;

private readonly object lockObject = new object();

public DotNetSystemTimeSource()
{
prevDateTime = DateTime.UtcNow;
time = 0.0;
hasStarted = false;
}

public void GetTime(out int seconds, out uint nanoseconds)
{
lock (lockObject)
{
DateTime currDateTime = DateTime.UtcNow;

if(!hasStarted)
{
hasStarted = true;

// get the time in millisecond since epoch
long timeOffset = ((DateTimeOffset)currDateTime).ToUnixTimeMilliseconds();
time = (double)timeOffset * 0.001;

prevDateTime = currDateTime;
}

TimeSpan timeSpan = currDateTime - prevDateTime;
prevDateTime = currDateTime;

time += timeSpan.TotalMilliseconds * 0.001f * TimeScaleProvider.TimeScale;
time += timeSpan.TotalMilliseconds * 0.001 * TimeScaleProvider.TimeScale;
TimeUtils.TimeFromTotalSeconds(time, out seconds, out nanoseconds);
}
}
Expand Down
41 changes: 41 additions & 0 deletions Assets/AWSIM/Scripts/Clock/Scripts/TimeAsDoubleProvider.cs
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 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.

5 changes: 1 addition & 4 deletions Assets/AWSIM/Scripts/Clock/Scripts/TimeScaleProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ public static void DoUpdate()
{
lock(lockObject)
{
if (Mathf.Abs(Time.timeScale - timeScale) > 0.01f)
{
timeScale = Time.timeScale;
}
timeScale = Time.timeScale;
}
}

Expand Down
Loading
Loading