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

ConcreteRobot uses ranker for PlantoTSR #503

Merged
merged 14 commits into from
Feb 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
* Distance

* Added methods to rank configurations based on specified metric: [#423](https://github.com/personalrobotics/aikido/pull/423)
* Added weights as optinal paramter to rankers: [#484] (https://github.com/personalrobotics/aikido/pull/484)

* Added weights as optional parameter to rankers: [#484](https://github.com/personalrobotics/aikido/pull/484)

* State Space

Expand Down Expand Up @@ -63,6 +62,7 @@
* Fixed segmentation fault on 32-bit machines in vector-field planner: [#459](https://github.com/personalrobotics/aikido/pull/459)
* Updated interface to OMPL planners to follow the style of the new refactored planning API: [#466](https://github.com/personalrobotics/aikido/pull/466)
* Added convenience function for converting SO(2) trajectories to R1 trajectories, support for postprocessing SO(2) trajectories: [#496](https://github.com/personalrobotics/aikido/pull/496)
* Used ConfigurationRanker in TSR planners: [#503](https://github.com/personalrobotics/aikido/pull/503)

* Robot

Expand Down
2 changes: 1 addition & 1 deletion include/aikido/distance/ConfigurationRanker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ConfigurationRanker
/// \param[in, out] configurations Vector of configurations to rank.
void rankConfigurations(
std::vector<statespace::dart::MetaSkeletonStateSpace::ScopedState>&
configurations);
configurations) const;

protected:
/// Returns the cost of the configuration.
Expand Down
2 changes: 2 additions & 0 deletions include/aikido/distance/NominalConfigurationRanker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
namespace aikido {
namespace distance {

AIKIDO_DECLARE_POINTERS(NominalConfigurationRanker)

/// Ranks configurations by their distance from a nominal configuration.
/// Configurations closer to the nominal configuration are ranked higher.
class NominalConfigurationRanker : public ConfigurationRanker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ class ConfigurationToConfiguration_to_ConfigurationToTSR
/// convert.
/// \param[in] metaSkeleton MetaSkeleton for adapted planner to operate on.
/// \param[in] configurationRanker Ranker to rank configurations.
/// \param[in] ranker Ranker to rank the sampled configurations. If nullptr,
/// NominalConfigurationRanker is used with the current metaSkeleton pose.
ConfigurationToConfiguration_to_ConfigurationToTSR(
std::shared_ptr<aikido::planner::ConfigurationToConfigurationPlanner>
planner,
::dart::dynamics::MetaSkeletonPtr metaSkeleton,
distance::ConfigurationRankerPtr configurationRanker = nullptr);
distance::ConstConfigurationRankerPtr configurationRanker = nullptr);

// Documentation inherited.
virtual trajectory::TrajectoryPtr plan(
Expand Down
4 changes: 2 additions & 2 deletions include/aikido/planner/dart/ConfigurationToTSRPlanner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ConfigurationToTSRPlanner
ConfigurationToTSRPlanner(
statespace::dart::ConstMetaSkeletonStateSpacePtr stateSpace,
::dart::dynamics::MetaSkeletonPtr metaSkeleton,
distance::ConfigurationRankerPtr configurationRanker = nullptr);
distance::ConstConfigurationRankerPtr configurationRanker = nullptr);

/// Solves \c problem returning the result to \c result.
///
Expand All @@ -43,7 +43,7 @@ class ConfigurationToTSRPlanner
// Note: SolvableProblem is defined in SingleProblemPlanner.

protected:
distance::ConfigurationRankerPtr mConfigurationRanker;
distance::ConstConfigurationRankerPtr mConfigurationRanker;
};

} // namespace dart
Expand Down
6 changes: 5 additions & 1 deletion include/aikido/robot/ConcreteRobot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "aikido/constraint/dart/CollisionFree.hpp"
#include "aikido/constraint/dart/TSR.hpp"
#include "aikido/control/TrajectoryExecutor.hpp"
#include "aikido/distance/ConfigurationRanker.hpp"
#include "aikido/planner/parabolic/ParabolicSmoother.hpp"
#include "aikido/planner/parabolic/ParabolicTimer.hpp"
#include "aikido/robot/Robot.hpp"
Expand Down Expand Up @@ -191,6 +192,8 @@ class ConcreteRobot : public Robot
/// \param[in] collisionFree Testable constraint to check for collision.
/// \param[in] timelimit Max time (seconds) to spend per planning to each IK
/// \param[in] maxNumTrials Max numer of trials to plan.
/// \param[in] ranker Ranker to rank the sampled configurations. If nullptr,
/// NominalConfigurationRanker is used with the current metaSkeleton pose.
/// \return Trajectory to a sample in TSR, or nullptr if planning fails.
aikido::trajectory::TrajectoryPtr planToTSR(
const aikido::statespace::dart::MetaSkeletonStateSpacePtr& stateSpace,
Expand All @@ -199,7 +202,8 @@ class ConcreteRobot : public Robot
const aikido::constraint::dart::TSRPtr& tsr,
const aikido::constraint::dart::CollisionFreePtr& collisionFree,
double timelimit,
std::size_t maxNumTrials);
std::size_t maxNumTrials,
const distance::ConstConfigurationRankerPtr& ranker = nullptr);

/// TODO: Replace this with Problem interface.
/// Returns a Trajectory that moves the configuration of the metakeleton such
Expand Down
6 changes: 5 additions & 1 deletion include/aikido/robot/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "aikido/constraint/dart/CollisionFree.hpp"
#include "aikido/constraint/dart/TSR.hpp"
#include "aikido/control/TrajectoryExecutor.hpp"
#include "aikido/distance/ConfigurationRanker.hpp"
#include "aikido/io/yaml.hpp"
#include "aikido/statespace/dart/MetaSkeletonStateSpace.hpp"
#include "aikido/trajectory/Interpolated.hpp"
Expand Down Expand Up @@ -133,6 +134,8 @@ trajectory::TrajectoryPtr planToConfigurations(
/// \param[in] rng Random number generator
/// \param[in] timelimit Max time (seconds) to spend per planning to each IK
/// \param[in] maxNumTrials Number of retries before failure.
/// \param[in] ranker Ranker to rank the sampled configurations. If nullptr,
/// NominalConfigurationRanker is used with the current metaSkeleton pose.
/// \return Trajectory to a sample in TSR, or nullptr if planning fails.
trajectory::TrajectoryPtr planToTSR(
const statespace::dart::MetaSkeletonStateSpacePtr& space,
Expand All @@ -142,7 +145,8 @@ trajectory::TrajectoryPtr planToTSR(
const constraint::TestablePtr& collisionTestable,
common::RNG* rng,
double timelimit,
std::size_t maxNumTrials);
std::size_t maxNumTrials,
const distance::ConstConfigurationRankerPtr& ranker = nullptr);

/// Returns a Trajectory that moves the configuration of the metakeleton such
/// that the specified bodynode is set to a sample in a goal TSR and
Expand Down
2 changes: 1 addition & 1 deletion src/distance/ConfigurationRanker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ ConfigurationRanker::ConfigurationRanker(

//==============================================================================
void ConfigurationRanker::rankConfigurations(
std::vector<MetaSkeletonStateSpace::ScopedState>& configurations)
std::vector<MetaSkeletonStateSpace::ScopedState>& configurations) const
{
std::unordered_map<const MetaSkeletonStateSpace::State*, double> costs;
costs.reserve(configurations.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
#include "aikido/common/RNG.hpp"
#include "aikido/constraint/dart/InverseKinematicsSampleable.hpp"
#include "aikido/constraint/dart/JointStateSpaceHelpers.hpp"
#include "aikido/distance/NominalConfigurationRanker.hpp"
#include "aikido/statespace/dart/MetaSkeletonStateSaver.hpp"
#include "aikido/statespace/dart/MetaSkeletonStateSpace.hpp"

using aikido::common::cloneRNGFrom;
using aikido::constraint::dart::InverseKinematicsSampleable;
using aikido::constraint::dart::TSR;
using aikido::constraint::dart::createSampleableBounds;
using aikido::distance::ConstConfigurationRankerPtr;
using aikido::distance::NominalConfigurationRanker;
using aikido::statespace::dart::MetaSkeletonStateSaver;
using aikido::statespace::dart::MetaSkeletonStateSpace;
using ::dart::dynamics::BodyNode;
Expand All @@ -27,7 +30,7 @@ ConfigurationToConfiguration_to_ConfigurationToTSR::
ConfigurationToConfiguration_to_ConfigurationToTSR(
std::shared_ptr<planner::ConfigurationToConfigurationPlanner> planner,
::dart::dynamics::MetaSkeletonPtr metaSkeleton,
distance::ConfigurationRankerPtr configurationRanker)
distance::ConstConfigurationRankerPtr configurationRanker)
: PlannerAdapter<planner::ConfigurationToConfigurationPlanner,
ConfigurationToTSRPlanner>(
std::move(planner), std::move(metaSkeleton))
Expand Down Expand Up @@ -84,24 +87,59 @@ ConfigurationToConfiguration_to_ConfigurationToTSR::plan(
auto saver = MetaSkeletonStateSaver(mMetaSkeleton);
DART_UNUSED(saver);

auto robot = mMetaSkeleton->getBodyNode(0)->getSkeleton();

std::vector<MetaSkeletonStateSpace::ScopedState> configurations;

// Use a ranker
ConstConfigurationRankerPtr configurationRanker(mConfigurationRanker);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to create another pointer here? Can we not do:

if (!mConfigurationRanker)
{
    mConfigurationRanker = NominalConfigurationRanker;
}

In robot/utils/planToTSR, the ranker has been taken with a const pointer so I guess that's fine there. Alternatively, we can leave this like this and change the data member to const since we are initializing it in the constructor anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summarizing my slack discussion with @aditya-vk , I think we should keep it as is, for the following reasons:

  1. if the planner is created with mConfigurationRanker = nullptr, the expectation is that a NominalConfigurationRanker would be created when this method is called and be initialized with the startState. So if we change the member variable as you suggested, the planner now has been mutated and it cannot be reused in the same way. Next time it is called, it'd use this ranker.
  2. To fix, the ideal solution would be the second way you suggested, to have it as const mConfigurationRanker so that it never gets changed. Unfortunately, to my best understanding, because this class becomes a TSRPlanner via the templated adapter, it does not have access to mConfigurationRanker during the initialization, i.e. I can't do : mConfigurationRanker(std::move(ranker)) { ... } . It only has access to the constructor parameters of the PlannerAdapter, which is the stateSpace and metaSkeleton. See stackoverflow .

Hence, in order to keep the member variable as-is (as if it's a const) and initialize it in constructor if it's passed in, I think we need to keep it the way I have it now.

@aditya-vk @brianhou please confirm!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right, there's no way to have it in the initialization list. Hmm.
I think what we have now is good then. Since we don't give an option to change the ranker at all unless a new planner is created, I think it should be fine 👍

if (!configurationRanker)
{
auto nominalState = mMetaSkeletonStateSpace->createState();
mMetaSkeletonStateSpace->copyState(startState, nominalState);
configurationRanker = std::make_shared<const NominalConfigurationRanker>(
mMetaSkeletonStateSpace,
mMetaSkeleton,
std::vector<double>(),
nominalState);
}

// Goal state
auto goalState = mMetaSkeletonStateSpace->createState();
while (generator->canSample())

// Sample valid configurations first.
static const std::size_t maxSamples{100};
std::size_t samples = 0;
while (samples < maxSamples && generator->canSample())
{
// Sample from TSR
{
std::lock_guard<std::mutex> lock(skeleton->getMutex());
bool sampled = generator->sample(goalState);
if (!sampled)
continue;
}
std::lock_guard<std::mutex> lock(robot->getMutex());
bool sampled = generator->sample(goalState);

// Increment even if it's not a valid sample since this loop
// has to terminate even if none are valid.
++samples;

if (!sampled)
continue;

configurations.emplace_back(goalState.clone());
}

if (configurations.empty())
return nullptr;

configurationRanker->rankConfigurations(configurations);

for (std::size_t i = 0; i < configurations.size(); ++i)
{
// Create ConfigurationToConfiguration Problem.
// NOTE: This is done here because the ConfigurationToConfiguration
// problem stores a *cloned* scoped state of the passed state.
auto delegateProblem = ConfigurationToConfiguration(
mMetaSkeletonStateSpace,
startState,
goalState,
configurations[i],
problem.getConstraint());

auto traj = mDelegate->plan(delegateProblem, result);
Expand Down
2 changes: 1 addition & 1 deletion src/planner/dart/ConfigurationToTSRPlanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace dart {
ConfigurationToTSRPlanner::ConfigurationToTSRPlanner(
statespace::dart::ConstMetaSkeletonStateSpacePtr stateSpace,
::dart::dynamics::MetaSkeletonPtr metaSkeleton,
distance::ConfigurationRankerPtr configurationRanker)
distance::ConstConfigurationRankerPtr configurationRanker)
: dart::SingleProblemPlanner<ConfigurationToTSRPlanner, ConfigurationToTSR>(
std::move(stateSpace), std::move(metaSkeleton))
, mConfigurationRanker(std::move(configurationRanker))
Expand Down
6 changes: 4 additions & 2 deletions src/robot/ConcreteRobot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,8 @@ TrajectoryPtr ConcreteRobot::planToTSR(
const TSRPtr& tsr,
const CollisionFreePtr& collisionFree,
double timelimit,
std::size_t maxNumTrials)
std::size_t maxNumTrials,
const distance::ConstConfigurationRankerPtr& ranker)
{
auto collisionConstraint
= getFullCollisionConstraint(stateSpace, metaSkeleton, collisionFree);
Expand All @@ -414,7 +415,8 @@ TrajectoryPtr ConcreteRobot::planToTSR(
collisionConstraint,
cloneRNG().get(),
timelimit,
maxNumTrials);
maxNumTrials,
ranker);
}

//==============================================================================
Expand Down
Loading