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 
40 #include <moveit_msgs/RobotTrajectory.h>
42 #include <ros/ros.h>
43 
48 
50 
51 #include <tf/transform_listener.h>
52 
54 
55 #include <vector>
56 #include <eigen3/Eigen/Core>
57 
58 namespace chomp
59 {
61 {
62 public:
64  {
65  if (!nh_.getParam("planning_time_limit", params_.planning_time_limit_))
66  {
68  ROS_INFO_STREAM("Param planning_time_limit was not set. Using default value: " << params_.planning_time_limit_);
69  }
70  if (!nh_.getParam("max_iterations", params_.max_iterations_))
71  {
73  ROS_INFO_STREAM("Param max_iterations was not set. Using default value: " << params_.max_iterations_);
74  }
75  if (!nh_.getParam("max_iterations_after_collision_free", params_.max_iterations_after_collision_free_))
76  {
78  ROS_INFO_STREAM("Param max_iterations_after_collision_free was not set. Using default value: "
80  }
81  if (!nh_.getParam("smoothness_cost_weight", params_.smoothness_cost_weight_))
82  {
85  "Param smoothness_cost_weight was not set. Using default value: " << params_.smoothness_cost_weight_);
86  }
87  if (!nh_.getParam("obstacle_cost_weight", params_.obstacle_cost_weight_))
88  {
90  ROS_INFO_STREAM("Param obstacle_cost_weight was not set. Using default value: " << params_.obstacle_cost_weight_);
91  }
92  if (!nh_.getParam("learning_rate", params_.learning_rate_))
93  {
94  params_.learning_rate_ = 0.01;
95  ROS_INFO_STREAM("Param learning_rate was not set. Using default value: " << params_.learning_rate_);
96  }
97  if (!nh_.getParam("smoothness_cost_velocity", params_.smoothness_cost_velocity_))
98  {
101  "Param smoothness_cost_velocity was not set. Using default value: " << params_.smoothness_cost_velocity_);
102  }
103  if (!nh_.getParam("smoothness_cost_acceleration", params_.smoothness_cost_acceleration_))
104  {
106  ROS_INFO_STREAM("Param smoothness_cost_acceleration was not set. Using default value: "
108  }
109  if (!nh_.getParam("smoothness_cost_jerk", params_.smoothness_cost_jerk_))
110  {
113  "Param smoothness_cost_jerk_ was not set. Using default value: " << params_.smoothness_cost_jerk_);
114  }
115  if (!nh_.getParam("ridge_factor", params_.ridge_factor_))
116  {
117  params_.ridge_factor_ = 0.0;
118  ROS_INFO_STREAM("Param ridge_factor_ was not set. Using default value: " << params_.ridge_factor_);
119  }
120  if (!nh_.getParam("use_pseudo_inverse", params_.use_pseudo_inverse_))
121  {
123  ROS_INFO_STREAM("Param use_pseudo_inverse_ was not set. Using default value: " << params_.use_pseudo_inverse_);
124  }
125  if (!nh_.getParam("pseudo_inverse_ridge_factor", params_.pseudo_inverse_ridge_factor_))
126  {
128  ROS_INFO_STREAM("Param pseudo_inverse_ridge_factor was not set. Using default value: "
130  }
131  if (!nh_.getParam("joint_update_limit", params_.joint_update_limit_))
132  {
134  ROS_INFO_STREAM("Param joint_update_limit was not set. Using default value: " << params_.joint_update_limit_);
135  }
136  if (!nh_.getParam("min_clearence", params_.min_clearence_))
137  {
138  params_.min_clearence_ = 0.2;
139  ROS_INFO_STREAM("Param min_clearence was not set. Using default value: " << params_.min_clearence_);
140  }
141  if (!nh_.getParam("collision_threshold", params_.collision_threshold_))
142  {
144  ROS_INFO_STREAM("Param collision_threshold_ was not set. Using default value: " << params_.collision_threshold_);
145  }
146  if (!nh_.getParam("use_stochastic_descent", params_.use_stochastic_descent_))
147  {
150  "Param use_stochastic_descent was not set. Using default value: " << params_.use_stochastic_descent_);
151  }
152  if (!nh_.getParam("trajectory_initialization_method", params_.trajectory_initialization_method_))
153  {
154  params_.trajectory_initialization_method_ = std::string("fillTrajectory");
155  ROS_INFO_STREAM("Param trajectory_initialization_method was not set. Using New value as: "
157  }
158  }
159 
160  virtual std::string getDescription() const
161  {
162  return "CHOMP Optimizer";
163  }
164 
165  virtual bool adaptAndPlan(const PlannerFn& planner, const planning_scene::PlanningSceneConstPtr& ps,
168  std::vector<std::size_t>& added_path_index) const
169  {
170  // following call to planner() calls the OMPL planner and stores the trajectory inside the MotionPlanResponse res
171  // variable which is then used by CHOMP for optimization of the computed trajectory
172  bool solved = planner(ps, req, res);
173 
174  // create a hybrid collision detector to set the collision checker as hybrid
175  collision_detection::CollisionDetectorAllocatorPtr hybrid_cd(
177 
178  // create a writable planning scene
179  planning_scene::PlanningScenePtr planning_scene = ps->diff();
180  ROS_INFO_STREAM("Configuring Planning Scene for CHOMP ....");
181  planning_scene->setActiveCollisionDetector(hybrid_cd, true);
182 
183  chomp::ChompPlanner chompPlanner;
185  moveit_msgs::MotionPlanDetailedResponse res_detailed_moveit_msgs;
186 
187  // populate the trajectory to pass to CHOMPPlanner::solve() method. Obtain trajectory from OMPL's
188  // planning_interface::MotionPlanResponse object and put / populate it in the
189  // moveit_msgs::MotionPlanDetailedResponse object
190  moveit_msgs::RobotTrajectory trajectory_msgs_from_response;
191  res.trajectory_->getRobotTrajectoryMsg(trajectory_msgs_from_response);
192  res_detailed_moveit_msgs.trajectory.resize(1);
193  res_detailed_moveit_msgs.trajectory[0] = trajectory_msgs_from_response;
194 
195  bool planning_success = chompPlanner.solve(planning_scene, req, params_, res_detailed_moveit_msgs);
196 
197  if (planning_success)
198  {
199  res_detailed.trajectory_.resize(1);
200  res_detailed.trajectory_[0] = robot_trajectory::RobotTrajectoryPtr(
201  new robot_trajectory::RobotTrajectory(res.trajectory_->getRobotModel(), res.trajectory_->getGroup()));
202 
203  moveit::core::RobotState start_state(planning_scene->getRobotModel());
204  robot_state::robotStateMsgToRobotState(res_detailed_moveit_msgs.trajectory_start, start_state);
205  res_detailed.trajectory_[0]->setRobotTrajectoryMsg(start_state, res_detailed_moveit_msgs.trajectory[0]);
206  res_detailed.description_.push_back("plan");
207  res_detailed.processing_time_ = res_detailed_moveit_msgs.processing_time;
208  res_detailed.error_code_ = res_detailed_moveit_msgs.error_code;
209  }
210  else
211  res_detailed.error_code_ = res_detailed_moveit_msgs.error_code;
212 
213  res.error_code_ = res_detailed.error_code_;
214 
215  // populate the original response object 'res' with the CHOMP's optimized trajectory.
216  if (planning_success)
217  {
218  res.trajectory_ = res_detailed.trajectory_[0];
219  res.planning_time_ = res_detailed.processing_time_[0];
220  }
221 
222  return solved;
223  }
224 
225 private:
228 };
229 }
230 
robot_trajectory::RobotTrajectoryPtr trajectory_
std::string trajectory_initialization_method_
virtual bool adaptAndPlan(const PlannerFn &planner, const planning_scene::PlanningSceneConstPtr &ps, const planning_interface::MotionPlanRequest &req, planning_interface::MotionPlanResponse &res, std::vector< std::size_t > &added_path_index) const
double pseudo_inverse_ridge_factor_
double smoothness_cost_acceleration_
moveit_msgs::MoveItErrorCodes error_code_
moveit_msgs::MoveItErrorCodes error_code_
bool solve(const planning_scene::PlanningSceneConstPtr &planning_scene, const moveit_msgs::MotionPlanRequest &req, const ChompParameters &params, moveit_msgs::MotionPlanDetailedResponse &res) const
moveit_msgs::MotionPlanRequest MotionPlanRequest
boost::function< bool(const planning_scene::PlanningSceneConstPtr &planning_scene, const planning_interface::MotionPlanRequest &req, planning_interface::MotionPlanResponse &res)> PlannerFn
chomp::ChompParameters params_
#define ROS_INFO_STREAM(args)
std::vector< robot_trajectory::RobotTrajectoryPtr > trajectory_
bool getParam(const std::string &key, std::string &s) const
virtual std::string getDescription() const
CLASS_LOADER_REGISTER_CLASS(chomp::OptimizerAdapter, planning_request_adapter::PlanningRequestAdapter)
int max_iterations_after_collision_free_


chomp_optimizer_adapter
Author(s): Raghavender Sahdev
autogenerated on Sun Oct 18 2020 13:17:14