@@ -16,9 +16,12 @@ use crate::{
1616 rcl_bindings:: * , Client , ClientBase , Clock , Context , ContextHandle , GuardCondition , LogParams ,
1717 Logger , ParameterBuilder , ParameterInterface , ParameterVariant , Parameters , Publisher ,
1818 QoSProfile , RclrsError , Service , ServiceBase , Subscription , SubscriptionBase ,
19- SubscriptionCallback , TimeSource , ToLogParams , ENTITY_LIFECYCLE_MUTEX ,
19+ SubscriptionCallback , Timer , TimerCallback , TimeSource , ToLogParams , ENTITY_LIFECYCLE_MUTEX ,
2020} ;
2121
22+ /// Constant conversion from seconds to nanoseconds
23+ const S_TO_NS : f64 = 1e9 ;
24+
2225// SAFETY: The functions accessing this type, including drop(), shouldn't care about the thread
2326// they are running in. Therefore, this type can be safely sent to another thread.
2427unsafe impl Send for rcl_node_t { }
@@ -63,6 +66,7 @@ pub struct Node {
6366 pub ( crate ) guard_conditions_mtx : Mutex < Vec < Weak < GuardCondition > > > ,
6467 pub ( crate ) services_mtx : Mutex < Vec < Weak < dyn ServiceBase > > > ,
6568 pub ( crate ) subscriptions_mtx : Mutex < Vec < Weak < dyn SubscriptionBase > > > ,
69+ pub ( crate ) timers_mtx : Mutex < Vec < Weak < Timer > > > ,
6670 time_source : TimeSource ,
6771 parameter : ParameterInterface ,
6872 pub ( crate ) handle : Arc < NodeHandle > ,
@@ -340,6 +344,31 @@ impl Node {
340344 Ok ( subscription)
341345 }
342346
347+ /// Creates a [`Timer`]
348+ pub ( crate ) fn create_timer (
349+ & self ,
350+ timer_period_s : i64 ,
351+ context : Context ,
352+ callback : Option < TimerCallback > ,
353+ clock : Option < Clock >
354+ ) -> Arc < Timer > {
355+ let timer_period_ns = ( timer_period_s as f64 * S_TO_NS ) as i64 ;
356+ let clock_used;
357+ match clock {
358+ Some ( value) => {
359+ clock_used = value;
360+ }
361+ None => {
362+ clock_used = self . get_clock ( ) ;
363+ }
364+ }
365+ let context = Context :: new ( vec ! [ ] ) . unwrap ( ) ;
366+ let timer = Timer :: new ( & clock_used, & context, timer_period_ns) ;
367+ let timer = Arc :: new ( timer. unwrap ( ) ) ;
368+ self . timers_mtx . lock ( ) . unwrap ( ) . push ( Arc :: downgrade ( & timer) as Weak < Timer > ) ;
369+ timer
370+ }
371+
343372 /// Returns the subscriptions that have not been dropped yet.
344373 pub ( crate ) fn live_subscriptions ( & self ) -> Vec < Arc < dyn SubscriptionBase > > {
345374 { self . subscriptions_mtx . lock ( ) . unwrap ( ) }
0 commit comments