Fix panic in subscriber_with_thread example and improve readme.

This commit is contained in:
Martin Dahl 2021-05-11 11:50:51 +02:00
parent 79d3d70608
commit d7bae8546b
2 changed files with 15 additions and 8 deletions

View File

@ -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 <https://github.com/sequenceplanner/r2r-echo>, which can be installed by running cargo install --git https://github.com/sequenceplanner/r2r-echo.
What works?

View File

@ -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::<std_msgs::msg::String>("/hej")?;
let publisher = node.create_publisher::<std_msgs::msg::String>("/hello")?;
let pubint = node.create_publisher::<std_msgs::msg::Int32>("/count")?;
let (tx, rx) = mpsc::channel::<String>();
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;