Skip to content

Commit

Permalink
Port #3464 from MoveIt1 (moveit#2456)
Browse files Browse the repository at this point in the history
* Port unit test

cherry-pick of moveit/moveit#3464

* Increment added_path_index in callAdapter

Doesn't work because previous=0 for all recursively called functions.

* Pass individual add_path_index vectors to callAdapter

---------

Co-authored-by: Hugal31 <hla@lescompanions.com>
  • Loading branch information
2 people authored and m-elwin committed Dec 4, 2023
1 parent fee2607 commit 55aab07
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 3 deletions.
8 changes: 8 additions & 0 deletions moveit_core/planning_request_adapter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ target_link_libraries(moveit_planning_request_adapter
)

install(DIRECTORY include/ DESTINATION include/moveit_core)

if(BUILD_TESTING)
ament_add_gmock(test_planning_request_adapter_chain test/test_planning_request_adapter_chain.cpp)
target_link_libraries(test_planning_request_adapter_chain
moveit_test_utils
moveit_planning_interface
moveit_planning_request_adapter)
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,14 @@ bool callPlannerInterfaceSolve(const planning_interface::PlannerManager& planner

bool callAdapter(const PlanningRequestAdapter& adapter, const PlanningRequestAdapter::PlannerFn& planner,
const planning_scene::PlanningSceneConstPtr& planning_scene,
const planning_interface::MotionPlanRequest& req, planning_interface::MotionPlanResponse& res)
const planning_interface::MotionPlanRequest& req, planning_interface::MotionPlanResponse& res,
std::vector<std::size_t>& added_path_index)
{
try
{
std::swap(res.added_path_index, added_path_index);
bool result = adapter.adaptAndPlan(planner, planning_scene, req, res);
std::swap(res.added_path_index, added_path_index);
RCLCPP_DEBUG_STREAM(LOGGER, adapter.getDescription() << ": " << moveit::core::error_code_to_string(res.error_code));
return result;
}
Expand Down Expand Up @@ -105,6 +108,8 @@ bool PlanningRequestAdapterChain::adaptAndPlan(const planning_interface::Planner
const planning_interface::MotionPlanRequest& req,
planning_interface::MotionPlanResponse& res) const
{
res.added_path_index.clear();

// if there are no adapters, run the planner directly
if (adapters_.empty())
{
Expand All @@ -120,15 +125,33 @@ bool PlanningRequestAdapterChain::adaptAndPlan(const planning_interface::Planner
return callPlannerInterfaceSolve(planner, scene, req, res);
};

std::vector<std::vector<std::size_t>> added_path_index_each(adapters_.size());
for (int i = adapters_.size() - 1; i >= 0; --i)
{
fn = [&adapter = *adapters_[i],
fn = [&adapter = *adapters_[i], &added_path_index = added_path_index_each[i],
fn](const planning_scene::PlanningSceneConstPtr& scene, const planning_interface::MotionPlanRequest& req,
planning_interface::MotionPlanResponse& res) { return callAdapter(adapter, fn, scene, req, res); };
planning_interface::MotionPlanResponse& res) {
return callAdapter(adapter, fn, scene, req, res, added_path_index);
};
}

bool result = fn(planning_scene, req, res);

// merge the index values from each adapter (in reverse order)
assert(res.added_path_index.empty());
for (auto added_state_by_each_adapter_it = added_path_index_each.rbegin();
added_state_by_each_adapter_it != added_path_index_each.rend(); ++added_state_by_each_adapter_it)
{
for (std::size_t added_index : *added_state_by_each_adapter_it)
{
for (std::size_t& index_in_path : res.added_path_index)
{
if (added_index <= index_in_path)
index_in_path++;
}
res.added_path_index.push_back(added_index);
}
}
std::sort(res.added_path_index.begin(), res.added_path_index.end());
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
/*********************************************************************
* Software License Agreement (BSD License)
*
* Copyright (c) 2023, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Willow Garage nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/

/* Author: Hugo Laloge */

#include <gmock/gmock.h>
#include <moveit/utils/robot_model_test_utils.h>
#include <moveit/planning_request_adapter/planning_request_adapter.h>

class AppendingPlanningRequestAdapter final : public planning_request_adapter::PlanningRequestAdapter
{
public:
void initialize(const rclcpp::Node::SharedPtr& /*node*/, const std::string& /*parameter_namespace*/) override
{
}
bool adaptAndPlan(const PlannerFn& planner, const planning_scene::PlanningSceneConstPtr& planning_scene,
const planning_interface::MotionPlanRequest& req,
planning_interface::MotionPlanResponse& res) const override
{
bool success = planner(planning_scene, req, res);
if (success)
{
res.trajectory->addSuffixWayPoint(res.trajectory->getLastWayPoint(), 1);
res.added_path_index.push_back(res.trajectory->getWayPointCount() - 1);
}
return success;
}
std::string getDescription() const override
{
return "AppendingPlanningRequestAdapter";
}
};

class PrependingPlanningRequestAdapter final : public planning_request_adapter::PlanningRequestAdapter
{
public:
void initialize(const rclcpp::Node::SharedPtr& /*node*/, const std::string& /*parameter_namespace*/) override
{
}
bool adaptAndPlan(const PlannerFn& planner, const planning_scene::PlanningSceneConstPtr& planning_scene,
const planning_interface::MotionPlanRequest& req,
planning_interface::MotionPlanResponse& res) const override
{
bool success = planner(planning_scene, req, res);
if (success)
{
res.trajectory->addPrefixWayPoint(res.trajectory->getFirstWayPoint(), 0);
res.added_path_index.push_back(0);
}
return success;
}
std::string getDescription() const override
{
return "PrependingPlanningRequestAdapter";
}
};

class PlannerManagerStub : public planning_interface::PlannerManager
{
public:
PlannerManagerStub(planning_interface::PlanningContextPtr planningContext)
: m_planningContext_{ std::move(planningContext) }
{
}
planning_interface::PlanningContextPtr
getPlanningContext(const planning_scene::PlanningSceneConstPtr& /*planning_scene*/,
const planning_interface::MotionPlanRequest& /*req*/,
moveit_msgs::msg::MoveItErrorCodes& /*error*/) const override
{
return m_planningContext_;
}

bool canServiceRequest(const planning_interface::MotionPlanRequest& /*req*/) const override
{
return true;
}

private:
planning_interface::PlanningContextPtr m_planningContext_;
};

class PlanningContextStub : public planning_interface::PlanningContext
{
public:
using planning_interface::PlanningContext::PlanningContext;

void setTrajectory(robot_trajectory::RobotTrajectoryPtr trajectory)
{
m_trajectory_ = std::move(trajectory);
}

bool solve(planning_interface::MotionPlanResponse& res) override
{
if (!m_trajectory_)
return false;

res.trajectory = m_trajectory_;
return true;
}

bool solve(planning_interface::MotionPlanDetailedResponse& res) override
{
if (!m_trajectory_)
return false;

res.trajectory.push_back(m_trajectory_);
return true;
}

bool terminate() override
{
return true;
}

void clear() override
{
}

private:
robot_trajectory::RobotTrajectoryPtr m_trajectory_;
};

class PlanningRequestAdapterTests : public testing::Test
{
protected:
void SetUp() override
{
robot_model_ = moveit::core::loadTestingRobotModel("panda");
group_name_ = "panda_arm";
planning_scene_ = std::make_shared<planning_scene::PlanningScene>(robot_model_);
planning_context_ = std::make_shared<PlanningContextStub>("stub", group_name_);
planner_manager_ = std::make_shared<PlannerManagerStub>(planning_context_);
}

robot_trajectory::RobotTrajectoryPtr createRandomTrajectory(std::size_t waypointCount)
{
robot_trajectory::RobotTrajectoryPtr robot_trajectory =
std::make_shared<robot_trajectory::RobotTrajectory>(robot_model_, group_name_);
for (std::size_t i = 0; i < waypointCount; ++i)
robot_trajectory->addSuffixWayPoint(std::make_shared<moveit::core::RobotState>(robot_model_), 1.);
return robot_trajectory;
}

protected:
std::string group_name_;
planning_request_adapter::PlanningRequestAdapterChain sut_;
moveit::core::RobotModelPtr robot_model_;
planning_scene::PlanningScenePtr planning_scene_;
std::shared_ptr<PlanningContextStub> planning_context_;
std::shared_ptr<PlannerManagerStub> planner_manager_;
};

TEST_F(PlanningRequestAdapterTests, testMergeAddedPathIndex)
{
sut_.addAdapter(std::make_shared<AppendingPlanningRequestAdapter>());
sut_.addAdapter(std::make_shared<PrependingPlanningRequestAdapter>());
sut_.addAdapter(std::make_shared<AppendingPlanningRequestAdapter>());

planning_context_->setTrajectory(createRandomTrajectory(4));

planning_interface::MotionPlanRequest req;
req.group_name = group_name_;
planning_interface::MotionPlanResponse res;
std::ignore = sut_.adaptAndPlan(planner_manager_, planning_scene_, req, res);

EXPECT_THAT(res.added_path_index, testing::ElementsAre(0, 5, 6));
}

int main(int argc, char** argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit 55aab07

Please sign in to comment.