Skip to content

Commit e482b2d

Browse files
committed
Port over timer modules
Signed-off-by: Michael X. Grey <[email protected]>
1 parent ac57416 commit e482b2d

File tree

9 files changed

+1197
-9
lines changed

9 files changed

+1197
-9
lines changed

rclrs/src/clock.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ impl Clock {
8888
Self { kind, rcl_clock }
8989
}
9090

91+
/// Returns the clock's `rcl_clock_t`.
92+
pub(crate) fn get_rcl_clock(&self) -> &Arc<Mutex<rcl_clock_t>> {
93+
&self.rcl_clock
94+
}
95+
9196
/// Returns the clock's `ClockType`.
9297
pub fn clock_type(&self) -> ClockType {
9398
self.kind

rclrs/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ mod service;
189189
mod subscription;
190190
mod time;
191191
mod time_source;
192+
mod timer;
192193
mod vendor;
193194
mod wait_set;
194195
mod worker;
@@ -217,5 +218,6 @@ pub use service::*;
217218
pub use subscription::*;
218219
pub use time::*;
219220
use time_source::*;
221+
pub use timer::*;
220222
pub use wait_set::*;
221223
pub use worker::*;

rclrs/src/node.rs

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ use async_std::future::timeout;
2929
use rosidl_runtime_rs::Message;
3030

3131
use crate::{
32-
rcl_bindings::*, Client, ClientOptions, ClientState, Clock, ContextHandle, ExecutorCommands,
33-
IntoAsyncServiceCallback, IntoAsyncSubscriptionCallback, IntoNodeServiceCallback,
34-
IntoNodeSubscriptionCallback, LogParams, Logger, ParameterBuilder, ParameterInterface,
35-
ParameterVariant, Parameters, Promise, Publisher, PublisherOptions, PublisherState, RclrsError,
36-
Service, ServiceOptions, ServiceState, Subscription, SubscriptionOptions, SubscriptionState,
37-
TimeSource, ToLogParams, Worker, WorkerOptions, WorkerState, ENTITY_LIFECYCLE_MUTEX,
32+
rcl_bindings::*, AnyTimerCallback, Client, ClientOptions, ClientState, Clock, ContextHandle,
33+
ExecutorCommands, IntoAsyncServiceCallback, IntoAsyncSubscriptionCallback,
34+
IntoNodeServiceCallback, IntoNodeSubscriptionCallback, IntoNodeTimerOneshotCallback,
35+
IntoNodeTimerRepeatingCallback, IntoTimerOptions, LogParams, Logger, ParameterBuilder,
36+
ParameterInterface, ParameterVariant, Parameters, Promise, Publisher, PublisherOptions,
37+
PublisherState, RclrsError, Service, ServiceOptions, ServiceState, Subscription,
38+
SubscriptionOptions, SubscriptionState, TimeSource, Timer, TimerState, ToLogParams, Worker,
39+
WorkerOptions, WorkerState, ENTITY_LIFECYCLE_MUTEX,
3840
};
3941

4042
/// A processing unit that can communicate with other nodes. See the API of
@@ -893,6 +895,70 @@ impl NodeState {
893895
)
894896
}
895897

898+
/// Create a [`Timer`] with a repeating callback.
899+
///
900+
/// See also:
901+
/// * [`Self::create_timer_oneshot`]
902+
/// * [`Self::create_timer_inert`]
903+
pub fn create_timer_repeating<'a, Args>(
904+
&self,
905+
options: impl IntoTimerOptions<'a>,
906+
callback: impl IntoNodeTimerRepeatingCallback<Args>,
907+
) -> Result<Timer, RclrsError> {
908+
self.create_timer(options, callback.into_node_timer_repeating_callback())
909+
}
910+
911+
/// Create a [`Timer`] whose callback will be triggered once after the period
912+
/// of the timer has elapsed. After that you will need to use
913+
/// [`Timer::set_repeating`] or [`Timer::set_oneshot`] or else nothing will happen
914+
/// the following times that the `Timer` elapses.
915+
///
916+
/// See also:
917+
/// * [`Self::create_timer_repeating`]
918+
/// * [`Self::create_time_inert`]
919+
pub fn create_timer_oneshot<'a, Args>(
920+
&self,
921+
options: impl IntoTimerOptions<'a>,
922+
callback: impl IntoNodeTimerOneshotCallback<Args>,
923+
) -> Result<Timer, RclrsError> {
924+
self.create_timer(options, callback.into_node_timer_oneshot_callback())
925+
}
926+
927+
/// Create a [`Timer`] without a callback. Nothing will happen when this
928+
/// `Timer` elapses until you use [`Timer::set_callback`] or a related method.
929+
///
930+
/// See also:
931+
/// * [`Self::create_timer_repeating`]
932+
/// * [`Self::create_timer_oneshot`]
933+
pub fn create_timer_inert<'a>(
934+
&self,
935+
options: impl IntoTimerOptions<'a>,
936+
) -> Result<Timer, RclrsError> {
937+
self.create_timer(options, AnyTimerCallback::Inert)
938+
}
939+
940+
/// Used internally to create a [`Timer`].
941+
///
942+
/// Downstream users should instead use:
943+
/// * [`Self::create_timer_repeating`]
944+
/// * [`Self::create_timer_oneshot`]
945+
/// * [`Self::create_timer_inert`]
946+
fn create_timer<'a>(
947+
&self,
948+
options: impl IntoTimerOptions<'a>,
949+
callback: AnyTimerCallback<Node>,
950+
) -> Result<Timer, RclrsError> {
951+
let options = options.into_timer_options();
952+
let clock = options.clock.as_clock(self);
953+
TimerState::create(
954+
options.period,
955+
clock,
956+
callback,
957+
self.commands.async_worker_commands(),
958+
&self.handle.context_handle,
959+
)
960+
}
961+
896962
/// Returns the ROS domain ID that the node is using.
897963
///
898964
/// The domain ID controls which nodes can send messages to each other, see the [ROS 2 concept article][1].

0 commit comments

Comments
 (0)