2626# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2727# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2828# POSSIBILITY OF SUCH DAMAGE.
29-
30-
29+ import os
30+ import sys
3131import time
3232import unittest
3333
3434import pytest
3535import 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
4236from rclpy .node import Node
43- from std_srvs .srv import Trigger
4437from ur_dashboard_msgs .msg import RobotMode
45- from ur_dashboard_msgs .srv import (
46- GetLoadedProgram ,
47- GetProgramState ,
48- GetRobotMode ,
49- IsProgramRunning ,
50- Load ,
51- )
5238
53- TIMEOUT_WAIT_SERVICE = 10
54- # If we download the docker image simultaneously to the tests, it can take quite some time until the
55- # dashboard server is reachable and usable.
56- TIMEOUT_WAIT_SERVICE_INITIAL = 120
39+ sys .path .append (os .path .dirname (__file__ ))
40+ from test_common import DashboardInterface , generate_dashboard_test_description # noqa: E402
5741
5842
5943@pytest .mark .launch_test
6044def 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 ])
45+ return generate_dashboard_test_description ()
10746
10847
10948class DashboardClientTest (unittest .TestCase ):
@@ -121,6 +60,7 @@ def tearDownClass(cls):
12160 rclpy .shutdown ()
12261
12362 def init_robot (self ):
63+ < << << << HEAD
12464
12565 # We wait longer for the first client, as the robot is still starting up
12666 power_on_client = waitForService (
@@ -154,6 +94,9 @@ def init_robot(self):
15494 #
15595 # Test functions
15696 #
97+ == == == =
98+ self ._dashboard_interface = DashboardInterface (self .node )
99+ >> >> >> > b28a870 (Simplify tests (#849))
157100
158101 def test_switch_on (self ):
159102 """Test power on a robot."""
@@ -162,59 +105,34 @@ def test_switch_on(self):
162105 mode = RobotMode .DISCONNECTED
163106 while mode != RobotMode .POWER_OFF and time .time () < end_time :
164107 time .sleep (0.1 )
165- result = self .call_dashboard_service ( "get_robot_mode" , GetRobotMode . Request () )
108+ result = self ._dashboard_interface . get_robot_mode ( )
166109 self .assertTrue (result .success )
167110 mode = result .robot_mode .mode
168111
169112 # Power on robot
170- self .assertTrue (self .call_dashboard_service ( "power_on" , Trigger . Request () ).success )
113+ self .assertTrue (self ._dashboard_interface . power_on ( ).success )
171114
172115 # Wait until robot mode changes
173116 end_time = time .time () + 10
174117 mode = RobotMode .DISCONNECTED
175118 while mode not in (RobotMode .IDLE , RobotMode .RUNNING ) and time .time () < end_time :
176119 time .sleep (0.1 )
177- result = self .call_dashboard_service ( "get_robot_mode" , GetRobotMode . Request () )
120+ result = self ._dashboard_interface . get_robot_mode ( )
178121 self .assertTrue (result .success )
179122 mode = result .robot_mode .mode
180123
181124 self .assertIn (mode , (RobotMode .IDLE , RobotMode .RUNNING ))
182125
183126 # Release robot brakes
184- self .assertTrue (self .call_dashboard_service ( "brake_release" , Trigger . Request () ).success )
127+ self .assertTrue (self ._dashboard_interface . brake_release ( ).success )
185128
186129 # Wait until robot mode is RUNNING
187130 end_time = time .time () + 10
188131 mode = RobotMode .DISCONNECTED
189132 while mode != RobotMode .RUNNING and time .time () < end_time :
190133 time .sleep (0.1 )
191- result = self .call_dashboard_service ( "get_robot_mode" , GetRobotMode . Request () )
134+ result = self ._dashboard_interface . get_robot_mode ( )
192135 self .assertTrue (result .success )
193136 mode = result .robot_mode .mode
194137
195138 self .assertEqual (mode , RobotMode .RUNNING )
196-
197- #
198- # Utility functions
199- #
200-
201- def call_dashboard_service (self , srv_name , request ):
202- self .node .get_logger ().info (
203- f"Calling dashboard service '{ srv_name } ' with request { request } "
204- )
205- future = self .dashboard_clients [srv_name ].call_async (request )
206- rclpy .spin_until_future_complete (self .node , future )
207- if future .result () is not None :
208- self .node .get_logger ().info (f"Received result { future .result ()} " )
209- return future .result ()
210- else :
211- raise Exception (f"Exception while calling service: { future .exception ()} " )
212-
213-
214- def waitForService (node , srv_name , srv_type , timeout = TIMEOUT_WAIT_SERVICE ):
215- client = node .create_client (srv_type , srv_name )
216- if client .wait_for_service (timeout ) is False :
217- raise Exception (f"Could not reach service '{ srv_name } ' within timeout of { timeout } " )
218-
219- node .get_logger ().info (f"Successfully connected to service '{ srv_name } '" )
220- return client
0 commit comments