@@ -32,6 +32,7 @@ const Publisher = require('./publisher.js');
3232const QoS = require ( './qos.js' ) ;
3333const Service = require ( './service.js' ) ;
3434const Subscription = require ( './subscription.js' ) ;
35+ const TimeSource = require ( './time_source.js' ) ;
3536const Timer = require ( './timer.js' ) ;
3637
3738const debug = require ( 'debug' ) ( 'rclnodejs:node' ) ;
@@ -91,6 +92,13 @@ class Node {
9192 }
9293 }
9394
95+ // Clock that has support for ROS time.
96+ // Note: parameter overrides and parameter event publisher need to be ready at this point
97+ // to be able to declare 'use_sim_time' if it was not declared yet.
98+ this . _clock = new Clock . ROSClock ( ) ;
99+ this . _timeSource = new TimeSource ( this ) ;
100+ this . _timeSource . attachClock ( this . _clock ) ;
101+
94102 if ( options . startParameterServices ) {
95103 this . _parameterService = new ParameterService ( this ) ;
96104 this . _parameterService . start ( ) ;
@@ -217,9 +225,15 @@ class Node {
217225 * @param {number } period - The number representing period in millisecond.
218226 * @param {function } callback - The callback to be called when timeout.
219227 * @param {Context } context - The context, default is Context.defaultContext().
228+ * @param {Clock } clock - The clock which the timer gets time from.
220229 * @return {Timer } - An instance of Timer.
221230 */
222- createTimer ( period , callback , context = Context . defaultContext ( ) ) {
231+ createTimer (
232+ period ,
233+ callback ,
234+ context = Context . defaultContext ( ) ,
235+ clock = null
236+ ) {
223237 if ( typeof period !== 'number' || typeof callback !== 'function' ) {
224238 throw new TypeError ( 'Invalid argument' ) ;
225239 }
@@ -237,7 +251,13 @@ class Node {
237251 ) ;
238252 }
239253
240- let timerHandle = rclnodejs . createTimer ( period , context . handle ( ) ) ;
254+ const timerClock = clock || this . _clock ;
255+
256+ let timerHandle = rclnodejs . createTimer (
257+ timerClock . handle ,
258+ context . handle ( ) ,
259+ period
260+ ) ;
241261 let timer = new Timer ( timerHandle , period , callback ) ;
242262 debug ( 'Finish creating timer, period = %d.' , period ) ;
243263 this . _timers . push ( timer ) ;
@@ -458,6 +478,7 @@ class Node {
458478 }
459479
460480 this . handle . release ( ) ;
481+ this . _clock = null ;
461482 this . _timers = [ ] ;
462483 this . _publishers = [ ] ;
463484 this . _subscriptions = [ ] ;
@@ -560,6 +581,14 @@ class Node {
560581 return this . _logger ;
561582 }
562583
584+ /**
585+ * Get the clock used by the node.
586+ * @returns {Clock } - The nodes clock.
587+ */
588+ getClock ( ) {
589+ return this . _clock ;
590+ }
591+
563592 /**
564593 * Get the list of published topics discovered by the provided node for the remote node name.
565594 * @param {string } nodeName - The name of the node.
@@ -1036,7 +1065,7 @@ class Node {
10361065 PARAMETER_EVENT_MSG_TYPE
10371066 ) ) ( ) ;
10381067
1039- const secondsAndNanos = new Clock . ROSClock ( ) . now ( ) . secondsAndNanoseconds ;
1068+ const secondsAndNanos = this . _clock . now ( ) . secondsAndNanoseconds ;
10401069 parameterEvent . stamp = {
10411070 sec : secondsAndNanos . seconds ,
10421071 nanosec : secondsAndNanos . nanoseconds ,
0 commit comments