Skip to content

Commit 7b42c02

Browse files
committed
Evaluates the Timer::new() againts different clock types.
Signed-off-by: Agustin Alba Chicar <[email protected]>
1 parent 6f9543c commit 7b42c02

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

rclrs/src/clock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +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.
3132
pub rcl_clock: Arc<Mutex<rcl_clock_t>>,
3233
// TODO(luca) Implement jump callbacks
3334
}

rclrs/src/timer.rs

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ unsafe extern "C" fn timer_callback(_: *mut rcl_timer_t, time_since_last_callbac
1616
impl Timer {
1717
pub fn new(clock: &Clock, context: &Context, period: i64) -> Result<Timer, RclrsError> {
1818
let mut rcl_timer;
19-
let timer_init_result;
20-
unsafe {
19+
let timer_init_result = unsafe {
2120
// SAFETY: Getting a default value is always safe.
2221
rcl_timer = rcl_get_zero_initialized_timer();
2322
let allocator = rcutils_get_default_allocator();
@@ -26,22 +25,36 @@ impl Timer {
2625
let callback: rcl_timer_callback_t = Some(timer_callback);
2726
// Function will return Err(_) only if there isn't enough memory to allocate a clock
2827
// object.
29-
timer_init_result = rcl_timer_init(
28+
rcl_timer_init(
3029
&mut rcl_timer,
3130
&mut *rcl_clock,
3231
&mut *rcl_context,
3332
period,
3433
callback,
3534
allocator,
36-
);
37-
}
35+
)
36+
};
3837
to_rclrs_result(timer_init_result).map(|_| {
3938
Timer {
4039
rcl_timer: Arc::new(Mutex::new(rcl_timer))
4140
}
4241
})
4342
}
4443

44+
pub fn time_since_last_call(&self) -> Result<i64, RclrsError> {
45+
let mut time_value_ns: i64 = 0;
46+
let time_since_last_call_result = unsafe {
47+
let rcl_timer = self.rcl_timer.lock().unwrap();
48+
rcl_timer_get_time_since_last_call(
49+
&* rcl_timer,
50+
&mut time_value_ns
51+
)
52+
};
53+
to_rclrs_result(time_since_last_call_result).map(|_| {
54+
time_value_ns
55+
})
56+
}
57+
4558
// handle() -> RCLC Timer Type
4659

4760
// destroy() -> None
@@ -89,4 +102,43 @@ mod tests {
89102
assert_send::<Timer>();
90103
assert_sync::<Timer>();
91104
}
105+
106+
#[test]
107+
fn test_new_with_system_clock() {
108+
let clock = Clock::system();
109+
let context = Context::new(vec![]).unwrap();
110+
let period: i64 = 1000000000; // 1000 milliseconds.
111+
112+
let dut = Timer::new(&clock, &context, period);
113+
assert!(dut.is_ok());
114+
}
115+
116+
#[test]
117+
fn test_new_with_steady_clock() {
118+
let clock = Clock::steady();
119+
let context = Context::new(vec![]).unwrap();
120+
let period: i64 = 1000000000; // 1000 milliseconds.
121+
122+
let dut = Timer::new(&clock, &context, period);
123+
assert!(dut.is_ok());
124+
}
125+
126+
#[ignore = "SIGSEGV when creating the timer with Clock::with_source()."]
127+
#[test]
128+
fn test_new_with_source_clock() {
129+
let (clock, source) = Clock::with_source();
130+
// No manual time set, it should default to 0
131+
assert!(clock.now().nsec == 0);
132+
let set_time = 1234i64;
133+
source.set_ros_time_override(set_time);
134+
// Ros time is set, should return the value that was set
135+
assert_eq!(clock.now().nsec, set_time);
136+
137+
138+
let context = Context::new(vec![]).unwrap();
139+
let period: i64 = 1000000000; // 1000 milliseconds.
140+
141+
let dut = Timer::new(&clock, &context, period);
142+
assert!(dut.is_ok());
143+
}
92144
}

0 commit comments

Comments
 (0)