moveit2
The MoveIt Motion Planning Framework for ROS 2.
chomp_optimizer_adapter.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2018, Raghavender Sahdev.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Raghavender Sahdev nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Raghavender Sahdev */
36 
44 
45 #include <eigen3/Eigen/Core>
46 #include <moveit_msgs/msg/robot_trajectory.hpp>
47 #include <pluginlib/class_list_macros.hpp>
48 #include <rclcpp/logger.hpp>
49 #include <rclcpp/logging.hpp>
50 #include <rclcpp/node.hpp>
51 #include <rclcpp/parameter_value.hpp>
52 #include <vector>
53 
54 namespace chomp
55 {
56 static rclcpp::Logger LOGGER = rclcpp::get_logger("chomp_planner");
57 
59 {
60 public:
62  {
63  }
64 
65  void initialize(const rclcpp::Node::SharedPtr& node, const std::string& /* unused */) override
66  {
67  if (!node->get_parameter("chomp.planning_time_limit", params_.planning_time_limit_))
68  {
69  params_.planning_time_limit_ = 10.0;
70  RCLCPP_DEBUG(LOGGER, "Param planning_time_limit was not set. Using default value: %f",
71  params_.planning_time_limit_);
72  }
73  if (!node->get_parameter("chomp.max_iterations", params_.max_iterations_))
74  {
75  params_.max_iterations_ = 200;
76  RCLCPP_DEBUG(LOGGER, "Param max_iterations was not set. Using default value: %d", params_.max_iterations_);
77  }
78  if (!node->get_parameter("chomp.max_iterations_after_collision_free", params_.max_iterations_after_collision_free_))
79  {
81  RCLCPP_DEBUG(LOGGER, "Param max_iterations_after_collision_free was not set. Using default value: %d",
83  }
84  if (!node->get_parameter("chomp.smoothness_cost_weight", params_.smoothness_cost_weight_))
85  {
86  params_.smoothness_cost_weight_ = 0.1;
87  RCLCPP_DEBUG(LOGGER, "Param smoothness_cost_weight was not set. Using default value: %f",
88  params_.smoothness_cost_weight_);
89  }
90  if (!node->get_parameter("chomp.obstacle_cost_weight", params_.obstacle_cost_weight_))
91  {
92  params_.obstacle_cost_weight_ = 1.0;
93  RCLCPP_DEBUG(LOGGER, "Param obstacle_cost_weight was not set. Using default value: %f",
94  params_.obstacle_cost_weight_);
95  }
96  if (!node->get_parameter("chomp.learning_rate", params_.learning_rate_))
97  {
98  params_.learning_rate_ = 0.01;
99  RCLCPP_DEBUG(LOGGER, "Param learning_rate was not set. Using default value: %f", params_.learning_rate_);
100  }
101  if (!node->get_parameter("chomp.smoothness_cost_velocity", params_.smoothness_cost_velocity_))
102  {
103  params_.smoothness_cost_velocity_ = 0.0;
104  RCLCPP_DEBUG(LOGGER, "Param smoothness_cost_velocity was not set. Using default value: %f",
105  params_.smoothness_cost_velocity_);
106  }
107  if (!node->get_parameter("chomp.smoothness_cost_acceleration", params_.smoothness_cost_acceleration_))
108  {
109  params_.smoothness_cost_acceleration_ = 1.0;
110  RCLCPP_DEBUG(LOGGER, "Param smoothness_cost_acceleration was not set. Using default value: %f",
112  }
113  if (!node->get_parameter("chomp.smoothness_cost_jerk", params_.smoothness_cost_jerk_))
114  {
115  params_.smoothness_cost_jerk_ = 0.0;
116  RCLCPP_DEBUG(LOGGER, "Param smoothness_cost_jerk_ was not set. Using default value: %f",
117  params_.smoothness_cost_jerk_);
118  }
119  if (!node->get_parameter("chomp.ridge_factor", params_.ridge_factor_))
120  {
121  params_.ridge_factor_ = 0.0;
122  RCLCPP_DEBUG(LOGGER, "Param ridge_factor_ was not set. Using default value: %f", params_.ridge_factor_);
123  }
124  if (!node->get_parameter("chomp.use_pseudo_inverse", params_.use_pseudo_inverse_))
125  {
126  params_.use_pseudo_inverse_ = 0.0;
127  RCLCPP_DEBUG(LOGGER, "Param use_pseudo_inverse_ was not set. Using default value: %d",
128  params_.use_pseudo_inverse_);
129  }
130  if (!node->get_parameter("chomp.pseudo_inverse_ridge_factor", params_.pseudo_inverse_ridge_factor_))
131  {
132  params_.pseudo_inverse_ridge_factor_ = 1e-4;
133  RCLCPP_DEBUG(LOGGER, "Param pseudo_inverse_ridge_factor was not set. Using default value: %f",
135  }
136  if (!node->get_parameter("chomp.joint_update_limit", params_.joint_update_limit_))
137  {
138  params_.joint_update_limit_ = 0.1;
139  RCLCPP_DEBUG(LOGGER, "Param joint_update_limit was not set. Using default value: %f", params_.joint_update_limit_);
140  }
141  if (!node->get_parameter("chomp.collision_clearance", params_.min_clearance_))
142  {
143  params_.min_clearance_ = 0.2;
144  RCLCPP_DEBUG(LOGGER, "Param collision_clearance was not set. Using default value: %f", params_.min_clearance_);
145  }
146  if (!node->get_parameter("chomp.collision_threshold", params_.collision_threshold_))
147  {
148  params_.collision_threshold_ = 0.07;
149  RCLCPP_DEBUG(LOGGER, "Param collision_threshold_ was not set. Using default value: %f",
150  params_.collision_threshold_);
151  }
152  if (!node->get_parameter("chomp.use_stochastic_descent", params_.use_stochastic_descent_))
153  {
154  params_.use_stochastic_descent_ = true;
155  RCLCPP_DEBUG(LOGGER, "Param use_stochastic_descent was not set. Using default value: %d",
156  params_.use_stochastic_descent_);
157  }
158  params_.trajectory_initialization_method_ = "quintic-spline";
159  std::string method;
160  if (node->get_parameter("chomp.trajectory_initialization_method", method) &&
161  !params_.setTrajectoryInitializationMethod(method))
162  {
163  RCLCPP_ERROR(LOGGER,
164  "Attempted to set trajectory_initialization_method to invalid value '%s'. Using default "
165  "'%s' instead.",
166  method.c_str(), params_.trajectory_initialization_method_.c_str());
167  }
168  }
169 
170  std::string getDescription() const override
171  {
172  return "CHOMP Optimizer";
173  }
174 
175  bool adaptAndPlan(const PlannerFn& planner, const planning_scene::PlanningSceneConstPtr& ps,
177  std::vector<std::size_t>& /*added_path_index*/) const override
178  {
179  RCLCPP_DEBUG(LOGGER, "CHOMP: adaptAndPlan ...");
180 
181  // following call to planner() calls the OMPL planner and stores the trajectory inside the MotionPlanResponse res
182  // variable which is then used by CHOMP for optimization of the computed trajectory
183  if (!planner(ps, req, res))
184  return false;
185 
186  // create a hybrid collision detector to set the collision checker as hybrid
187  collision_detection::CollisionDetectorAllocatorPtr hybrid_cd(
189 
190  // create a writable planning scene
191  planning_scene::PlanningScenePtr planning_scene = ps->diff();
192  RCLCPP_DEBUG(LOGGER, "Configuring Planning Scene for CHOMP ...");
193  planning_scene->allocateCollisionDetector(hybrid_cd);
194 
195  chomp::ChompPlanner chomp_planner;
197  res_detailed.trajectory_.push_back(res.trajectory_);
198 
199  bool planning_success = chomp_planner.solve(planning_scene, req, params_, res_detailed);
200 
201  if (planning_success)
202  {
203  res.trajectory_ = res_detailed.trajectory_[0];
204  res.planning_time_ += res_detailed.processing_time_[0];
205  }
206  res.error_code_ = res_detailed.error_code_;
207 
208  return planning_success;
209  }
210 
211 private:
212  chomp::ChompParameters params_;
213 };
214 } // namespace chomp
215 
PLUGINLIB_EXPORT_CLASS(cached_ik_kinematics_plugin::CachedIKKinematicsPlugin< kdl_kinematics_plugin::KDLKinematicsPlugin >, kinematics::KinematicsBase)
std::string trajectory_initialization_method_
bool setTrajectoryInitializationMethod(std::string method)
bool solve(const planning_scene::PlanningSceneConstPtr &planning_scene, const planning_interface::MotionPlanRequest &req, const ChompParameters &params, planning_interface::MotionPlanDetailedResponse &res) const
std::string getDescription() const override
Get a short string that identifies the planning request adapter.
bool adaptAndPlan(const PlannerFn &planner, const planning_scene::PlanningSceneConstPtr &ps, const planning_interface::MotionPlanRequest &req, planning_interface::MotionPlanResponse &res, std::vector< std::size_t > &) const override
Adapt the planning request if needed, call the planner function planner and update the planning respo...
void initialize(const rclcpp::Node::SharedPtr &node, const std::string &) override
Initialize parameters using the passed Node and parameter namespace. If no initialization is needed,...
std::function< bool(const planning_scene::PlanningSceneConstPtr &, const planning_interface::MotionPlanRequest &, planning_interface::MotionPlanResponse &)> PlannerFn
moveit_msgs::msg::MotionPlanRequest MotionPlanRequest
Generic interface to adapting motion planning requests.
This namespace includes the central class for representing planning contexts.
moveit_msgs::msg::MoveItErrorCodes error_code_
std::vector< robot_trajectory::RobotTrajectoryPtr > trajectory_
robot_trajectory::RobotTrajectoryPtr trajectory_
moveit_msgs::msg::MoveItErrorCodes error_code_