Skip to content

Commit d923747

Browse files
committed
Final fixes of clocks and dotnet time set to unix epoch
Signed-off-by: Adam Dabrowski <adam.dabrowski@robotec.ai>
1 parent 89e978e commit d923747

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

src/Ros2ForUnity/Scripts/ROS2UnityComponent.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public bool Ok()
4545
{
4646
lock (mutex)
4747
{
48+
if (ros2forUnity == null)
49+
LazyConstruct();
4850
return (nodes != null && ros2forUnity.Ok());
4951
}
5052
}

src/Ros2ForUnity/Scripts/Time/DotnetTimeSource.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,32 @@ public class DotnetTimeSource : ITimeSource
3434

3535
private double stopwatchStartTimeStamp;
3636

37-
public DotnetTimeSource()
37+
private double TotalSystemTimeSeconds()
38+
{
39+
return TimeSpan.FromTicks(DateTime.UtcNow.Ticks).TotalSeconds;
40+
}
41+
42+
private void UpdateSystemTime()
3843
{
44+
systemTimeIntervalStart = TotalSystemTimeSeconds();
3945
stopwatchStartTimeStamp = Stopwatch.GetTimestamp();
4046
}
4147

48+
public DotnetTimeSource()
49+
{
50+
UpdateSystemTime();
51+
}
52+
4253
public void GetTime(out int seconds, out uint nanoseconds)
4354
{
4455
lock(mutex) // Threading
4556
{
4657
double endTimestamp = Stopwatch.GetTimestamp();
47-
var durationInSeconds = (endTimestamp - stopwatchStartTimeStamp) / Stopwatch.Frequency;
58+
var durationInSeconds = endTimestamp - stopwatchStartTimeStamp;
4859
double timeOffset = 0;
4960
if (durationInSeconds >= maxUnsyncedSeconds)
5061
{ // acquire DateTime to sync
51-
stopwatchStartTimeStamp = Stopwatch.GetTimestamp();
52-
systemTimeIntervalStart = TimeSpan.FromTicks(DateTime.UtcNow.Ticks).TotalSeconds;
62+
UpdateSystemTime();
5363
}
5464
else
5565
{ // use Stopwatch offset

src/Ros2ForUnity/Scripts/Time/ROS2Clock.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public class ROS2Clock
2525
{
2626
private ITimeSource _timeSource;
2727

28-
public ROS2Clock() : this(new DotnetTimeSource())
29-
{ // By default, use DotnetTimeSource
28+
public ROS2Clock() : this(new ROS2TimeSource())
29+
{ // By default, use ROS2TimeSource
3030
}
3131

3232
public ROS2Clock(ITimeSource ts)

0 commit comments

Comments
 (0)