Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce preempt stop time in stages #598

Merged
merged 5 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
rhaschke marked this conversation as resolved.
Show resolved Hide resolved
}
};

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();
rhaschke marked this conversation as resolved.
Show resolved Hide resolved

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);
rhaschke marked this conversation as resolved.
Show resolved Hide resolved
/// is the stage preempted ? defaults to false
bool preempted() const;
rhaschke marked this conversation as resolved.
Show resolved Hide resolved

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

const std::atomic<bool>* 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(const std::atomic<bool>* 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