forked from ros2-rust/ros2_rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrclrs_timer_demo.rs
More file actions
46 lines (43 loc) · 1.51 KB
/
rclrs_timer_demo.rs
File metadata and controls
46 lines (43 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/// Creates a SimpleTimerNode, initializes a node and the timer with a callback
/// that prints the timer callback execution iteration. The callback is executed
/// thanks to the spin, which is in charge of executing the timer's events among
/// other entities' events.
use rclrs::{create_node, Context, Node, RclrsError, Timer};
use std::{
env,
sync::Arc,
time::Duration,
};
/// Contains both the node and timer.
struct SimpleTimerNode {
node: Arc<Node>,
#[allow(unused)]
timer: Arc<Timer>,
}
impl SimpleTimerNode {
/// Creates a node and a timer with a callback.
///
/// The callback will simply print to stdout:
/// "Drinking 🧉 for the xth time every p nanoseconds."
/// where x is the iteration callback counter and p is the period of the timer.
fn new(context: &Context, timer_period: Duration) -> Result<Self, RclrsError> {
let node = create_node(context, "simple_timer_node")?;
let mut x = 0;
let timer = node.create_timer_repeating(
timer_period,
move || {
x += 1;
println!(
"Drinking 🧉 for the {x}th time every {:?}.",
timer_period,
);
},
)?;
Ok(Self { node, timer })
}
}
fn main() -> Result<(), RclrsError> {
let context = Context::new(env::args()).unwrap();
let simple_timer_node = Arc::new(SimpleTimerNode::new(&context, Duration::from_secs(1)).unwrap());
rclrs::spin(simple_timer_node.node.clone())
}