MyCobot 280 M5 ROS moveit 抓取物体

官方给的moveit例程是基于rviz手动拖动模型来生成目标点的,这里提供了一种通过脚本参数来生成目标点的方式,代码如下:

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


class MoveItIkDemo:
    def __init__(self):

        # 初始化move_group的API
        moveit_commander.roscpp_initialize(sys.argv)
        
        # 初始化ROS节点
        rospy.init_node('moveit_ik_demo')
                
        # 初始化需要使用move group控制的机械臂中的arm group
        arm = moveit_commander.MoveGroupCommander('arm_group')

        arm.set_end_effector_link('joint6_flange')
                
        # 获取终端link的名称,这个在setup assistant中设置过了
        end_effector_link = arm.get_end_effector_link()
                        
        # 设置目标位置所使用的参考坐标系
        # reference_frame = 'base_link'
        reference_frame = 'joint1'
        arm.set_pose_reference_frame(reference_frame)
                
        # 当运动规划失败后,允许重新规划
        arm.allow_replanning(True)
        
        # 设置位置(单位:米)和姿态(单位:弧度)的允许误差
        arm.set_goal_position_tolerance(0.001)
        arm.set_goal_orientation_tolerance(0.01)
       
        # 设置允许的最大速度和加速度
        arm.set_max_acceleration_scaling_factor(0.5)
        arm.set_max_velocity_scaling_factor(0.5)

        arm.remember_joint_values('home',[0,0,0,0,0,0])
 
        # 控制机械臂先回到初始化位置
        mc.set_gripper_state(0, 70)
        rospy.sleep(3)
        arm.set_named_target('home')
        arm.go()
        rospy.sleep(1)
               
        # 设置机械臂工作空间中的目标位姿,位置使用x、y、z坐标描述,
        # 姿态使用四元数描述,基于base_link坐标系
        target_pose = PoseStamped()
        #参考坐标系,前面设置了
        target_pose.header.frame_id = reference_frame
        target_pose.header.stamp = rospy.Time.now() #时间戳?
        #末端位置   
        target_pose.pose.position.x = 0.184892
        target_pose.pose.position.y = 0.0788132
        target_pose.pose.position.z = 0.1199491
        #末端姿态,四元数
        target_pose.pose.orientation.x = 0.202164
        target_pose.pose.orientation.y = 0.662784
        target_pose.pose.orientation.z = 0.652578
        target_pose.pose.orientation.w = -0.306577
        
        # 设置机器臂当前的状态作为运动初始状态
        arm.set_start_state_to_current_state()
        
        # 设置机械臂终端运动的目标位姿
        arm.set_pose_target(target_pose, end_effector_link)
        
        # 规划运动路径,返回虚影的效果
        plan_success, traj, planning_time, error_code = arm.plan()
        
        # 按照规划的运动路径控制机械臂运动
        arm.execute(traj)

        waypoints = []
        start_pose = arm.get_current_pose(end_effector_link).pose
        waypoints.append(start_pose)
        wpose = deepcopy(start_pose)#拷贝对象
        wpose.position.z -= 0.065
        waypoints.append(wpose)

        # 设置机器臂当前的状态作为运动初始状态
        arm.set_start_state_to_current_state()
        
        fraction = 0.0
        maxtries = 100   #最大尝试规划次数
        attempts = 0

        while fraction < 1.0 and attempts < maxtries:
        #规划路径 ,fraction返回1代表规划成功
            (plan, fraction) = arm.compute_cartesian_path (
                                    waypoints,   # waypoint poses,路点列表,这里是5个点
                                    0.01,        # eef_step,终端步进值,每隔0.01m计算一次逆解判断能否可达
                                    0.0,         # jump_threshold,跳跃阈值,设置为0代表不允许跳跃
                                    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)  #执行完成后休息1s
        mc.set_gripper_state(1, 70)
        rospy.sleep(3)

 
        # 控制机械臂回到初始化位置
        arm.set_named_target('home')
        arm.go()
        rospy.sleep(3)
 
        # 关闭并退出moveit
        moveit_commander.roscpp_shutdown()
        moveit_commander.os._exit(0)
 
if __name__ == "__main__":
    port = rospy.get_param("~port", "/dev/ttyACM0")
    baud = rospy.get_param("~baud", 115200)
    mc = MyCobot(port, baud)
    MoveItIkDemo()
    rospy.spin()

