Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions rclrs/src/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ impl From<ClockType> for rcl_clock_type_t {
#[derive(Clone, Debug)]
pub struct Clock {
kind: ClockType,
// TODO(ekumen): Fix the extra pub here.
pub rcl_clock: Arc<Mutex<rcl_clock_t>>,
rcl_clock: Arc<Mutex<rcl_clock_t>>,
// TODO(luca) Implement jump callbacks
}

Expand Down Expand Up @@ -84,6 +83,11 @@ impl Clock {
}
}

/// Return the 'rcl_clock_t' of the Clock
pub(crate) fn get_rcl_clock(&self) -> &Arc<Mutex<rcl_clock_t>> {
&self.rcl_clock
}

/// Returns the clock's `ClockType`.
pub fn clock_type(&self) -> ClockType {
self.kind
Expand Down
27 changes: 17 additions & 10 deletions rclrs/src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct Timer {
}

impl Timer {

/// Creates a new timer (constructor)
pub fn new(clock: &Clock, context: &Context, period: i64) -> Result<Timer, RclrsError> {
Self::with_callback(clock, context, period, None)
}
Expand All @@ -23,8 +23,8 @@ impl Timer {
let timer_init_result = unsafe {
// SAFETY: Getting a default value is always safe.
rcl_timer = rcl_get_zero_initialized_timer();
let mut rcl_clock = clock.get_rcl_clock().lock().unwrap();
let allocator = rcutils_get_default_allocator();
let mut rcl_clock = clock.rcl_clock.lock().unwrap();
let mut rcl_context = context.handle.rcl_context.lock().unwrap();
// Callbacks will be handled in the WaitSet.
let rcl_timer_callback: rcl_timer_callback_t = None;
Expand All @@ -47,7 +47,8 @@ impl Timer {
})
}

pub fn timer_period_ns(&self) -> Result<i64, RclrsError> {
/// Gets the period of the timer in nanoseconds
pub fn get_timer_period_ns(&self) -> Result<i64, RclrsError> {
let mut timer_period_ns = 0;
let get_period_result = unsafe {
let rcl_timer = self.rcl_timer.lock().unwrap();
Expand All @@ -61,12 +62,14 @@ impl Timer {
})
}

/// Cancels the timer, stopping the execution of the callback
pub fn cancel(&self) -> Result<(), RclrsError> {
let mut rcl_timer = self.rcl_timer.lock().unwrap();
let cancel_result = unsafe { rcl_timer_cancel(&mut *rcl_timer) };
to_rclrs_result(cancel_result)
}

/// Checks whether the timer is canceled or not
pub fn is_canceled(&self) -> Result<bool, RclrsError> {
let mut is_canceled = false;
let is_canceled_result = unsafe {
Expand All @@ -81,6 +84,7 @@ impl Timer {
})
}

/// Retrieves the time since the last call to the callback
pub fn time_since_last_call(&self) -> Result<i64, RclrsError> {
let mut time_value_ns: i64 = 0;
let time_since_last_call_result = unsafe {
Expand All @@ -95,6 +99,7 @@ impl Timer {
})
}

/// Retrieves the time until the next call of the callback
pub fn time_until_next_call(&self) -> Result<i64, RclrsError> {
let mut time_value_ns: i64 = 0;
let time_until_next_call_result = unsafe {
Expand All @@ -109,18 +114,21 @@ impl Timer {
})
}

/// Resets the timer, setting the last call time to now
pub fn reset(&mut self) -> Result<(), RclrsError>
{
let mut rcl_timer = self.rcl_timer.lock().unwrap();
to_rclrs_result(unsafe {rcl_timer_reset(&mut *rcl_timer)})
}

/// Executes the callback of the timer (this is triggered by the executor or the node directly)
pub fn call(&mut self) -> Result<(), RclrsError>
{
let mut rcl_timer = self.rcl_timer.lock().unwrap();
to_rclrs_result(unsafe {rcl_timer_call(&mut *rcl_timer)})
}

/// Checks if the timer is ready (not canceled)
pub fn is_ready(&self) -> Result<bool, RclrsError>
{
let (is_ready, is_ready_result) = unsafe {
Expand All @@ -137,10 +145,9 @@ impl Timer {
})
}
// handle() -> RCLC Timer Type

// clock() -> Clock ?
}

/// 'Drop' trait implementation to be able to release the resources
impl Drop for rcl_timer_t {
fn drop(&mut self) {
// SAFETY: No preconditions for this function
Expand Down Expand Up @@ -215,7 +222,7 @@ mod tests {
let dut = Timer::new(&clock, &context, period);
assert!(dut.is_ok());
let dut = dut.unwrap();
let period_result = dut.timer_period_ns();
let period_result = dut.get_timer_period_ns();
assert!(period_result.is_ok());
let period_result = period_result.unwrap();
assert_eq!(period_result, 1e6 as i64);
Expand Down Expand Up @@ -293,14 +300,14 @@ mod tests {
let mut dut = Timer::new(&clock, &context, period_ns).unwrap();
let elapsed = period_ns - dut.time_until_next_call().unwrap();
assert!(elapsed < tolerance , "elapsed before reset: {}", elapsed);

thread::sleep(time::Duration::from_micros(1500));

let elapsed = period_ns - dut.time_until_next_call().unwrap();
assert!(elapsed > 1500000i64, "time_until_next_call before call: {}", elapsed);

assert!(dut.call().is_ok());

let elapsed = dut.time_until_next_call().unwrap();
assert!(elapsed < 500000i64, "time_until_next_call after call: {}", elapsed);
}
Expand All @@ -311,7 +318,7 @@ mod tests {
let context = Context::new(vec![]).unwrap();
let period_ns: i64 = 1e6 as i64; // 1 millisecond.
let dut = Timer::new(&clock, &context, period_ns).unwrap();

let is_ready = dut.is_ready();
assert!(is_ready.is_ok());
assert!(!is_ready.unwrap());
Expand Down