Skip to content

Commit 1095351

Browse files
Added comments to avoid warnings (ros2-rust#3)
* Added timer_period_ns * Adds Timer::is_ready(). Signed-off-by: Agustin Alba Chicar <ag.albachicar@gmail.com> * Added comments to avoid warnings * Added the getter for rcl_clock_t --------- Signed-off-by: Agustin Alba Chicar <ag.albachicar@gmail.com> Co-authored-by: Agustin Alba Chicar <ag.albachicar@gmail.com>
1 parent 132c9db commit 1095351

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

rclrs/src/clock.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ impl From<ClockType> for rcl_clock_type_t {
2828
#[derive(Clone, Debug)]
2929
pub struct Clock {
3030
kind: ClockType,
31-
// TODO(ekumen): Fix the extra pub here.
32-
pub rcl_clock: Arc<Mutex<rcl_clock_t>>,
31+
rcl_clock: Arc<Mutex<rcl_clock_t>>,
3332
// TODO(luca) Implement jump callbacks
3433
}
3534

@@ -84,6 +83,11 @@ impl Clock {
8483
}
8584
}
8685

86+
/// Return the 'rcl_clock_t' of the Clock
87+
pub(crate) fn get_rcl_clock(&self) -> &Arc<Mutex<rcl_clock_t>> {
88+
&self.rcl_clock
89+
}
90+
8791
/// Returns the clock's `ClockType`.
8892
pub fn clock_type(&self) -> ClockType {
8993
self.kind

rclrs/src/timer.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub struct Timer {
1313
}
1414

1515
impl 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
144151
impl 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

Comments
 (0)