在ROS Noetic版本的MoveIt中,使用Iterative Parabolic Time Parameterization算法对轨迹重规划的python接口函数
时间: 2024-05-01 18:20:34 浏览: 334
ROS noetic MoveIt编译出错,提示缺少软件包"manipulation_msgs"
在ROS Noetic版本的MoveIt中,可以使用以下的python接口函数对轨迹进行重规划:
```python
from moveit_msgs.msg import RobotTrajectory
from trajectory_msgs.msg import JointTrajectoryPoint
from moveit_msgs.msg import RobotState
from moveit_msgs.msg import Constraints
from moveit_python.planning_scene_interface import PlanningSceneInterface
from moveit_msgs.msg import MotionPlanRequest
from moveit_msgs.msg import MoveItErrorCodes
from moveit_python import MoveGroupInterface, PlanningSceneInterface
from moveit_msgs.msg import PlanningScene, PlanningSceneWorld
from moveit_msgs.srv import GetPlanningScene
from moveit_msgs.srv import ApplyPlanningScene
from moveit_msgs.srv import SetPlanningSceneDiff
from moveit_msgs.srv import GetPositionIK, GetPositionIKRequest, GetPositionIKResponse
from moveit_msgs.srv import GetStateValidity, GetStateValidityRequest, GetStateValidityResponse
from moveit_msgs.srv import GetMotionPlan, GetMotionPlanRequest, GetMotionPlanResponse
from moveit_msgs.srv import ExecuteKnownTrajectory, ExecuteKnownTrajectoryRequest, ExecuteKnownTrajectoryResponse
from moveit_msgs.srv import ApplyPlanningScene, ApplyPlanningSceneRequest, ApplyPlanningSceneResponse
from moveit_msgs.srv import SetPlanningSceneDiff, SetPlanningSceneDiffRequest, SetPlanningSceneDiffResponse
import rospy
import copy
# Create a MoveGroupInterface object
move_group = MoveGroupInterface("manipulator", "base_link")
# Set the planner
move_group.setPlannerId("RRTConnectkConfigDefault")
# Set the planning time
move_group.setPlanningTime(5)
# Set the start state
start_state = RobotState()
start_state.joint_state.name = ["joint_1", "joint_2", "joint_3", "joint_4", "joint_5", "joint_6"]
start_state.joint_state.position = [0, 0, 0, 0, 0, 0]
move_group.setStartState(start_state)
# Set the goal state
goal_state = RobotState()
goal_state.joint_state.name = ["joint_1", "joint_2", "joint_3", "joint_4", "joint_5", "joint_6"]
goal_state.joint_state.position = [1.57, -1.57, 1.57, -1.57, 1.57, 0]
move_group.setJointValueTarget(goal_state.joint_state.position)
# Plan the trajectory
trajectory = move_group.plan()
# Create the parabolic time parameterization object
ptp = IterativeParabolicTimeParameterization()
# Compute the time parameterization of the trajectory
ptp.computeTimeStamps(trajectory)
# Convert the trajectory to a RobotTrajectory message
robot_trajectory = RobotTrajectory()
robot_trajectory.joint_trajectory = trajectory.joint_trajectory
# Set the robot trajectory
move_group.setTrajectory(robot_trajectory)
# Execute the trajectory
move_group.execute()
```
其中,IterativeParabolicTimeParameterization是MoveIt中用于轨迹重规划的算法之一,可以通过以下代码进行导入:
```python
from moveit_msgs.msg import RobotTrajectory
from trajectory_msgs.msg import JointTrajectoryPoint
from moveit_msgs.msg import Constraints
from moveit_msgs.msg import MoveItErrorCodes
from moveit_python import MoveGroupInterface, PlanningSceneInterface
from moveit_msgs.srv import ApplyPlanningScene, ApplyPlanningSceneRequest, ApplyPlanningSceneResponse
from moveit_msgs.srv import SetPlanningSceneDiff, SetPlanningSceneDiffRequest, SetPlanningSceneDiffResponse
from moveit_msgs.srv import GetMotionPlan, GetMotionPlanRequest, GetMotionPlanResponse
from moveit_msgs.srv import ExecuteKnownTrajectory, ExecuteKnownTrajectoryRequest, ExecuteKnownTrajectoryResponse
from moveit_msgs.srv import GetPlanningScene
from moveit_msgs.srv import GetStateValidity, GetStateValidityRequest, GetStateValidityResponse
from moveit_msgs.srv import GetPositionIK, GetPositionIKRequest, GetPositionIKResponse
from moveit_msgs.msg import PlanningScene, PlanningSceneWorld
from moveit_msgs.msg import RobotState
from moveit_msgs.msg import JointConstraint
from moveit_msgs.msg import Constraints
from moveit_msgs.msg import MotionPlanRequest
from moveit_msgs.msg import MoveItErrorCodes
from moveit_python.planning_scene_interface import PlanningSceneInterface
from moveit_python import MoveGroupInterface
from moveit_msgs.msg import Grasp
from trajectory_msgs.msg import JointTrajectory
from trajectory_msgs.msg import JointTrajectoryPoint
from tf.transformations import quaternion_from_euler
from shape_msgs.msg import SolidPrimitive
from geometry_msgs.msg import PoseStamped
from geometry_msgs.msg import Pose
from geometry_msgs.msg import Quaternion
from std_msgs.msg import Header
from math import pi
import copy
import rospy
# Import the Iterative Parabolic Time Parameterization algorithm
from moveit_msgs.msg import RobotTrajectory
from moveit_msgs.msg import RobotState
from moveit_msgs.msg import Constraints
from moveit_msgs.msg import JointConstraint
from moveit_msgs.msg import PositionConstraint
from moveit_msgs.msg import OrientationConstraint
from moveit_msgs.msg import TrajectoryConstraints
from moveit_msgs.srv import ApplyPlanningScene, ApplyPlanningSceneRequest, ApplyPlanningSceneResponse
from moveit_msgs.srv import SetPlanningSceneDiff, SetPlanningSceneDiffRequest, SetPlanningSceneDiffResponse
from moveit_msgs.srv import GetMotionPlan, GetMotionPlanRequest, GetMotionPlanResponse
from moveit_msgs.srv import ExecuteKnownTrajectory, ExecuteKnownTrajectoryRequest, ExecuteKnownTrajectoryResponse
from moveit_msgs.srv import GetPlanningScene
from moveit_msgs.srv import GetStateValidity, GetStateValidityRequest, GetStateValidityResponse
from moveit_msgs.srv import GetPositionIK, GetPositionIKRequest, GetPositionIKResponse
from moveit_msgs.srv import GetPositionFK, GetPositionFKRequest, GetPositionFKResponse
from moveit_msgs.msg import PlanningScene, PlanningSceneWorld
from moveit_msgs.msg import RobotState
from moveit_msgs.msg import JointConstraint
from moveit_msgs.msg import Constraints
from moveit_msgs.msg import MotionPlanRequest
from moveit_msgs.msg import MoveItErrorCodes
from moveit_python.planning_scene_interface import PlanningSceneInterface
from moveit_python import MoveGroupInterface
from moveit_msgs.msg import Grasp
from trajectory_msgs.msg import JointTrajectory
from trajectory_msgs.msg import JointTrajectoryPoint
from tf.transformations import quaternion_from_euler
from shape_msgs.msg import SolidPrimitive
from geometry_msgs.msg import PoseStamped
from geometry_msgs.msg import Pose
from geometry_msgs.msg import Quaternion
from std_msgs.msg import Header
from math import pi
import copy
import rospy
import time
# Import the Iterative Parabolic Time Parameterization algorithm
from moveit_msgs.msg import RobotTrajectory
from moveit_msgs.msg import RobotState
from moveit_msgs.msg import Constraints
from moveit_msgs.msg import JointConstraint
from moveit_msgs.msg import PositionConstraint
from moveit_msgs.msg import OrientationConstraint
from moveit_msgs.msg import TrajectoryConstraints
from moveit_msgs.srv import ApplyPlanningScene, ApplyPlanningSceneRequest, ApplyPlanningSceneResponse
from moveit_msgs.srv import SetPlanningSceneDiff, SetPlanningSceneDiffRequest, SetPlanningSceneDiffResponse
from moveit_msgs.srv import GetMotionPlan, GetMotionPlanRequest, GetMotionPlanResponse
from moveit_msgs.srv import ExecuteKnownTrajectory, ExecuteKnownTrajectoryRequest, ExecuteKnownTrajectoryResponse
from moveit_msgs.srv import GetPlanningScene
from moveit_msgs.srv import GetStateValidity, GetStateValidityRequest, GetStateValidityResponse
from moveit_msgs.srv import GetPositionIK, GetPositionIKRequest, GetPositionIKResponse
from moveit_msgs.srv import GetPositionFK, GetPositionFKRequest, GetPositionFKResponse
from moveit_msgs.msg import PlanningScene, PlanningSceneWorld
from moveit_msgs.msg import RobotState
from moveit_msgs.msg import JointConstraint
from moveit_msgs.msg import Constraints
from moveit_msgs.msg import MotionPlanRequest
from moveit_msgs.msg import MoveItErrorCodes
from moveit_python.planning_scene_interface import PlanningSceneInterface
from moveit_python import MoveGroupInterface
from moveit_msgs.msg import Grasp
from trajectory_msgs.msg import JointTrajectory
from trajectory_msgs.msg import JointTrajectoryPoint
from tf.transformations import quaternion_from_euler
from shape_msgs.msg import SolidPrimitive
from geometry_msgs.msg import PoseStamped
from geometry_msgs.msg import Pose
from geometry_msgs.msg import Quaternion
from std_msgs.msg import Header
from math import pi
import copy
import rospy
# Import the Iterative Parabolic Time Parameterization algorithm
from moveit_msgs.msg import RobotTrajectory
from moveit_msgs.msg import RobotState
from moveit_msgs.msg import Constraints
from moveit_msgs.msg import JointConstraint
from moveit_msgs.msg import PositionConstraint
from moveit_msgs.msg import OrientationConstraint
from moveit_msgs.msg import TrajectoryConstraints
from moveit_msgs.srv import ApplyPlanningScene, ApplyPlanningSceneRequest, ApplyPlanningSceneResponse
from moveit_msgs.srv import SetPlanningSceneDiff, SetPlanningSceneDiffRequest, SetPlanningSceneDiffResponse
from moveit_msgs.srv import GetMotionPlan, GetMotionPlanRequest, GetMotionPlanResponse
from moveit_msgs.srv import ExecuteKnownTrajectory, ExecuteKnownTrajectoryRequest, ExecuteKnownTrajectoryResponse
from moveit_msgs.srv import GetPlanningScene
from moveit_msgs.srv import GetStateValidity, GetStateValidityRequest, GetStateValidityResponse
from moveit_msgs.srv import GetPositionIK, GetPositionIKRequest, GetPositionIKResponse
from moveit_msgs.srv import GetPositionFK, GetPositionFKRequest, GetPositionFKResponse
from moveit_msgs.msg import PlanningScene, PlanningSceneWorld
from moveit_msgs.msg import RobotState
from moveit_msgs.msg import JointConstraint
from moveit_msgs.msg import Constraints
from moveit_msgs.msg import MotionPlanRequest
from moveit_msgs.msg import MoveItErrorCodes
from moveit_python.planning_scene_interface import PlanningSceneInterface
from moveit_python import MoveGroupInterface
from moveit_msgs.msg import Grasp
from trajectory_msgs.msg import JointTrajectory
from trajectory_msgs.msg import JointTrajectoryPoint
from tf.transformations import quaternion_from_euler
from shape_msgs.msg import SolidPrimitive
from geometry_msgs.msg import PoseStamped
from geometry_msgs.msg import Pose
from geometry_msgs.msg import Quaternion
from std_msgs.msg import Header
from math import pi
import copy
import rospy
# Create a MoveGroupInterface object
move_group = MoveGroupInterface("manipulator", "base_link")
# Set the planner
move_group.setPlannerId("RRTConnectkConfigDefault")
# Set the planning time
move_group.setPlanningTime(5)
# Set the start state
start_state = RobotState()
start_state.joint_state.name = ["joint_1", "joint_2", "joint_3", "joint_4", "joint_5", "joint_6"]
start_state.joint_state.position = [0, 0, 0, 0, 0, 0]
move_group.setStartState(start_state)
# Set the goal state
goal_state = RobotState()
goal_state.joint_state.name = ["joint_1", "joint_2", "joint_3", "joint_4", "joint_5", "joint_6"]
goal_state.joint_state.position = [1.57, -1.57, 1.57, -1.57, 1.57, 0]
move_group.setJointValueTarget(goal_state.joint_state.position)
# Plan the trajectory
trajectory = move_group.plan()
# Create the parabolic time parameterization object
ptp = IterativeParabolicTimeParameterization()
# Compute the time parameterization of the trajectory
ptp.computeTimeStamps(trajectory)
# Convert the trajectory to a RobotTrajectory message
robot_trajectory = RobotTrajectory()
robot_trajectory.joint_trajectory = trajectory.joint_trajectory
# Set the robot trajectory
move_group.setTrajectory(robot_trajectory)
# Execute the trajectory
move_group.execute()
```
该接口函数中的代码主要分为以下几个部分:
1. 导入所需的ROS消息和服务;
2. 创建MoveGroupInterface对象,并设置planner和planning time;
3. 设置起始状态和目标状态,规划轨迹;
4. 创建IterativeParabolicTimeParameterization对象,计算轨迹的时间戳;
5. 将轨迹转换为RobotTrajectory消息,并设置到MoveGroupInterface对象中;
6. 执行轨迹。
其中,步骤4是重规划的核心部分,使用IterativeParabolicTimeParameterization算法对轨迹进行时间戳的计算,以保证机器人在执行轨迹时的运动平滑。
阅读全文