Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use correct service mapping based on RMW_IMPLEMENTATION and ROS distro #452

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ jobs:
run: |
# Reset only the turtlesim instance as it is not destroyed at the end of the previous job
source /opt/ros/humble/setup.bash && ros2 service call /reset std_srvs/srv/Empty &
cargo run --example cxx-ros2-dataflow --features="ros2-examples"
source /opt/ros/humble/setup.bash && cargo run --example cxx-ros2-dataflow --features="ros2-examples"

bench:
name: "Bench"
Expand Down
4 changes: 3 additions & 1 deletion apis/c++/node/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ mod ros2 {
pub fn generate() -> PathBuf {
use rust_format::Formatter;
let paths = ament_prefix_paths();
let generated = dora_ros2_bridge_msg_gen::gen(paths.as_slice(), true);
let generated = dora_ros2_bridge_msg_gen::gen(paths.as_slice(), true).unwrap();
let generated_string = rust_format::PrettyPlease::default()
.format_tokens(generated)
.unwrap();
Expand Down Expand Up @@ -81,6 +81,8 @@ mod ros2 {
}
};
println!("cargo:rerun-if-env-changed=AMENT_PREFIX_PATH");
println!("cargo:rerun-if-env-changed=RMW_IMPLEMENTATION");
println!("cargo:rerun-if-env-changed=ROS_DISTRO");

let paths: Vec<_> = ament_prefix_path.split(':').map(PathBuf::from).collect();
for path in &paths {
Expand Down
2 changes: 1 addition & 1 deletion libraries/extensions/ros2-bridge/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
fn main() {
use rust_format::Formatter;
let paths = ament_prefix_paths();
let generated = dora_ros2_bridge_msg_gen::gen(paths.as_slice(), false);
let generated = dora_ros2_bridge_msg_gen::gen(paths.as_slice(), false).unwrap();
let generated_string = rust_format::PrettyPlease::default()
.format_tokens(generated)
.unwrap();
Expand All @@ -20,7 +20,7 @@
let _build = cxx_build::bridge(&target_file);
}

fn ament_prefix_paths() -> Vec<PathBuf> {

Check warning on line 23 in libraries/extensions/ros2-bridge/build.rs

View workflow job for this annotation

GitHub Actions / CLI Test (ubuntu-latest)

function `ament_prefix_paths` is never used

Check warning on line 23 in libraries/extensions/ros2-bridge/build.rs

View workflow job for this annotation

GitHub Actions / CLI Test (macos-latest)

function `ament_prefix_paths` is never used

Check warning on line 23 in libraries/extensions/ros2-bridge/build.rs

View workflow job for this annotation

GitHub Actions / Examples (macos-latest)

function `ament_prefix_paths` is never used

Check warning on line 23 in libraries/extensions/ros2-bridge/build.rs

View workflow job for this annotation

GitHub Actions / Examples (macos-latest)

function `ament_prefix_paths` is never used

Check warning on line 23 in libraries/extensions/ros2-bridge/build.rs

View workflow job for this annotation

GitHub Actions / CLI Test (windows-latest)

function `ament_prefix_paths` is never used

Check warning on line 23 in libraries/extensions/ros2-bridge/build.rs

View workflow job for this annotation

GitHub Actions / Examples (ubuntu-latest)

function `ament_prefix_paths` is never used

Check warning on line 23 in libraries/extensions/ros2-bridge/build.rs

View workflow job for this annotation

GitHub Actions / Examples (ubuntu-latest)

function `ament_prefix_paths` is never used

Check warning on line 23 in libraries/extensions/ros2-bridge/build.rs

View workflow job for this annotation

GitHub Actions / Examples (windows-latest)

function `ament_prefix_paths` is never used

Check warning on line 23 in libraries/extensions/ros2-bridge/build.rs

View workflow job for this annotation

GitHub Actions / Examples (windows-latest)

function `ament_prefix_paths` is never used

Check warning on line 23 in libraries/extensions/ros2-bridge/build.rs

View workflow job for this annotation

GitHub Actions / ROS2 Bridge Examples

function `ament_prefix_paths` is never used

Check warning on line 23 in libraries/extensions/ros2-bridge/build.rs

View workflow job for this annotation

GitHub Actions / ROS2 Bridge Examples

function `ament_prefix_paths` is never used
let ament_prefix_path: String = match std::env::var("AMENT_PREFIX_PATH") {
Ok(path) => path,
Err(std::env::VarError::NotPresent) => {
Expand Down
9 changes: 5 additions & 4 deletions libraries/extensions/ros2-bridge/msg-gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub mod types;

pub use crate::parser::get_packages;

pub fn gen<P>(paths: &[P], create_cxx_bridge: bool) -> proc_macro2::TokenStream
pub fn gen<P>(paths: &[P], create_cxx_bridge: bool) -> anyhow::Result<proc_macro2::TokenStream>
where
P: AsRef<Path>,
{
Expand Down Expand Up @@ -48,7 +48,7 @@ where
service_impls.push(imp);
if create_cxx_bridge {
let (service_creation_def, service_creation_impl) =
service.cxx_service_creation_functions(&package.name);
service.cxx_service_creation_functions(&package.name)?;
service_creation_defs.push(service_creation_def);
service_creation_impls.push(service_creation_impl);
}
Expand Down Expand Up @@ -240,7 +240,7 @@ where
)
};

quote! {
let generated = quote! {
#attributes
mod ffi {
#imports_and_functions
Expand Down Expand Up @@ -271,5 +271,6 @@ where
#(#service_impls)*

#(#aliases)*
}
};
Ok(generated)
}
32 changes: 29 additions & 3 deletions libraries/extensions/ros2-bridge/msg-gen/src/types/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Service {
pub fn cxx_service_creation_functions(
&self,
package_name: &str,
) -> (impl ToTokens, impl ToTokens) {
) -> anyhow::Result<(impl ToTokens, impl ToTokens)> {
let client_name = format_ident!("Client__{package_name}__{}", self.name);
let cxx_client_name = format_ident!("Client_{}", self.name);
let create_client = format_ident!("new_Client__{package_name}__{}", self.name);
Expand All @@ -102,6 +102,32 @@ impl Service {
let downcast = format_ident!("downcast__{package_name}__{}", self.name);
let cxx_downcast = format_ident!("downcast");

let ros_service_mapping = {
let enhanced = format_ident!("Enhanced");
let cyclone = format_ident!("Cyclone");
match std::env::var("RMW_IMPLEMENTATION") {
Ok(middleware) => match middleware.as_str() {
"rmw_fastrtps_cpp" => enhanced,
"rmw_cyclonedds_cpp" => cyclone,
other => anyhow::bail!("unsupported RMW_IMPLEMENTATION `{other}`"),
},
Err(std::env::VarError::NotPresent) => match std::env::var("ROS_DISTRO") {
Ok(distro) => match distro.as_str() {
"humble" | "iron" => enhanced,
"galactic" => cyclone,
other => anyhow::bail!("unsupported ROS_DISTRO `{other}`"),
},
Err(std::env::VarError::NotPresent) => anyhow::bail!("no ROS_DISTRO set"),
Err(std::env::VarError::NotUnicode(_)) => {
anyhow::bail!("ROS_DISTRO is not valid unicode")
}
},
Err(std::env::VarError::NotUnicode(other)) => {
anyhow::bail!("RMW_IMPLEMENTATION is not valid unicode `{:?}`", other)
}
}
};

let def = quote! {
#[namespace = #package_name]
#[cxx_name = #cxx_client_name]
Expand Down Expand Up @@ -130,7 +156,7 @@ impl Service {
use futures::StreamExt as _;

let client = self.node.create_client::< #package :: service :: #self_name >(
ros2_client::ServiceMapping::Enhanced,
ros2_client::ServiceMapping:: #ros_service_mapping,
&ros2_client::Name::new(name_space, base_name).unwrap(),
&ros2_client::ServiceTypeName::new(#package_name, #self_name_str),
qos.clone().into(),
Expand Down Expand Up @@ -222,7 +248,7 @@ impl Service {
}
}
};
(def, imp)
Ok((def, imp))
}

pub fn token_stream_with_mod(&self) -> impl ToTokens {
Expand Down
Loading