r2r/examples/tokio_publisher.rs

25 lines
735 B
Rust

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let ctx = r2r::Context::create()?;
let mut node = r2r::Node::create(ctx, "testnode", "")?;
let duration = std::time::Duration::from_millis(2500);
let mut timer = node.create_wall_timer(duration)?;
let publisher = node.create_publisher::<r2r::std_msgs::msg::String>("/topic")?;
let handle = tokio::task::spawn_blocking(move || loop {
node.spin_once(std::time::Duration::from_millis(100));
});
for _ in 1..10 {
timer.tick().await?;
let msg = r2r::std_msgs::msg::String {
data: "hello from r2r".to_string(),
};
publisher.publish(&msg)?;
}
handle.await?;
Ok(())
}