-
Notifications
You must be signed in to change notification settings - Fork 532
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
State cost function interface #2153
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
add_library(moveit_cost_functions SHARED | ||
src/cost_functions.cpp | ||
) | ||
target_include_directories(moveit_cost_functions PUBLIC | ||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> | ||
$<INSTALL_INTERFACE:include/moveit_core> | ||
) | ||
set_target_properties(moveit_cost_functions PROPERTIES VERSION "${${PROJECT_NAME}_VERSION}") | ||
target_link_libraries(moveit_cost_functions | ||
moveit_planning_interface | ||
moveit_planning_scene | ||
moveit_robot_state | ||
) | ||
|
||
install(DIRECTORY include/ DESTINATION include/moveit_core) | ||
|
||
|
||
#if(BUILD_TESTING) | ||
# TODO(sjahr): Add tests | ||
#endif() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/********************************************************************* | ||
* Software License Agreement (BSD License) | ||
* | ||
* Copyright (c) 2023, PickNik 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 PickNik Inc. 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: Sebastian Jahr | ||
Desc: Cost functions for MoveIt */ | ||
|
||
#pragma once | ||
|
||
#include <moveit/planning_interface/planning_interface.h> | ||
#include <moveit/planning_interface/planning_request.h> | ||
#include <moveit/planning_scene/planning_scene.h> | ||
|
||
namespace moveit | ||
{ | ||
namespace cost_functions | ||
{ | ||
[[nodiscard]] ::planning_interface::StateCostFn | ||
createMinJointDisplacementCostFn(moveit::core::RobotState& robot_state, const std::string& group_name, | ||
const planning_scene::PlanningSceneConstPtr& planning_scene); | ||
|
||
[[nodiscard]] ::planning_interface::StateCostFn | ||
createClearanceCostFn(moveit::core::RobotState& robot_state, const std::string& group_name, | ||
const planning_scene::PlanningSceneConstPtr& planning_scene); | ||
} // namespace cost_functions | ||
} // namespace moveit |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/********************************************************************* | ||
* Software License Agreement (BSD License) | ||
* | ||
* Copyright (c) 2023, PickNik 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 PickNik Inc. 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: Sebastian Jahr */ | ||
|
||
#include <moveit/cost_functions/cost_functions.hpp> | ||
|
||
namespace moveit | ||
{ | ||
namespace cost_functions | ||
{ | ||
[[nodiscard]] ::planning_interface::StateCostFn | ||
createClearanceCostFn(moveit::core::RobotState& robot_state, const std::string& group_name, | ||
const planning_scene::PlanningSceneConstPtr& planning_scene) | ||
{ | ||
// Create cost function | ||
return [&](const Eigen::VectorXd& state_vector) mutable { | ||
assert(state_vector.size() == robot_state.getRobotModel()->getJointModelGroup(group_name)->getActiveVariableCount()); | ||
robot_state.setJointGroupActivePositions(group_name, state_vector); | ||
sjahr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
auto const shortest_distance_to_collision = planning_scene->distanceToCollision(robot_state); | ||
|
||
// Return cost based on shortest_distance if the robot is not in contact or penetrated a collision object | ||
if (shortest_distance_to_collision > 0.0) | ||
{ | ||
// The closer the collision object the higher the cost | ||
return 1.0 / shortest_distance_to_collision; | ||
} | ||
return std::numeric_limits<double>::infinity(); // Return a max cost cost by default | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't there a max distance associated with the distance check? We don't want to return infinity if the returned distance is larger than that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question, I have not found anything about this in the documentation/ comments 😅 On the other hand, it only returns infinity, if the distance check returns a negative distance (penetration of the collision object). For simplicity sake I decided that this is always associated with the highest possible cost There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still think that infinity will basically break optimization since there is no way to approximate a gradient based in this value. IMO, a better cost function would take a specific clearance target as 0 (or max_distance if set), and then returning a linear cost for distances smaller than that, including negative distances. It's very useful to also consider negative distances since they also produce a gradient and STOMP can optimize out collisions. The cost function could looks like this |
||
}; | ||
} | ||
|
||
[[nodiscard]] ::planning_interface::StateCostFn | ||
createMinJointDisplacementCostFn(moveit::core::RobotState& robot_state, const std::string& group_name, | ||
const planning_scene::PlanningSceneConstPtr& planning_scene) | ||
{ | ||
return [&](const Eigen::VectorXd& state_vector) mutable { | ||
assert(state_vector.size() == robot_state.getRobotModel()->getJointModelGroup(group_name)->getActiveVariableCount()); | ||
robot_state.setJointGroupActivePositions(group_name, state_vector); | ||
const auto& current_state = planning_scene->getCurrentState(); | ||
|
||
return current_state.distance(robot_state, robot_state.getJointModelGroup(group_name)); | ||
}; | ||
} | ||
} // namespace cost_functions | ||
} // namespace moveit |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/********************************************************************* | ||
* Software License Agreement (BSD License) | ||
* | ||
* Copyright (c) 2023, PickNik 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 PickNik Inc. 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: Sebastian Jahr */ | ||
|
||
#include <moveit/planning_interface/planning_request.h> | ||
|
||
namespace planning_interface | ||
{ | ||
MotionPlanRequest::MotionPlanRequest(moveit_msgs::msg::MotionPlanRequest request_msg, | ||
planning_interface::StateCostFn state_cost_function) | ||
: moveit_msgs::msg::MotionPlanRequest{ std::move(request_msg) }, state_cost_function{ std::move(state_cost_function) } | ||
{ | ||
} | ||
} // namespace planning_interface |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,7 +58,7 @@ class ConstrainedPlanningStateSpaceFactory : public ModelBasedStateSpaceFactory | |
* For more details on this state space selection process, see: | ||
* https://github.com/JeroenDM/moveit/pull/2 | ||
* **/ | ||
int canRepresentProblem(const std::string& group, const moveit_msgs::msg::MotionPlanRequest& req, | ||
int canRepresentProblem(const std::string& group, const planning_interface::MotionPlanRequest& req, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the idea of all these functions now taking a MoveIt! struct, as opposed to a ROS message. Even though the struct is still inheriting from the message, this can be a step towards additional ROS independency. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! I think that's a good idea, especially since the message structures are not allowed to have function pointers as members |
||
const moveit::core::RobotModelConstPtr& robot_model) const override; | ||
|
||
protected: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will need comments on what these do, and tests for them.