Skip to content

Commit

Permalink
Add demo for worker feature
Browse files Browse the repository at this point in the history
Signed-off-by: Michael X. Grey <greyxmike@gmail.com>
  • Loading branch information
mxgrey committed Dec 12, 2024
1 parent 6608192 commit 073c25e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/worker_demo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "worker_demo"
version = "0.1.0"
edition = "2021"

[dependencies]
rclrs = "0.4"
std_msgs = "*"
39 changes: 39 additions & 0 deletions examples/worker_demo/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use rclrs::*;
use std::time::Duration;

fn main() -> Result<(), RclrsError> {
let mut executor = Context::default_from_env()?.create_basic_executor();
let node = executor.create_node("worker_demo")?;

let publisher = node.create_publisher("output_topic")?;
let worker = node.create_worker(String::new());

let _timer = worker.create_timer_repeating(
Duration::from_secs(1),
move |data: &mut String| {
let msg = std_msgs::msg::String {
data: data.clone()
};

publisher.publish(msg).ok();
}
)?;

let _subscription = worker.create_subscription(
"input_topic",
move |data: &mut String, msg: std_msgs::msg::String| {
*data = msg.data;
}
)?;

println!(
"Beginning repeater... \n >> \
Publish a std_msg::msg::String to \"input_topic\" and we will periodically republish it to \"output_topic\".\n\n\
To see this in action run the following commands in two different terminals:\n \
$ ros2 topic echo output_topic\n \
$ ros2 topic pub input_topic std_msgs/msg/String \"{{data: Hello}}\""
);
executor.spin(SpinOptions::default());

Ok(())
}

0 comments on commit 073c25e

Please sign in to comment.