Skip to content

Commit 03b7a7f

Browse files
committed
Make custom cost function optional
1 parent 2d738cf commit 03b7a7f

File tree

2 files changed

+35
-44
lines changed

2 files changed

+35
-44
lines changed

doc/how_to_guides/planner_cost_functions/config/planner_cost_moveit_cpp.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ plan_request_params:
1818
planner_id: "stomp"
1919
max_velocity_scaling_factor: 1.0
2020
max_acceleration_scaling_factor: 1.0
21-
planning_time: 10.0
21+
planning_time: 4.0

doc/how_to_guides/planner_cost_functions/src/planner_cost_functions_main.cpp

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -208,42 +208,22 @@ class Demo
208208
return true;
209209
}
210210

211-
/// \brief Set goal state for next planning attempt based on query loaded from the database
212-
void setQueryGoal(moveit_msgs::msg::MotionPlanRequest const& motion_plan_request)
211+
struct PipelineConfig
213212
{
214-
// Set goal state
215-
if (!motion_plan_request.goal_constraints.empty())
216-
{
217-
// for (auto constraint : motion_plan_request.goal_constraints)
218-
// {
219-
// for (auto joint_constraint : constraint.joint_constraints)
220-
// {
221-
// RCLCPP_INFO_STREAM(LOGGER, "position: " << joint_constraint.position << "\n"
222-
// << "tolerance_above: " << joint_constraint.tolerance_above << "\n"
223-
// << "tolerance_below: " << joint_constraint.tolerance_below << "\n"
224-
// << "weight: " << joint_constraint.weight << "\n");
225-
// }
226-
// for (auto position_constraint : constraint.position_constraints)
227-
// {
228-
// RCLCPP_WARN(LOGGER, "Goal constraints contain position constrains, please use joint constraints only in this "
229-
// "example.");
230-
// }
231-
// }
232-
planning_component_->setGoal(motion_plan_request.goal_constraints);
233-
}
234-
else
235-
{
236-
RCLCPP_ERROR(LOGGER, "Planning query request does not contain any goal constraints");
237-
}
238-
}
213+
std::string planning_pipeline;
214+
std::string planner_id;
215+
bool use_cost_function;
216+
};
239217

240218
/// \brief Request a motion plan based on the assumption that a goal is set and print debug information.
241-
void planAndVisualize(std::vector<std::pair<std::string, std::string>> pipeline_planner_pairs,
219+
void planAndVisualize(std::vector<PipelineConfig> pipeline_configs,
242220
moveit_msgs::msg::MotionPlanRequest const& motion_plan_request)
243221
{
244222
visual_tools_.deleteAllMarkers();
245223
visual_tools_.prompt("Press 'next' in the RvizVisualToolsGui window to continue the demo");
246224

225+
// Set goal state
226+
planning_component_->setGoal(motion_plan_request.goal_constraints);
247227
// Set start state as current state
248228
planning_component_->setStartStateToCurrentState();
249229

@@ -259,14 +239,6 @@ class Demo
259239
}();
260240

261241
auto group_name = motion_plan_request.group_name;
262-
// Set cost function
263-
planning_component_->setStateCostFunction(
264-
[robot_start_state, group_name, planning_scene](const Eigen::VectorXd& state_vector) mutable {
265-
// rviz_visual_tools::MEDIUM); this->getVisualTools().trigger();
266-
auto clearance_cost_fn =
267-
moveit::cost_functions::getClearanceCostFn(*robot_start_state, group_name, planning_scene);
268-
return clearance_cost_fn(state_vector);
269-
});
270242

271243
moveit_cpp::PlanningComponent::PlanRequestParameters plan_request_parameters;
272244
plan_request_parameters.load(node_);
@@ -280,11 +252,28 @@ class Demo
280252
<< " max_acceleration_scaling_factor: " << plan_request_parameters.max_acceleration_scaling_factor);
281253

282254
std::vector<planning_interface::MotionPlanResponse> solutions;
283-
solutions.reserve(pipeline_planner_pairs.size());
284-
for (auto const& pipeline_planner_pair : pipeline_planner_pairs)
255+
solutions.reserve(pipeline_configs.size());
256+
for (auto const& pipeline_config : pipeline_configs)
285257
{
286-
plan_request_parameters.planning_pipeline = pipeline_planner_pair.first;
287-
plan_request_parameters.planner_id = pipeline_planner_pair.second;
258+
plan_request_parameters.planning_pipeline = pipeline_config.planning_pipeline;
259+
plan_request_parameters.planner_id = pipeline_config.planner_id;
260+
261+
// Set cost function
262+
if (pipeline_config.use_cost_function)
263+
{
264+
planning_component_->setStateCostFunction(
265+
[robot_start_state, group_name, planning_scene](const Eigen::VectorXd& state_vector) mutable {
266+
// rviz_visual_tools::MEDIUM); this->getVisualTools().trigger();
267+
auto clearance_cost_fn =
268+
moveit::cost_functions::getClearanceCostFn(*robot_start_state, group_name, planning_scene);
269+
return clearance_cost_fn(state_vector);
270+
});
271+
}
272+
else
273+
{
274+
planning_component_->setStateCostFunction(nullptr);
275+
}
276+
288277
solutions.push_back(planning_component_->plan(plan_request_parameters, planning_scene));
289278
}
290279

@@ -297,7 +286,8 @@ class Demo
297286
if (plan_solution.trajectory)
298287
{
299288
RCLCPP_INFO_STREAM(LOGGER, plan_solution.planner_id.c_str()
300-
<< ": " << colorToString(rviz_visual_tools::Colors(color_index)));
289+
<< ", " << std::to_string(plan_solution.use_cost_function) << ": "
290+
<< colorToString(rviz_visual_tools::Colors(color_index)));
301291
// Visualize the trajectory in Rviz
302292
visual_tools_.publishTrajectoryLine(plan_solution.trajectory, joint_model_group_ptr,
303293
rviz_visual_tools::Colors(color_index));
@@ -350,8 +340,9 @@ int main(int argc, char** argv)
350340
RCLCPP_INFO(LOGGER, "Starting Cost Function Tutorial...");
351341
for (auto const& motion_plan_req : demo.getMotionPlanRequests())
352342
{
353-
demo.setQueryGoal(motion_plan_req);
354-
demo.planAndVisualize({ { "ompl", "RRTConnectkConfigDefault" }, { "stomp", "stomp" } }, motion_plan_req);
343+
demo.planAndVisualize(
344+
{ { "ompl", "RRTConnectkConfigDefault", false }, { "stomp", "stomp", false }, { "stomp", "stomp", true } },
345+
motion_plan_req);
355346
}
356347

357348
demo.getVisualTools().prompt("Press 'next' in the RvizVisualToolsGui window to finish the demo");

0 commit comments

Comments
 (0)