Skip to content

Commit 5589c29

Browse files
authored
Merge pull request clearpathrobotics#63 from clearpathrobotics/discovery-server
Discovery server support
2 parents df2720d + d6ed989 commit 5589c29

File tree

6 files changed

+171
-14
lines changed

6 files changed

+171
-14
lines changed

clearpath_generator_common/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ find_package(ament_cmake_python REQUIRED)
1111

1212
install(PROGRAMS
1313
${PROJECT_NAME}/description/generate_description
14+
${PROJECT_NAME}/discovery_server/generate_discovery_server
1415
${PROJECT_NAME}/bash/generate_bash
1516
DESTINATION lib/${PROJECT_NAME}
1617
)

clearpath_generator_common/clearpath_generator_common/bash/generator.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,36 +32,60 @@
3232
# modification, is not permitted without the express permission
3333
# of Clearpath Robotics.
3434

35+
from clearpath_config.common.types.discovery import Discovery
3536
from clearpath_generator_common.bash.writer import BashWriter
3637
from clearpath_generator_common.common import BashFile, BaseGenerator
3738

3839

3940
class BashGenerator(BaseGenerator):
4041

42+
ROS_DISTRO_PATH = '/opt/ros/humble/'
43+
4144
def generate(self) -> None:
45+
# Generate setup.bash
4246
self.generate_setup_bash()
4347

4448
def generate_setup_bash(self) -> None:
45-
clearpath_setup_bash = BashFile(name='setup', path=self.setup_path)
49+
clearpath_setup_bash = BashFile(filename='setup.bash', path=self.setup_path)
4650
bash_writer = BashWriter(clearpath_setup_bash)
4751

4852
workspaces = self.clearpath_config.system.workspaces
4953

5054
# Source Humble
51-
humble_setup_bash = BashFile(name='setup', path='/opt/ros/humble/')
55+
humble_setup_bash = BashFile(filename='setup.bash', path=self.ROS_DISTRO_PATH)
5256
bash_writer.add_source(humble_setup_bash)
5357

5458
# Additional workspaces
5559
for workspace in workspaces:
5660
bash_writer.add_source(
57-
BashFile(name='setup', path=workspace.strip('setup.bash')))
61+
BashFile(filename='setup.bash', path=workspace.strip('setup.bash')))
5862

5963
# ROS_DOMAIN_ID
6064
domain_id = self.clearpath_config.system.domain_id
6165
bash_writer.add_export('ROS_DOMAIN_ID', domain_id)
6266

6367
# RMW_IMPLEMENTATION
64-
rmw = self.clearpath_config.system.rmw_implementation
68+
rmw = self.clearpath_config.system.middleware.rmw_implementation
6569
bash_writer.add_export('RMW_IMPLEMENTATION', rmw)
6670

71+
# Custom DDS Profile
72+
if self.clearpath_config.system.middleware.profile:
73+
bash_writer.add_export(
74+
'FASTRTPS_DEFAULT_PROFILES_FILE', self.clearpath_config.system.middleware.profile)
75+
76+
# Fast DDS Discovery Server
77+
if self.clearpath_config.system.middleware.discovery == Discovery.SERVER:
78+
server_str = self.clearpath_config.system.middleware.get_servers_string()
79+
bash_writer.add_export('ROS_DISCOVERY_SERVER', f'"{server_str}"')
80+
81+
# If this is not being called by systemd then set as super user (for ROS 2 cli tools)
82+
bash_writer.write('if [ -t 0 ]; then')
83+
bash_writer.add_export('ROS_SUPER_CLIENT', True, indent_level=1)
84+
bash_writer.write('else')
85+
bash_writer.add_unset('ROS_SUPER_CLIENT', indent_level=1)
86+
bash_writer.write('fi')
87+
88+
else:
89+
bash_writer.add_unset('ROS_DISCOVERY_SERVER')
90+
6791
bash_writer.close()

clearpath_generator_common/clearpath_generator_common/bash/writer.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,19 @@ def __init__(self, bash_file: BashFile):
4141
self.file = open(self.bash_file.full_path, 'w')
4242

4343
def write(self, string, indent_level=0):
44-
self.file.write('{0}{1}\n'.format(self.tab * indent_level, string))
44+
self.file.write(f'{self.tab * indent_level}{string}\n')
4545

46-
def add_export(self, envar: str, value):
47-
self.write('export {0}={1}'.format(envar, value))
46+
def add_export(self, envar: str, value, indent_level=0):
47+
self.write(f'{self.tab * indent_level}export {envar}={value}')
4848

49-
def add_unset(self, envar: str):
50-
self.write('unset {0}'.format(envar))
49+
def add_unset(self, envar: str, indent_level=0):
50+
self.write(f'{self.tab * indent_level}unset {envar}')
5151

52-
def add_source(self, bash_file: BashFile):
53-
self.write('source {0}'.format(bash_file.full_path))
52+
def add_source(self, bash_file: BashFile, indent_level=0):
53+
self.write(f'{self.tab * indent_level}source {bash_file.full_path}')
54+
55+
def add_echo(self, msg: str, indent_level=0):
56+
self.write(f'{self.tab * indent_level}echo "{msg}"')
5457

5558
def close(self):
5659
self.file.close()

