Skip to content

Commit

Permalink
Better handling of node_name in ros1 client
Browse files Browse the repository at this point in the history
  • Loading branch information
carter committed Jun 4, 2024
1 parent bee983b commit 4564cd6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
7 changes: 3 additions & 4 deletions roslibrust/examples/ros1_service_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ async fn main() -> Result<(), anyhow::Error> {
.init()
.unwrap();

// TODO: for some reason the preceding slash is required for service client (on ros1 native)
// but not for publishers/subscribers
// Not sure exactly why it got built that way and we we should clean it up
let nh = NodeHandle::new("http://localhost:11311", "/service_client_rs").await?;
let nh = NodeHandle::new("http://localhost:11311", "service_client_rs").await?;
log::info!("Connected!");

let response: rosapi::GetTimeResponse = nh
Expand All @@ -23,6 +20,8 @@ async fn main() -> Result<(), anyhow::Error> {
.call(&rosapi::GetTimeRequest {})
.await?;

tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;

log::info!("Got time: {:?}", response);

Ok(())
Expand Down
1 change: 1 addition & 0 deletions roslibrust/src/ros1/names.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt::Display;

lazy_static::lazy_static! {
// See: https://wiki.ros.org/Names
static ref GRAPH_NAME_REGEX: regex::Regex = regex::Regex::new(r"^([/~a-zA-Z]){1}([a-zA-Z0-9_/])*([A-z0-9_])$").unwrap();
}

Expand Down
15 changes: 13 additions & 2 deletions roslibrust/src/ros1/node/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,22 @@ pub struct NodeHandle {

impl NodeHandle {
// TODO builder, result, better error type
/// Creates a new node connect and returns a handle to it
/// Creates a new node, connects, and returns a handle to it
/// It is idiomatic to call this once per process and treat the created node as singleton.
/// The returned handle can be freely clone'd to create additional handles without creating additional connections.
/// - master_uri: Expects a fully resolved http uri for the master e.g. "http://my_host_name:11311"
/// - name: The name of the node, expected to be a valid ros name, all names are interpreted as 'global' in
/// ROS's namespace system. e.g. "my_node" -> "/my_node". "~my_node" is not supported
pub async fn new(master_uri: &str, name: &str) -> Result<NodeHandle, NodeError> {
let name = Name::new(name)?;
let name = if name.starts_with("/") {
Name::new(name)?
} else {
Name::new(&format!("/{}", name))?
};

// Extra safety check that our name resolves now
let _ = Name::new("test").unwrap().resolve_to_global(&name);

// Follow ROS rules and determine our IP and hostname
let (addr, hostname) = super::determine_addr().await?;

Expand Down

0 comments on commit 4564cd6

Please sign in to comment.