Skip to content

Commit

Permalink
Cancel in-progress trajectories. (#400)
Browse files Browse the repository at this point in the history
* Cancel actionlib goal.

* Rename abort to cancel to match actionlib.

* Add warning when no trajectory is in progress.

* Update CHANGELOG.md.

* Address PR comments.
  • Loading branch information
brianhou authored Aug 4, 2018
1 parent e827fac commit de4ea8b
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 34 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* Rewrote executors for faster-than-realtime simulation: [#316](https://github.com/personalrobotics/aikido/pull/316), [#450](https://github.com/personalrobotics/aikido/pull/450)
* Introduced uniform and dart namespaces: [#342](https://github.com/personalrobotics/aikido/pull/342)
* Removed Barrett-specific hand executors: [#380](https://github.com/personalrobotics/aikido/pull/380)
* Supported canceling in-progress trajectories: [#400](https://github.com/personalrobotics/aikido/pull/400)

* Perception

Expand Down Expand Up @@ -75,6 +76,10 @@
* Renamed NonColliding to CollisionFree: [#256](https://github.com/personalrobotics/aikido/pull/256)
* Added TestableOutcome class: [#266](https://github.com/personalrobotics/aikido/pull/266)

* Control

* Added Instantaneous and Queued TrajectoryExecutors: [#259](https://github.com/personalrobotics/aikido/pull/259)

* Perception

* Added RcnnPoseModule: [#264](https://github.com/personalrobotics/aikido/pull/264)
Expand Down
6 changes: 3 additions & 3 deletions include/aikido/control/InstantaneousTrajectoryExecutor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ class InstantaneousTrajectoryExecutor : public TrajectoryExecutor
std::future<void> execute(
const trajectory::ConstTrajectoryPtr& traj) override;

// Do nothing.
/// Does nothing.
void step(
const std::chrono::system_clock::time_point& /*timepoint*/) override;

// Do nothing.
void abort() override;
/// Does nothing.
void cancel() override;

private:
/// Skeleton to execute trajectories on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class KinematicSimulationTrajectoryExecutor : public TrajectoryExecutor
/// calling this method.
void step(const std::chrono::system_clock::time_point& timepoint) override;

/// Aborts the current trajectory.
void abort() override;
/// Cancels the current trajectory.
void cancel() override;

private:
/// Skeleton to execute trajectories on
Expand Down
8 changes: 4 additions & 4 deletions include/aikido/control/QueuedTrajectoryExecutor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class QueuedTrajectoryExecutor : public TrajectoryExecutor

/// Execute trajectory and set future upon completion. If another trajectory
/// is already running, queue the trajectory for later execution. If executing
/// a trajectory terminates in an error, all queued trajectories are aborted.
/// a trajectory terminates in an error, all queued trajectories are canceled.
///
/// \param traj Trajectory to be executed or queued.
/// \return future<void> for trajectory execution. If trajectory terminates
Expand All @@ -37,10 +37,10 @@ class QueuedTrajectoryExecutor : public TrajectoryExecutor
// Documentation inherited.
void step(const std::chrono::system_clock::time_point& timepoint) override;

/// Abort the current trajectory, as well as all trajectories currently in the
/// queue. Does NOT stop the trajectory that is currently executing if the
/// Cancel the current trajectory, as well as all trajectories currently in
/// the queue. Does NOT stop the trajectory that is currently executing if the
/// underlying executor does not support it.
void abort() override;
void cancel() override;

private:
/// Underlying TrajectoryExecutor
Expand Down
5 changes: 2 additions & 3 deletions include/aikido/control/TrajectoryExecutor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ class TrajectoryExecutor
/// \param timepoint Time to simulate to
virtual void step(const std::chrono::system_clock::time_point& timepoint) = 0;

/// Abort the current trajectory.
/// \note This is currently only supported in simulation.
virtual void abort() = 0;
/// Cancel the current trajectory.
virtual void cancel() = 0;

protected:
/// Set of trajectories validated by executor
Expand Down
4 changes: 2 additions & 2 deletions include/aikido/control/ros/RosTrajectoryExecutor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class RosTrajectoryExecutor : public aikido::control::TrajectoryExecutor
/// Regularly checks for the completion of a sent trajectory.
void step(const std::chrono::system_clock::time_point& timepoint) override;

// Do nothing.
void abort() override;
/// \copydoc TrajectoryExecutor::cancel()
void cancel() override;

private:
using TrajectoryActionClient
Expand Down
2 changes: 1 addition & 1 deletion src/control/InstantaneousTrajectoryExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void InstantaneousTrajectoryExecutor::step(
}

//==============================================================================
void InstantaneousTrajectoryExecutor::abort()
void InstantaneousTrajectoryExecutor::cancel()
{
// Do nothing
}
Expand Down
12 changes: 9 additions & 3 deletions src/control/KinematicSimulationTrajectoryExecutor.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "aikido/control/KinematicSimulationTrajectoryExecutor.hpp"
#include <dart/common/Console.hpp>
#include <dart/common/StlHelpers.hpp>
#include "aikido/control/TrajectoryRunningException.hpp"

Expand Down Expand Up @@ -32,7 +33,7 @@ KinematicSimulationTrajectoryExecutor::~KinematicSimulationTrajectoryExecutor()
{
mInProgress = false;
mPromise->set_exception(
std::make_exception_ptr(std::runtime_error("Trajectory aborted.")));
std::make_exception_ptr(std::runtime_error("Trajectory canceled.")));
}
}
}
Expand Down Expand Up @@ -147,7 +148,7 @@ void KinematicSimulationTrajectoryExecutor::step(
}

//==============================================================================
void KinematicSimulationTrajectoryExecutor::abort()
void KinematicSimulationTrajectoryExecutor::cancel()
{
std::lock_guard<std::mutex> lock(mMutex);

Expand All @@ -158,7 +159,12 @@ void KinematicSimulationTrajectoryExecutor::abort()
mMetaSkeleton.reset();
mInProgress = false;
mPromise->set_exception(
std::make_exception_ptr(std::runtime_error("Trajectory aborted.")));
std::make_exception_ptr(std::runtime_error("Trajectory canceled.")));
}
else
{
dtwarn << "[KinematicSimulationTrajectoryExecutor::cancel] Attempting to "
<< "cancel trajectory, but no trajectory in progress.\n";
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/control/QueuedTrajectoryExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void QueuedTrajectoryExecutor::step(
catch (const std::exception& e)
{
promise->set_exception(std::current_exception());
abort();
cancel();
}
}

Expand All @@ -90,22 +90,22 @@ void QueuedTrajectoryExecutor::step(
}

//==============================================================================
void QueuedTrajectoryExecutor::abort()
void QueuedTrajectoryExecutor::cancel()
{
std::lock_guard<std::mutex> lock(mMutex);
DART_UNUSED(lock); // Suppress unused variable warning

std::exception_ptr abort
= std::make_exception_ptr(std::runtime_error("Trajectory aborted."));
std::exception_ptr cancel
= std::make_exception_ptr(std::runtime_error("Trajectory canceled."));

if (mInProgress)
{
mExecutor->abort();
mExecutor->cancel();

// Set our own exception, since abort may not be supported
// Set our own exception, since cancel may not be supported
auto promise = mPromiseQueue.front();
mPromiseQueue.pop();
promise->set_exception(abort);
promise->set_exception(cancel);

mInProgress = false;
}
Expand All @@ -115,7 +115,7 @@ void QueuedTrajectoryExecutor::abort()
{
auto promise = mPromiseQueue.front();
mPromiseQueue.pop();
promise->set_exception(abort);
promise->set_exception(cancel);

mTrajectoryQueue.pop();
}
Expand Down
16 changes: 14 additions & 2 deletions src/control/ros/RosTrajectoryExecutor.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "aikido/control/ros/RosTrajectoryExecutor.hpp"
#include <dart/common/Console.hpp>
#include "aikido/control/TrajectoryRunningException.hpp"
#include "aikido/control/ros/Conversions.hpp"
#include "aikido/control/ros/RosTrajectoryExecutionException.hpp"
Expand Down Expand Up @@ -232,9 +233,20 @@ void RosTrajectoryExecutor::step(
}

//==============================================================================
void RosTrajectoryExecutor::abort()
void RosTrajectoryExecutor::cancel()
{
// TODO: cancel the actionlib goal (once there is support in ReWD controller)
std::lock_guard<std::mutex> lock(mMutex);
DART_UNUSED(lock); // Suppress unused variable warning.

if (mInProgress)
{
mGoalHandle.cancel();
}
else
{
dtwarn << "[RosTrajectoryExecutor::cancel] Attempting to "
<< "cancel trajectory, but no trajectory in progress.\n";
}
}

} // namespace ros
Expand Down
5 changes: 3 additions & 2 deletions tests/control/test_KinematicSimulationTrajectoryExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,16 @@ TEST_F(
}

TEST_F(
KinematicSimulationTrajectoryExecutorTest, abort_TrajectoryInProgress_Halts)
KinematicSimulationTrajectoryExecutorTest,
cancel_TrajectoryInProgress_Halts)
{
KinematicSimulationTrajectoryExecutor executor(mSkeleton);

auto simulationClock = std::chrono::system_clock::now();
auto future = executor.execute(mTraj);
future.wait_for(waitTime);
executor.step(simulationClock + stepTime);
executor.abort();
executor.cancel();

EXPECT_THROW(future.get(), std::runtime_error);

Expand Down
8 changes: 4 additions & 4 deletions tests/control/test_QueuedTrajectoryExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ TEST_F(QueuedTrajectoryExecutorTest, step_NegativeTimepoint_NoThrows)

TEST_F(
QueuedTrajectoryExecutorTest,
abort_NoRunningTrajectories_QueuedTrajectoriesAborted)
cancel_NoRunningTrajectories_QueuedTrajectoriesCanceled)
{
QueuedTrajectoryExecutor executor(std::move(mExecutor));

Expand All @@ -230,7 +230,7 @@ TEST_F(
auto f1 = executor.execute(mTraj1);
auto f2 = executor.execute(mTraj2);

executor.abort();
executor.cancel();

EXPECT_EQ(f1.wait_for(waitTime), std::future_status::ready);
EXPECT_EQ(f2.wait_for(waitTime), std::future_status::ready);
Expand All @@ -243,7 +243,7 @@ TEST_F(

TEST_F(
QueuedTrajectoryExecutorTest,
abort_OneRunningTrajectory_QueuedTrajectoriesAborted)
cancel_OneRunningTrajectory_QueuedTrajectoriesCanceled)
{
QueuedTrajectoryExecutor executor(std::move(mExecutor));

Expand All @@ -260,7 +260,7 @@ TEST_F(
simulationClock += stepTime;
executor.step(simulationClock);

executor.abort();
executor.cancel();

EXPECT_EQ(f1.wait_for(waitTime), std::future_status::ready);
EXPECT_EQ(f2.wait_for(waitTime), std::future_status::ready);
Expand Down

0 comments on commit de4ea8b

Please sign in to comment.