clearpath_generator_common/clearpath_generator_common/common.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,13 @@ def update(self, parameters: dict) -> None:
194194

195195
class BashFile():
196196
def __init__(self,
197-
name: str,
197+
filename: str,
198198
path: str,
199199
package: Package = None,
200200
) -> None:
201201
self.package = package
202202
self.path = path
203-
self.name = name
204-
self.file = '{0}.bash'.format(name)
203+
self.file = filename
205204

206205
@property
207206
def full_path(self):
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python3
2+
3+
# Software License Agreement (BSD)
4+
#
5+
# @author Hilary Luo <[email protected]>
6+
# @copyright (c) 2024, Clearpath Robotics, Inc., All rights reserved.
7+
#
8+
# Redistribution and use in source and binary forms, with or without
9+
# modification, are permitted provided that the following conditions are met:
10+
# * Redistributions of source code must retain the above copyright notice,
11+
# this list of conditions and the following disclaimer.
12+
# * Redistributions in binary form must reproduce the above copyright notice,
13+
# this list of conditions and the following disclaimer in the documentation
14+
# and/or other materials provided with the distribution.
15+
# * Neither the name of Clearpath Robotics nor the names of its contributors
16+
# may be used to endorse or promote products derived from this software
17+
# without specific prior written permission.
18+
#
19+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
# POSSIBILITY OF SUCH DAMAGE.
30+
31+
# Redistribution and use in source and binary forms, with or without
32+
# modification, is not permitted without the express permission
33+
# of Clearpath Robotics.
34+
35+
from clearpath_generator_common.common import BaseGenerator
36+
from clearpath_generator_common.discovery_server.generator import DiscoveryServerGenerator
37+
38+
39+
def main():
40+
setup_path = BaseGenerator.get_args()
41+
dsg = DiscoveryServerGenerator(setup_path)
42+
dsg.generate()
43+
44+
45+
if __name__ == '__main__':
46+
main()
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env python3
2+
3+
# Software License Agreement (BSD)
4+
#
5+
# @author Hilary Luo <[email protected]>
6+
# @copyright (c) 2024, Clearpath Robotics, Inc., All rights reserved.
7+
#
8+
# Redistribution and use in source and binary forms, with or without
9+
# modification, are permitted provided that the following conditions are met:
10+
# * Redistributions of source code must retain the above copyright notice,
11+
# this list of conditions and the following disclaimer.
12+
# * Redistributions in binary form must reproduce the above copyright notice,
13+
# this list of conditions and the following disclaimer in the documentation
14+
# and/or other materials provided with the distribution.
15+
# * Neither the name of Clearpath Robotics nor the names of its contributors
16+
# may be used to endorse or promote products derived from this software
17+
# without specific prior written permission.
18+
#
19+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
# POSSIBILITY OF SUCH DAMAGE.
30+
31+
# Redistribution and use in source and binary forms, with or without
32+
# modification, is not permitted without the express permission
33+
# of Clearpath Robotics.
34+
35+
from clearpath_config.common.types.discovery import Discovery
36+
from clearpath_config.common.types.rmw_implementation import RMWImplementation
37+
from clearpath_generator_common.bash.writer import BashWriter
38+
from clearpath_generator_common.common import BaseGenerator, BashFile
39+
40+
41+
class DiscoveryServerGenerator(BaseGenerator):
42+
43+
ROS_DISTRO_PATH = '/opt/ros/humble/'
44+
45+
def generate(self) -> None:
46+
# Generate the file that launches the FastDDS discovery server
47+
self.generate_server_start()
48+
49+
def generate_server_start(self) -> None:
50+
# Generate script that launches the discovery server
51+
discovery_server_start = BashFile(filename='discovery-server-start', path=self.setup_path)
52+
bash_writer = BashWriter(discovery_server_start)
53+
54+
# Source Humble
55+
humble_setup_bash = BashFile(filename='setup.bash', path=self.ROS_DISTRO_PATH)
56+
bash_writer.add_source(humble_setup_bash)
57+
58+
# If Fast DDS Discovery Server is selected then check if a local server should be run
59+
middleware_config = self.clearpath_config.system.middleware
60+
if ((middleware_config.rmw_implementation == RMWImplementation.FAST_RTPS) and
61+
(middleware_config.discovery == Discovery.SERVER)):
62+
63+
# For Debug:
64+
# for i, s in enumerate(self.clearpath_config.system.middleware.servers.get_all()):
65+
# print(f"Server {i} is {str(s)}")
66+
# print(f"Localhost is {self.clearpath_config.system.localhost}")
67+
68+
ls = middleware_config.get_local_server()
69+
if ls and ls.enabled:
70+
bash_writer.write(
71+
f'fastdds discovery -i {ls.server_id} -l 127.0.0.1 -p {ls.port}'
72+
)
73+
else:
74+
bash_writer.add_echo(
75+
'No local discovery server enabled. ' +
76+
'If this was launched as a service then the service will now end.'
77+
)
78+
else:
79+
bash_writer.add_echo(
80+
'Discovery server not enabled. ' +
81+
'If this was launched as a service then the service will now end.'
82+
)
83+
84+
bash_writer.close()

0 commit comments

Comments
 (0)