Skip to content

Commit f503c84

Browse files
Add create_timer to node (WIP) (#4)
* Added timer_period_ns * Adds Timer::is_ready(). Signed-off-by: Agustin Alba Chicar <[email protected]> * Added comments to avoid warnings * Added the getter for rcl_clock_t * WIP add create_timer to node * Cleaning some stuff --------- Signed-off-by: Agustin Alba Chicar <[email protected]> Co-authored-by: Agustin Alba Chicar <[email protected]>
1 parent 965ca22 commit f503c84

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

rclrs/src/node.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ use crate::{
1616
rcl_bindings::*, Client, ClientBase, Clock, Context, ContextHandle, GuardCondition, LogParams,
1717
Logger, ParameterBuilder, ParameterInterface, ParameterVariant, Parameters, Publisher,
1818
QoSProfile, RclrsError, Service, ServiceBase, Subscription, SubscriptionBase,
19-
SubscriptionCallback, TimeSource, ToLogParams, ENTITY_LIFECYCLE_MUTEX,
19+
SubscriptionCallback, Timer, TimerCallback, TimeSource, ToLogParams, ENTITY_LIFECYCLE_MUTEX,
2020
};
2121

22+
/// Constant conversion from seconds to nanoseconds
23+
const S_TO_NS: f64 = 1e9;
24+
2225
// SAFETY: The functions accessing this type, including drop(), shouldn't care about the thread
2326
// they are running in. Therefore, this type can be safely sent to another thread.
2427
unsafe impl Send for rcl_node_t {}
@@ -63,6 +66,7 @@ pub struct Node {
6366
pub(crate) guard_conditions_mtx: Mutex<Vec<Weak<GuardCondition>>>,
6467
pub(crate) services_mtx: Mutex<Vec<Weak<dyn ServiceBase>>>,
6568
pub(crate) subscriptions_mtx: Mutex<Vec<Weak<dyn SubscriptionBase>>>,
69+
pub(crate) timers_mtx: Mutex<Vec<Weak<Timer>>>,
6670
time_source: TimeSource,
6771
parameter: ParameterInterface,
6872
pub(crate) handle: Arc<NodeHandle>,
@@ -340,6 +344,31 @@ impl Node {
340344
Ok(subscription)
341345
}
342346

347+
/// Creates a [`Timer`]
348+
pub(crate) fn create_timer(
349+
&self,
350+
timer_period_s: i64,
351+
context: Context,
352+
callback: Option<TimerCallback>,
353+
clock: Option<Clock>
354+
) -> Arc<Timer> {
355+
let timer_period_ns = (timer_period_s as f64 * S_TO_NS) as i64;
356+
let clock_used;
357+
match clock {
358+
Some(value) => {
359+
clock_used = value;
360+
}
361+
None => {
362+
clock_used = self.get_clock();
363+
}
364+
}
365+
let context = Context::new(vec![]).unwrap();
366+
let timer = Timer::new(&clock_used, &context, timer_period_ns);
367+
let timer = Arc::new(timer.unwrap());
368+
self.timers_mtx.lock().unwrap().push(Arc::downgrade(&timer) as Weak<Timer>);
369+
timer
370+
}
371+
343372
/// Returns the subscriptions that have not been dropped yet.
344373
pub(crate) fn live_subscriptions(&self) -> Vec<Arc<dyn SubscriptionBase>> {
345374
{ self.subscriptions_mtx.lock().unwrap() }

rclrs/src/node/builder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ impl NodeBuilder {
340340
guard_conditions_mtx: Mutex::new(vec![]),
341341
services_mtx: Mutex::new(vec![]),
342342
subscriptions_mtx: Mutex::new(vec![]),
343+
timers_mtx: Mutex::new(vec![]),
343344
time_source: TimeSource::builder(self.clock_type)
344345
.clock_qos(self.clock_qos)
345346
.build(),

0 commit comments

Comments
 (0)