Skip to content

Commit a60d9f3

Browse files
committed
Implement time_until_next_call
Signed-off-by: Agustin Alba Chicar <[email protected]>
1 parent 7b42c02 commit a60d9f3

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

rclrs/src/timer.rs

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ impl Timer {
5555
})
5656
}
5757

58+
pub fn time_until_next_call(&self) -> Result<i64, RclrsError> {
59+
let mut time_value_ns: i64 = 0;
60+
let time_until_next_call_result = unsafe {
61+
let rcl_timer = self.rcl_timer.lock().unwrap();
62+
rcl_timer_get_time_until_next_call(
63+
&* rcl_timer,
64+
&mut time_value_ns
65+
)
66+
};
67+
to_rclrs_result(time_until_next_call_result).map(|_| {
68+
time_value_ns
69+
})
70+
}
71+
5872
// handle() -> RCLC Timer Type
5973

6074
// destroy() -> None
@@ -94,6 +108,7 @@ unsafe impl Send for rcl_timer_t {}
94108
#[cfg(test)]
95109
mod tests {
96110
use super::*;
111+
use std::{thread, time};
97112

98113
#[test]
99114
fn traits() {
@@ -107,7 +122,7 @@ mod tests {
107122
fn test_new_with_system_clock() {
108123
let clock = Clock::system();
109124
let context = Context::new(vec![]).unwrap();
110-
let period: i64 = 1000000000; // 1000 milliseconds.
125+
let period: i64 = 1e6 as i64; // 1 milliseconds.
111126

112127
let dut = Timer::new(&clock, &context, period);
113128
assert!(dut.is_ok());
@@ -117,7 +132,7 @@ mod tests {
117132
fn test_new_with_steady_clock() {
118133
let clock = Clock::steady();
119134
let context = Context::new(vec![]).unwrap();
120-
let period: i64 = 1000000000; // 1000 milliseconds.
135+
let period: i64 = 1e6 as i64; // 1 milliseconds.
121136

122137
let dut = Timer::new(&clock, &context, period);
123138
assert!(dut.is_ok());
@@ -134,11 +149,43 @@ mod tests {
134149
// Ros time is set, should return the value that was set
135150
assert_eq!(clock.now().nsec, set_time);
136151

137-
138152
let context = Context::new(vec![]).unwrap();
139-
let period: i64 = 1000000000; // 1000 milliseconds.
153+
let period: i64 = 1e6 as i64; // 1 milliseconds..
140154

141155
let dut = Timer::new(&clock, &context, period);
142156
assert!(dut.is_ok());
143157
}
158+
159+
#[test]
160+
fn test_time_since_last_call_before_first_event() {
161+
let clock = Clock::steady();
162+
let context = Context::new(vec![]).unwrap();
163+
let period_ns: i64 = 2e6 as i64; // 2 milliseconds.
164+
let sleep_period_ms = time::Duration::from_millis(1);
165+
166+
let dut = Timer::new(&clock, &context, period_ns);
167+
assert!(dut.is_ok());
168+
let dut = dut.unwrap();
169+
thread::sleep(sleep_period_ms);
170+
let time_since_last_call = dut.time_since_last_call();
171+
assert!(time_since_last_call.is_ok());
172+
let time_since_last_call = time_since_last_call.unwrap();
173+
assert!(time_since_last_call > 9e5 as i64, "time_since_last_call: {}", time_since_last_call);
174+
}
175+
176+
#[test]
177+
fn test_time_until_next_call_before_first_event() {
178+
let clock = Clock::steady();
179+
let context = Context::new(vec![]).unwrap();
180+
let period_ns: i64 = 2e6 as i64; // 2 milliseconds.
181+
let sleep_period_ms = time::Duration::from_millis(1);
182+
183+
let dut = Timer::new(&clock, &context, period_ns);
184+
assert!(dut.is_ok());
185+
let dut = dut.unwrap();
186+
let time_until_next_call = dut.time_until_next_call();
187+
assert!(time_until_next_call.is_ok());
188+
let time_until_next_call = time_until_next_call.unwrap();
189+
assert!(time_until_next_call < period_ns, "time_until_next_call: {}", time_until_next_call);
190+
}
144191
}

0 commit comments

Comments
 (0)