Skip to content

Commit 6f9543c

Browse files
committed
Basic Timer type compiling :).
Signed-off-by: Agustin Alba Chicar <[email protected]>
1 parent a604ad7 commit 6f9543c

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed

rclrs/src/clock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl From<ClockType> for rcl_clock_type_t {
2828
#[derive(Clone, Debug)]
2929
pub struct Clock {
3030
kind: ClockType,
31-
rcl_clock: Arc<Mutex<rcl_clock_t>>,
31+
pub rcl_clock: Arc<Mutex<rcl_clock_t>>,
3232
// TODO(luca) Implement jump callbacks
3333
}
3434

rclrs/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mod qos;
1919
mod service;
2020
mod subscription;
2121
mod time;
22+
mod timer;
2223
mod time_source;
2324
mod vendor;
2425
mod wait;
@@ -48,6 +49,7 @@ pub use rcl_bindings::rmw_request_id_t;
4849
pub use service::*;
4950
pub use subscription::*;
5051
pub use time::*;
52+
pub use timer::*;
5153
use time_source::*;
5254
pub use wait::*;
5355

rclrs/src/rcl_bindings.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ cfg_if::cfg_if! {
8989
#[derive(Debug)]
9090
pub struct rcl_wait_set_t;
9191

92+
#[repr(C)]
93+
#[derive(Debug)]
94+
pub struct rcl_timer_t;
95+
9296
#[repr(C)]
9397
#[derive(Debug)]
9498
pub struct rcutils_string_array_t;

rclrs/src/timer.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
use crate::{
2+
clock::Clock, context::Context, error::RclrsError, rcl_bindings::*, to_rclrs_result
3+
};
4+
use std::sync::{Arc, Mutex};
5+
6+
7+
#[derive(Debug)]
8+
pub struct Timer {
9+
rcl_timer: Arc<Mutex<rcl_timer_t>>,
10+
}
11+
12+
unsafe extern "C" fn timer_callback(_: *mut rcl_timer_t, time_since_last_callback_ns: i64) {
13+
println!("timer_callback, time_since_last_callback_ns {0}", time_since_last_callback_ns);
14+
}
15+
16+
impl Timer {
17+
pub fn new(clock: &Clock, context: &Context, period: i64) -> Result<Timer, RclrsError> {
18+
let mut rcl_timer;
19+
let timer_init_result;
20+
unsafe {
21+
// SAFETY: Getting a default value is always safe.
22+
rcl_timer = rcl_get_zero_initialized_timer();
23+
let allocator = rcutils_get_default_allocator();
24+
let mut rcl_clock = clock.rcl_clock.lock().unwrap();
25+
let mut rcl_context = context.handle.rcl_context.lock().unwrap();
26+
let callback: rcl_timer_callback_t = Some(timer_callback);
27+
// Function will return Err(_) only if there isn't enough memory to allocate a clock
28+
// object.
29+
timer_init_result = rcl_timer_init(
30+
&mut rcl_timer,
31+
&mut *rcl_clock,
32+
&mut *rcl_context,
33+
period,
34+
callback,
35+
allocator,
36+
);
37+
}
38+
to_rclrs_result(timer_init_result).map(|_| {
39+
Timer {
40+
rcl_timer: Arc::new(Mutex::new(rcl_timer))
41+
}
42+
})
43+
}
44+
45+
// handle() -> RCLC Timer Type
46+
47+
// destroy() -> None
48+
49+
// clock() -> Clock ?
50+
51+
// timer_period_ns -> i64 ?
52+
53+
// is_ready() -> bool
54+
55+
// is_cancelled() -> bool
56+
57+
// cancel() -> None
58+
59+
// reset() -> None
60+
61+
// time_since_last_call() -> i64
62+
63+
// time_until_next_call() -> Option<i64>
64+
65+
}
66+
67+
impl Drop for rcl_timer_t {
68+
fn drop(&mut self) {
69+
// SAFETY: No preconditions for this function
70+
let rc = unsafe { rcl_timer_fini(&mut *self) };
71+
if let Err(e) = to_rclrs_result(rc) {
72+
panic!("Unable to release Timer. {:?}", e)
73+
}
74+
}
75+
}
76+
77+
// SAFETY: The functions accessing this type, including drop(), shouldn't care about the thread
78+
// they are running in. Therefore, this type can be safely sent to another thread.
79+
unsafe impl Send for rcl_timer_t {}
80+
81+
#[cfg(test)]
82+
mod tests {
83+
use super::*;
84+
85+
#[test]
86+
fn traits() {
87+
use crate::test_helpers::*;
88+
89+
assert_send::<Timer>();
90+
assert_sync::<Timer>();
91+
}
92+
}

0 commit comments

Comments
 (0)