Skip to content

Commit 331a44b

Browse files
author
Javier Moreno
committed
Refs #3365. Integration with rmw_microros
1 parent becb074 commit 331a44b

5 files changed

Lines changed: 156 additions & 95 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,6 @@ install(
6868
)
6969

7070
install(
71-
DIRECTORY cmake
71+
DIRECTORY cmake resource
7272
DESTINATION share/${PROJECT_NAME}
7373
)

‎cmake/micrortps_agent_xml_generator.cmake‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ rosidl_write_generator_arguments(
4949
ROS_INTERFACE_FILES "${rosidl_generate_interfaces_IDL_FILES}"
5050
ROS_INTERFACE_DEPENDENCIES "${_dependencies}"
5151
OUTPUT_DIR "${_output_path}"
52-
TEMPLATE_DIR "NONE"
52+
TEMPLATE_DIR "${micro_ros_agent_DEFAULT_PROFILES_DIR}"
5353
TARGET_DEPENDENCIES ${target_dependencies}
5454
ADDITIONAL_FILES ${_dds_idl_files}
5555
)

‎micro_ros_agent-extras.cmake.in‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ set(micro_ros_agent_BIN "${micro_ros_agent_DIR}/../../../lib/micro_ros_agent/mic
88
normalize_path(micro_ros_agent_BIN "${micro_ros_agent_BIN}")
99

1010
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}")
11+
normalize_path(micro_ros_agent_GENERATOR_FILES "${micro_ros_agent_GENERATOR_FILES}")
12+
13+
set(micro_ros_agent_DEFAULT_PROFILES_DIR "${micro_ros_agent_DIR}/../resource")
14+
normalize_path(micro_ros_agent_DEFAULT_PROFILES_DIR "${micro_ros_agent_DEFAULT_PROFILES_DIR}")

‎micro_ros_agent/__init__.py‎

Lines changed: 111 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import struct
16+
import fcntl
1517
import os
18+
import sys
1619

1720
from rosidl_cmake import convert_camel_case_to_lower_case_underscore
1821
from rosidl_cmake import expand_template
@@ -21,6 +24,7 @@
2124
from rosidl_parser import parse_message_file
2225
from rosidl_parser import parse_service_file
2326
from rosidl_parser import validate_field_types
27+
from shutil import copyfile
2428
from pathlib import Path
2529

2630

@@ -33,6 +37,34 @@ def generate_micro_ros_agent_xml_support(args):
3337
'get_header_filename_from_msg_name': convert_camel_case_to_lower_case_underscore,
3438
}
3539

40+
41+
# Set file format
42+
file_format = ".xml"
43+
ros2_prefix = "rt/"
44+
45+
46+
# Check destination dir
47+
dest_dir = args['output_dir']
48+
if not os.path.exists(dest_dir):
49+
os.makedirs(dest_dir)
50+
51+
52+
# Check source dir
53+
srcs_dir = os.path.join(dest_dir, "srcs")
54+
if not os.path.exists(srcs_dir):
55+
os.makedirs(srcs_dir)
56+
57+
58+
# Copy all included xml files
59+
for filename in os.listdir(args['template_dir']):
60+
if filename.endswith(file_format):
61+
template_src_path = os.path.join(args['template_dir'], filename)
62+
template_dest_path = os.path.join(srcs_dir, filename)
63+
if not os.path.isfile(template_dest_path):
64+
copyfile(template_src_path, template_dest_path)
65+
66+
67+
# Iterate throw all msgs/srvs
3668
for idl_file in args['ros_interface_files']:
3769
extension = os.path.splitext(idl_file)[1]
3870
if extension == '.msg':
@@ -50,97 +82,84 @@ def generate_micro_ros_agent_xml_support(args):
5082
data.update(functions)
5183

