Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions rclrs/dynamic_pub_sub/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "dynamic_pub_sub"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
# TODO(luca) change this to the correct version once dynamic message support is released on crates.io
rclrs = { version = "0.4", features = ["dyn_msg"] }
anyhow = {version = "1", features = ["backtrace"]}
23 changes: 23 additions & 0 deletions rclrs/dynamic_pub_sub/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use anyhow::{Error, Result};
use rclrs::*;

fn main() -> Result<(), Error> {
let context = Context::default_from_env()?;
let mut executor = context.create_basic_executor();

let node = executor.create_node("dynamic_subscriber")?;

let worker = node.create_worker::<usize>(0);
let _subscription = worker.create_dynamic_subscription(
"rclrs_example_msgs/msg/VariousTypes".try_into()?,
"topic",
move |num_messages: &mut usize, msg, _msg_info| {
*num_messages += 1;
println!("#{} | I heard: '{:#?}'", *num_messages, msg.structure());
},
)?;

println!("Waiting for messages...");
executor.spin(SpinOptions::default()).first_error()?;
Ok(())
}