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 58d1b6a commit eb597c8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
24 changes: 24 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 All @@ -69,6 +80,9 @@ class StagePrivate
StagePrivate(Stage* me, const std::string& name);
virtual ~StagePrivate() = default;

/// optional callback checking if this stage is preempted
using PreemptedCallback = std::function<bool()>;

/// actually configured interface of this stage (only valid after init())
InterfaceFlags interfaceFlags() const;

Expand Down Expand Up @@ -146,6 +160,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 +177,10 @@ class StagePrivate
/** compute cost for solution through configured CostTerm */
void computeCost(const InterfaceState& from, const InterfaceState& to, SolutionBase& solution);

void setPreemptedCallback(const PreemptedCallback& preempted_callback);
/// is the stage preempted ? defaults to false if callback is not set
bool preempted() const;

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

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

Introspection* introspection_; // task's introspection instance

PreemptedCallback preempted_callback_;
};
PIMPL_FUNCTIONS(Stage)
std::ostream& operator<<(std::ostream& os, const StagePrivate& stage);
Expand Down
11 changes: 11 additions & 0 deletions core/src/stage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,17 @@ void StagePrivate::computeCost(const InterfaceState& from, const InterfaceState&
}
}

void StagePrivate::setPreemptedCallback(const PreemptedCallback& preempted_callback) {
preempted_callback_ = preempted_callback;
}

bool StagePrivate::preempted() const {
if (preempted_callback_)
return preempted_callback_();

return false;
}

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

0 comments on commit eb597c8

Please sign in to comment.