3333
3434import java .util .concurrent .LinkedBlockingQueue ;
3535
36+ /**
37+ * Implementation of the ROS time source abstraction.
38+ *
39+ * This class updates the time of one or more @{link Clock}s with a ROS time source.
40+ * A ROS time source is expected to be published to the "/clock" topic.
41+ * A TimeSource object needs a @{link Node} in order create a subscription to the "/clock" topic.
42+ *
43+ * There are two ways to enable (or disable) using a ROS time source:
44+ *
45+ * 1. Calling the method @{link #setRosTimeIsActive(boolean)}, or
46+ * 2. Setting the 'use_sim_time' parameter on the @{link Node} that is attached to this TimeSource
47+ *
48+ * More information can be found in the design document
49+ * <a href="http://design.ros2.org/articles/clock_and_time.html">Clock and Time</a>.
50+ */
3651public final class TimeSource {
3752 private Node node ;
3853 private boolean rosTimeIsActive ;
@@ -43,10 +58,18 @@ public final class TimeSource {
4358
4459 private static final Logger logger = LoggerFactory .getLogger (TimeSource .class );
4560
61+ /**
62+ * Default constructor.
63+ */
4664 public TimeSource () {
4765 this (null );
4866 }
4967
68+ /**
69+ * Attach a node to the time source.
70+ *
71+ * @param node The node to attach to the time source.
72+ */
5073 public TimeSource (Node node ) {
5174 this .rosTimeIsActive = false ;
5275 this .associatedClocks = new LinkedBlockingQueue <Clock >();
@@ -57,6 +80,10 @@ public TimeSource(Node node) {
5780 }
5881 }
5982
83+ /**
84+ * @return true if ROS time is active, false otherwise.
85+ * Being active means we are listening to the "/clock" topic.
86+ */
6087 public boolean getRosTimeIsActive () {
6188 return this .rosTimeIsActive ;
6289 }
@@ -77,6 +104,11 @@ public void accept(final rosgraph_msgs.msg.Clock msg) {
77104 }
78105 }
79106
107+ /**
108+ * Enable or disable ROS time.
109+ *
110+ * @param enabled Flag to enable or disable ROS time.
111+ */
80112 public void setRosTimeIsActive (boolean enabled ) {
81113 if (this .rosTimeIsActive == enabled ) {
82114 return ;
@@ -97,6 +129,14 @@ public void setRosTimeIsActive(boolean enabled) {
97129 }
98130 }
99131
132+ /**
133+ * Attach a @{link Node} to the time source.
134+ *
135+ * This node is used to create a subscription to the "/clock" topic.
136+ * Any previously attached @{link Node} is detached.
137+ *
138+ * @param node The node to attach to the time source.
139+ */
100140 public void attachNode (Node node ) {
101141 if (this .node != null ) {
102142 detachNode ();
@@ -148,6 +188,13 @@ public rcl_interfaces.msg.SetParametersResult callback(List<ParameterVariant> pa
148188 this .node .addOnSetParametersCallback (this .simTimeCB );
149189 }
150190
191+ /**
192+ * Detach a @{link Node} from the time source.
193+ *
194+ * Unsubscribes from the "/clock" topic, effectively disabling ROS time.
195+ *
196+ * If no Node is attached, nothing happens.
197+ */
151198 public void detachNode () {
152199 if (this .node == null ) {
153200 return ;
@@ -157,6 +204,15 @@ public void detachNode() {
157204 this .node = null ;
158205 }
159206
207+ /**
208+ * Attach a @{link Clock} to the time source.
209+ *
210+ * More than one clock may be attached to a time source.
211+ * Attached clocks will be updated to the the time received on the "/clock" topic.
212+ *
213+ * @param clock The Clock to attach.
214+ * Must be of type @{link ClockType.ROS_TIME}, otherwise an exception is thrown.
215+ */
160216 public void attachClock (Clock clock ) {
161217 if (clock .getClockType () != ClockType .ROS_TIME ) {
162218 // TODO(clalancette): Make this a custom exception
@@ -167,6 +223,13 @@ public void attachClock(Clock clock) {
167223 this .associatedClocks .add (clock );
168224 }
169225
226+ /**
227+ * Detach a @{link Clock} from the time source.
228+ *
229+ * The Clock will no longer receive time updates from this time source.
230+ *
231+ * @param clock The Clock to detach.
232+ */
170233 public void detachClock (Clock clock ) {
171234 this .associatedClocks .remove (clock );
172235 }
0 commit comments