From d7bae8546bf40b9136175b867879f068d18bca60 Mon Sep 17 00:00:00 2001 From: Martin Dahl Date: Tue, 11 May 2021 11:50:51 +0200 Subject: [PATCH] Fix panic in subscriber_with_thread example and improve readme. --- README.md | 3 +++ examples/subscriber_with_thread.rs | 20 ++++++++++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 3134f5a..7e89787 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,10 @@ A couple of examples are included in examples/ . /opt/ros/foxy/setup.sh cargo build cargo run --example subscriber_with_thread +# In other shell +ros2 topic pub /hi std_msgs/msg/String "data: 'Hello, world!'" ``` + An example application can be found here , which can be installed by running cargo install --git https://github.com/sequenceplanner/r2r-echo. What works? diff --git a/examples/subscriber_with_thread.rs b/examples/subscriber_with_thread.rs index 151e2f5..9874dc7 100644 --- a/examples/subscriber_with_thread.rs +++ b/examples/subscriber_with_thread.rs @@ -9,17 +9,21 @@ fn main() -> Result<(), Error> { let ctx = r2r::Context::create()?; let mut node = r2r::Node::create(ctx, "testnode", "")?; - let publisher = node.create_publisher::("/hej")?; + let publisher = node.create_publisher::("/hello")?; let pubint = node.create_publisher::("/count")?; let (tx, rx) = mpsc::channel::(); thread::spawn(move || loop { - let msg = rx.recv().unwrap(); - let deserialized: std_msgs::msg::String = serde_json::from_str(&msg).unwrap(); - println!( - "received: {}, deserialized ros msg = {:?}", - msg, deserialized - ); + if let Ok(msg) = rx.recv() { + let deserialized: std_msgs::msg::String = serde_json::from_str(&msg).unwrap(); + println!( + "received: {}, deserialized ros msg = {:?}", + msg, deserialized + ); + } else { + println!("stopping"); + break; + } }); let mut c = 0; @@ -34,7 +38,7 @@ fn main() -> Result<(), Error> { pubint.publish(&to_send).unwrap(); }; - let _ws2 = node.subscribe("/hopp", Box::new(cb))?; + let _ws2 = node.subscribe("/hi", Box::new(cb))?; // run for 10 seconds let mut count = 0;