Skip to content

Commit 7dbe0b8

Browse files
authored
Return MoveItErrorCode from task::plan (#319)
... to know whether the plan failed due to timeout, preemption, or actual planning failure
1 parent f9c0a89 commit 7dbe0b8

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

core/include/moveit/task_constructor/task.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include <moveit/macros/class_forward.h>
4848

4949
#include <moveit_msgs/MoveItErrorCodes.h>
50+
#include <moveit/utils/moveit_error_code.h>
5051

5152
namespace moveit {
5253
namespace core {
@@ -117,11 +118,11 @@ class Task : protected WrapperBase
117118
void init();
118119

119120
/// reset, init scene (if not yet done), and init all stages, then start planning
120-
bool plan(size_t max_solutions = 0);
121+
moveit::core::MoveItErrorCode plan(size_t max_solutions = 0);
121122
/// interrupt current planning (or execution)
122123
void preempt();
123124
/// execute solution, return the result
124-
moveit_msgs::MoveItErrorCodes execute(const SolutionBase& s);
125+
moveit::core::MoveItErrorCode execute(const SolutionBase& s);
125126

126127
/// print current task state (number of found solutions and propagated states) to std::cout
127128
void printState(std::ostream& os = std::cout) const;

core/src/task.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -246,30 +246,37 @@ void Task::compute() {
246246
stages()->pimpl()->runCompute();
247247
}
248248

249-
bool Task::plan(size_t max_solutions) {
249+
moveit::core::MoveItErrorCode Task::plan(size_t max_solutions) {
250250
auto impl = pimpl();
251251
init();
252252

253+
// Print state and return success if there are solutions otherwise the input error_code
254+
const auto success_or = [this](const int32_t error_code) {
255+
printState();
256+
return numSolutions() > 0 ? moveit::core::MoveItErrorCode::SUCCESS : error_code;
257+
};
253258
impl->preempt_requested_ = false;
254259
const double available_time = timeout();
255260
const auto start_time = std::chrono::steady_clock::now();
256-
while (!impl->preempt_requested_ && canCompute() && (max_solutions == 0 || numSolutions() < max_solutions) &&
257-
std::chrono::duration<double>(std::chrono::steady_clock::now() - start_time).count() < available_time) {
261+
while (canCompute() && (max_solutions == 0 || numSolutions() < max_solutions)) {
262+
if (impl->preempt_requested_)
263+
return success_or(moveit::core::MoveItErrorCode::PREEMPTED);
264+
if (std::chrono::duration<double>(std::chrono::steady_clock::now() - start_time).count() > available_time)
265+
return success_or(moveit::core::MoveItErrorCode::TIMED_OUT);
258266
compute();
259267
for (const auto& cb : impl->task_cbs_)
260268
cb(*this);
261269
if (impl->introspection_)
262270
impl->introspection_->publishTaskState();
263-
}
264-
printState();
265-
return numSolutions() > 0;
271+
};
272+
return success_or(moveit::core::MoveItErrorCode::PLANNING_FAILED);
266273
}
267274

268275
void Task::preempt() {
269276
pimpl()->preempt_requested_ = true;
270277
}
271278

272-
moveit_msgs::MoveItErrorCodes Task::execute(const SolutionBase& s) {
279+
moveit::core::MoveItErrorCode Task::execute(const SolutionBase& s) {
273280
actionlib::SimpleActionClient<moveit_task_constructor_msgs::ExecuteTaskSolutionAction> ac("execute_task_solution");
274281
ac.waitForServer();
275282

core/test/test_container.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ TEST(Task, timeout) {
655655
// zero timeout fails
656656
t.reset();
657657
t.setTimeout(0.0);
658-
EXPECT_FALSE(t.plan());
658+
EXPECT_EQ(t.plan(), moveit::core::MoveItErrorCode::TIMED_OUT);
659659

660660
// time for 1 solution
661661
t.reset();

demo/src/pick_place_task.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ bool PickPlaceTask::plan() {
490490
ROS_INFO_NAMED(LOGNAME, "Start searching for task solutions");
491491
int max_solutions = pnh_.param<int>("max_solutions", 10);
492492

493-
return task_->plan(max_solutions);
493+
return static_cast<bool>(task_->plan(max_solutions));
494494
}
495495

496496
bool PickPlaceTask::execute() {

0 commit comments

Comments
 (0)