Skip to content

Commit

Permalink
Merge pull request #212 from tier4/feature/flexible_clock
Browse files Browse the repository at this point in the history
Flexible clock (FixedUpdate independent)
  • Loading branch information
mackierx111 authored Oct 30, 2023
2 parents 1a96d7b + cfb8d6f commit 3655578
Showing 1 changed file with 9 additions and 23 deletions.
32 changes: 9 additions & 23 deletions Assets/AWSIM/Scripts/ROS/ClockPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,21 @@ public class ClockPublisher : MonoBehaviour
{
[SerializeField] string topic;
[SerializeField] QoSSettings qosSettings;
[SerializeField, Range(0, 100)] int publishHz;
[SerializeField, Range(1, 100)] int publishHz;

IPublisher<rosgraph_msgs.msg.Clock> clockPublisher;
rosgraph_msgs.msg.Clock clockMsg;
float timer = 0;

void Reset()
void PublishClock()
{
topic = "/clock";
qosSettings = new QoSSettings()
{
ReliabilityPolicy = ReliabilityPolicy.QOS_POLICY_RELIABILITY_BEST_EFFORT,
DurabilityPolicy = DurabilityPolicy.QOS_POLICY_DURABILITY_VOLATILE,
HistoryPolicy = HistoryPolicy.QOS_POLICY_HISTORY_KEEP_LAST,
Depth = 1,
};
publishHz = 100;
SimulatorROS2Node.UpdateROSClockTime(clockMsg.Clock_);
clockPublisher.Publish(clockMsg);
}

void Start()
{
InvokeRepeating("PublishClock", 1.0f, 1.0f/publishHz);
}

// Start is called before the first frame update
Expand All @@ -39,18 +37,6 @@ void Awake()
clockMsg = new rosgraph_msgs.msg.Clock();
}

void FixedUpdate()
{
timer += Time.deltaTime;
var interval = 1.0f / publishHz;
interval -= 0.00001f; // Allow for accuracy errors.
if (timer < interval)
return;
timer = 0;

SimulatorROS2Node.UpdateROSClockTime(clockMsg.Clock_);
clockPublisher.Publish(clockMsg);
}

void OnDestroy()
{
Expand Down

0 comments on commit 3655578

Please sign in to comment.