Skip to content

Commit

Permalink
use rate instead of thread::sleep to react to Ctrl-C (#348)
Browse files Browse the repository at this point in the history
Signed-off-by: Dirk Thomas <dirk-thomas@users.noreply.github.com>
  • Loading branch information
dirk-thomas authored May 22, 2019
1 parent 45c8fa9 commit cee4332
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions lifecycle/src/lifecycle_service_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class LifecycleServiceClient : public rclcpp::Node
void
callee_script(std::shared_ptr<LifecycleServiceClient> lc_client)
{
auto sleep_time = 10s;
rclcpp::WallRate time_between_state_changes(0.1); // 10s

// configure
{
Expand All @@ -215,7 +215,10 @@ callee_script(std::shared_ptr<LifecycleServiceClient> 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;
}
Expand All @@ -226,7 +229,10 @@ callee_script(std::shared_ptr<LifecycleServiceClient> 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;
}
Expand All @@ -237,7 +243,10 @@ callee_script(std::shared_ptr<LifecycleServiceClient> 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;
}
Expand All @@ -248,7 +257,10 @@ callee_script(std::shared_ptr<LifecycleServiceClient> 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;
}
Expand All @@ -259,7 +271,10 @@ callee_script(std::shared_ptr<LifecycleServiceClient> 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;
}
Expand All @@ -273,7 +288,10 @@ callee_script(std::shared_ptr<LifecycleServiceClient> 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;
Expand Down

0 comments on commit cee4332

Please sign in to comment.