Skip to content

Commit

Permalink
wait_for_service timeout added to C++
Browse files Browse the repository at this point in the history
  • Loading branch information
mgonzs13 committed Jun 8, 2024
1 parent 6dd149d commit 4607265
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
1 change: 1 addition & 0 deletions yasmin_ros/include/yasmin_ros/basic_outcomes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace basic_outcomes {
constexpr char SUCCEED[] = "succeeded";
constexpr char ABORT[] = "aborted";
constexpr char CANCEL[] = "canceled";
constexpr char TIMEOUT[] = "timeout";

} // namespace basic_outcomes
} // namespace yasmin_ros
Expand Down
29 changes: 21 additions & 8 deletions yasmin_ros/include/yasmin_ros/service_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,29 @@ template <typename ServiceT> class ServiceState : public yasmin::State {
public:
ServiceState(std::string srv_name,
CreateRequestHandler create_request_handler,
std::vector<std::string> outcomes)
: ServiceState(srv_name, create_request_handler, outcomes, nullptr) {}
std::vector<std::string> outcomes, int timeout = -1.0)
: ServiceState(srv_name, create_request_handler, outcomes, nullptr,
timeout) {}

ServiceState(std::string srv_name,
CreateRequestHandler create_request_handler,
std::vector<std::string> outcomes,
ResponseHandler response_handler)
ResponseHandler response_handler, int timeout = -1.0)
: ServiceState(nullptr, srv_name, create_request_handler, outcomes,
response_handler) {}
response_handler, timeout) {}

ServiceState(rclcpp::Node *node, std::string srv_name,
CreateRequestHandler create_request_handler,
std::vector<std::string> outcomes,
ResponseHandler response_handler)
: State({}) {
ResponseHandler response_handler, int timeout = -1.0)
: State({}), timeout(timeout) {

this->outcomes = {basic_outcomes::SUCCEED, basic_outcomes::ABORT};

if (this->timeout >= 0) {
this->outcomes.push_back(basic_outcomes::TIMEOUT);
}

if (outcomes.size() > 0) {
for (std::string outcome : outcomes) {
this->outcomes.push_back(outcome);
Expand All @@ -70,7 +75,8 @@ template <typename ServiceT> class ServiceState : public yasmin::State {
this->node = node;
}

this->service_client = this->node->template create_client<ServiceT>(srv_name);
this->service_client =
this->node->template create_client<ServiceT>(srv_name);

this->create_request_handler = create_request_handler;
this->response_handler = response_handler;
Expand All @@ -85,7 +91,13 @@ template <typename ServiceT> class ServiceState : public yasmin::State {

Request request = this->create_request(blackboard);

this->service_client->wait_for_service();
bool serv_available = this->service_client->wait_for_service(
std::chrono::duration<int64_t, std::ratio<1>>(this->timeout));

if (!serv_available) {
return basic_outcomes::TIMEOUT;
}

auto future = this->service_client->async_send_request(request);

future.wait();
Expand All @@ -110,6 +122,7 @@ template <typename ServiceT> class ServiceState : public yasmin::State {
std::shared_ptr<rclcpp::Client<ServiceT>> service_client;
CreateRequestHandler create_request_handler;
ResponseHandler response_handler;
int timeout;

Request
create_request(std::shared_ptr<yasmin::blackboard::Blackboard> blackboard) {
Expand Down
14 changes: 9 additions & 5 deletions yasmin_ros/yasmin_ros/service_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ def __init__(
) -> None:

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

self._timeout = timeout
if self._timeout:
_outcomes.append(TIMEOUT)

if outcomes:
_outcomes = _outcomes + outcomes
Expand All @@ -63,18 +67,18 @@ 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)
serv_available = self._service_client.wait_for_service(timeout_sec = self._timeout)
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))
self._node.get_logger().error(
"Specified timeout achieved. Service {} is not available and thus returning TIMEOUT outcome".format(self._srv_name))
return TIMEOUT

try:
Expand Down

0 comments on commit 4607265

Please sign in to comment.