Skip to content

Commit

Permalink
Merge pull request #248 from tier4/feature/clock-timescale-independent
Browse files Browse the repository at this point in the history
Made clock published independently from time scale
  • Loading branch information
mackierx111 authored Jan 22, 2024
2 parents 42e3c98 + fb82ec2 commit 45cce7b
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions Assets/AWSIM/Scripts/ROS/ClockPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,50 @@ public class ClockPublisher : MonoBehaviour

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

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

void Start()
{
InvokeRepeating("PublishClock", 1.0f, 1.0f/publishHz);
}
#region [Life Cycle]

// Start is called before the first frame update
void Awake()
{
var qos = qosSettings.GetQoSProfile();
clockPublisher = SimulatorROS2Node.CreatePublisher<rosgraph_msgs.msg.Clock>(topic, qos);
clockMsg = new rosgraph_msgs.msg.Clock();
}


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

#endregion

void Start()
{
timeScale = Time.timeScale;
InvokeRepeating("PublishClock", 1.0f, timeScale/publishHz);
}

void Update()
{
if (Mathf.Abs(Time.timeScale - timeScale) > 0.01f)
{
timeScale = Time.timeScale;
OnTimeScaleChanged();
}
}

void OnTimeScaleChanged()
{
CancelInvoke();
InvokeRepeating("PublishClock", 0.0f, timeScale/publishHz);
}

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

0 comments on commit 45cce7b

Please sign in to comment.