From 6d3f7e4660cf6c82e2416139829331c9b1566b0c Mon Sep 17 00:00:00 2001 From: "Michael X. Grey" Date: Mon, 9 Dec 2024 19:31:58 +0800 Subject: [PATCH] Update examples Signed-off-by: Michael X. Grey --- .../src/minimal_client.rs | 30 +++++++++---------- .../src/minimal_client_async.rs | 28 ++++++++--------- .../src/minimal_service.rs | 17 +++++++---- .../minimal_pub_sub/src/minimal_subscriber.rs | 18 ++++++----- .../src/zero_copy_subscriber.rs | 16 +++++----- examples/rust_pubsub/src/simple_publisher.rs | 6 ++-- examples/rust_pubsub/src/simple_subscriber.rs | 11 +++---- 7 files changed, 66 insertions(+), 60 deletions(-) diff --git a/examples/minimal_client_service/src/minimal_client.rs b/examples/minimal_client_service/src/minimal_client.rs index 49f18e242..d1b35c0d9 100644 --- a/examples/minimal_client_service/src/minimal_client.rs +++ b/examples/minimal_client_service/src/minimal_client.rs @@ -1,34 +1,32 @@ use anyhow::{Error, Result}; +use rclrs::{Context, SpinOptions, Promise}; fn main() -> Result<(), Error> { - let mut executor = rclrs::Context::default_from_env()?.create_basic_executor(); + let mut executor = Context::default_from_env()?.create_basic_executor(); let node = executor.create_node("minimal_client")?; let client = node.create_client::("add_two_ints")?; - let request = example_interfaces::srv::AddTwoInts_Request { a: 41, b: 1 }; - println!("Starting client"); while !client.service_is_ready()? { std::thread::sleep(std::time::Duration::from_millis(10)); } - client.async_send_request_with_callback( - &request, - move |response: example_interfaces::srv::AddTwoInts_Response| { - println!( - "Result of {} + {} is: {}", - request.a, request.b, response.sum - ); - }, - )?; + let request = example_interfaces::srv::AddTwoInts_Request { a: 41, b: 1 }; + + let response: Promise = client.call(&request).unwrap(); - std::thread::sleep(std::time::Duration::from_millis(500)); + let promise = executor.commands().run(async move { + let response = response.await.unwrap(); + println!( + "Result of {} + {} is: {}", + request.a, request.b, response.sum, + ); + }); println!("Waiting for response"); - executor - .spin(rclrs::SpinOptions::default()) - .map_err(|err| err.into()) + executor.spin(SpinOptions::new().until_promise_resolved(promise))?; + Ok(()) } diff --git a/examples/minimal_client_service/src/minimal_client_async.rs b/examples/minimal_client_service/src/minimal_client_async.rs index 8babe04e7..b4bca1372 100644 --- a/examples/minimal_client_service/src/minimal_client_async.rs +++ b/examples/minimal_client_service/src/minimal_client_async.rs @@ -1,8 +1,8 @@ use anyhow::{Error, Result}; +use rclrs::{Context, SpinOptions}; -#[tokio::main] -async fn main() -> Result<(), Error> { - let mut executor = rclrs::Context::default_from_env()?.create_basic_executor(); +fn main() -> Result<(), Error> { + let mut executor = Context::default_from_env()?.create_basic_executor(); let node = executor.create_node("minimal_client")?; @@ -16,19 +16,17 @@ async fn main() -> Result<(), Error> { let request = example_interfaces::srv::AddTwoInts_Request { a: 41, b: 1 }; - let future = client.call_async(&request); + let promise = client.call_then( + &request, + move |response: example_interfaces::srv::AddTwoInts_Response| { + println!( + "Result of {} + {} is: {}", + request.a, request.b, response.sum, + ); + } + ).unwrap(); println!("Waiting for response"); - - let rclrs_spin = - tokio::task::spawn_blocking(move || executor.spin(rclrs::SpinOptions::default())); - - let response = future.await?; - println!( - "Result of {} + {} is: {}", - request.a, request.b, response.sum - ); - - rclrs_spin.await.ok(); + executor.spin(SpinOptions::new().until_promise_resolved(promise))?; Ok(()) } diff --git a/examples/minimal_client_service/src/minimal_service.rs b/examples/minimal_client_service/src/minimal_service.rs index 84d154fec..f249940bf 100644 --- a/examples/minimal_client_service/src/minimal_service.rs +++ b/examples/minimal_client_service/src/minimal_service.rs @@ -1,17 +1,23 @@ use anyhow::{Error, Result}; +use rclrs::{Context, ServiceInfo, SpinOptions}; fn handle_service( - _request_header: &rclrs::rmw_request_id_t, request: example_interfaces::srv::AddTwoInts_Request, + info: ServiceInfo, ) -> example_interfaces::srv::AddTwoInts_Response { - println!("request: {} + {}", request.a, request.b); + let timestamp = info + .received_timestamp + .map(|t| format!(" at [{t:?}]")) + .unwrap_or(String::new()); + + println!("request{timestamp}: {} + {}", request.a, request.b); example_interfaces::srv::AddTwoInts_Response { sum: request.a + request.b, } } fn main() -> Result<(), Error> { - let mut executor = rclrs::Context::default_from_env()?.create_basic_executor(); + let mut executor = Context::default_from_env()?.create_basic_executor(); let node = executor.create_node("minimal_service")?; @@ -19,7 +25,6 @@ fn main() -> Result<(), Error> { .create_service::("add_two_ints", handle_service)?; println!("Starting server"); - executor - .spin(rclrs::SpinOptions::default()) - .map_err(|err| err.into()) + executor.spin(SpinOptions::default())?; + Ok(()) } diff --git a/examples/minimal_pub_sub/src/minimal_subscriber.rs b/examples/minimal_pub_sub/src/minimal_subscriber.rs index ebe7406ee..59fa37b1e 100644 --- a/examples/minimal_pub_sub/src/minimal_subscriber.rs +++ b/examples/minimal_pub_sub/src/minimal_subscriber.rs @@ -1,23 +1,25 @@ use anyhow::{Error, Result}; +use std::sync::Mutex; +use rclrs::{Context, SpinOptions}; fn main() -> Result<(), Error> { - let context = rclrs::Context::default_from_env()?; + let context = Context::default_from_env()?; let mut executor = context.create_basic_executor(); let node = executor.create_node("minimal_subscriber")?; - let mut num_messages: usize = 0; - + let num_messages = Mutex::new(0usize); let _subscription = node.create_subscription::( "topic", move |msg: std_msgs::msg::String| { - num_messages += 1; + let mut num = num_messages.lock().unwrap(); + *num += 1; println!("I heard: '{}'", msg.data); - println!("(Got {} messages so far)", num_messages); + println!("(Got {} messages so far)", num); }, )?; - executor - .spin(rclrs::SpinOptions::default()) - .map_err(|err| err.into()) + println!("Waiting for messages..."); + executor.spin(SpinOptions::default())?; + Ok(()) } diff --git a/examples/minimal_pub_sub/src/zero_copy_subscriber.rs b/examples/minimal_pub_sub/src/zero_copy_subscriber.rs index b44752d65..4769e9f12 100644 --- a/examples/minimal_pub_sub/src/zero_copy_subscriber.rs +++ b/examples/minimal_pub_sub/src/zero_copy_subscriber.rs @@ -1,22 +1,24 @@ use anyhow::{Error, Result}; +use std::sync::Mutex; +use rclrs::ReadOnlyLoanedMessage; fn main() -> Result<(), Error> { let mut executor = rclrs::Context::default_from_env()?.create_basic_executor(); let node = executor.create_node("minimal_subscriber")?; - let mut num_messages: usize = 0; + let num_messages = Mutex::new(0usize); let _subscription = node.create_subscription::( "topic", - move |msg: rclrs::ReadOnlyLoanedMessage<'_, std_msgs::msg::UInt32>| { - num_messages += 1; + move |msg: ReadOnlyLoanedMessage| { + let mut num = num_messages.lock().unwrap(); + *num += 1; println!("I heard: '{}'", msg.data); - println!("(Got {} messages so far)", num_messages); + println!("(Got {} messages so far)", *num); }, )?; - executor - .spin(rclrs::SpinOptions::default()) - .map_err(|err| err.into()) + executor.spin(rclrs::SpinOptions::default())?; + Ok(()) } diff --git a/examples/rust_pubsub/src/simple_publisher.rs b/examples/rust_pubsub/src/simple_publisher.rs index b4283d073..8b5b467a7 100644 --- a/examples/rust_pubsub/src/simple_publisher.rs +++ b/examples/rust_pubsub/src/simple_publisher.rs @@ -1,5 +1,5 @@ -use rclrs::{Context, Executor, Publisher, RclrsError, SpinOptions, QOS_PROFILE_DEFAULT}; -use std::{sync::Arc, thread, time::Duration}; +use rclrs::{Context, Executor, Publisher, RclrsError, SpinOptions}; +use std::{thread, time::Duration}; use std_msgs::msg::String as StringMsg; struct SimplePublisherNode { @@ -26,7 +26,7 @@ impl SimplePublisherNode { fn main() -> Result<(), RclrsError> { let mut executor = Context::default_from_env().unwrap().create_basic_executor(); - let node = Arc::new(SimplePublisher::new(&executor).unwrap()); + let node = SimplePublisherNode::new(&executor).unwrap(); let mut count: i32 = 0; thread::spawn(move || loop { thread::sleep(Duration::from_millis(1000)); diff --git a/examples/rust_pubsub/src/simple_subscriber.rs b/examples/rust_pubsub/src/simple_subscriber.rs index bae5f551b..5e11f5fda 100644 --- a/examples/rust_pubsub/src/simple_subscriber.rs +++ b/examples/rust_pubsub/src/simple_subscriber.rs @@ -1,4 +1,4 @@ -use rclrs::{Context, Executor, RclrsError, SpinOptions, Subscription, QOS_PROFILE_DEFAULT}; +use rclrs::{Context, Executor, RclrsError, SpinOptions, Subscription}; use std::{ sync::{Arc, Mutex}, thread, @@ -7,7 +7,8 @@ use std::{ use std_msgs::msg::String as StringMsg; pub struct SimpleSubscriptionNode { - _subscriber: Arc>, + #[allow(unused)] + subscriber: Subscription, data: Arc>>, } @@ -16,7 +17,7 @@ impl SimpleSubscriptionNode { let node = executor.create_node("simple_subscription").unwrap(); let data: Arc>> = Arc::new(Mutex::new(None)); let data_mut: Arc>> = Arc::clone(&data); - let _subscriber = node + let subscriber = node .create_subscription::( "publish_hello", move |msg: StringMsg| { @@ -24,7 +25,7 @@ impl SimpleSubscriptionNode { }, ) .unwrap(); - Ok(Self { _subscriber, data }) + Ok(Self { subscriber, data }) } fn data_callback(&self) -> Result<(), RclrsError> { if let Some(data) = self.data.lock().unwrap().as_ref() { @@ -37,7 +38,7 @@ impl SimpleSubscriptionNode { } fn main() -> Result<(), RclrsError> { let mut executor = Context::default_from_env().unwrap().create_basic_executor(); - let node = Arc::new(SimpleSubscriptionNode::new(&executor).unwrap()); + let node = SimpleSubscriptionNode::new(&executor).unwrap(); thread::spawn(move || loop { thread::sleep(Duration::from_millis(1000)); node.data_callback().unwrap()