ROS moveit 机械臂笛卡尔空间运动

机械臂moveit编程(python)

机械臂在笛卡尔空间的运动只能走点到点的直线运动,通过将位姿点加入waypoints中,机械臂会一次按照waypoints中的唯一依次沿直线运动到下一个点。

程序流程:
1.初始化需要控制的规划组;
2.设置运动约束(可选);
3.设置终端link;
4.设置坐标系;
5.建立一个waypoints空列表,将所需要到达的位姿加入waypoints列表;
6.用compute_catesian_path AP进行求解。

笛卡尔空间画折线

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import rospy, sys
import moveit_commander
from moveit_commander import MoveGroupCommander
from geometry_msgs.msg import Pose
from copy import deepcopy

class MoveItCartesianDemo:
    def __init__(self):
        # 初始化move_group的API
        moveit_commander.roscpp_initialize(sys.argv)

        # 初始化ROS节点
        rospy.init_node('moveit_cartesian_demo', anonymous=True)
      
        # 初始化需要使用move group控制的机械臂中的arm group
        arm = MoveGroupCommander('arm')
        
        # 当运动规划失败后,允许重新规划
        arm.allow_replanning(True)
        
        # 设置目标位置所使用的参考坐标系
        arm.set_pose_reference_frame('base_link')
                
        # 设置位置(单位:米)和姿态(单位:弧度)的允许误差
        arm.set_goal_position_tolerance(0.001)
        arm.set_goal_orientation_tolerance(0.001)
        
        # 设置允许的最大速度和加速度
        arm.set_max_acceleration_scaling_factor(0.5)
        arm.set_max_velocity_scaling_factor(0.5)
        
        # 获取终端link的名称
        end_effector_link = arm.get_end_effector_link()

        # 控制机械臂先回到初始化位置
        arm.set_named_target('home')
        arm.go()
        rospy.sleep(1)
                                               
        # 获取当前位姿数据最为机械臂运动的起始位姿
        start_pose = arm.get_current_pose(end_effector_link).pose
                
        # 初始化路点列表
        waypoints = []

        # 将初始位姿加入路点列表
     
        waypoints.append(start_pose)
            
        # 设置路点数据,并加入路点列表
        wpose = deepcopy(start_pose)
        wpose.position.z -= 0.2
        waypoints.append(deepcopy(wpose))
        

        wpose.position.x += 0.1
        waypoints.append(deepcopy(wpose))
       
        
        wpose.position.y += 0.075
        waypoints.append(deepcopy(wpose))

		wpose.position.y -= 0.075
        waypoints.append(deepcopy(wpose))

		wpose.position.x -= 0.1
        waypoints.append(deepcopy(wpose))

		wpose.position.x += 0.05
        wpose.position.y += 0.075
        waypoints.append(deepcopy(wpose))

		wpose.position.y -= 0.15
        waypoints.append(deepcopy(wpose))


		wpose.position.x -= 0.05
        wpose.position.y += 0.075
        waypoints.append(deepcopy(wpose))

        
        wpose.position.x -= 0.03
        wpose.position.y -= 0.075
        waypoints.append(deepcopy(wpose))
       
		fraction = 0.0   #路径规划覆盖率
		maxtries = 100   #最大尝试规划次数
		attempts = 0     #已经尝试规划次数
		
		# 设置机器臂当前的状态作为运动初始状态
		arm.set_start_state_to_current_state()
	 
		# 尝试规划一条笛卡尔空间下的路径,依次通过所有路点
		while fraction < 1.0 and attempts < maxtries:
	    	(plan, fraction) = arm.compute_cartesian_path (
		                    waypoints,   # waypoint poses,路点列表
		                    0.01,        # eef_step,终端步进值
		                    0.0,         # jump_threshold,跳跃阈值
		                    True)        # avoid_collisions,避障规划
		    
		    # 尝试次数累加
	    	attempts += 1
		    
		    # 打印运动规划进程
	   	 if attempts % 10 == 0:
		   	 rospy.loginfo("Still trying after " + str(attempts) + " attempts...")
		             
			# 如果路径规划成功(覆盖率100%),则开始控制机械臂运动
	  	 if fraction == 1.0:
			rospy.loginfo("Path computed successfully. Moving the arm.")
			arm.execute(plan)
			rospy.loginfo("Path execution complete.")
		# 如果路径规划失败,则打印失败信息
	   	 else:
			rospy.loginfo("Path planning failed with only " + str(fraction) + " success after " + str(maxtries) + " attempts.")  

			rospy.sleep(1)

        # 控制机械臂先回到初始化位置
        arm.set_named_target('home')
        arm.go()
        rospy.sleep(1)
        
        # 关闭并退出moveit
        moveit_commander.roscpp_shutdown()
        moveit_commander.os._exit(0)

