42 #include <moveit_msgs/MotionPlanRequest.h> 52 moveit_msgs::MotionPlanDetailedResponse& res)
const 57 res.error_code.val = moveit_msgs::MoveItErrorCodes::FAILURE;
61 if (req.start_state.joint_state.position.empty())
64 res.error_code.val = moveit_msgs::MoveItErrorCodes::INVALID_ROBOT_STATE;
68 if (not planning_scene->getRobotModel()->satisfiesPositionBounds(req.start_state.joint_state.position.data()))
71 res.error_code.val = moveit_msgs::MoveItErrorCodes::INVALID_ROBOT_STATE;
78 jointStateToArray(planning_scene->getRobotModel(), req.start_state.joint_state, req.group_name,
81 if (req.goal_constraints.empty())
84 res.error_code.val = moveit_msgs::MoveItErrorCodes::INVALID_GOAL_CONSTRAINTS;
88 if (req.goal_constraints[0].joint_constraints.empty())
91 res.error_code.val = moveit_msgs::MoveItErrorCodes::INVALID_GOAL_CONSTRAINTS;
95 int goal_index =
trajectory.getNumPoints() - 1;
97 sensor_msgs::JointState js;
98 for (
unsigned int i = 0; i < req.goal_constraints[0].joint_constraints.size(); i++)
100 js.name.push_back(req.goal_constraints[0].joint_constraints[i].joint_name);
101 js.position.push_back(req.goal_constraints[0].joint_constraints[i].position);
102 ROS_INFO_STREAM_NAMED(
"chomp_planner",
"Setting joint " << req.goal_constraints[0].joint_constraints[i].joint_name
104 << req.goal_constraints[0].joint_constraints[i].position);
109 planning_scene->getRobotModel()->getJointModelGroup(req.group_name);
111 for (
size_t i = 0; i < model_group->getActiveJointModels().size(); i++)
117 if (revolute_joint != NULL)
129 const std::vector<std::string>& active_joint_names = model_group->getActiveJointModelNames();
130 const Eigen::MatrixXd goal_state =
trajectory.getTrajectoryPoint(goal_index);
133 active_joint_names, std::vector<double>(goal_state.data(), goal_state.data() + active_joint_names.size()));
138 res.error_code.val = moveit_msgs::MoveItErrorCodes::INVALID_ROBOT_STATE;
154 "trajectory must contain at least start and goal state");
159 ROS_ERROR_STREAM_NAMED(
"chomp_planner",
"invalid interpolation method specified in the chomp_planner file");
161 ROS_INFO_NAMED(
"chomp_planner",
"CHOMP trajectory initialized using method: %s ",
167 start_state.update();
171 int replan_count = 0;
172 bool replan_flag =
false;
173 double org_learning_rate = 0.04, org_ridge_factor = 0.0, org_planning_time_limit = 10;
174 int org_max_iterations = 200;
182 std::unique_ptr<ChompOptimizer> optimizer;
201 if (!optimizer->isInitialized())
204 res.error_code.val = moveit_msgs::MoveItErrorCodes::PLANNING_FAILED;
211 bool optimization_result = optimizer->optimize();
216 ROS_INFO_NAMED(
"chomp_planner",
"Planned with Chomp Parameters (learning_rate, ridge_factor, " 217 "planning_time_limit, max_iterations), attempt: # %d ",
219 ROS_INFO_NAMED(
"chomp_planner",
"Learning rate: %f ridge factor: %f planning time limit: %f max_iterations %d ",
238 params_nonconst.
setRecoveryParams(org_learning_rate, org_ridge_factor, org_planning_time_limit, org_max_iterations);
240 ROS_DEBUG_NAMED(
"chomp_planner",
"Optimization actually took %f sec to run",
247 res.trajectory.resize(1);
249 res.trajectory[0].joint_trajectory.joint_names = active_joint_names;
251 res.trajectory[0].joint_trajectory.header = req.start_state.joint_state.header;
254 res.trajectory[0].joint_trajectory.points.resize(
trajectory.getNumPoints());
255 for (
int i = 0; i <
trajectory.getNumPoints(); i++)
257 res.trajectory[0].joint_trajectory.points[i].positions.resize(
trajectory.getNumJoints());
258 for (
size_t j = 0; j < res.trajectory[0].joint_trajectory.points[i].positions.size(); j++)
260 res.trajectory[0].joint_trajectory.points[i].positions[j] =
trajectory.getTrajectoryPoint(i)(j);
264 res.trajectory[0].joint_trajectory.points[i].time_from_start =
ros::Duration(0.0);
268 ROS_DEBUG_NAMED(
"chomp_planner",
"Serviced planning request in %f wall-seconds, trajectory duration is %f",
270 res.trajectory[0].joint_trajectory.points[goal_index].time_from_start.toSec());
271 res.error_code.val = moveit_msgs::MoveItErrorCodes::SUCCESS;
275 if (not optimizer->isCollisionFree())
278 res.error_code.val = moveit_msgs::MoveItErrorCodes::INVALID_MOTION_PLAN;
284 robot_state::RobotState last_state(start_state);
285 last_state.setVariablePositions(res.trajectory[0].joint_trajectory.joint_names,
286 res.trajectory[0].joint_trajectory.points.back().positions);
288 bool constraints_are_ok =
true;
289 for (
const moveit_msgs::JointConstraint& constraint : req.goal_constraints[0].joint_constraints)
291 constraints_are_ok = constraints_are_ok and jc.configure(constraint);
292 constraints_are_ok = constraints_are_ok and jc.decide(last_state).satisfied;
293 if (not constraints_are_ok)
296 res.error_code.val = moveit_msgs::MoveItErrorCodes::GOAL_CONSTRAINTS_VIOLATED;
std::string trajectory_initialization_method_
#define ROS_INFO_NAMED(name,...)
double planning_time_limit_
#define ROS_ERROR_STREAM_NAMED(name, args)
ChompParameters getNonConstParams(ChompParameters params)
int max_recovery_attempts_
#define ROS_INFO_STREAM_NAMED(name, args)
void setVariablePositions(const double *position)
bool isContinuous() const
#define ROS_DEBUG_NAMED(name,...)
void setRecoveryParams(double learning_rate, double ridge_factor, int planning_time_limit, int max_iterations)
robot_trajectory::RobotTrajectory trajectory(rmodel,"right_arm")
bool robotStateMsgToRobotState(const Transforms &tf, const moveit_msgs::RobotState &robot_state, RobotState &state, bool copy_attached_bodies=true)
bool solve(const planning_scene::PlanningSceneConstPtr &planning_scene, const moveit_msgs::MotionPlanRequest &req, const ChompParameters ¶ms, moveit_msgs::MotionPlanDetailedResponse &res) const
static void jointStateToArray(const moveit::core::RobotModelConstPtr &kmodel, const sensor_msgs::JointState &joint_state, const std::string &planning_group_name, Eigen::MatrixXd::RowXpr joint_array)
bool satisfiesBounds(double margin=0.0) const
int max_iterations_
maximum time the optimizer can take to find a solution before terminating
Represents a discretized joint-space trajectory for CHOMP.
static double shortestAngularDistance(double start, double end)
#define ROS_INFO_STREAM(args)
bool enable_failure_recovery_
trajectory initialization method to be specified
#define ROS_ERROR_STREAM(args)