Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include <ros/ros.h>
00038 #include <chomp_motion_planner/chomp_planner.h>
00039 #include <chomp_motion_planner/chomp_trajectory.h>
00040 #include <chomp_motion_planner/chomp_optimizer.h>
00041 #include <moveit/robot_state/conversions.h>
00042 #include <moveit_msgs/MotionPlanRequest.h>
00043
00044 namespace chomp
00045 {
00046 ChompPlanner::ChompPlanner()
00047 {
00048 }
00049
00050 bool ChompPlanner::solve(const planning_scene::PlanningSceneConstPtr& planning_scene,
00051 const moveit_msgs::MotionPlanRequest& req, const chomp::ChompParameters& params,
00052 moveit_msgs::MotionPlanDetailedResponse& res) const
00053 {
00054 if (!planning_scene)
00055 ROS_ERROR_STREAM("No planning scene initialized.");
00056 else
00057 {
00058 ROS_INFO_STREAM("Planning scene is configured. Beginning to plan.");
00059 }
00060 ros::WallTime start_time = ros::WallTime::now();
00061 ChompTrajectory trajectory(planning_scene->getRobotModel(), 3.0, .03, req.group_name);
00062 jointStateToArray(planning_scene->getRobotModel(), req.start_state.joint_state, req.group_name,
00063 trajectory.getTrajectoryPoint(0));
00064
00065 int goal_index = trajectory.getNumPoints() - 1;
00066 trajectory.getTrajectoryPoint(goal_index) = trajectory.getTrajectoryPoint(0);
00067 sensor_msgs::JointState js;
00068 for (unsigned int i = 0; i < req.goal_constraints[0].joint_constraints.size(); i++)
00069 {
00070 js.name.push_back(req.goal_constraints[0].joint_constraints[i].joint_name);
00071 js.position.push_back(req.goal_constraints[0].joint_constraints[i].position);
00072 ROS_INFO_STREAM("Setting joint " << req.goal_constraints[0].joint_constraints[i].joint_name << " to position "
00073 << req.goal_constraints[0].joint_constraints[i].position);
00074 }
00075 jointStateToArray(planning_scene->getRobotModel(), js, req.group_name, trajectory.getTrajectoryPoint(goal_index));
00076
00077 const moveit::core::JointModelGroup* model_group =
00078 planning_scene->getRobotModel()->getJointModelGroup(req.group_name);
00079
00080 for (size_t i = 0; i < model_group->getActiveJointModels().size(); i++)
00081 {
00082 const moveit::core::JointModel* model = model_group->getActiveJointModels()[i];
00083 const moveit::core::RevoluteJointModel* revolute_joint =
00084 dynamic_cast<const moveit::core::RevoluteJointModel*>(model);
00085
00086 if (revolute_joint != NULL)
00087 {
00088 if (revolute_joint->isContinuous())
00089 {
00090 double start = (trajectory)(0, i);
00091 double end = (trajectory)(goal_index, i);
00092 ROS_INFO_STREAM("Start is " << start << " end " << end << " short " << shortestAngularDistance(start, end));
00093 (trajectory)(goal_index, i) = start + shortestAngularDistance(start, end);
00094 }
00095 }
00096 }
00097
00098
00099 trajectory.fillInMinJerk();
00100
00101
00102 moveit::core::RobotState start_state(planning_scene->getCurrentState());
00103 moveit::core::robotStateMsgToRobotState(req.start_state, start_state);
00104 start_state.update();
00105
00106 ros::WallTime create_time = ros::WallTime::now();
00107 ChompOptimizer optimizer(&trajectory, planning_scene, req.group_name, ¶ms, start_state);
00108 if (!optimizer.isInitialized())
00109 {
00110 ROS_WARN_STREAM("Could not initialize optimizer");
00111 res.error_code.val = moveit_msgs::MoveItErrorCodes::PLANNING_FAILED;
00112 return false;
00113 }
00114 ROS_INFO("Optimization took %f sec to create", (ros::WallTime::now() - create_time).toSec());
00115 optimizer.optimize();
00116 ROS_INFO("Optimization actually took %f sec to run", (ros::WallTime::now() - create_time).toSec());
00117 create_time = ros::WallTime::now();
00118
00119
00120 ROS_INFO("Output trajectory has %d joints", trajectory.getNumJoints());
00121
00122 res.trajectory.resize(1);
00123
00124 for (size_t i = 0; i < model_group->getActiveJointModels().size(); i++)
00125 {
00126 res.trajectory[0].joint_trajectory.joint_names.push_back(model_group->getActiveJointModels()[i]->getName());
00127 }
00128
00129 res.trajectory[0].joint_trajectory.header = req.start_state.joint_state.header;
00130
00131
00132 res.trajectory[0].joint_trajectory.points.resize(trajectory.getNumPoints());
00133 for (int i = 0; i < trajectory.getNumPoints(); i++)
00134 {
00135 res.trajectory[0].joint_trajectory.points[i].positions.resize(trajectory.getNumJoints());
00136 for (size_t j = 0; j < res.trajectory[0].joint_trajectory.points[i].positions.size(); j++)
00137 {
00138 res.trajectory[0].joint_trajectory.points[i].positions[j] = trajectory.getTrajectoryPoint(i)(j);
00139 if (i == trajectory.getNumPoints() - 1)
00140 {
00141 ROS_INFO_STREAM("Joint " << j << " " << res.trajectory[0].joint_trajectory.points[i].positions[j]);
00142 }
00143 }
00144
00145
00146 res.trajectory[0].joint_trajectory.points[i].time_from_start = ros::Duration(0.0);
00147 }
00148
00149 ROS_INFO("Bottom took %f sec to create", (ros::WallTime::now() - create_time).toSec());
00150 ROS_INFO("Serviced planning request in %f wall-seconds, trajectory duration is %f",
00151 (ros::WallTime::now() - start_time).toSec(),
00152 res.trajectory[0].joint_trajectory.points[goal_index].time_from_start.toSec());
00153 res.error_code.val = moveit_msgs::MoveItErrorCodes::SUCCESS;
00154 res.processing_time.push_back((ros::WallTime::now() - start_time).toSec());
00155 return true;
00156 }
00157 }