|
| 1 | +// Copyright 2022 Robotec.ai. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using System.Threading; |
| 16 | +using UnityEngine; |
| 17 | + |
| 18 | +namespace ROS2 |
| 19 | +{ |
| 20 | + |
| 21 | +/// <summary> |
| 22 | +/// ros2 time source (system time by default). |
| 23 | +/// </summary> |
| 24 | +public class ROS2ScalableTimeSource : ITimeSource |
| 25 | +{ |
| 26 | + private Thread mainThread; |
| 27 | + private double lastReadingSecs; |
| 28 | + private ROS2.Clock clock; |
| 29 | + private double initialTime = 0; |
| 30 | + private double initialTimeScale = 0; |
| 31 | + private bool initialTimeAcquired = false; |
| 32 | + private bool initialTimeScaleAcquired = false; |
| 33 | + private bool timeScaleChanged = false; |
| 34 | + |
| 35 | + public ROS2ScalableTimeSource() |
| 36 | + { |
| 37 | + mainThread = Thread.CurrentThread; |
| 38 | + } |
| 39 | + |
| 40 | + public void GetTime(out int seconds, out uint nanoseconds) |
| 41 | + { |
| 42 | + if (!ROS2.Ros2cs.Ok()) |
| 43 | + { |
| 44 | + seconds = 0; |
| 45 | + nanoseconds = 0; |
| 46 | + Debug.LogWarning("Cannot acquire valid ros time, ros either not initialized or shut down already"); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + if (clock == null) |
| 51 | + { // Create clock which uses system time by default (unless use_sim_time is set in ros2) |
| 52 | + clock = new ROS2.Clock(); |
| 53 | + } |
| 54 | + |
| 55 | + if (!initialTimeScaleAcquired) |
| 56 | + { |
| 57 | + initialTimeScaleAcquired = true; |
| 58 | + initialTimeScale = Time.timeScale; |
| 59 | + } |
| 60 | + |
| 61 | + if (initialTimeScale != Time.timeScale) |
| 62 | + { |
| 63 | + timeScaleChanged = true; |
| 64 | + } |
| 65 | + |
| 66 | + lastReadingSecs = mainThread.Equals(Thread.CurrentThread) ? Time.timeAsDouble : lastReadingSecs; |
| 67 | + |
| 68 | + if (initialTimeScale == 1.0 && !timeScaleChanged) |
| 69 | + { |
| 70 | + TimeUtils.TimeFromTotalSeconds(clock.Now.Seconds, out seconds, out nanoseconds); |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + if (!initialTimeAcquired) |
| 75 | + { |
| 76 | + initialTimeAcquired = true; |
| 77 | + initialTime = clock.Now.Seconds - Time.timeAsDouble; |
| 78 | + } |
| 79 | + TimeUtils.TimeFromTotalSeconds(lastReadingSecs + initialTime, out seconds, out nanoseconds); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + ~ROS2ScalableTimeSource() |
| 84 | + { |
| 85 | + if (clock != null) |
| 86 | + { |
| 87 | + clock.Dispose(); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +} // namespace ROS2 |
0 commit comments