Skip to content

Commit

Permalink
wait for service with variable timeout (#1090)
Browse files Browse the repository at this point in the history
* wait for service with variable timeout

Signed-off-by: Karsten Knese <karsten@openrobotics.org>

Co-authored-by: chapulina <louise@openrobotics.org>
  • Loading branch information
Karsten1987 and chapulina authored Apr 28, 2020
1 parent db2881f commit e855eec
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions gazebo_ros/scripts/spawn_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit e855eec

Please sign in to comment.