From cee4332944d9250ecf61b8492b71aa65a40ff972 Mon Sep 17 00:00:00 2001 From: Dirk Thomas Date: Wed, 22 May 2019 15:35:15 -0700 Subject: [PATCH] use rate instead of thread::sleep to react to Ctrl-C (#348) Signed-off-by: Dirk Thomas --- lifecycle/src/lifecycle_service_client.cpp | 32 +++++++++++++++++----- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/lifecycle/src/lifecycle_service_client.cpp b/lifecycle/src/lifecycle_service_client.cpp index 554c62d50..275312730 100644 --- a/lifecycle/src/lifecycle_service_client.cpp +++ b/lifecycle/src/lifecycle_service_client.cpp @@ -201,7 +201,7 @@ class LifecycleServiceClient : public rclcpp::Node void callee_script(std::shared_ptr lc_client) { - auto sleep_time = 10s; + rclcpp::WallRate time_between_state_changes(0.1); // 10s // configure { @@ -215,7 +215,10 @@ callee_script(std::shared_ptr lc_client) // activate { - std::this_thread::sleep_for(sleep_time); + time_between_state_changes.sleep(); + if (!rclcpp::ok()) { + return; + } if (!lc_client->change_state(lifecycle_msgs::msg::Transition::TRANSITION_ACTIVATE)) { return; } @@ -226,7 +229,10 @@ callee_script(std::shared_ptr lc_client) // deactivate { - std::this_thread::sleep_for(sleep_time); + time_between_state_changes.sleep(); + if (!rclcpp::ok()) { + return; + } if (!lc_client->change_state(lifecycle_msgs::msg::Transition::TRANSITION_DEACTIVATE)) { return; } @@ -237,7 +243,10 @@ callee_script(std::shared_ptr lc_client) // activate it again { - std::this_thread::sleep_for(sleep_time); + time_between_state_changes.sleep(); + if (!rclcpp::ok()) { + return; + } if (!lc_client->change_state(lifecycle_msgs::msg::Transition::TRANSITION_ACTIVATE)) { return; } @@ -248,7 +257,10 @@ callee_script(std::shared_ptr lc_client) // and deactivate it again { - std::this_thread::sleep_for(sleep_time); + time_between_state_changes.sleep(); + if (!rclcpp::ok()) { + return; + } if (!lc_client->change_state(lifecycle_msgs::msg::Transition::TRANSITION_DEACTIVATE)) { return; } @@ -259,7 +271,10 @@ callee_script(std::shared_ptr lc_client) // we cleanup { - std::this_thread::sleep_for(sleep_time); + time_between_state_changes.sleep(); + if (!rclcpp::ok()) { + return; + } if (!lc_client->change_state(lifecycle_msgs::msg::Transition::TRANSITION_CLEANUP)) { return; } @@ -273,7 +288,10 @@ callee_script(std::shared_ptr lc_client) // We are currently in the unconfigured state and thus have to call // TRANSITION_UNCONFIGURED_SHUTDOWN { - std::this_thread::sleep_for(sleep_time); + time_between_state_changes.sleep(); + if (!rclcpp::ok()) { + return; + } if (!lc_client->change_state(lifecycle_msgs::msg::Transition::TRANSITION_UNCONFIGURED_SHUTDOWN)) { return;