-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for clients and services (#146)
* Added support for clients and services
- Loading branch information
Showing
29 changed files
with
1,241 additions
and
298 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
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[package] | ||
name = "examples_rclrs_minimal_client_service" | ||
version = "0.2.0" | ||
authors = ["Esteve Fernandez <[email protected]>"] | ||
edition = "2021" | ||
|
||
[[bin]] | ||
name = "minimal_client" | ||
path = "src/minimal_client.rs" | ||
|
||
[[bin]] | ||
name = "minimal_client_async" | ||
path = "src/minimal_client_async.rs" | ||
|
||
[[bin]] | ||
name = "minimal_service" | ||
path = "src/minimal_service.rs" | ||
|
||
[dependencies] | ||
anyhow = {version = "1", features = ["backtrace"]} | ||
tokio = { version = "1", features = ["macros", "rt", "rt-multi-thread", "time"] } | ||
|
||
[dependencies.rclrs] | ||
version = "*" | ||
|
||
[dependencies.rosidl_runtime_rs] | ||
version = "*" | ||
|
||
[dependencies.example_interfaces] | ||
version = "*" |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0"?> | ||
<?xml-model | ||
href="http://download.ros.org/schema/package_format3.xsd" | ||
schematypens="http://www.w3.org/2001/XMLSchema"?> | ||
<package format="3"> | ||
<name>examples_rclrs_minimal_client_service</name> | ||
<version>0.2.0</version> | ||
<description>Package containing an example of the client-service mechanism in rclrs.</description> | ||
<maintainer email="[email protected]">Esteve Fernandez</maintainer> | ||
<license>Apache License 2.0</license> | ||
|
||
<build_depend>example_interfaces</build_depend> | ||
<build_depend>rclrs</build_depend> | ||
<build_depend>rosidl_runtime_rs</build_depend> | ||
|
||
<exec_depend>example_interfaces</exec_depend> | ||
<exec_depend>rclrs</exec_depend> | ||
<exec_depend>rosidl_runtime_rs</exec_depend> | ||
|
||
<export> | ||
<build_type>ament_cargo</build_type> | ||
</export> | ||
</package> |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use anyhow::{Error, Result}; | ||
use std::env; | ||
|
||
fn main() -> Result<(), Error> { | ||
let context = rclrs::Context::new(env::args())?; | ||
|
||
let mut node = context.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"); | ||
|
||
std::thread::sleep(std::time::Duration::from_millis(500)); | ||
|
||
client.async_send_request_with_callback( | ||
&request, | ||
move |response: example_interfaces::srv::AddTwoInts_Response| { | ||
println!( | ||
"Result of {} + {} is: {}", | ||
request.a, request.b, response.sum | ||
); | ||
}, | ||
)?; | ||
|
||
std::thread::sleep(std::time::Duration::from_millis(500)); | ||
|
||
println!("Waiting for response"); | ||
rclrs::spin(&node).map_err(|err| err.into()) | ||
} |
32 changes: 32 additions & 0 deletions
32
examples/minimal_client_service/src/minimal_client_async.rs
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use anyhow::{Error, Result}; | ||
use std::env; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Error> { | ||
let context = rclrs::Context::new(env::args())?; | ||
|
||
let mut node = context.create_node("minimal_client")?; | ||
|
||
let client = node.create_client::<example_interfaces::srv::AddTwoInts>("add_two_ints")?; | ||
|
||
println!("Starting client"); | ||
|
||
std::thread::sleep(std::time::Duration::from_millis(500)); | ||
|
||
let request = example_interfaces::srv::AddTwoInts_Request { a: 41, b: 1 }; | ||
|
||
let future = client.call_async(&request); | ||
|
||
println!("Waiting for response"); | ||
|
||
let rclrs_spin = tokio::task::spawn_blocking(move || rclrs::spin(&node)); | ||
|
||
let response = future.await?; | ||
println!( | ||
"Result of {} + {} is: {}", | ||
request.a, request.b, response.sum | ||
); | ||
|
||
rclrs_spin.await.ok(); | ||
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use anyhow::{Error, Result}; | ||
use std::env; | ||
|
||
fn handle_service( | ||
_request_header: &rclrs::rmw_request_id_t, | ||
request: example_interfaces::srv::AddTwoInts_Request, | ||
) -> example_interfaces::srv::AddTwoInts_Response { | ||
println!("request: {} + {}", request.a, request.b); | ||
example_interfaces::srv::AddTwoInts_Response { | ||
sum: request.a + request.b, | ||
} | ||
} | ||
|
||
fn main() -> Result<(), Error> { | ||
let context = rclrs::Context::new(env::args())?; | ||
|
||
let mut node = context.create_node("minimal_service")?; | ||
|
||
let _server = node | ||
.create_service::<example_interfaces::srv::AddTwoInts, _>("add_two_ints", handle_service)?; | ||
|
||
println!("Starting server"); | ||
rclrs::spin(&node).map_err(|err| err.into()) | ||
} |
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
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
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
Oops, something went wrong.