Fix panic in subscriber_with_thread example and improve readme.
This commit is contained in:
parent
79d3d70608
commit
d7bae8546b
|
|
@ -20,7 +20,10 @@ A couple of examples are included in examples/
|
||||||
. /opt/ros/foxy/setup.sh
|
. /opt/ros/foxy/setup.sh
|
||||||
cargo build
|
cargo build
|
||||||
cargo run --example subscriber_with_thread
|
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.
|
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?
|
What works?
|
||||||
|
|
|
||||||
|
|
@ -9,17 +9,21 @@ fn main() -> Result<(), Error> {
|
||||||
let ctx = r2r::Context::create()?;
|
let ctx = r2r::Context::create()?;
|
||||||
let mut node = r2r::Node::create(ctx, "testnode", "")?;
|
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 pubint = node.create_publisher::<std_msgs::msg::Int32>("/count")?;
|
||||||
|
|
||||||
let (tx, rx) = mpsc::channel::<String>();
|
let (tx, rx) = mpsc::channel::<String>();
|
||||||
thread::spawn(move || loop {
|
thread::spawn(move || loop {
|
||||||
let msg = rx.recv().unwrap();
|
if let Ok(msg) = rx.recv() {
|
||||||
let deserialized: std_msgs::msg::String = serde_json::from_str(&msg).unwrap();
|
let deserialized: std_msgs::msg::String = serde_json::from_str(&msg).unwrap();
|
||||||
println!(
|
println!(
|
||||||
"received: {}, deserialized ros msg = {:?}",
|
"received: {}, deserialized ros msg = {:?}",
|
||||||
msg, deserialized
|
msg, deserialized
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
println!("stopping");
|
||||||
|
break;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut c = 0;
|
let mut c = 0;
|
||||||
|
|
@ -34,7 +38,7 @@ fn main() -> Result<(), Error> {
|
||||||
pubint.publish(&to_send).unwrap();
|
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
|
// run for 10 seconds
|
||||||
let mut count = 0;
|
let mut count = 0;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue