Skip to content

Commit 40a9bfa

Browse files
Remove duplication of launch description in tests
1 parent f16ae2a commit 40a9bfa

File tree

4 files changed

+158
-208
lines changed

4 files changed

+158
-208
lines changed

ur_robot_driver/test/dashboard_client.py

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,13 @@
2828
# POSSIBILITY OF SUCH DAMAGE.
2929

3030

31+
import os
32+
import sys
3133
import time
3234
import unittest
3335

3436
import pytest
3537
import rclpy
36-
from launch import LaunchDescription
37-
from launch.actions import DeclareLaunchArgument, ExecuteProcess, IncludeLaunchDescription
38-
from launch.launch_description_sources import PythonLaunchDescriptionSource
39-
from launch.substitutions import LaunchConfiguration, PathJoinSubstitution
40-
from launch_ros.substitutions import FindPackagePrefix, FindPackageShare
41-
from launch_testing.actions import ReadyToTest
4238
from rclpy.node import Node
4339
from std_srvs.srv import Trigger
4440
from ur_dashboard_msgs.msg import RobotMode
@@ -50,6 +46,9 @@
5046
Load,
5147
)
5248

49+
sys.path.append(os.path.dirname(__file__))
50+
from test_common import generate_dashboard_test_description # noqa: E402
51+
5352
TIMEOUT_WAIT_SERVICE = 10
5453
# If we download the docker image simultaneously to the tests, it can take quite some time until the
5554
# dashboard server is reachable and usable.
@@ -58,52 +57,7 @@
5857

5958
@pytest.mark.launch_test
6059
def generate_test_description():
61-
declared_arguments = []
62-
63-
declared_arguments.append(
64-
DeclareLaunchArgument(
65-
"ur_type",
66-
default_value="ur5e",
67-
description="Type/series of used UR robot.",
68-
choices=["ur3", "ur3e", "ur5", "ur5e", "ur10", "ur10e", "ur16e", "ur20"],
69-
)
70-
)
71-
72-
ur_type = LaunchConfiguration("ur_type")
73-
74-
dashboard_client = IncludeLaunchDescription(
75-
PythonLaunchDescriptionSource(
76-
PathJoinSubstitution(
77-
[
78-
FindPackageShare("ur_robot_driver"),
79-
"launch",
80-
"ur_dashboard_client.launch.py",
81-
]
82-
)
83-
),
84-
launch_arguments={
85-
"robot_ip": "192.168.56.101",
86-
}.items(),
87-
)
88-
ursim = ExecuteProcess(
89-
cmd=[
90-
PathJoinSubstitution(
91-
[
92-
FindPackagePrefix("ur_client_library"),
93-
"lib",
94-
"ur_client_library",
95-
"start_ursim.sh",
96-
]
97-
),
98-
" ",
99-
"-m ",
100-
ur_type,
101-
],
102-
name="start_ursim",
103-
output="screen",
104-
)
105-
106-
return LaunchDescription(declared_arguments + [ReadyToTest(), dashboard_client, ursim])
60+
return generate_dashboard_test_description()
10761

10862

10963
class DashboardClientTest(unittest.TestCase):

ur_robot_driver/test/robot_driver.py

Lines changed: 7 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
# POSSIBILITY OF SUCH DAMAGE.
2929

3030

31+
import os
32+
import sys
3133
import time
3234
import unittest
3335

@@ -37,18 +39,6 @@
3739
from builtin_interfaces.msg import Duration
3840
from control_msgs.action import FollowJointTrajectory
3941
from controller_manager_msgs.srv import SwitchController
40-
from launch import LaunchDescription
41-
from launch.actions import (
42-
DeclareLaunchArgument,
43-
ExecuteProcess,
44-
IncludeLaunchDescription,
45-
RegisterEventHandler,
46-
)
47-
from launch.event_handlers import OnProcessExit
48-
from launch.launch_description_sources import PythonLaunchDescriptionSource
49-
from launch.substitutions import LaunchConfiguration, PathJoinSubstitution
50-
from launch_ros.substitutions import FindPackagePrefix, FindPackageShare
51-
from launch_testing.actions import ReadyToTest
5242
from rclpy.action import ActionClient
5343
from rclpy.node import Node
5444
from sensor_msgs.msg import JointState
@@ -59,6 +49,9 @@
5949
from ur_msgs.msg import IOStates
6050
from ur_msgs.srv import SetIO
6151

52+
sys.path.append(os.path.dirname(__file__))
53+
from test_common import generate_driver_test_description # noqa: 402
54+
6255
TIMEOUT_WAIT_SERVICE = 10
6356
TIMEOUT_WAIT_SERVICE_INITIAL = 60
6457
TIMEOUT_WAIT_ACTION = 10
@@ -80,69 +73,8 @@
8073
[(""), ("my_ur_")],
8174
)
8275
def generate_test_description(tf_prefix):
83-
declared_arguments = []
84-
85-
declared_arguments.append(
86-
DeclareLaunchArgument(
87-
"ur_type",
88-
default_value="ur5e",
89-
description="Type/series of used UR robot.",
90-
choices=["ur3", "ur3e", "ur5", "ur5e", "ur10", "ur10e", "ur16e", "ur20"],
91-
)
92-
)
93-
94-
ur_type = LaunchConfiguration("ur_type")
95-
96-
robot_driver = IncludeLaunchDescription(
97-
PythonLaunchDescriptionSource(
98-
PathJoinSubstitution(
99-
[FindPackageShare("ur_robot_driver"), "launch", "ur_control.launch.py"]
100-
)
101-
),
102-
launch_arguments={
103-
"robot_ip": "192.168.56.101",
104-
"ur_type": ur_type,
105-
"launch_rviz": "false",
106-
"controller_spawner_timeout": str(TIMEOUT_WAIT_SERVICE_INITIAL),
107-
"initial_joint_controller": "scaled_joint_trajectory_controller",
108-
"headless_mode": "true",
109-
"launch_dashboard_client": "false",
110-
"start_joint_controller": "false",
111-
"tf_prefix": tf_prefix,
112-
}.items(),
113-
)
114-
ursim = ExecuteProcess(
115-
cmd=[
116-
PathJoinSubstitution(
117-
[
118-
FindPackagePrefix("ur_client_library"),
119-
"lib",
120-
"ur_client_library",
121-
"start_ursim.sh",
122-
]
123-
),
124-
" ",
125-
"-m ",
126-
ur_type,
127-
],
128-
name="start_ursim",
129-
output="screen",
130-
)
131-
wait_dashboard_server = ExecuteProcess(
132-
cmd=[
133-
PathJoinSubstitution(
134-
[FindPackagePrefix("ur_robot_driver"), "bin", "wait_dashboard_server.sh"]
135-
)
136-
],
137-
name="wait_dashboard_server",
138-
output="screen",
139-
)
140-
driver_starter = RegisterEventHandler(
141-
OnProcessExit(target_action=wait_dashboard_server, on_exit=robot_driver)
142-
)
143-
144-
return LaunchDescription(
145-
declared_arguments + [ReadyToTest(), wait_dashboard_server, ursim, driver_starter]
76+
return generate_driver_test_description(
77+
tf_prefix=tf_prefix, controller_spawner_timeout=TIMEOUT_WAIT_SERVICE_INITIAL
14678
)
14779

14880

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Copyright 2023, FZI Forschungszentrum Informatik
2+
#
3+
# Redistribution and use in source and binary forms, with or without
4+
# modification, are permitted provided that the following conditions are met:
5+
#
6+
# * Redistributions of source code must retain the above copyright
7+
# notice, this list of conditions and the following disclaimer.
8+
#
9+
# * Redistributions in binary form must reproduce the above copyright
10+
# notice, this list of conditions and the following disclaimer in the
11+
# documentation and/or other materials provided with the distribution.
12+
#
13+
# * Neither the name of the {copyright_holder} nor the names of its
14+
# contributors may be used to endorse or promote products derived from
15+
# this software without specific prior written permission.
16+
#
17+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27+
# POSSIBILITY OF SUCH DAMAGE.
28+
from launch import LaunchDescription
29+
from launch.actions import (
30+
DeclareLaunchArgument,
31+
ExecuteProcess,
32+
IncludeLaunchDescription,
33+
RegisterEventHandler,
34+
)
35+
from launch.event_handlers import OnProcessExit
36+
from launch.launch_description_sources import PythonLaunchDescriptionSource
37+
from launch.substitutions import LaunchConfiguration, PathJoinSubstitution
38+
from launch_ros.substitutions import FindPackagePrefix, FindPackageShare
39+
from launch_testing.actions import ReadyToTest
40+
41+
42+
def _declare_launch_arguments():
43+
declared_arguments = []
44+
45+
declared_arguments.append(
46+
DeclareLaunchArgument(
47+
"ur_type",
48+
default_value="ur5e",
49+
description="Type/series of used UR robot.",
50+
choices=["ur3", "ur3e", "ur5", "ur5e", "ur10", "ur10e", "ur16e", "ur20"],
51+
)
52+
)
53+
54+
return declared_arguments
55+
56+
57+
def _ursim_action():
58+
ur_type = LaunchConfiguration("ur_type")
59+
60+
return ExecuteProcess(
61+
cmd=[
62+
PathJoinSubstitution(
63+
[
64+
FindPackagePrefix("ur_client_library"),
65+
"lib",
66+
"ur_client_library",
67+
"start_ursim.sh",
68+
]
69+
),
70+
" ",
71+
"-m ",
72+
ur_type,
73+
],
74+
name="start_ursim",
75+
output="screen",
76+
)
77+
78+
79+
def generate_dashboard_test_description():
80+
dashboard_client = IncludeLaunchDescription(
81+
PythonLaunchDescriptionSource(
82+
PathJoinSubstitution(
83+
[
84+
FindPackageShare("ur_robot_driver"),
85+
"launch",
86+
"ur_dashboard_client.launch.py",
87+
]
88+
)
89+
),
90+
launch_arguments={
91+
"robot_ip": "192.168.56.101",
92+
}.items(),
93+
)
94+
95+
return LaunchDescription(
96+
_declare_launch_arguments() + [ReadyToTest(), dashboard_client, _ursim_action()]
97+
)
98+
99+
100+
def generate_driver_test_description(tf_prefix="", controller_spawner_timeout=60):
101+
ur_type = LaunchConfiguration("ur_type")
102+
103+
robot_driver = IncludeLaunchDescription(
104+
PythonLaunchDescriptionSource(
105+
PathJoinSubstitution(
106+
[FindPackageShare("ur_robot_driver"), "launch", "ur_control.launch.py"]
107+
)
108+
),
109+
launch_arguments={
110+
"robot_ip": "192.168.56.101",
111+
"ur_type": ur_type,
112+
"launch_rviz": "false",
113+
"controller_spawner_timeout": str(controller_spawner_timeout),
114+
"initial_joint_controller": "scaled_joint_trajectory_controller",
115+
"headless_mode": "true",
116+
"launch_dashboard_client": "false",
117+
"start_joint_controller": "false",
118+
"tf_prefix": tf_prefix,
119+
}.items(),
120+
)
121+
wait_dashboard_server = ExecuteProcess(
122+
cmd=[
123+
PathJoinSubstitution(
124+
[FindPackagePrefix("ur_robot_driver"), "bin", "wait_dashboard_server.sh"]
125+
)
126+
],
127+
name="wait_dashboard_server",
128+
output="screen",
129+
)
130+
driver_starter = RegisterEventHandler(
131+
OnProcessExit(target_action=wait_dashboard_server, on_exit=robot_driver)
132+
)
133+
134+
return LaunchDescription(
135+
_declare_launch_arguments()
136+
+ [ReadyToTest(), wait_dashboard_server, _ursim_action(), driver_starter]
137+
)

0 commit comments

Comments
 (0)