stomp_smoothing_adapter.cpp
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2019, Henning Kayser
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: Henning Kayser */
36 
37 // ROS
38 #include <ros/ros.h>
40 
41 // MoveIt
44 
45 // STOMP
47 
48 namespace stomp_moveit
49 {
51 {
52 public:
54  {
55  ros::NodeHandle nh("~");
56  initialize(nh);
57  }
58 
59  // todo[noetic] add override again
60  virtual void initialize(const ros::NodeHandle& node_handle)
61  {
62  ros::NodeHandle nh(node_handle);
63  if (!StompPlanner::getConfigData(nh, group_config_))
64  ROS_ERROR("Unable to find valid group config for StompSmoothingAdapter");
65  }
66 
67  virtual std::string getDescription() const override
68  {
69  return "Stomp Smoothing Adapter";
70  }
71 
72  bool adaptAndPlan(const PlannerFn& planner, const planning_scene::PlanningSceneConstPtr& ps,
74  std::vector<std::size_t>& added_path_index) const override
75  {
76  // Following call to planner() calls the OMPL planner and stores the trajectory inside the MotionPlanResponse res
77  // variable which is then used by STOMP for optimization of the computed trajectory
78  if (!planner(ps, req, res))
79  return false;
80 
81  // STOMP reads the seed trajectory from trajectory constraints so we need to convert the waypoints first
82  const size_t seed_waypoint_count = res.trajectory_->getWayPointCount();
83  const std::vector<std::string> variable_names =
84  res.trajectory_->getFirstWayPoint().getJointModelGroup(req.group_name)->getVariableNames();
85  const size_t variable_count = variable_names.size();
87  seed_req.trajectory_constraints.constraints.clear();
88  seed_req.trajectory_constraints.constraints.resize(seed_waypoint_count);
89  for (size_t i = 0; i < seed_waypoint_count; ++i)
90  {
91  seed_req.trajectory_constraints.constraints[i].joint_constraints.resize(variable_count);
92  for (size_t j = 0; j < variable_count; ++j)
93  {
94  seed_req.trajectory_constraints.constraints[i].joint_constraints[j].joint_name = variable_names[j];
95  seed_req.trajectory_constraints.constraints[i].joint_constraints[j].position =
96  res.trajectory_->getWayPoint(i).getVariablePosition(variable_names[j]);
97  }
98  }
99 
100  // Get group config
101  const auto& group_config_it = group_config_.find(req.group_name);
102  if (group_config_it == group_config_.end())
103  {
104  ROS_ERROR_STREAM("STOMP is not configured for planning group " << req.group_name);
105  res.error_code_.val = moveit_msgs::MoveItErrorCodes::FAILURE;
106  return false;
107  }
108 
109  // Initialize STOMP Planner
110  stomp_moveit::StompPlanner stomp_planner(req.group_name, group_config_it->second, ps->getRobotModel());
111  if(!stomp_planner.canServiceRequest(seed_req))
112  {
113  ROS_ERROR("STOMP planner unable to service request");
114  res.error_code_.val = moveit_msgs::MoveItErrorCodes::FAILURE;
115  return false;
116  }
117 
118  // Setup Planning Context
119  stomp_planner.clear();
120  stomp_planner.setPlanningScene(ps);
121  stomp_planner.setMotionPlanRequest(seed_req);
122 
123  // Solve
124  ROS_DEBUG("Smoothing result trajectory with STOMP");
126  bool success = stomp_planner.solve(stomp_res);
127  if (success)
128  {
129  // Successful responses always contain one entry for trajectory_ and proccessing_time_
130  res.trajectory_ = stomp_res.trajectory_.back();
131  res.planning_time_ += stomp_res.processing_time_.back();
132  }
133  res.error_code_ = stomp_res.error_code_;
134  return success;
135  }
136 
137 private:
138  std::map<std::string, XmlRpc::XmlRpcValue> group_config_;
139 };
140 } // namespace stomp_moveit
141 
robot_trajectory::RobotTrajectoryPtr trajectory_
This defines the stomp planner for MoveIt.
moveit_msgs::MoveItErrorCodes error_code_
moveit_msgs::MoveItErrorCodes error_code_
moveit_msgs::MotionPlanRequest MotionPlanRequest
boost::function< bool(const planning_scene::PlanningSceneConstPtr &planning_scene, const planning_interface::MotionPlanRequest &req, planning_interface::MotionPlanResponse &res)> PlannerFn
std::vector< robot_trajectory::RobotTrajectoryPtr > trajectory_
static bool getConfigData(ros::NodeHandle &nh, std::map< std::string, XmlRpc::XmlRpcValue > &config, std::string param=std::string("stomp"))
Convenience method to load extract the parameters for each supported planning group.
#define ROS_ERROR_STREAM(args)
The PlanningContext specialization that wraps the STOMP algorithm.
Definition: stomp_planner.h:48
#define ROS_ERROR(...)
CLASS_LOADER_REGISTER_CLASS(Dog, Base)
#define ROS_DEBUG(...)


stomp_moveit
Author(s): Jorge Nicho
autogenerated on Fri May 8 2020 03:35:47