-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: test: Add e2e test for ros service calls
- Loading branch information
1 parent
5ab7811
commit 112486d
Showing
9 changed files
with
247 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod opc_ua_publisher_single_linear; | ||
pub mod ros_service_caller; | ||
pub mod ros_subscriber; |
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,62 @@ | ||
use rclrs::{Node, RclrsError}; | ||
use std::{ | ||
env, | ||
sync::{Arc, Mutex}, | ||
time::Duration, | ||
}; | ||
|
||
pub struct ServiceCaller { | ||
pub client: Arc<rclrs::Client<std_srvs::srv::Empty>>, | ||
pub node: Arc<Node>, | ||
pub request: std_srvs::srv::Empty_Request, | ||
pub number_of_calls: Arc<Mutex<u32>>, | ||
} | ||
|
||
impl ServiceCaller { | ||
pub fn new(service: &str) -> Result<Arc<Self>, RclrsError> { | ||
let context = rclrs::Context::new(env::args())?; | ||
|
||
let node = rclrs::create_node(&context, "minimal_client")?; | ||
|
||
let client = node.create_client::<std_srvs::srv::Empty>(service)?; | ||
|
||
let request = std_srvs::srv::Empty_Request { | ||
structure_needs_at_least_one_member: 0, | ||
}; | ||
let action_caller = Arc::new(ServiceCaller { | ||
client, | ||
node, | ||
request, | ||
number_of_calls: Arc::new(Mutex::new(0)), | ||
}); | ||
|
||
Ok(action_caller) | ||
} | ||
|
||
pub fn start(&self) { | ||
println!("Starting ROS Service Client"); | ||
|
||
while !self.client.service_is_ready().unwrap() { | ||
std::thread::sleep(std::time::Duration::from_millis(10)); | ||
} | ||
println!("ROS Service Client is ready"); | ||
} | ||
pub fn call(&self) { | ||
println!("Calling a ROS Service"); | ||
let num_calls_clone = Arc::clone(&self.number_of_calls); | ||
self.client | ||
.async_send_request_with_callback( | ||
&self.request, | ||
move |_response: std_srvs::srv::Empty_Response| { | ||
println!("Got an answer"); | ||
let mut num_calls = num_calls_clone.lock().unwrap(); | ||
*num_calls += 1; | ||
}, | ||
) | ||
.unwrap(); | ||
|
||
let timeout = Some(Duration::new(5, 0)); | ||
let node_clone = Arc::clone(&self.node); | ||
rclrs::spin_once(node_clone, timeout).unwrap(); | ||
} | ||
} |
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