mirror of
https://github.com/moveit/moveit_task_constructor.git
synced 2025-11-04 14:49:57 +08:00
add PredicateFilter
This commit is contained in:
parent
5af54b5a21
commit
cc90d5f3f2
@ -0,0 +1,69 @@
|
||||
/*********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright (c) 2018, Hamburg University
|
||||
* 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 Bielefeld University 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.
|
||||
*********************************************************************/
|
||||
/* Authors: Michael Goerner */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <moveit/task_constructor/container.h>
|
||||
|
||||
namespace moveit {
|
||||
namespace core {
|
||||
MOVEIT_CLASS_FORWARD(RobotState)
|
||||
}
|
||||
}
|
||||
|
||||
namespace moveit { namespace task_constructor { namespace stages {
|
||||
|
||||
/** Generator Wrapper to filter generated solutions by custom criteria
|
||||
*
|
||||
* All solutions of the wrapped class are passed to predicate.
|
||||
* Solutions are accepted if predicate(s) == true.
|
||||
* Rejected solutions are forwarded as failures with an optional comment
|
||||
*/
|
||||
class PredicateFilter : public WrapperBase {
|
||||
public:
|
||||
typedef std::function<bool(const SolutionBase&, std::string&)> Predicate;
|
||||
|
||||
PredicateFilter(const std::string &name, Stage::pointer &&child = Stage::pointer());
|
||||
|
||||
void init(const moveit::core::RobotModelConstPtr& robot_model) override;
|
||||
|
||||
void onNewSolution(const SolutionBase &s) override;
|
||||
|
||||
void setPredicate(const Predicate& p){
|
||||
setProperty("predicate", p);
|
||||
}
|
||||
};
|
||||
|
||||
} } }
|
||||
@ -7,6 +7,7 @@ add_library(${PROJECT_NAME}_stages
|
||||
${PROJECT_INCLUDE}/stages/generate_pose.h
|
||||
${PROJECT_INCLUDE}/stages/generate_grasp_pose.h
|
||||
${PROJECT_INCLUDE}/stages/compute_ik.h
|
||||
${PROJECT_INCLUDE}/stages/predicate_filter.h
|
||||
|
||||
${PROJECT_INCLUDE}/stages/connect.h
|
||||
${PROJECT_INCLUDE}/stages/move_to.h
|
||||
@ -23,6 +24,7 @@ add_library(${PROJECT_NAME}_stages
|
||||
generate_grasp_pose.cpp
|
||||
generate_pose.cpp
|
||||
compute_ik.cpp
|
||||
predicate_filter.cpp
|
||||
|
||||
connect.cpp
|
||||
move_to.cpp
|
||||
|
||||
88
core/src/stages/predicate_filter.cpp
Normal file
88
core/src/stages/predicate_filter.cpp
Normal file
@ -0,0 +1,88 @@
|
||||
/*********************************************************************
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright (c) 2018, Hamburg University
|
||||
* 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 Bielefeld University 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.
|
||||
*********************************************************************/
|
||||
/* Authors: Michael Goerner */
|
||||
|
||||
#include <moveit/task_constructor/stages/predicate_filter.h>
|
||||
|
||||
#include <moveit/task_constructor/storage.h>
|
||||
|
||||
#include <moveit/planning_scene/planning_scene.h>
|
||||
|
||||
#include <moveit/robot_state/conversions.h>
|
||||
#include <moveit/robot_state/robot_state.h>
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace moveit { namespace task_constructor { namespace stages {
|
||||
|
||||
PredicateFilter::PredicateFilter(const std::string &name, Stage::pointer &&child)
|
||||
: WrapperBase(name, std::move(child))
|
||||
{
|
||||
auto& p = properties();
|
||||
p.declare<Predicate>("predicate", "predicate to filter wrapped solutions");
|
||||
p.declare<bool>("ignore_filter", false);
|
||||
}
|
||||
|
||||
void PredicateFilter::init(const moveit::core::RobotModelConstPtr& robot_model)
|
||||
{
|
||||
InitStageException errors;
|
||||
|
||||
try { WrapperBase::init(robot_model); } catch (InitStageException &e) { errors.append(e); }
|
||||
|
||||
const auto& props = properties();
|
||||
|
||||
// In theory this could be set in interface states
|
||||
// but we enforce it here to keep code flow sane and maintainable
|
||||
if (props.get("predicate").empty()) {
|
||||
InitStageException e(*this, "predicate is not specified");
|
||||
errors.append(e);
|
||||
}
|
||||
|
||||
if (errors) throw errors;
|
||||
}
|
||||
|
||||
void PredicateFilter::onNewSolution(const SolutionBase &s)
|
||||
{
|
||||
const auto& props = properties();
|
||||
|
||||
std::string comment = s.comment();
|
||||
|
||||
double cost = s.cost();
|
||||
if (!props.get<Predicate>("predicate")(s, comment))
|
||||
cost = std::numeric_limits<double>::infinity();
|
||||
|
||||
liftSolution(s, cost, comment);
|
||||
}
|
||||
|
||||
} } }
|
||||
Loading…
Reference in New Issue
Block a user