From 331a44bc9c23de521766d457045aa11e2ed0aa2f Mon Sep 17 00:00:00 2001 From: Javier Moreno Date: Thu, 20 Sep 2018 17:10:29 +0200 Subject: [PATCH] Refs #3365. Integration with rmw_microros --- CMakeLists.txt | 2 +- cmake/micrortps_agent_xml_generator.cmake | 2 +- micro_ros_agent-extras.cmake.in | 5 +- micro_ros_agent/__init__.py | 185 ++++++++++++---------- resource/StaticValues.xml | 39 +++++ 5 files changed, 147 insertions(+), 86 deletions(-) create mode 100644 resource/StaticValues.xml diff --git a/CMakeLists.txt b/CMakeLists.txt index 5066069..0ff4bbc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -68,6 +68,6 @@ install( ) install( - DIRECTORY cmake + DIRECTORY cmake resource DESTINATION share/${PROJECT_NAME} ) \ No newline at end of file diff --git a/cmake/micrortps_agent_xml_generator.cmake b/cmake/micrortps_agent_xml_generator.cmake index 1cf3f6c..acf6d1b 100644 --- a/cmake/micrortps_agent_xml_generator.cmake +++ b/cmake/micrortps_agent_xml_generator.cmake @@ -49,7 +49,7 @@ rosidl_write_generator_arguments( ROS_INTERFACE_FILES "${rosidl_generate_interfaces_IDL_FILES}" ROS_INTERFACE_DEPENDENCIES "${_dependencies}" OUTPUT_DIR "${_output_path}" - TEMPLATE_DIR "NONE" + TEMPLATE_DIR "${micro_ros_agent_DEFAULT_PROFILES_DIR}" TARGET_DEPENDENCIES ${target_dependencies} ADDITIONAL_FILES ${_dds_idl_files} ) diff --git a/micro_ros_agent-extras.cmake.in b/micro_ros_agent-extras.cmake.in index 7cce9bb..aa4e047 100644 --- a/micro_ros_agent-extras.cmake.in +++ b/micro_ros_agent-extras.cmake.in @@ -8,4 +8,7 @@ set(micro_ros_agent_BIN "${micro_ros_agent_DIR}/../../../lib/micro_ros_agent/mic normalize_path(micro_ros_agent_BIN "${micro_ros_agent_BIN}") set(micro_ros_agent_GENERATOR_FILES "${micro_ros_agent_DIR}/../../../@PYTHON_INSTALL_DIR@/micro_ros_agent/__init__.py") -normalize_path(micro_ros_agent_GENERATOR_FILES "${micro_ros_agent_GENERATOR_FILES}") \ No newline at end of file +normalize_path(micro_ros_agent_GENERATOR_FILES "${micro_ros_agent_GENERATOR_FILES}") + +set(micro_ros_agent_DEFAULT_PROFILES_DIR "${micro_ros_agent_DIR}/../resource") +normalize_path(micro_ros_agent_DEFAULT_PROFILES_DIR "${micro_ros_agent_DEFAULT_PROFILES_DIR}") \ No newline at end of file diff --git a/micro_ros_agent/__init__.py b/micro_ros_agent/__init__.py index 67535e7..f07a321 100644 --- a/micro_ros_agent/__init__.py +++ b/micro_ros_agent/__init__.py @@ -12,7 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +import struct +import fcntl import os +import sys from rosidl_cmake import convert_camel_case_to_lower_case_underscore from rosidl_cmake import expand_template @@ -21,6 +24,7 @@ from rosidl_cmake import get_newest_modification_time from rosidl_parser import parse_message_file from rosidl_parser import parse_service_file from rosidl_parser import validate_field_types +from shutil import copyfile from pathlib import Path @@ -33,6 +37,34 @@ def generate_micro_ros_agent_xml_support(args): 'get_header_filename_from_msg_name': convert_camel_case_to_lower_case_underscore, } + + # Set file format + file_format = ".xml" + ros2_prefix = "rt/" + + + # Check destination dir + dest_dir = args['output_dir'] + if not os.path.exists(dest_dir): + os.makedirs(dest_dir) + + + # Check source dir + srcs_dir = os.path.join(dest_dir, "srcs") + if not os.path.exists(srcs_dir): + os.makedirs(srcs_dir) + + + # Copy all included xml files + for filename in os.listdir(args['template_dir']): + if filename.endswith(file_format): + template_src_path = os.path.join(args['template_dir'], filename) + template_dest_path = os.path.join(srcs_dir, filename) + if not os.path.isfile(template_dest_path): + copyfile(template_src_path, template_dest_path) + + + # Iterate throw all msgs/srvs for idl_file in args['ros_interface_files']: extension = os.path.splitext(idl_file)[1] if extension == '.msg': @@ -50,97 +82,84 @@ def generate_micro_ros_agent_xml_support(args): data.update(functions) - # Make destinatino dir - if not os.path.exists(args['output_dir']): - os.makedirs(args['output_dir']) - - # Check if publixher exists - pub_file_path = os.path.join(args['output_dir'], 'publisher.xml') - pub_file = Path(pub_file_path) - if not pub_file.is_file(): - publ = open(pub_file_path, 'a') - publ.write("\n") - else: - publ = open(pub_file_path) - lines = publ.readlines() - publ.close() - publ = open(pub_file_path,'w') - publ.writelines([item for item in lines[:-1]]) - #publ = open(pub_file_path, "w") - #publ.seek(publ.seek(-1,2) - len("\n")) + # Generate source file path + src_file = os.path.join(srcs_dir, "%s_%s_%s.xml" % (spec.base_type.pkg_name, subfolder, spec.msg_name)) - - publ.write(" \n") - publ.write(" \n") - publ.write(" NO_KEY\n") - publ.write(" %sPubSubTopic\n" % (spec.msg_name)) - publ.write(" %s::%s::dds_::%s_\n" % (spec.base_type.pkg_name, subfolder, spec.msg_name)) - publ.write(" \n") - publ.write(" KEEP_LAST\n") - publ.write(" 5\n") - publ.write(" \n") - publ.write(" \n") - publ.write(" TRANSIENT_LOCAL\n") - publ.write(" \n") - publ.write(" \n") - publ.write(" \n") - publ.write("\n") - publ.close() + # Publisher + file_content = " \n" % (spec.base_type.pkg_name, subfolder, spec.msg_name) + file_content += " \n" % (spec.base_type.pkg_name, subfolder, spec.msg_name) + file_content += " NO_KEY\n" + file_content += " %s%s_%s_%s\n" % (ros2_prefix, spec.base_type.pkg_name, subfolder, spec.msg_name) + file_content += " %s::%s::dds_::%s_\n" % (spec.base_type.pkg_name, subfolder, spec.msg_name) + file_content += " \n" + file_content += " KEEP_LAST\n" + file_content += " 5\n" + file_content += " \n" + file_content += " \n" + file_content += " TRANSIENT_LOCAL\n" + file_content += " \n" + file_content += " \n" + file_content += " \n" - - # Check if subcriber exists - subs_file_path = os.path.join(args['output_dir'], 'subscriber.xml') - subs_file = Path(subs_file_path) - if not subs_file.is_file(): - subs = open(subs_file_path, 'a') - subs.write("\n") - else: - subs = open(subs_file_path) - lines = subs.readlines() - subs.close() - subs = open(subs_file_path,'w') - subs.writelines([item for item in lines[:-1]]) + # Subscriber + file_content += " \n" % (spec.base_type.pkg_name, subfolder, spec.msg_name) + file_content += " \n" % (spec.base_type.pkg_name, subfolder, spec.msg_name) + file_content += " NO_KEY\n" + file_content += " %s%s_%s_%s\n" % (ros2_prefix, spec.base_type.pkg_name, subfolder, spec.msg_name) + file_content += " %s::%s::dds_::%s_\n" % (spec.base_type.pkg_name, subfolder, spec.msg_name) + file_content += " \n" + file_content += " KEEP_LAST\n" + file_content += " 5\n" + file_content += " \n" + file_content += " \n" + file_content += " TRANSIENT_LOCAL\n" + file_content += " \n" + file_content += " \n" + file_content += " \n" - subs.write(" \n") - subs.write(" \n") - subs.write(" NO_KEY\n") - subs.write(" %sPubSubTopic\n" % (spec.msg_name)) - subs.write(" %s::%s::dds_::%s_\n" % (spec.base_type.pkg_name, subfolder, spec.msg_name)) - subs.write(" \n") - subs.write(" KEEP_LAST\n") - subs.write(" 5\n") - subs.write(" \n") - subs.write(" \n") - subs.write(" TRANSIENT_LOCAL\n") - subs.write(" \n") - subs.write(" \n") - subs.write(" \n") - subs.write("\n") - subs.close() + # Topic + file_content += " \n" % (spec.base_type.pkg_name, subfolder, spec.msg_name) + file_content += " %s%s_%s_%s\n" % (ros2_prefix, spec.base_type.pkg_name, subfolder, spec.msg_name) + file_content += " %s::%s::dds_::%s_\n" % (spec.base_type.pkg_name, subfolder, spec.msg_name) + file_content += " \n" - # Check if topic exists - topi_file_path = os.path.join(args['output_dir'], 'topic.xml') - topi_file = Path(topi_file_path) - if not topi_file.is_file(): - topi = open(topi_file_path, 'a') - topi.write("\n") - else: - topi = open(topi_file_path) - lines = topi.readlines() - topi.close() - topi = open(topi_file_path,'w') - topi.writelines([item for item in lines[:-1]]) + # Write file content + fd1 = open(src_file, "w") + fcntl.lockf(fd1, fcntl.LOCK_EX) + fd1.write(file_content) + fd1.close() + + + # Open collect file + collec_file = os.path.join(args['output_dir'], "DEFAULT_FASTRTPS_PROFILES.xml") + fd2 = open(collec_file, "w") + fcntl.lockf(fd2, fcntl.LOCK_EX) + + + # Generate head + fd2.write("\n") + + + # Append all files contents + for filename in os.listdir(srcs_dir): + if filename.endswith(".xml"): + fd3 = open(os.path.join(srcs_dir, filename), "r+") + fcntl.lockf(fd3, fcntl.LOCK_EX) + fd2.write(fd3.read()) + fd3.close() + + + # Generate tail + fd2.write("\n") + + + # Close file + fd2.close() - topi.write(" \n") - topi.write(" %sPubSubTopic\n" % (spec.msg_name)) - topi.write(" %s::%s::dds_::%s_\n" % (spec.base_type.pkg_name, subfolder, spec.msg_name)) - topi.write(" \n") - topi.write("\n") - topi.close() #elif extension == '.srv': @@ -153,4 +172,4 @@ def generate_micro_ros_agent_xml_support(args): #f = open(os.path.join(args['output_dir'], 'demofile.txt'), 'w+') #f.write("%s\n" % spec) #f.close() - return 0 + return 0 \ No newline at end of file diff --git a/resource/StaticValues.xml b/resource/StaticValues.xml new file mode 100644 index 0000000..7444a10 --- /dev/null +++ b/resource/StaticValues.xml @@ -0,0 +1,39 @@ + + + + + INFINITE + + 0 + + DataReader_participant_subscriber + + + + + WITH_KEY + Square + ShapeType + + KEEP_LAST + 5 + + + TRANSIENT_LOCAL + + + + + + WITH_KEY + Square + ShapeType + + KEEP_LAST + 5 + + + TRANSIENT_LOCAL + + +