Skip to content

Commit

Permalink
Merge pull request #21 from santiregui/add_timeout_srv
Browse files Browse the repository at this point in the history
Added TIMEOUT in service_state
  • Loading branch information
mgonzs13 authored Jun 8, 2024
2 parents 0a7bd7c + 1a516d0 commit 6dd149d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions yasmin_ros/yasmin_ros/basic_outcomes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
SUCCEED = "succeeded"
ABORT = "aborted"
CANCEL = "canceled"
TIMEOUT = "timeout"
18 changes: 14 additions & 4 deletions yasmin_ros/yasmin_ros/service_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from yasmin import State
from yasmin import Blackboard
from yasmin_ros.yasmin_node import YasminNode
from yasmin_ros.basic_outcomes import SUCCEED, ABORT
from yasmin_ros.basic_outcomes import SUCCEED, ABORT, TIMEOUT


class ServiceState(State):
Expand All @@ -31,6 +31,8 @@ class ServiceState(State):
_service_client: Client
_create_request_handler: Callable
_response_handler: Callable
_timeout: float
_srv_name: str

def __init__(
self,
Expand All @@ -39,10 +41,12 @@ def __init__(
create_request_handler: Callable,
outcomes: List[str] = None,
response_handler: Callable = None,
node: Node = None
node: Node = None,
timeout: float = None
) -> None:

_outcomes = [SUCCEED, ABORT]
self._srv_name = srv_name
_outcomes = [SUCCEED, ABORT, TIMEOUT]

if outcomes:
_outcomes = _outcomes + outcomes
Expand All @@ -59,13 +63,19 @@ def __init__(

if not self._create_request_handler:
raise Exception("create_request_handler is needed")

self._timeout = timeout

super().__init__(_outcomes)

def execute(self, blackboard: Blackboard) -> str:

request = self._create_request_handler(blackboard)
self._service_client.wait_for_service()
serv_available = self._service_client.wait_for_service(timeout_sec = self._timeout)

if not serv_available:
self._node.get_logger().error("Specified timeout achieved. Service {} is not available and thus returning TIMEOUT outcome".format(self._srv_name))
return TIMEOUT

try:
response = self._service_client.call(request)
Expand Down

0 comments on commit 6dd149d

Please sign in to comment.