Skip to content

Commit 214a991

Browse files
committed
Working E2E timer with node.
Signed-off-by: Agustin Alba Chicar <[email protected]>
1 parent ed78b35 commit 214a991

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

rclrs/src/node.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -345,28 +345,21 @@ impl Node {
345345
}
346346

347347
/// Creates a [`Timer`]
348-
pub(crate) fn create_timer(
348+
pub fn create_timer(
349349
&self,
350-
timer_period_s: i64,
351-
context: Context,
350+
period_ns: i64,
351+
context: &Context,
352352
callback: Option<TimerCallback>,
353353
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());
354+
) -> Result<Arc<Timer>, RclrsError> {
355+
let clock_used = match clock {
356+
Some(value) => value,
357+
None => self.get_clock(),
358+
};
359+
let timer = Timer::new_with_callback(&clock_used, &context, period_ns, callback)?;
360+
let timer = Arc::new(timer);
368361
self.timers_mtx.lock().unwrap().push(Arc::downgrade(&timer) as Weak<Timer>);
369-
timer
362+
Ok(timer)
370363
}
371364

372365
/// Returns the subscriptions that have not been dropped yet.
@@ -398,6 +391,13 @@ impl Node {
398391
.collect()
399392
}
400393

394+
pub(crate) fn live_timers(&self) -> Vec<Arc<Timer>> {
395+
{ self.timers_mtx.lock().unwrap() }
396+
.iter()
397+
.filter_map(Weak::upgrade)
398+
.collect()
399+
}
400+
401401
/// Returns the ROS domain ID that the node is using.
402402
///
403403
/// The domain ID controls which nodes can send messages to each other, see the [ROS 2 concept article][1].

rclrs/src/timer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl Timer {
146146
})
147147
}
148148

149-
pub/* (crate)*/ fn execute(&self) -> Result<(), RclrsError>
149+
pub fn execute(&self) -> Result<(), RclrsError>
150150
{
151151
if self.is_ready()?
152152
{

rclrs/src/wait.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,14 @@ impl WaitSet {
142142
let live_clients = node.live_clients();
143143
let live_guard_conditions = node.live_guard_conditions();
144144
let live_services = node.live_services();
145+
let live_timers = node.live_timers();
145146
let ctx = Context {
146147
handle: Arc::clone(&node.handle.context_handle),
147148
};
148149
let mut wait_set = WaitSet::new(
149150
live_subscriptions.len(),
150151
live_guard_conditions.len(),
151-
0,
152+
live_timers.len(),
152153
live_clients.len(),
153154
live_services.len(),
154155
0,
@@ -170,6 +171,10 @@ impl WaitSet {
170171
for live_service in &live_services {
171172
wait_set.add_service(live_service.clone())?;
172173
}
174+
175+
for live_timer in &live_timers {
176+
wait_set.add_timer(live_timer.clone())?;
177+
}
173178
Ok(wait_set)
174179
}
175180

0 commit comments

Comments
 (0)