|
1 | 1 | use anyhow::{Error, Result}; |
| 2 | +use example_interfaces::srv::*; |
2 | 3 | use rclrs::*; |
3 | 4 |
|
4 | 5 | fn main() -> Result<(), Error> { |
5 | 6 | let mut executor = Context::default_from_env()?.create_basic_executor(); |
6 | 7 |
|
7 | 8 | let node = executor.create_node("minimal_client")?; |
8 | 9 |
|
9 | | - let client = node.create_client::<example_interfaces::srv::AddTwoInts>("add_two_ints")?; |
| 10 | + let client = node.create_client::<AddTwoInts>("add_two_ints")?; |
10 | 11 |
|
11 | | - let request = example_interfaces::srv::AddTwoInts_Request { a: 41, b: 1 }; |
| 12 | + let promise = executor.commands().run(async move { |
| 13 | + println!("Waiting for service..."); |
| 14 | + client.notify_on_service_ready().await.unwrap(); |
12 | 15 |
|
13 | | - println!("Starting client"); |
| 16 | + let request = AddTwoInts_Request { a: 41, b: 1 }; |
14 | 17 |
|
15 | | - while !client.service_is_ready()? { |
16 | | - std::thread::sleep(std::time::Duration::from_millis(10)); |
17 | | - } |
| 18 | + println!("Waiting for response"); |
| 19 | + let response: AddTwoInts_Response = client.call(&request).unwrap().await.unwrap(); |
18 | 20 |
|
19 | | - client.async_send_request_with_callback( |
20 | | - &request, |
21 | | - move |response: example_interfaces::srv::AddTwoInts_Response| { |
22 | | - println!( |
23 | | - "Result of {} + {} is: {}", |
24 | | - request.a, request.b, response.sum |
25 | | - ); |
26 | | - }, |
27 | | - )?; |
| 21 | + println!( |
| 22 | + "Result of {} + {} is: {}", |
| 23 | + request.a, request.b, response.sum, |
| 24 | + ); |
| 25 | + }); |
28 | 26 |
|
29 | | - std::thread::sleep(std::time::Duration::from_millis(500)); |
30 | | - |
31 | | - println!("Waiting for response"); |
32 | 27 | executor |
33 | | - .spin(SpinOptions::default()) |
34 | | - .first_error() |
35 | | - .map_err(|err| err.into()) |
| 28 | + .spin(SpinOptions::new().until_promise_resolved(promise)) |
| 29 | + .first_error()?; |
| 30 | + Ok(()) |
36 | 31 | } |
0 commit comments