Skip to content

Commit c54a384

Browse files
author
Javier Moreno
committed
Refs #3366. XML generator works fine. The path of the generated xml may need to be changed.
1 parent 4790bdb commit c54a384

7 files changed

Lines changed: 395 additions & 9 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ cmake_minimum_required(VERSION 3.5)
1818

1919

2020
# Set proyect name
21-
project(micro-ros-agent)
21+
project(micro_ros_agent)
2222

2323

2424
# Find packages depencences
@@ -28,16 +28,46 @@ find_package(fastcdr REQUIRED CONFIG)
2828
find_package(fastrtps REQUIRED CONFIG)
2929
find_package(micrortps_agent REQUIRED CONFIG)
3030

31-
#find_package(ament_cmake_python REQUIRED)
31+
find_package(ament_cmake_python REQUIRED)
3232

3333

3434
# Export dependencies to downstream packages
3535
ament_export_dependencies(fastcdr)
3636
ament_export_dependencies(fastrtps)
3737
ament_export_dependencies(micrortps_agent)
3838

39-
#ament_export_dependencies(rosidl_cmake)
40-
#ament_export_dependencies(rosidl_generator_c)
41-
#ament_export_dependencies(rosidl_generator_dds_idl)
39+
ament_export_dependencies(rosidl_cmake)
40+
ament_export_dependencies(rosidl_generator_c)
41+
ament_export_dependencies(rosidl_generator_dds_idl)
4242

43-
ament_package()
43+
44+
#ament_export_include_directories(include)
45+
46+
# Install python files
47+
ament_python_install_package(${PROJECT_NAME})
48+
49+
50+
ament_index_register_resource("rosidl_typesupport_c")
51+
52+
53+
# Install the package.xml file, and generate code for
54+
# ``find_package`` so that other packages can get information about
55+
# this package.
56+
#
57+
# :param CONFIG_EXTRAS: a list of CMake files containing extra stuff
58+
# that should be accessible to users of this package after
59+
# ``find_package``\ -ing it.
60+
ament_package(
61+
CONFIG_EXTRAS "micro_ros_agent-extras.cmake.in"
62+
)
63+
64+
65+
install(
66+
PROGRAMS bin/micro_ros_agent
67+
DESTINATION lib/micro_ros_agent
68+
)
69+
70+
install(
71+
DIRECTORY cmake resource
72+
DESTINATION share/${PROJECT_NAME}
73+
)

