Skip to content

Commit d03d5e6

Browse files
Add example for dynamic messages
Signed-off-by: Luca Della Vedova <lucadv@intrinsic.ai>
1 parent c3d190d commit d03d5e6

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

rclrs/dynamic_pub_sub/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "dynamic_pub_sub"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
# TODO(luca) change this to the correct version once dynamic message support is released on crates.io
10+
rclrs = { version = "0.4", features = ["dyn_msg"] }
11+
anyhow = {version = "1", features = ["backtrace"]}

rclrs/dynamic_pub_sub/src/main.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use anyhow::{Error, Result};
2+
use rclrs::*;
3+
4+
fn main() -> Result<(), Error> {
5+
let context = Context::default_from_env()?;
6+
let mut executor = context.create_basic_executor();
7+
8+
let node = executor.create_node("dynamic_subscriber")?;
9+
10+
let worker = node.create_worker::<usize>(0);
11+
let _subscription = worker.create_dynamic_subscription(
12+
"rclrs_example_msgs/msg/VariousTypes".try_into()?,
13+
"topic",
14+
move |num_messages: &mut usize, msg, _msg_info| {
15+
*num_messages += 1;
16+
println!("#{} | I heard: '{:#?}'", *num_messages, msg.structure());
17+
},
18+
)?;
19+
20+
println!("Waiting for messages...");
21+
executor.spin(SpinOptions::default()).first_error()?;
22+
Ok(())
23+
}

0 commit comments

Comments
 (0)