@@ -13,7 +13,7 @@ pub struct Timer {
1313}
1414
1515impl Timer {
16-
16+ /// Creates a new timer (constructor)
1717 pub fn new ( clock : & Clock , context : & Context , period : i64 ) -> Result < Timer , RclrsError > {
1818 Self :: with_callback ( clock, context, period, None )
1919 }
@@ -23,8 +23,8 @@ impl Timer {
2323 let timer_init_result = unsafe {
2424 // SAFETY: Getting a default value is always safe.
2525 rcl_timer = rcl_get_zero_initialized_timer ( ) ;
26+ let mut rcl_clock = clock. get_rcl_clock ( ) . lock ( ) . unwrap ( ) ;
2627 let allocator = rcutils_get_default_allocator ( ) ;
27- let mut rcl_clock = clock. rcl_clock . lock ( ) . unwrap ( ) ;
2828 let mut rcl_context = context. handle . rcl_context . lock ( ) . unwrap ( ) ;
2929 // Callbacks will be handled in the WaitSet.
3030 let rcl_timer_callback: rcl_timer_callback_t = None ;
@@ -47,7 +47,8 @@ impl Timer {
4747 } )
4848 }
4949
50- pub fn timer_period_ns ( & self ) -> Result < i64 , RclrsError > {
50+ /// Gets the period of the timer in nanoseconds
51+ pub fn get_timer_period_ns ( & self ) -> Result < i64 , RclrsError > {
5152 let mut timer_period_ns = 0 ;
5253 let get_period_result = unsafe {
5354 let rcl_timer = self . rcl_timer . lock ( ) . unwrap ( ) ;
@@ -61,12 +62,14 @@ impl Timer {
6162 } )
6263 }
6364
65+ /// Cancels the timer, stopping the execution of the callback
6466 pub fn cancel ( & self ) -> Result < ( ) , RclrsError > {
6567 let mut rcl_timer = self . rcl_timer . lock ( ) . unwrap ( ) ;
6668 let cancel_result = unsafe { rcl_timer_cancel ( & mut * rcl_timer) } ;
6769 to_rclrs_result ( cancel_result)
6870 }
6971
72+ /// Checks whether the timer is canceled or not
7073 pub fn is_canceled ( & self ) -> Result < bool , RclrsError > {
7174 let mut is_canceled = false ;
7275 let is_canceled_result = unsafe {
@@ -81,6 +84,7 @@ impl Timer {
8184 } )
8285 }
8386
87+ /// Retrieves the time since the last call to the callback
8488 pub fn time_since_last_call ( & self ) -> Result < i64 , RclrsError > {
8589 let mut time_value_ns: i64 = 0 ;
8690 let time_since_last_call_result = unsafe {
@@ -95,6 +99,7 @@ impl Timer {
9599 } )
96100 }
97101
102+ /// Retrieves the time until the next call of the callback
98103 pub fn time_until_next_call ( & self ) -> Result < i64 , RclrsError > {
99104 let mut time_value_ns: i64 = 0 ;
100105 let time_until_next_call_result = unsafe {
@@ -109,18 +114,21 @@ impl Timer {
109114 } )
110115 }
111116
117+ /// Resets the timer, setting the last call time to now
112118 pub fn reset ( & mut self ) -> Result < ( ) , RclrsError >
113119 {
114120 let mut rcl_timer = self . rcl_timer . lock ( ) . unwrap ( ) ;
115121 to_rclrs_result ( unsafe { rcl_timer_reset ( & mut * rcl_timer) } )
116122 }
117123
124+ /// Executes the callback of the timer (this is triggered by the executor or the node directly)
118125 pub fn call ( & mut self ) -> Result < ( ) , RclrsError >
119126 {
120127 let mut rcl_timer = self . rcl_timer . lock ( ) . unwrap ( ) ;
121128 to_rclrs_result ( unsafe { rcl_timer_call ( & mut * rcl_timer) } )
122129 }
123130
131+ /// Checks if the timer is ready (not canceled)
124132 pub fn is_ready ( & self ) -> Result < bool , RclrsError >
125133 {
126134 let ( is_ready, is_ready_result) = unsafe {
@@ -137,10 +145,9 @@ impl Timer {
137145 } )
138146 }
139147 // handle() -> RCLC Timer Type
140-
141- // clock() -> Clock ?
142148}
143149
150+ /// 'Drop' trait implementation to be able to release the resources
144151impl Drop for rcl_timer_t {
145152 fn drop ( & mut self ) {
146153 // SAFETY: No preconditions for this function
@@ -215,7 +222,7 @@ mod tests {
215222 let dut = Timer :: new ( & clock, & context, period) ;
216223 assert ! ( dut. is_ok( ) ) ;
217224 let dut = dut. unwrap ( ) ;
218- let period_result = dut. timer_period_ns ( ) ;
225+ let period_result = dut. get_timer_period_ns ( ) ;
219226 assert ! ( period_result. is_ok( ) ) ;
220227 let period_result = period_result. unwrap ( ) ;
221228 assert_eq ! ( period_result, 1e6 as i64 ) ;
@@ -293,14 +300,14 @@ mod tests {
293300 let mut dut = Timer :: new ( & clock, & context, period_ns) . unwrap ( ) ;
294301 let elapsed = period_ns - dut. time_until_next_call ( ) . unwrap ( ) ;
295302 assert ! ( elapsed < tolerance , "elapsed before reset: {}" , elapsed) ;
296-
303+
297304 thread:: sleep ( time:: Duration :: from_micros ( 1500 ) ) ;
298305
299306 let elapsed = period_ns - dut. time_until_next_call ( ) . unwrap ( ) ;
300307 assert ! ( elapsed > 1500000i64 , "time_until_next_call before call: {}" , elapsed) ;
301-
308+
302309 assert ! ( dut. call( ) . is_ok( ) ) ;
303-
310+
304311 let elapsed = dut. time_until_next_call ( ) . unwrap ( ) ;
305312 assert ! ( elapsed < 500000i64 , "time_until_next_call after call: {}" , elapsed) ;
306313 }
@@ -311,7 +318,7 @@ mod tests {
311318 let context = Context :: new ( vec ! [ ] ) . unwrap ( ) ;
312319 let period_ns: i64 = 1e6 as i64 ; // 1 millisecond.
313320 let dut = Timer :: new ( & clock, & context, period_ns) . unwrap ( ) ;
314-
321+
315322 let is_ready = dut. is_ready ( ) ;
316323 assert ! ( is_ready. is_ok( ) ) ;
317324 assert ! ( !is_ready. unwrap( ) ) ;
0 commit comments