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-
3129import os
3230import sys
3331import time
3634import pytest
3735import rclpy
3836from rclpy .node import Node
39- from std_srvs .srv import Trigger
4037from ur_dashboard_msgs .msg import RobotMode
41- from ur_dashboard_msgs .srv import (
42- GetLoadedProgram ,
43- GetProgramState ,
44- GetRobotMode ,
45- IsProgramRunning ,
46- Load ,
47- )
4838
4939sys .path .append (os .path .dirname (__file__ ))
50- from test_common import generate_dashboard_test_description # noqa: E402
51-
52- TIMEOUT_WAIT_SERVICE = 10
53- # If we download the docker image simultaneously to the tests, it can take quite some time until the
54- # dashboard server is reachable and usable.
55- TIMEOUT_WAIT_SERVICE_INITIAL = 120
40+ from test_common import DashboardInterface , generate_dashboard_test_description # noqa: E402
5641
5742
5843@pytest .mark .launch_test
@@ -75,38 +60,7 @@ def tearDownClass(cls):
7560 rclpy .shutdown ()
7661
7762 def init_robot (self ):
78- # We wait longer for the first client, as the robot is still starting up
79- power_on_client = waitForService (
80- self .node , "/dashboard_client/power_on" , Trigger , timeout = TIMEOUT_WAIT_SERVICE_INITIAL
81- )
82-
83- # Connect to all other expected services
84- dashboard_interfaces = {
85- "power_off" : Trigger ,
86- "brake_release" : Trigger ,
87- "unlock_protective_stop" : Trigger ,
88- "restart_safety" : Trigger ,
89- "get_robot_mode" : GetRobotMode ,
90- "load_installation" : Load ,
91- "load_program" : Load ,
92- "close_popup" : Trigger ,
93- "get_loaded_program" : GetLoadedProgram ,
94- "program_state" : GetProgramState ,
95- "program_running" : IsProgramRunning ,
96- "play" : Trigger ,
97- "stop" : Trigger ,
98- }
99- self .dashboard_clients = {
100- srv_name : waitForService (self .node , f"/dashboard_client/{ srv_name } " , srv_type )
101- for (srv_name , srv_type ) in dashboard_interfaces .items ()
102- }
103-
104- # Add first client to dict
105- self .dashboard_clients ["power_on" ] = power_on_client
106-
107- #
108- # Test functions
109- #
63+ self ._dashboard_interface = DashboardInterface (self .node )
11064
11165 def test_switch_on (self ):
11266 """Test power on a robot."""
@@ -115,59 +69,34 @@ def test_switch_on(self):
11569 mode = RobotMode .DISCONNECTED
11670 while mode != RobotMode .POWER_OFF and time .time () < end_time :
11771 time .sleep (0.1 )
118- result = self .call_dashboard_service ( "get_robot_mode" , GetRobotMode . Request () )
72+ result = self ._dashboard_interface . get_robot_mode ( )
11973 self .assertTrue (result .success )
12074 mode = result .robot_mode .mode
12175
12276 # Power on robot
123- self .assertTrue (self .call_dashboard_service ( "power_on" , Trigger . Request () ).success )
77+ self .assertTrue (self ._dashboard_interface . power_on ( ).success )
12478
12579 # Wait until robot mode changes
12680 end_time = time .time () + 10
12781 mode = RobotMode .DISCONNECTED
12882 while mode not in (RobotMode .IDLE , RobotMode .RUNNING ) and time .time () < end_time :
12983 time .sleep (0.1 )
130- result = self .call_dashboard_service ( "get_robot_mode" , GetRobotMode . Request () )
84+ result = self ._dashboard_interface . get_robot_mode ( )
13185 self .assertTrue (result .success )
13286 mode = result .robot_mode .mode
13387
13488 self .assertIn (mode , (RobotMode .IDLE , RobotMode .RUNNING ))
13589
13690 # Release robot brakes
137- self .assertTrue (self .call_dashboard_service ( "brake_release" , Trigger . Request () ).success )
91+ self .assertTrue (self ._dashboard_interface . brake_release ( ).success )
13892
13993 # Wait until robot mode is RUNNING
14094 end_time = time .time () + 10
14195 mode = RobotMode .DISCONNECTED
14296 while mode != RobotMode .RUNNING and time .time () < end_time :
14397 time .sleep (0.1 )
144- result = self .call_dashboard_service ( "get_robot_mode" , GetRobotMode . Request () )
98+ result = self ._dashboard_interface . get_robot_mode ( )
14599 self .assertTrue (result .success )
146100 mode = result .robot_mode .mode
147101
148102 self .assertEqual (mode , RobotMode .RUNNING )
149-
150- #
151- # Utility functions
152- #
153-
154- def call_dashboard_service (self , srv_name , request ):
155- self .node .get_logger ().info (
156- f"Calling dashboard service '{ srv_name } ' with request { request } "
157- )
158- future = self .dashboard_clients [srv_name ].call_async (request )
159- rclpy .spin_until_future_complete (self .node , future )
160- if future .result () is not None :
161- self .node .get_logger ().info (f"Received result { future .result ()} " )
162- return future .result ()
163- else :
164- raise Exception (f"Exception while calling service: { future .exception ()} " )
165-
166-
167- def waitForService (node , srv_name , srv_type , timeout = TIMEOUT_WAIT_SERVICE ):
168- client = node .create_client (srv_type , srv_name )
169- if client .wait_for_service (timeout ) is False :
170- raise Exception (f"Could not reach service '{ srv_name } ' within timeout of { timeout } " )
171-
172- node .get_logger ().info (f"Successfully connected to service '{ srv_name } '" )
173- return client
0 commit comments