Skip to content

Commit 073c25e

Browse files
committed
Add demo for worker feature
Signed-off-by: Michael X. Grey <[email protected]>
1 parent 6608192 commit 073c25e

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

examples/worker_demo/Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "worker_demo"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
rclrs = "0.4"
8+
std_msgs = "*"

examples/worker_demo/src/main.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use rclrs::*;
2+
use std::time::Duration;
3+
4+
fn main() -> Result<(), RclrsError> {
5+
let mut executor = Context::default_from_env()?.create_basic_executor();
6+
let node = executor.create_node("worker_demo")?;
7+
8+
let publisher = node.create_publisher("output_topic")?;
9+
let worker = node.create_worker(String::new());
10+
11+
let _timer = worker.create_timer_repeating(
12+
Duration::from_secs(1),
13+
move |data: &mut String| {
14+
let msg = std_msgs::msg::String {
15+
data: data.clone()
16+
};
17+
18+
publisher.publish(msg).ok();
19+
}
20+
)?;
21+
22+
let _subscription = worker.create_subscription(
23+
"input_topic",
24+
move |data: &mut String, msg: std_msgs::msg::String| {
25+
*data = msg.data;
26+
}
27+
)?;
28+
29+
println!(
30+
"Beginning repeater... \n >> \
31+
Publish a std_msg::msg::String to \"input_topic\" and we will periodically republish it to \"output_topic\".\n\n\
32+
To see this in action run the following commands in two different terminals:\n \
33+
$ ros2 topic echo output_topic\n \
34+
$ ros2 topic pub input_topic std_msgs/msg/String \"{{data: Hello}}\""
35+
);
36+
executor.spin(SpinOptions::default());
37+
38+
Ok(())
39+
}

0 commit comments

Comments
 (0)