目标点的位置和姿态是在这里设置的:

#末端位置   
target_pose.pose.position.x = 0.184892
target_pose.pose.position.y = 0.0788132
target_pose.pose.position.z = 0.1199491
#末端姿态,四元数
target_pose.pose.orientation.x = 0.202164
target_pose.pose.orientation.y = 0.662784
target_pose.pose.orientation.z = 0.652578
target_pose.pose.orientation.w = -0.306577

脚本使用方式:

1.运行mycobot官方的moveit启动文件

roslaunch mycobot_280_moveit mycobot_moveit.launch 

2.运行sync_plan脚本,让机械臂能够跟随规划的轨迹

rosrun mycobot_280_moveit sync_plan.py

3.运行上面的脚本

rosrun mycobot_280_moveit moveit_ik_computation.py

由于是自己添加的脚本,需要在CmakeList.txt中做以下修改,并catkin_make一下

catkin_install_python(PROGRAMS
  scripts/sync_plan.py
  scripts/path_planning_and_obstacle_avoidance_demo.py
  scripts/moveit_ik_computation.py
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

整个过程分为四个步骤:

1.初始化(机械臂和夹爪回零位);

2.机械臂夹爪移动到目标上方;

3.机械臂夹爪向下移动并抓取目标;

4.回到初始位置。

视频服务器有问题,暂时没法上传视频,等修复了再上传视频。

这是一个比较复杂的问题,需要详细的说明和代码实现,以下是一个大致的思路和步骤: 1. 确定机器人的抓取方式和机械臂的动作,比如使用夹爪或者吸盘等方式,并且确定机械臂的运动轨迹和动作顺序。 2. 使用ROS中的机械臂控制库,比如MoveIt,来进行机械臂的运动规划和控制。 3. 使用机器人视觉库,比如OpenCV,来进行物体检测和定位。 4. 将物体检测和定位的结果传递给机械臂控制程序,使得机械臂能够准确地抓取物体。 5. 进行抓取后,将物体移动到指定的位置,并且将机械臂归位。 以下是一个简单的Python代码示例: ```python import rospy from moveit_msgs.msg import MoveItErrorCodes from moveit_python import MoveGroupInterface, PlanningSceneInterface import cv2 import numpy as np # 初始化ROS节点 rospy.init_node('grasping_node') # 初始化机械臂控制接口 move_group = MoveGroupInterface("manipulator", "base_link") planning_scene = PlanningSceneInterface("base_link") # 定义夹爪的开合程度 GRIPPER_OPEN = [0.02, 0.02] GRIPPER_CLOSE = [0.0, 0.0] # 定义物体检测的函数 def detect_object(): # 使用OpenCV进行物体检测和定位 ... # 定义机械臂移动的函数 def move_arm(position, orientation): # 设置机械臂的目标位置和姿态 pose_goal = GeometryPose() pose_goal.position.x = position[0] pose_goal.position.y = position[1] pose_goal.position.z = position[2] pose_goal.orientation.x = orientation[0] pose_goal.orientation.y = orientation[1] pose_goal.orientation.z = orientation[2] pose_goal.orientation.w = orientation[3] # 进行机械臂运动规划和控制 move_group.set_pose_target(pose_goal) result = move_group.go(wait=True) # 判断机械臂运动是否成功 if result == MoveItErrorCodes.SUCCESS: return True else: return False # 定义机械臂抓取的函数 def grasp_object(): # 检测物体并获取物体的位置和姿态 position, orientation = detect_object() # 将物体移动到抓取位置 move_arm(position, orientation) # 打开夹爪 move_group.set_joint_value_target(GRIPPER_OPEN) move_group.go() # 将夹爪闭合并抓取物体 move_group.set_joint_value_target(GRIPPER_CLOSE) move_group.go() # 将物体移动到指定位置 move_arm([0.5, 0.0, 0.1], [0.0, 0.0, 0.0, 1.0]) # 归位 move_arm([0.0, 0.0, 0.1], [0.0, 0.0, 0.0, 1.0]) # 进行机械臂抓取 grasp_object() # 关闭ROS节点 rospy.shutdown() ``` 需要注意的是,以上代码仅作为一个简单示例,实际情况中还需要根据具体的机器人和物体进行相应的修改和调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值