msg: add message translation node for ROS

This commit is contained in:
Beat Küng
2025-02-11 13:19:25 +01:00
committed by Silvan Fuhrer
parent 975ec30c9c
commit f6bfa9812e
32 changed files with 3200 additions and 0 deletions
+76
View File
@@ -0,0 +1,76 @@
cmake_minimum_required(VERSION 3.5)
project(px4_msgs_old)
list(INSERT CMAKE_MODULE_PATH 0 "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra)
endif()
find_package(ament_cmake REQUIRED)
find_package(builtin_interfaces REQUIRED)
find_package(rosidl_default_generators REQUIRED)
# ##############################################################################
# Generate ROS messages, ROS2 interfaces and IDL files #
# ##############################################################################
# get all msg files
set(MSGS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/msg")
file(GLOB PX4_MSGS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "${MSGS_DIR}/*.msg")
# get all srv files
set(SRVS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/srv")
file(GLOB PX4_SRVS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "${SRVS_DIR}/*.srv")
# For the versioned topics, replace the namespace (px4_msgs_old -> px4_msgs) and message type name (<msg>Vx -> <msg>),
# so that DDS does not reject the subscription/publication due to mismatching type
# rosidl_typesupport_fastrtps_cpp
set(rosidl_typesupport_fastrtps_cpp_BIN ${CMAKE_CURRENT_BINARY_DIR}/rosidl_typesupport_fastrtps_cpp_wrapper.py)
file(TOUCH ${rosidl_typesupport_fastrtps_cpp_BIN})
# rosidl_typesupport_fastrtps_c
set(rosidl_typesupport_fastrtps_c_BIN ${CMAKE_CURRENT_BINARY_DIR}/rosidl_typesupport_fastrtps_c_wrapper.py)
file(TOUCH ${rosidl_typesupport_fastrtps_c_BIN})
# rosidl_typesupport_introspection_cpp (for cyclonedds)
set(rosidl_typesupport_introspection_cpp_BIN ${CMAKE_CURRENT_BINARY_DIR}/rosidl_typesupport_introspection_cpp_wrapper.py)
file(TOUCH ${rosidl_typesupport_introspection_cpp_BIN})
# Generate introspection typesupport for C and C++ and IDL files
if(PX4_MSGS)
rosidl_generate_interfaces(${PROJECT_NAME}
${PX4_MSGS}
${PX4_SRVS}
DEPENDENCIES builtin_interfaces
ADD_LINTER_TESTS
)
endif()
# rosidl_typesupport_fastrtps_cpp
set(rosidl_typesupport_fastrtps_cpp_orig ${rosidl_typesupport_fastrtps_cpp_DIR})
string(REPLACE "share/rosidl_typesupport_fastrtps_cpp/cmake" "lib/rosidl_typesupport_fastrtps_cpp/rosidl_typesupport_fastrtps_cpp"
rosidl_typesupport_fastrtps_cpp_orig ${rosidl_typesupport_fastrtps_cpp_DIR})
set(original_script_path ${rosidl_typesupport_fastrtps_cpp_orig})
configure_file(rename_msg_type.py.in ${rosidl_typesupport_fastrtps_cpp_BIN} @ONLY)
# rosidl_typesupport_fastrtps_c
set(rosidl_typesupport_fastrtps_c_orig ${rosidl_typesupport_fastrtps_c_DIR})
string(REPLACE "share/rosidl_typesupport_fastrtps_c/cmake" "lib/rosidl_typesupport_fastrtps_c/rosidl_typesupport_fastrtps_c"
rosidl_typesupport_fastrtps_c_orig ${rosidl_typesupport_fastrtps_c_DIR})
set(original_script_path ${rosidl_typesupport_fastrtps_c_orig})
configure_file(rename_msg_type.py.in ${rosidl_typesupport_fastrtps_c_BIN} @ONLY)
# rosidl_typesupport_introspection_cpp
set(rosidl_typesupport_introspection_cpp_orig ${rosidl_typesupport_introspection_cpp_DIR})
string(REPLACE "share/rosidl_typesupport_introspection_cpp/cmake" "lib/rosidl_typesupport_introspection_cpp/rosidl_typesupport_introspection_cpp"
rosidl_typesupport_introspection_cpp_orig ${rosidl_typesupport_introspection_cpp_DIR})
set(original_script_path ${rosidl_typesupport_introspection_cpp_orig})
configure_file(rename_msg_type.py.in ${rosidl_typesupport_introspection_cpp_BIN} @ONLY)
ament_export_dependencies(rosidl_default_runtime)
ament_package()
+25
View File
@@ -0,0 +1,25 @@
<?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>px4_msgs_old</name>
<version>2.0.1</version>
<description>Package with the ROS-equivalent of PX4 uORB msgs (old message definitions)</description>
<maintainer email="info@px4.io">PX4</maintainer>
<license>BSD 3-Clause</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<buildtool_depend>rosidl_default_generators</buildtool_depend>
<depend>builtin_interfaces</depend>
<depend>ros_environment</depend>
<exec_depend>rosidl_default_runtime</exec_depend>
<test_depend>ament_lint_common</test_depend>
<member_of_group>rosidl_interface_packages</member_of_group>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
+39
View File
@@ -0,0 +1,39 @@
#! /bin/python
import sys
import subprocess
import json
import os
import re
original_script = "@original_script_path@"
args = sys.argv[1:]
json_file = [arg for arg in args if arg.endswith('.json')][0]
proc = subprocess.run(['python3', original_script] + args)
proc.check_returncode()
def replace_namespace_and_type(content: str):
# Replace namespace type
content = content.replace('"px4_msgs_old"', '"px4_msgs"')
content = content.replace('"px4_msgs_old::msg"', '"px4_msgs::msg"')
# Replace versioned type with non-versioned one
content = re.sub(r'("[a-zA-Z0-9]+)V[0-9]+"', '\\1"', content)
# Services
content = content.replace('"px4_msgs_old::srv"', '"px4_msgs::srv"')
content = re.sub(r'("[a-zA-Z0-9]+)V[0-9]+_Request"', '\\1_Request"', content)
content = re.sub(r'("[a-zA-Z0-9]+)V[0-9]+_Response"', '\\1_Response"', content)
return content
with open(json_file, 'r') as f:
data = json.load(f)
output_dir = data['output_dir']
# Iterate files recursively
for root, dirs, files in os.walk(output_dir):
for file in files:
with open(os.path.join(root, file), 'r+') as f:
content = f.read()
f.seek(0)
f.write(replace_namespace_and_type(content))
f.truncate()