-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Michael X. Grey <greyxmike@gmail.com>
- Loading branch information
Showing
7 changed files
with
66 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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::<example_interfaces::srv::AddTwoInts>("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<example_interfaces::srv::AddTwoInts_Response> = 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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,30 @@ | ||
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")?; | ||
|
||
let _server = node | ||
.create_service::<example_interfaces::srv::AddTwoInts, _>("add_two_ints", handle_service)?; | ||
|
||
println!("Starting server"); | ||
executor | ||
.spin(rclrs::SpinOptions::default()) | ||
.map_err(|err| err.into()) | ||
executor.spin(SpinOptions::default())?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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::<std_msgs::msg::String, _>( | ||
"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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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::<std_msgs::msg::UInt32, _>( | ||
"topic", | ||
move |msg: rclrs::ReadOnlyLoanedMessage<'_, std_msgs::msg::UInt32>| { | ||
num_messages += 1; | ||
move |msg: ReadOnlyLoanedMessage<std_msgs::msg::UInt32>| { | ||
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters