在ROS Noetic版本的MoveIt中,使用Iterative Parabolic Time Parameterization算法对轨迹重规划的python函数
时间: 2024-06-09 17:11:39 浏览: 188
机器人学中轨迹规划算法
3星 · 编辑精心推荐
在ROS Noetic版本的MoveIt中,可以使用以下python函数对轨迹进行重规划,该函数使用Iterative Parabolic Time Parameterization算法:
```
from moveit_msgs.msg import RobotTrajectory
from moveit_msgs.msg import PositionConstraint
from moveit_msgs.msg import Constraints
from trajectory_msgs.msg import JointTrajectoryPoint
from moveit_msgs.msg import RobotState
from moveit_msgs.srv import GetPositionIKRequest
from moveit_msgs.srv import GetPositionIKResponse
from moveit_msgs.srv import GetPositionIK
from urdf_parser_py.urdf import URDF
from pykdl_utils.kdl_kinematics import KDLKinematics
from tf.transformations import quaternion_from_matrix
from tf.transformations import quaternion_multiply
from tf.transformations import translation_matrix
import numpy as np
import rospy
import math
def rescale_velocity(time_old, time_new, trajectory):
'''Rescale the velocities of a given MoveIt trajectory to fit a new time duration.
Args:
time_old (float): The original time duration of the trajectory.
time_new (float): The new time duration to fit the trajectory to.
trajectory (moveit_msgs.msg.RobotTrajectory): The trajectory to rescale.
Returns:
moveit_msgs.msg.RobotTrajectory: The rescaled trajectory.
'''
if time_old == time_new:
return trajectory
scale_factor = time_new / time_old
new_traj = RobotTrajectory()
new_traj.joint_trajectory.header = trajectory.joint_trajectory.header
new_traj.joint_trajectory.joint_names = trajectory.joint_trajectory.joint_names
num_points = len(trajectory.joint_trajectory.points)
for i in range(num_points):
point = JointTrajectoryPoint()
point.positions = trajectory.joint_trajectory.points[i].positions
point.velocities = []
point.accelerations = []
point.time_from_start = trajectory.joint_trajectory.points[i].time_from_start * scale_factor
if i > 0:
for j in range(len(point.positions)):
vel = (point.positions[j] - new_traj.joint_trajectory.points[-1].positions[j]) / (point.time_from_start - new_traj.joint_trajectory.points[-1].time_from_start)
new_traj.joint_trajectory.points[-1].velocities.append(vel)
new_traj.joint_trajectory.points[-1].time_from_start = point.time_from_start - (new_traj.joint_trajectory.points[-1].time_from_start - trajectory.joint_trajectory.points[i-1].time_from_start) * scale_factor
new_traj.joint_trajectory.points.append(point)
return new_traj
def time_parameterization(plan, velocity_scaling_factor):
'''Perform time parameterization using the Iterative Parabolic Time Parameterization algorithm.
Args:
plan (moveit_msgs.msg.RobotTrajectory): The trajectory to perform time parameterization on.
velocity_scaling_factor (float): The velocity scaling factor to apply.
Returns:
moveit_msgs.msg.RobotTrajectory: The time-parameterized trajectory.
'''
num_points = len(plan.joint_trajectory.points)
time_diff = [0]
for i in range(1, num_points):
time_diff.append((plan.joint_trajectory.points[i].time_from_start - plan.joint_trajectory.points[i-1].time_from_start).to_sec())
time_sum = sum(time_diff)
velocity_scaling_factor = min(velocity_scaling_factor, 1.0)
if time_sum <= 0:
return None
time_target = time_sum * velocity_scaling_factor
time_current = time_sum
trajectory = plan
while time_current > time_target:
time_factor = time_target / time_current
new_trajectory = RobotTrajectory()
new_trajectory.joint_trajectory.header = trajectory.joint_trajectory.header
new_trajectory.joint_trajectory.joint_names = trajectory.joint_trajectory.joint_names
new_trajectory.joint_trajectory.points.append(trajectory.joint_trajectory.points[0])
for i in range(1, num_points):
point = JointTrajectoryPoint()
point.positions = trajectory.joint_trajectory.points[i].positions
point.velocities = []
point.accelerations = []
point.time_from_start = trajectory.joint_trajectory.points[i-1].time_from_start + (trajectory.joint_trajectory.points[i].time_from_start - trajectory.joint_trajectory.points[i-1].time_from_start) * time_factor
for j in range(len(point.positions)):
vel = (point.positions[j] - new_trajectory.joint_trajectory.points[-1].positions[j]) / (point.time_from_start - new_trajectory.joint_trajectory.points[-1].time_from_start)
point.velocities.append(vel)
new_trajectory.joint_trajectory.points.append(point)
trajectory = new_trajectory
time_diff = [0]
for i in range(1, len(trajectory.joint_trajectory.points)):
time_diff.append((trajectory.joint_trajectory.points[i].time_from_start - trajectory.joint_trajectory.points[i-1].time_from_start).to_sec())
time_current = sum(time_diff)
return trajectory
def retime_trajectory(robot, group_name, trajectory, velocity_scaling_factor):
'''Retiming the trajectory with the given velocity scaling factor.
Args:
robot (moveit_commander.RobotCommander): The RobotCommander object.
group_name (str): The name of the MoveGroup.
trajectory (moveit_msgs.msg.RobotTrajectory): The trajectory to retiming.
velocity_scaling_factor (float): The velocity scaling factor to apply.
Returns:
moveit_msgs.msg.RobotTrajectory: The retimed trajectory.
'''
if velocity_scaling_factor <= 0:
return None
# Rescale the trajectory velocity to fit the new time duration.
time_old = trajectory.joint_trajectory.points[-1].time_from_start.to_sec()
trajectory = rescale_velocity(time_old, time_old / velocity_scaling_factor, trajectory)
# Perform time parameterization with the Iterative Parabolic Time Parameterization algorithm.
trajectory = time_parameterization(trajectory, velocity_scaling_factor)
return trajectory
```
这个函数可以将给定的轨迹重新计时,以适应给定的速度缩放因子,并使用迭代抛物线时间参数化算法对轨迹进行重新计时。
阅读全文