Skip to content

Commit

Permalink
Allow reaction to cancellation and preemption on wait recovery plugin (
Browse files Browse the repository at this point in the history
…#1636)

* Changed onCycleUpdate to allow preemption (#1622)

Signed-off-by: Aitor Miguel Blanco <aitormibl@gmail.com>

* Deleted unnecessary wait and corrected style (#1622)

Signed-off-by: Aitor Miguel Blanco <aitormibl@gmail.com>
  • Loading branch information
gimait authored Apr 15, 2020
1 parent e346948 commit d653088
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 11 additions & 5 deletions nav2_recoveries/plugins/wait.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace nav2_recoveries
Wait::Wait()
: Recovery<WaitAction>()
{
duration_.sec = 0.0;
}

Wait::~Wait()
Expand All @@ -33,15 +32,22 @@ Wait::~Wait()

Status Wait::onRun(const std::shared_ptr<const WaitAction::Goal> command)
{
duration_ = command->time;
wait_end_ = std::chrono::steady_clock::now() +
rclcpp::Duration(command->time).to_chrono<std::chrono::nanoseconds>();
return Status::SUCCEEDED;
}

Status Wait::onCycleUpdate()
{
rclcpp::sleep_for(
rclcpp::Duration(duration_).to_chrono<std::chrono::nanoseconds>());
return Status::SUCCEEDED;
auto current_point = std::chrono::steady_clock::now();
auto time_left =
std::chrono::duration_cast<std::chrono::nanoseconds>(wait_end_ - current_point).count();

if (time_left > 0) {
return Status::RUNNING;
} else {
return Status::SUCCEEDED;
}
}

} // namespace nav2_recoveries
Expand Down
2 changes: 1 addition & 1 deletion nav2_recoveries/plugins/wait.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Wait : public Recovery<WaitAction>
Status onCycleUpdate() override;

protected:
builtin_interfaces::msg::Duration duration_;
std::chrono::time_point<std::chrono::steady_clock> wait_end_;
};

} // namespace nav2_recoveries
Expand Down

0 comments on commit d653088

Please sign in to comment.