‎bin/AAA‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python3
2+
3+
import argparse
4+
import os
5+
import sys
6+
7+
from rosidl_cmake import read_generator_arguments
8+
from rosidl_parser import UnknownMessageType
9+
from micro_ros_agent import generate_typesupport_micrortps_c
10+
11+
def is_valid_file(parser, file_name):
12+
if not os.path.exists(file_name):
13+
parser.error("File does not exist: '{0}'".format(file_name))
14+
file_name_abs = os.path.abspath(file_name)
15+
if not os.path.isfile(file_name_abs):
16+
parser.error("Path exists but is not a file: '{0}'".format(file_name_abs))
17+
return file_name
18+
19+
20+
def main(argv=sys.argv[1:]):
21+
22+
print "-------------------------B-------------"
23+
return 0
24+
25+
parser = argparse.ArgumentParser(
26+
description='Generate the C interfaces for Micro RTPS.',
27+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
28+
parser.add_argument(
29+
'--generator-arguments-file',
30+
required=True,
31+
help='The location of the file containing the generator arguments')
32+
args = parser.parse_args(argv)
33+
34+
generator_args = read_generator_arguments(args.generator_arguments_file)
35+
36+
try:
37+
rc = generate_typesupport_micrortps_c(generator_args)
38+
39+
return 0
40+
except UnknownMessageType as e:
41+
print(str(e), file=sys.stderr)
42+
return 1
43+
if rc:
44+
return rc
45+
return 0
46+
47+
48+
if __name__ == '__main__':
49+
print "-------------------------C-------------"
50+
51+
sys.exit(main())

‎bin/micro_ros_agent‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python3
2+
3+
import argparse
4+
import os
5+
import sys
6+
7+
from rosidl_cmake import read_generator_arguments
8+
from rosidl_parser import UnknownMessageType
9+
from micro_ros_agent import generate_micro_ros_agent_xml_support
10+
11+
def is_valid_file(parser, file_name):
12+
if not os.path.exists(file_name):
13+
parser.error("File does not exist: '{0}'".format(file_name))
14+
file_name_abs = os.path.abspath(file_name)
15+
if not os.path.isfile(file_name_abs):
16+
parser.error("Path exists but is not a file: '{0}'".format(file_name_abs))
17+
return file_name
18+
19+
20+
def main(argv=sys.argv[1:]):
21+
parser = argparse.ArgumentParser(
22+
description='Generate xml files for micrortps agent.',
23+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
24+
parser.add_argument(
25+
'--generator-arguments-file',
26+
required=True,
27+
help='The location of the file containing the generator arguments')
28+
args = parser.parse_args(argv)
29+
30+
generator_args = read_generator_arguments(args.generator_arguments_file)
31+
32+
try:
33+
rc = generate_micro_ros_agent_xml_support(generator_args)
34+
35+
return 0
36+
except UnknownMessageType as e:
37+
print(str(e), file=sys.stderr)
38+
return 1
39+
if rc:
40+
return rc
41+
return 0
42+
43+
44+
if __name__ == '__main__':
45+
sys.exit(main())
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright 2014-2015 Open Source Robotics Foundation, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
# list msg files
17+
set(_ros_idl_files "")
18+
foreach(_idl_file ${rosidl_generate_interfaces_IDL_FILES})
19+
get_filename_component(_extension "${_idl_file}" EXT)
20+
# Skip .srv files
21+
if(_extension STREQUAL ".msg")
22+
list(APPEND _ros_idl_files "${_idl_file}")
23+
endif()
24+
endforeach()
25+
26+
27+
# Set output dir
28+
set(_output_path "${CMAKE_CURRENT_BINARY_DIR}/micro_ros_agent/${PROJECT_NAME}")
29+
30+
31+
# check if all templates exits
32+
set(target_dependencies
33+
"${micro_ros_agent_BIN}"
34+
"${micro_ros_agent_GENERATOR_FILES}"
35+
${rosidl_generate_interfaces_IDL_FILES}
36+
${_dependency_files})
37+
foreach(dep ${target_dependencies})
38+
if(NOT EXISTS "${dep}")
39+
message(FATAL_ERROR "Target dependency '${dep}' does not exist")
40+
endif()
41+
endforeach()
42+
43+
44+
# generate script argument file
45+
set(generator_arguments_file "${CMAKE_CURRENT_BINARY_DIR}/micro_ros_agent__arguments.json")
46+
rosidl_write_generator_arguments(
47+
"${generator_arguments_file}"
48+
PACKAGE_NAME "${PROJECT_NAME}"
49+
ROS_INTERFACE_FILES "${rosidl_generate_interfaces_IDL_FILES}"
50+
ROS_INTERFACE_DEPENDENCIES "${_dependencies}"
51+
OUTPUT_DIR "${_output_path}"
52+
TEMPLATE_DIR "${micro_ros_agent_TEMPLATE_DIR}"
53+
TARGET_DEPENDENCIES ${target_dependencies}
54+
ADDITIONAL_FILES ${_dds_idl_files}
55+
)
56+
57+
58+
# Execute python script
59+
execute_process(
60+
COMMAND ${PYTHON_EXECUTABLE} ${micro_ros_agent_BIN}
61+
--generator-arguments-file "${generator_arguments_file}"
62+
)

‎micro_ros_agent-extras.cmake.in‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
find_package(ament_cmake_core QUIET REQUIRED)
2+
ament_register_extension(
3+
"rosidl_generate_interfaces"
4+
"micro_ros_agent"
5+
"micrortps_agent_xml_generator.cmake")
6+
7+
set(micro_ros_agent_BIN "${micro_ros_agent_DIR}/../../../lib/micro_ros_agent/micro_ros_agent")
8+
normalize_path(micro_ros_agent_BIN "${micro_ros_agent_BIN}")
9+
10+
set(micro_ros_agent_GENERATOR_FILES "${micro_ros_agent_DIR}/../../../@PYTHON_INSTALL_DIR@/micro_ros_agent/__init__.py")
11+
normalize_path(micro_ros_agent_GENERATOR_FILES "${micro_ros_agent_GENERATOR_FILES}")

‎micro_ros_agent/__init__.py‎

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# Copyright 2016 Open Source Robotics Foundation, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
from rosidl_cmake import convert_camel_case_to_lower_case_underscore
18+
from rosidl_cmake import expand_template
19+
from rosidl_cmake import extract_message_types
20+
from rosidl_cmake import get_newest_modification_time
21+
from rosidl_parser import parse_message_file
22+
from rosidl_parser import parse_service_file
23+
from rosidl_parser import validate_field_types
24+
from pathlib import Path
25+
26+
27+
def generate_micro_ros_agent_xml_support(args):
28+
pkg_name = args['package_name']
29+
known_msg_types = extract_message_types(
30+
pkg_name, args['ros_interface_files'], args.get('ros_interface_dependencies', []))
31+
32+
functions = {
33+
'get_header_filename_from_msg_name': convert_camel_case_to_lower_case_underscore,
34+
}
35+
36+
for idl_file in args['ros_interface_files']:
37+
extension = os.path.splitext(idl_file)[1]
38+
if extension == '.msg':
39+
spec = parse_message_file(pkg_name, idl_file)
40+
validate_field_types(spec, known_msg_types)
41+
subfolder = os.path.basename(os.path.dirname(idl_file))
42+
43+
data = {
44+
'spec': spec,
45+
'pkg': spec.base_type.pkg_name,
46+
'msg': spec.msg_name,
47+
'type': spec.base_type.type,
48+
'subfolder': subfolder,
49+
}
50+
data.update(functions)
51+
52+
53+
# Make destinatino dir
54+
if not os.path.exists(args['output_dir']):
55+
os.makedirs(args['output_dir'])
56+
57+
# Check if publixher exists
58+
pub_file_path = "/root/install/publisher.xml"
59+
pub_file = Path(pub_file_path)
60+
if not pub_file.is_file():
61+
publ = open(pub_file_path, 'a')
62+
publ.write("<profiles>\n")
63+
else:
64+
publ = open(pub_file_path)
65+
lines = publ.readlines()
66+
publ.close()
67+
publ = open(pub_file_path,'w')
68+
publ.writelines([item for item in lines[:-1]])
69+
70+
71+
#publ = open(os.path.join(args['output_dir'], 'publisher.xml'), 'w+')
72+
#publ = open('/root/install/publisher.xml', 'a')
73+
publ.write(" <publisher profile_name=\"default_xrce_publisher_profile\">\n")
74+
publ.write(" <topic>\n")
75+
publ.write(" <kind>NO_KEY</kind>\n")
76+
publ.write(" <name>%sPubSubTopic</name>\n" % (spec.msg_name))
77+
publ.write(" <dataType>%s::%s::dds_::%s_</dataType>\n" % (spec.base_type.pkg_name, subfolder, spec.msg_name))
78+
publ.write(" <historyQos>\n")
79+
publ.write(" <kind>KEEP_LAST</kind>\n")
80+
publ.write(" <depth>5</depth>\n")
81+
publ.write(" </historyQos>\n")
82+
publ.write(" <durability>\n")
83+
publ.write(" <kind>TRANSIENT_LOCAL</kind>\n")
84+
publ.write(" </durability>\n")
85+
publ.write(" </topic>\n")
86+
publ.write(" </publisher>\n")
87+
publ.write("</profiles>\n")
88+
publ.close()
89+
90+
91+
92+
# Check if subcriber exists
93+
subs_file_path = "/root/install/subscriber.xml"
94+
subs_file = Path(subs_file_path)
95+
if not subs_file.is_file():
96+
subs = open(subs_file_path, 'a')
97+
subs.write("<profiles>\n")
98+
else:
99+
subs = open(subs_file_path)
100+
lines = subs.readlines()
101+
subs.close()
102+
subs = open(subs_file_path,'w')
103+
subs.writelines([item for item in lines[:-1]])
104+
105+
106+
#subs = open(os.path.join(args['output_dir'], 'subscriber.xml'), 'w+')
107+
#subs = open('/root/install/subscriber.xml', 'a')
108+
#subs.write("<profiles>\n")
109+
subs.write(" <subscriber profile_name=\"default_xrce_subscriber_profile\">\n")
110+
subs.write(" <topic>\n")
111+
subs.write(" <kind>NO_KEY</kind>\n")
112+
subs.write(" <name>%sPubSubTopic</name>\n" % (spec.msg_name))
113+
subs.write(" <dataType>%s::%s::dds_::%s_</dataType>\n" % (spec.base_type.pkg_name, subfolder, spec.msg_name))
114+
subs.write(" <historyQos>\n")
115+
subs.write(" <kind>KEEP_LAST</kind>\n")
116+
subs.write(" <depth>5</depth>\n")
117+
subs.write(" </historyQos>\n")
118+
subs.write(" <durability>\n")
119+
subs.write(" <kind>TRANSIENT_LOCAL</kind>\n")
120+
subs.write(" </durability>\n")
121+
subs.write(" </topic>\n")
122+
subs.write(" </subscriber>\n")
123+
subs.write("</profiles>\n")
124+
subs.close()
125+
126+
127+
# Check if topic exists
128+
topi_file_path = "/root/install/topic.xml"
129+
topi_file = Path(topi_file_path)
130+
if not topi_file.is_file():
131+
topi = open(topi_file_path, 'a')
132+
topi.write("<dds>\n")
133+
else:
134+
topi = open(topi_file_path)
135+
lines = topi.readlines()
136+
topi.close()
137+
topi = open(topi_file_path,'w')
138+
topi.writelines([item for item in lines[:-1]])
139+
140+
141+
#topi = open(os.path.join(args['output_dir'], 'topic.xml'), 'w+')
142+
#topi = open('/root/install/topic.xml', 'a')
143+
#topi.write("<dds>\n")
144+
topi.write(" <topic>\n")
145+
topi.write(" <name>%sPubSubTopic</name>\n" % (spec.msg_name))
146+
topi.write(" <dataType>%s::%s::dds_::%s_</dataType>\n" % (spec.base_type.pkg_name, subfolder, spec.msg_name))
147+
topi.write(" </topic>\n")
148+
topi.write("</dds>\n")
149+
topi.close()
150+
151+
#elif extension == '.srv':
152+
153+
#data = {'spec': spec}
154+
#data.update(functions)
155+
156+
#if not os.path.exists(args['output_dir']):
157+
# os.makedirs(args['output_dir'])
158+
159+
#f = open(os.path.join(args['output_dir'], 'demofile.txt'), 'w+')
160+
#f.write("%s\n" % spec)
161+
#f.close()
162+
return 0

0 commit comments

Comments
 (0)