-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathClockSensor.cs
176 lines (149 loc) · 4.75 KB
/
ClockSensor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* Copyright (c) 2019-2021 LG Electronics, Inc.
*
* This software contains code licensed as described in LICENSE.
*
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using Simulator.Bridge;
using Simulator.Bridge.Data;
using Simulator.Utilities;
using Simulator.Sensors.UI;
namespace Simulator.Sensors
{
[SensorType("Clock", new[] { typeof(ClockData) })]
public partial class ClockSensor : SensorBase
{
Queue<Tuple<double, float, Action>> MessageQueue = new Queue<Tuple<double, float, Action>>();
BridgeInstance Bridge;
Publisher<ClockData> Publish;
bool Destroyed = false;
bool IsFirstFixedUpdate = true;
double LastTimestamp;
ClockData latestData;
float realTimeStart;
ClockData data;
private bool Sending = false;
public override SensorDistributionType DistributionType => SensorDistributionType.MainOrClient;
public override float PerformanceLoad { get; } = 0.05f;
public override void OnBridgeSetup(BridgeInstance bridge)
{
Bridge = bridge;
Publish = bridge.AddPublisher<ClockData>(Topic);
}
protected override void OnDestroy()
{
base.OnDestroy();
Destroyed = true;
}
protected override void Initialize()
{
Task.Run(Publisher);
realTimeStart = Time.time;
}
protected override void Deinitialize()
{
}
void Publisher()
{
var nextPublish = Stopwatch.GetTimestamp();
while (!Destroyed)
{
long now = Stopwatch.GetTimestamp();
if (now < nextPublish)
{
Thread.Sleep(0);
continue;
}
Tuple<double, float, Action> msg = null;
lock (MessageQueue)
{
if (MessageQueue.Count > 0)
{
msg = MessageQueue.Dequeue();
}
}
if (msg != null)
{
if (!Sending) // Drop this message if previous sending has not finished.
{
try
{
Sending = true;
msg.Item3();
}
catch
{
Sending = false;
}
}
nextPublish = now + (long)(Stopwatch.Frequency * msg.Item2);
LastTimestamp = msg.Item1;
}
}
}
void FixedUpdate()
{
if (IsFirstFixedUpdate)
{
if (Bridge != null && Bridge.Status == Status.Connected)
{
lock (MessageQueue)
{
MessageQueue.Clear();
}
}
IsFirstFixedUpdate = false;
}
var time = SimulatorManager.Instance.CurrentTime;
if (time < LastTimestamp)
{
return;
}
var data = new ClockData()
{
Clock = time,
};
latestData = data;
if (Bridge != null && Bridge.Status == Status.Connected)
{
lock (MessageQueue)
{
MessageQueue.Enqueue(Tuple.Create(time, Time.fixedDeltaTime,
(Action)(() => Publish(data, () => Sending = false))));
}
}
}
void Update()
{
IsFirstFixedUpdate = true;
}
[AnalysisMeasurement(MeasurementType.Duration)]
public double SimulationDuration => SimulatorManager.Instance.CurrentTime - SimulatorManager.Instance.SessionStartTime;
[AnalysisMeasurement(MeasurementType.Duration)]
public double RealtimeDuration => Time.time - realTimeStart;
public override void OnVisualize(Visualizer visualizer)
{
UnityEngine.Debug.Assert(visualizer != null);
if (latestData == null)
{
return;
}
var graphData = new Dictionary<string, object>()
{
{"Time", latestData.Clock},
{"Fixed DeltaTime", Time.fixedDeltaTime}
};
visualizer.UpdateGraphValues(graphData);
}
public override void OnVisualizeToggle(bool state)
{
//
}
}
}