From cfc42c16b35cbb98f2185a1c8ce1cd3548ddd951 Mon Sep 17 00:00:00 2001 From: Karsten Knese Date: Tue, 28 Apr 2020 12:19:21 -0700 Subject: [PATCH 1/2] wait for service with variable timeout Signed-off-by: Karsten Knese --- gazebo_ros/scripts/spawn_entity.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/gazebo_ros/scripts/spawn_entity.py b/gazebo_ros/scripts/spawn_entity.py index 7d7062857..db6603ceb 100755 --- a/gazebo_ros/scripts/spawn_entity.py +++ b/gazebo_ros/scripts/spawn_entity.py @@ -76,6 +76,8 @@ def __init__(self, args): help='unpause physics after spawning entity') parser.add_argument('-wait', type=str, metavar='ENTITY_NAME', help='Wait for entity to exist') + parser.add_argument('-spawn_service_timeout', type=float, metavar='TIMEOUT', + default=5.0, help='Spawn service wait timeout') parser.add_argument('-x', type=float, default=0, help='x component of initial position, meters') parser.add_argument('-y', type=float, default=0, @@ -215,7 +217,7 @@ def entity_xml_cb(msg): initial_pose.orientation.y = q[2] initial_pose.orientation.z = q[3] - success = self._spawn_entity(entity_xml, initial_pose) + success = self._spawn_entity(entity_xml, initial_pose, self.args.spawn_service_timeout) if not success: self.get_logger().error('Spawn service failed. Exiting.') return 1 @@ -254,10 +256,16 @@ def entity_xml_cb(msg): return 0 - def _spawn_entity(self, entity_xml, initial_pose): + def _spawn_entity(self, entity_xml, initial_pose, timeout=5.0): + if timeout < 0: + self.get_logger().error('spawn_entity timeout must be greater than zero') + return False + self.get_logger().info( + 'Waiting for service %s/spawn_entity, timeout = %.f' % ( + self.args.gazebo_namespace, timeout)) self.get_logger().info('Waiting for service %s/spawn_entity' % self.args.gazebo_namespace) client = self.create_client(SpawnEntity, '%s/spawn_entity' % self.args.gazebo_namespace) - if client.wait_for_service(timeout_sec=5.0): + if client.wait_for_service(timeout_sec=timeout): req = SpawnEntity.Request() req.name = self.args.entity req.xml = str(entity_xml, 'utf-8') From 99c964edc8382be875329254477c9d108d0df3a1 Mon Sep 17 00:00:00 2001 From: Karsten Knese Date: Tue, 28 Apr 2020 13:32:35 -0700 Subject: [PATCH 2/2] Update gazebo_ros/scripts/spawn_entity.py Co-Authored-By: chapulina --- gazebo_ros/scripts/spawn_entity.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gazebo_ros/scripts/spawn_entity.py b/gazebo_ros/scripts/spawn_entity.py index db6603ceb..fb9aea3dc 100755 --- a/gazebo_ros/scripts/spawn_entity.py +++ b/gazebo_ros/scripts/spawn_entity.py @@ -77,7 +77,7 @@ def __init__(self, args): parser.add_argument('-wait', type=str, metavar='ENTITY_NAME', help='Wait for entity to exist') parser.add_argument('-spawn_service_timeout', type=float, metavar='TIMEOUT', - default=5.0, help='Spawn service wait timeout') + default=5.0, help='Spawn service wait timeout in seconds') parser.add_argument('-x', type=float, default=0, help='x component of initial position, meters') parser.add_argument('-y', type=float, default=0,