5284

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 = os.path.join(args['output_dir'], '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-
#publ = open(pub_file_path, "w")
70-
#publ.seek(publ.seek(-1,2) - len("</profiles>\n"))
71-
72-
73-
74-
publ.write(" <publisher profile_name=\"default_xrce_publisher_profile\">\n")
75-
publ.write(" <topic>\n")
76-
publ.write(" <kind>NO_KEY</kind>\n")
77-
publ.write(" <name>%sPubSubTopic</name>\n" % (spec.msg_name))
78-
publ.write(" <dataType>%s::%s::dds_::%s_</dataType>\n" % (spec.base_type.pkg_name, subfolder, spec.msg_name))
79-
publ.write(" <historyQos>\n")
80-
publ.write(" <kind>KEEP_LAST</kind>\n")
81-
publ.write(" <depth>5</depth>\n")
82-
publ.write(" </historyQos>\n")
83-
publ.write(" <durability>\n")
84-
publ.write(" <kind>TRANSIENT_LOCAL</kind>\n")
85-
publ.write(" </durability>\n")
86-
publ.write(" </topic>\n")
87-
publ.write(" </publisher>\n")
88-
publ.write("</profiles>\n")
89-
publ.close()
90-
91-
92-
93-
# Check if subcriber exists
94-
subs_file_path = os.path.join(args['output_dir'], 'subscriber.xml')
95-
subs_file = Path(subs_file_path)
96-
if not subs_file.is_file():
97-
subs = open(subs_file_path, 'a')
98-
subs.write("<profiles>\n")
99-
else:
100-
subs = open(subs_file_path)
101-
lines = subs.readlines()
102-
subs.close()
103-
subs = open(subs_file_path,'w')
104-
subs.writelines([item for item in lines[:-1]])
105-
106-
107-
subs.write(" <subscriber profile_name=\"default_xrce_subscriber_profile\">\n")
108-
subs.write(" <topic>\n")
109-
subs.write(" <kind>NO_KEY</kind>\n")
110-
subs.write(" <name>%sPubSubTopic</name>\n" % (spec.msg_name))
111-
subs.write(" <dataType>%s::%s::dds_::%s_</dataType>\n" % (spec.base_type.pkg_name, subfolder, spec.msg_name))
112-
subs.write(" <historyQos>\n")
113-
subs.write(" <kind>KEEP_LAST</kind>\n")
114-
subs.write(" <depth>5</depth>\n")
115-
subs.write(" </historyQos>\n")
116-
subs.write(" <durability>\n")
117-
subs.write(" <kind>TRANSIENT_LOCAL</kind>\n")
118-
subs.write(" </durability>\n")
119-
subs.write(" </topic>\n")
120-
subs.write(" </subscriber>\n")
121-
subs.write("</profiles>\n")
122-
subs.close()
123-
124-
125-
# Check if topic exists
126-
topi_file_path = os.path.join(args['output_dir'], 'topic.xml')
127-
topi_file = Path(topi_file_path)
128-
if not topi_file.is_file():
129-
topi = open(topi_file_path, 'a')
130-
topi.write("<dds>\n")
131-
else:
132-
topi = open(topi_file_path)
133-
lines = topi.readlines()
134-
topi.close()
135-
topi = open(topi_file_path,'w')
136-
topi.writelines([item for item in lines[:-1]])
137-
138-
topi.write(" <topic>\n")
139-
topi.write(" <name>%sPubSubTopic</name>\n" % (spec.msg_name))
140-
topi.write(" <dataType>%s::%s::dds_::%s_</dataType>\n" % (spec.base_type.pkg_name, subfolder, spec.msg_name))
141-
topi.write(" </topic>\n")
142-
topi.write("</dds>\n")
143-
topi.close()
85+
# Generate source file path
86+
src_file = os.path.join(srcs_dir, "%s_%s_%s.xml" % (spec.base_type.pkg_name, subfolder, spec.msg_name))
87+
88+
89+
# Publisher
90+
file_content = " <publisher profile_name=\"%s_%s_%s_p\">\n" % (spec.base_type.pkg_name, subfolder, spec.msg_name)
91+
file_content += " <topic profile_name=\"%s_%s_%s_t\">\n" % (spec.base_type.pkg_name, subfolder, spec.msg_name)
92+
file_content += " <kind>NO_KEY</kind>\n"
93+
file_content += " <name>%s%s_%s_%s</name>\n" % (ros2_prefix, spec.base_type.pkg_name, subfolder, spec.msg_name)
94+
file_content += " <dataType>%s::%s::dds_::%s_</dataType>\n" % (spec.base_type.pkg_name, subfolder, spec.msg_name)
95+
file_content += " <historyQos>\n"
96+
file_content += " <kind>KEEP_LAST</kind>\n"
97+
file_content += " <depth>5</depth>\n"
98+
file_content += " </historyQos>\n"
99+
file_content += " <durability>\n"
100+
file_content += " <kind>TRANSIENT_LOCAL</kind>\n"
101+
file_content += " </durability>\n"
102+
file_content += " </topic>\n"
103+
file_content += " </publisher>\n"
104+
105+
106+
# Subscriber
107+
file_content += " <subscriber profile_name=\"%s_%s_%s_s\">\n" % (spec.base_type.pkg_name, subfolder, spec.msg_name)
108+
file_content += " <topic profile_name=\"%s_%s_%s_t\">\n" % (spec.base_type.pkg_name, subfolder, spec.msg_name)
109+
file_content += " <kind>NO_KEY</kind>\n"
110+
file_content += " <name>%s%s_%s_%s</name>\n" % (ros2_prefix, spec.base_type.pkg_name, subfolder, spec.msg_name)
111+
file_content += " <dataType>%s::%s::dds_::%s_</dataType>\n" % (spec.base_type.pkg_name, subfolder, spec.msg_name)
112+
file_content += " <historyQos>\n"
113+
file_content += " <kind>KEEP_LAST</kind>\n"
114+
file_content += " <depth>5</depth>\n"
115+
file_content += " </historyQos>\n"
116+
file_content += " <durability>\n"
117+
file_content += " <kind>TRANSIENT_LOCAL</kind>\n"
118+
file_content += " </durability>\n"
119+
file_content += " </topic>\n"
120+
file_content += " </subscriber>\n"
121+
122+
123+
# Topic
124+
file_content += " <topic profile_name=\"%s_%s_%s_t\">\n" % (spec.base_type.pkg_name, subfolder, spec.msg_name)
125+
file_content += " <name>%s%s_%s_%s</name>\n" % (ros2_prefix, spec.base_type.pkg_name, subfolder, spec.msg_name)
126+
file_content += " <dataType>%s::%s::dds_::%s_</dataType>\n" % (spec.base_type.pkg_name, subfolder, spec.msg_name)
127+
file_content += " </topic>\n"
128+
129+
130+
# Write file content
131+
fd1 = open(src_file, "w")
132+
fcntl.lockf(fd1, fcntl.LOCK_EX)
133+
fd1.write(file_content)
134+
fd1.close()
135+
136+
137+
# Open collect file
138+
collec_file = os.path.join(args['output_dir'], "DEFAULT_FASTRTPS_PROFILES.xml")
139+
fd2 = open(collec_file, "w")
140+
fcntl.lockf(fd2, fcntl.LOCK_EX)
141+
142+
143+
# Generate head
144+
fd2.write("<profiles>\n")
145+
146+
147+
# Append all files contents
148+
for filename in os.listdir(srcs_dir):
149+
if filename.endswith(".xml"):
150+
fd3 = open(os.path.join(srcs_dir, filename), "r+")
151+
fcntl.lockf(fd3, fcntl.LOCK_EX)
152+
fd2.write(fd3.read())
153+
fd3.close()
154+
155+
156+
# Generate tail
157+
fd2.write("</profiles>\n")
158+
159+
160+
# Close file
161+
fd2.close()
162+
144163

145164
#elif extension == '.srv':
146165

@@ -153,4 +172,4 @@ def generate_micro_ros_agent_xml_support(args):
153172
#f = open(os.path.join(args['output_dir'], 'demofile.txt'), 'w+')
154173
#f.write("%s\n" % spec)
155174
#f.close()
156-
return 0
175+
return 0

‎resource/StaticValues.xml‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<participant profile_name="participant_profile">
2+
<rtps>
3+
<builtin>
4+
<leaseDuration>
5+
<durationbyname>INFINITE</durationbyname>
6+
</leaseDuration>
7+
<domainId>0</domainId>
8+
</builtin>
9+
<name>DataReader_participant_subscriber</name>
10+
</rtps>
11+
</participant>
12+
<publisher profile_name="publisher_profile">
13+
<topic>
14+
<kind>WITH_KEY</kind>
15+
<name>Square</name>
16+
<dataType>ShapeType</dataType>
17+
<historyQos>
18+
<kind>KEEP_LAST</kind>
19+
<depth>5</depth>
20+
</historyQos>
21+
<durability>
22+
<kind>TRANSIENT_LOCAL</kind>
23+
</durability>
24+
</topic>
25+
</publisher>
26+
<subscriber profile_name="subscriber_profile">
27+
<topic>
28+
<kind>WITH_KEY</kind>
29+
<name>Square</name>
30+
<dataType>ShapeType</dataType>
31+
<historyQos>
32+
<kind>KEEP_LAST</kind>
33+
<depth>5</depth>
34+
</historyQos>
35+
<durability>
36+
<kind>TRANSIENT_LOCAL</kind>
37+
</durability>
38+
</topic>
39+
</subscriber>

0 commit comments

Comments
 (0)