Skip to content

Commit

Permalink
[impl] don't compute stage if preempted
Browse files Browse the repository at this point in the history
Checked at the start of the StagePrivate::runCompute(). A callback must
be issued, otherwise the stage/s cannot be preempted.
  • Loading branch information
captain-yoshi committed Jul 17, 2024
1 parent ec69724 commit 000d3be
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
21 changes: 21 additions & 0 deletions core/include/moveit/task_constructor/stage_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@
namespace moveit {
namespace task_constructor {

/// exception thrown by StagePrivate::runCompute()
class PreemptStageException : public std::exception
{
public:
explicit PreemptStageException() {}
const char* what() const noexcept override {
static const char* msg = "";
return msg;
}
};

class ContainerBase;
class StagePrivate
{
Expand Down Expand Up @@ -146,6 +157,10 @@ class StagePrivate
bool storeFailures() const { return introspection_ != nullptr; }
void runCompute() {
ROS_DEBUG_STREAM_NAMED("Stage", fmt::format("Computing stage '{}'", name()));

if (preempted())
throw PreemptStageException();

auto compute_start_time = std::chrono::steady_clock::now();
try {
compute();
Expand All @@ -159,6 +174,10 @@ class StagePrivate
/** compute cost for solution through configured CostTerm */
void computeCost(const InterfaceState& from, const InterfaceState& to, SolutionBase& solution);

void setPreemptedCheck(const std::atomic<bool>* preempt_requested);
/// is the stage preempted ? defaults to false
bool preempted() const;

protected:
StagePrivate& operator=(StagePrivate&& other);

Expand Down Expand Up @@ -197,6 +216,8 @@ class StagePrivate
InterfaceWeakPtr next_starts_; // interface to be used for sendForward()

Introspection* introspection_; // task's introspection instance

std::atomic<bool> const* preempt_requested_;
};
PIMPL_FUNCTIONS(Stage)
std::ostream& operator<<(std::ostream& os, const StagePrivate& stage);
Expand Down
14 changes: 13 additions & 1 deletion core/src/stage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ StagePrivate::StagePrivate(Stage* me, const std::string& name)
, cost_term_{ std::make_unique<CostTerm>() }
, total_compute_time_{}
, parent_{ nullptr }
, introspection_{ nullptr } {}
, introspection_{ nullptr }
, preempt_requested_{ nullptr } {}

StagePrivate& StagePrivate::operator=(StagePrivate&& other) {
assert(typeid(*this) == typeid(other));
Expand Down Expand Up @@ -305,6 +306,17 @@ void StagePrivate::computeCost(const InterfaceState& from, const InterfaceState&
}
}

void StagePrivate::setPreemptedCheck(std::atomic<bool> const* preempt_requested) {
preempt_requested_ = preempt_requested;
}

bool StagePrivate::preempted() const {
if (preempt_requested_)
return *preempt_requested_;

return false;
}

Stage::Stage(StagePrivate* impl) : pimpl_(impl) {
assert(impl);
auto& p = properties();
Expand Down

0 comments on commit 000d3be

Please sign in to comment.