if __name__ == "__main__":
    try:
        MoveItCartesianDemo()
    except rospy.ROSInterruptException:
        pass

重点API整理

第一步初始化:与上一篇初始化一样。

moveit_commander.roscpp_initialize(sys.argv)
rospy.init_node('moveit_cartesian_demo', anonymous=True)
arm = MoveGroupCommander('arm')

第二步:获取机械臂的终端link及设置坐标系。

arm.set_pose_reference_frame('base_link')
end_effector_link = arm.get_end_effector_link()

第三步:将机械臂回到home位姿,当前位姿数据最为机械臂运动的起始位姿,然后设置机器臂当前的状态作为运动初始状态。

arm.set_named_target('home')
arm.go()
rospy.sleep(1)
start_pose = arm.get_current_pose(end_effector_link).pose
arm.set_start_state_to_current_state()

第四步:建立waypoints空列表,先将起始位姿加入列表中,然后依次加入其他位姿,位姿可以自己根据需要随便定义,依次将所定义的位姿加入waypoints中,此时并没有开始规划。

 waypoints = []
 waypoints.append(start_pose)

 wpose = deepcopy(start_pose)
 wpose.position.z -= 0.2
 waypoints.append(deepcopy(wpose))     

 wpose.position.x += 0.1
 waypoints.append(deepcopy(wpose))    
        
 wpose.position.y += 0.075
 waypoints.append(deepcopy(wpose))

 wpose.position.y -= 0.075
 waypoints.append(deepcopy(wpose))

 wpose.position.x -= 0.1
 waypoints.append(deepcopy(wpose))

 wpose.position.x += 0.05
 wpose.position.y += 0.075
 waypoints.append(deepcopy(wpose))

 wpose.position.y -= 0.15
 waypoints.append(deepcopy(wpose))

 wpose.position.x -= 0.05
 wpose.position.y += 0.075
 waypoints.append(deepcopy(wpose))
        
 wpose.position.x -= 0.03
 wpose.position.y -= 0.075
 waypoints.append(deepcopy(wpose))

第五步:这一步是整个笛卡尔运动规划的核心,使用笛卡尔路径规划API compute_cartesian_path(),其输入参数有四个:
第一个参数为之前创建的路径点列表;
第二个参数为终端步进值,即每一步规划的步长;
第三个参数为跳跃阈值,每一步规划时,有无法达到的点是否跳过;
第四个参数用于运动过程中是否考虑避障。
compute_cartesian_path()执行后会有两个返回值:
plan表示规划出来的运动路径;
fraction表示规划路径的覆盖率,范围在0到1之间,fraction为1表示路径规划成功。

fraction = 0.0   #路径规划覆盖率
maxtries = 100   #最大尝试规划次数
attempts = 0     #已经尝试规划次数

while fraction < 1.0 and attempts < maxtries:
	    (plan, fraction) = arm.compute_cartesian_path (
		                    waypoints,   # waypoint poses,路点列表
		                    0.01,        # eef_step,终端步进值
		                    0.0,         # jump_threshold,跳跃阈值
		                    True)        # avoid_collisions,避障规划
		    
		    # 尝试次数累加
	    	attempts += 1
		    
		    # 打印运动规划进程
	   	 if attempts % 10 == 0:
		   	 rospy.loginfo("Still trying after " + str(attempts) + " attempts...")
		             
			# 如果路径规划成功(覆盖率100%),则开始控制机械臂运动
	  	 if fraction == 1.0:
			rospy.loginfo("Path computed successfully. Moving the arm.")
			arm.execute(plan)
			rospy.loginfo("Path execution complete.")
		# 如果路径规划失败,则打印失败信息
	   	 else:
			rospy.loginfo("Path planning failed with only " + str(fraction) + " success after " + str(maxtries) + " attempts.")  

该程序运行结果如下图,运行轨迹为在一个平面上写了一个王字。
在这里插入图片描述

笛卡尔空间画圆弧

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import rospy, sys
import moveit_commander
from moveit_commander import MoveGroupCommander
from geometry_msgs.msg import Pose, PoseStamped
from copy import deepcopy

import math
import numpy

class MoveItCartesianDemo:
    def __init__(self):
        # 初始化move_group的API
        moveit_commander.roscpp_initialize(sys.argv)

        # 初始化ROS节点
        rospy.init_node('moveit_cartesian_demo', anonymous=True)

      
                      
        # 初始化需要使用move group控制的机械臂中的arm group
        arm = MoveGroupCommander('arm')
        
        # 当运动规划失败后,允许重新规划
        arm.allow_replanning(True)
        
        # 设置目标位置所使用的参考坐标系
		reference_frame = 'base_link'
        arm.set_pose_reference_frame('base_link')
                
        # 设置位置(单位:米)和姿态(单位:弧度)的允许误差
        arm.set_goal_position_tolerance(0.001)
        arm.set_goal_orientation_tolerance(0.001)
        
        # 设置允许的最大速度和加速度
        arm.set_max_acceleration_scaling_factor(0.5)
        arm.set_max_velocity_scaling_factor(0.5)
        
        # 获取终端link的名称
        end_effector_link = arm.get_end_effector_link()

        # 控制机械臂先回到初始化位置
        arm.set_named_target('home')
        arm.go()
        rospy.sleep(1)
        
        # 设置路点数据,并加入路点列表
                                         
        target_pose = PoseStamped()
        target_pose.header.frame_id = reference_frame
        target_pose.header.stamp = rospy.Time.now()     
        target_pose.pose.position.x = 0.331958
        target_pose.pose.position.y = 0.0
        target_pose.pose.position.z = 0.307887
        target_pose.pose.orientation.x = -0.482974
        target_pose.pose.orientation.y = 0.517043
        target_pose.pose.orientation.z = -0.504953
        target_pose.pose.orientation.w = -0.494393
        
        # 设置机械臂终端运动的目标位姿
        arm.set_pose_target(target_pose, end_effector_link)
        arm.go()

        # 初始化路点列表
        waypoints = []
                
        # 将圆弧上的路径点加入列表
        waypoints.append(target_pose.pose)

        centerA = target_pose.pose.position.y
        centerB = target_pose.pose.position.z
        radius = 0.1

        for th in numpy.arange(0, 6.28, 0.02):
            target_pose.pose.position.y = centerA + 0.5*radius * math.cos(th)
            target_pose.pose.position.z = centerB + 1.2*radius * math.sin(th)
            wpose = deepcopy(target_pose.pose)
            waypoints.append(deepcopy(wpose))

       
	fraction = 0.0   #路径规划覆盖率
	maxtries = 100   #最大尝试规划次数
	attempts = 0     #已经尝试规划次数
		
	# 设置机器臂当前的状态作为运动初始状态
	arm.set_start_state_to_current_state()
	 
	# 尝试规划一条笛卡尔空间下的路径,依次通过所有路点
	while fraction < 1.0 and attempts < maxtries:
	    (plan, fraction) = arm.compute_cartesian_path (
		                    waypoints,   # waypoint poses,路点列表
		                    0.01,        # eef_step,终端步进值
		                    0.0,         # jump_threshold,跳跃阈值
		                    True)        # avoid_collisions,避障规划
		    
		    # 尝试次数累加
	    attempts += 1
		    
		    # 打印运动规划进程
	    if attempts % 10 == 0:
		    rospy.loginfo("Still trying after " + str(attempts) + " attempts...")
		             
		# 如果路径规划成功(覆盖率100%),则开始控制机械臂运动
	    if fraction == 1.0:
		rospy.loginfo("Path computed successfully. Moving the arm.")
		arm.execute(plan)
		rospy.loginfo("Path execution complete.")
		# 如果路径规划失败,则打印失败信息
	    else:
		rospy.loginfo("Path planning failed with only " + str(fraction) + " success after " + str(maxtries) + " attempts.")  

		rospy.sleep(1)

        # 控制机械臂先回到初始化位置
        arm.set_named_target('home')
        arm.go()
        rospy.sleep(1)
        
        # 关闭并退出moveit
        moveit_commander.roscpp_shutdown()
        moveit_commander.os._exit(0)

if __name__ == "__main__":
    try:
        MoveItCartesianDemo()
    except rospy.ROSInterruptException:
        pass

编程思想与上面画折线完全一致,唯一的区别在与waypoints列表,将圆表示为参数方程。

waypoints = []
 # 将圆弧上的路径点加入列表
waypoints.append(target_pose.pose)
centerA = target_pose.pose.position.y
centerB = target_pose.pose.position.z
radius = 0.1

for th in numpy.arange(0, 6.28, 0.02):
        target_pose.pose.position.y = centerA + radius * math.cos(th)
        target_pose.pose.position.z = centerB + radius * math.sin(th)
        wpose = deepcopy(target_pose.pose)
        waypoints.append(deepcopy(wpose))

运行结果如下:在这里插入图片描述
同理,只要知道曲线的参数方程,就可以在笛卡尔空间中画曲线,例如画椭圆。
在这里插入图片描述在这里插入图片描述

  • 12
    点赞
  • 97
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
MoveIt编程中的笛卡尔空间机械臂运动可以通过以下步骤实现: 1. 设置机械臂的终端link和坐标系。使用`arm.set_pose_reference_frame('base_link')`来设置机械臂的坐标系,并使用`end_effector_link = arm.get_end_effector_link()`获取机械臂的终端link。\[2\] 2. 将机械臂回到home位姿,并设置当前位姿数据作为机械臂运动的起始位姿。使用`arm.set_named_target('home')`将机械臂设置为home位姿,然后使用`arm.go()`使机械臂运动到该位姿。接着使用`start_pose = arm.get_current_pose(end_effector_link).pose`获取当前位姿数据,并使用`arm.set_start_state_to_current_state()`将当前状态设置为运动的初始状态。\[1\] 3. 在MoveIt运动规划过程中,需要使用插值后的每个轨迹点上的目标末端执行器姿态信息。这是因为在运动学逆解时,需要通过目标位姿来计算关节角度。在MoveIt中,通常先通过路径规划算法生成离散的轨迹点,再对这些点进行插值,得到更加平滑连续的轨迹。每个插值后的轨迹点都表示机器人的末端执行器姿态,需要通过逆解算法来求解机器人达到该姿态所需的关节状态,从而实现机器人的精确定位和运动控制。\[3\] 通过以上步骤,可以在MoveIt中实现笛卡尔空间机械臂运动。 #### 引用[.reference_title] - *1* *2* [ROS moveit 机械臂笛卡尔空间运动](https://blog.csdn.net/sinat_38625360/article/details/103042802)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [moveit是如何控制机械臂运动的](https://blog.csdn.net/qhwd123/article/details/130143767)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值