From 2020b77a438b4c3ecc4fc609902dadd4c73f2d87 Mon Sep 17 00:00:00 2001 From: TSC21 Date: Thu, 2 Apr 2020 11:21:20 +0100 Subject: [PATCH] microRTPS: use FastRTPSGen '-typeros2' option to generate the typenaming required to interface the bridge with ROS2 topics --- msg/templates/urtps/Publisher.cpp.em | 7 ----- msg/templates/urtps/Subscriber.cpp.em | 8 ----- msg/tools/generate_microRTPS_bridge.py | 42 +++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 16 deletions(-) diff --git a/msg/templates/urtps/Publisher.cpp.em b/msg/templates/urtps/Publisher.cpp.em index 3519aaafef..f69b4cb784 100644 --- a/msg/templates/urtps/Publisher.cpp.em +++ b/msg/templates/urtps/Publisher.cpp.em @@ -101,13 +101,6 @@ bool @(topic)_Publisher::init() if(mp_participant == nullptr) return false; -@[if ros2_distro and (ros2_distro == "dashing" or ros2_distro == "eloquent")]@ - // Type name should match the expected type name on ROS2 - // Note: the change is being done here since the 'fastrtpsgen' example - // generator does not allow to change the type naming on the template - @(topic)DataType.setName("@(package)::msg::dds_::@(topic)_"); -@[end if]@ - // Register the type Domain::registerType(mp_participant, static_cast(&@(topic)DataType)); diff --git a/msg/templates/urtps/Subscriber.cpp.em b/msg/templates/urtps/Subscriber.cpp.em index 4b76477438..d6c6afb328 100644 --- a/msg/templates/urtps/Subscriber.cpp.em +++ b/msg/templates/urtps/Subscriber.cpp.em @@ -101,14 +101,6 @@ bool @(topic)_Subscriber::init(uint8_t topic_ID, std::condition_variable* t_send if(mp_participant == nullptr) return false; -@[if ros2_distro and (ros2_distro == "dashing" or ros2_distro == "eloquent")]@ - // Type name should match the expected type name on ROS2 - // Note: the change is being done here since the 'fastrtpsgen' example - // generator does not allow to change the type naming on the template of - // "*PubSubTypes.cpp" file - @(topic)DataType.setName("@(package)::msg::dds_::@(topic)_"); -@[end if]@ - //Register the type Domain::registerType(mp_participant, static_cast(&@(topic)DataType)); diff --git a/msg/tools/generate_microRTPS_bridge.py b/msg/tools/generate_microRTPS_bridge.py index fea35bfd4d..73c44e1c5e 100644 --- a/msg/tools/generate_microRTPS_bridge.py +++ b/msg/tools/generate_microRTPS_bridge.py @@ -58,6 +58,16 @@ except ImportError as e: print("") sys.exit(1) +try: + from packaging import version +except ImportError as e: + print("Failed to import packaging: " + str(e)) + print("") + print("You may need to install it using:") + print(" pip3 install --user packaging") + print("") + sys.exit(1) + def check_rtps_id_uniqueness(classifier): """ @@ -227,6 +237,28 @@ if fastrtpsgen_include is not None and fastrtpsgen_include != '': os.path.abspath( args.fastrtpsgen_include) + " " +# get FastRTPSGen version +# .. note:: since Fast-RTPS 1.8.0 release, FastRTPSGen is a separated repository +# and not included in the Fast-RTPS project. +# The starting version since this separation is 1.0.0, which follows its own +# versioning +fastrtpsgen_version = version.Version("1.0.0") +if(os.path.exists(fastrtpsgen_path)): + try: + fastrtpsgen_version_out = subprocess.check_output( + [fastrtpsgen_path, "-version"]).decode("utf-8").strip()[-5:] + except OSError: + raise + + try: + fastrtpsgen_version = version.parse(fastrtpsgen_version_out) + except version.InvalidVersion: + raise Exception( + "'fastrtpsgen -version' returned None or an invalid version") +else: + raise Exception( + "FastRTPSGen not found. Specify the location of fastrtpsgen with the -f flag") + # get FastRTPS version fastrtps_version = subprocess.check_output( "ldconfig -v | grep libfastrtps", shell=True).decode("utf-8").strip().split('so.')[-1] @@ -386,10 +418,18 @@ def generate_agent(out_dir): os.chdir(os.path.join(out_dir, "fastrtpsgen")) if not glob.glob(os.path.join(idl_dir, "*.idl")): raise Exception("No IDL files found in %s" % idl_dir) + + # If it is generating the bridge code for interfacing with ROS2, then set + # the '-typeros2' option in fastrtpsgen. + # .. note:: This is only available in FastRTPSGen 1.0.4 and above + gen_ros2_typename = "" + if ros2_distro and fastrtpsgen_version >= version.Version("1.0.4"): + gen_ros2_typename = "-typeros2 " + for idl_file in glob.glob(os.path.join(idl_dir, "*.idl")): try: ret = subprocess.check_call(fastrtpsgen_path + " -d " + out_dir + - "/fastrtpsgen -example x64Linux2.6gcc " + fastrtpsgen_include + idl_file, shell=True) + "/fastrtpsgen -example x64Linux2.6gcc " + gen_ros2_typename + fastrtpsgen_include + idl_file, shell=True) except OSError: raise