在工作空间/src目录新建功能包moveit_project
添加依赖rospy moveit_commander control_msgs
新建文件夹scripts
在scripts文件夹中新建
1 2 3 4
| moveit_fk_demo.py moveit_ik_demo.py moveit_line_demo.py moveit_cycle_demo.py
|
然后在scripts中打开终端
添加权限sudo chmod +x *.py
更改CMakeList.txt
1 2 3 4 5 6 7
| catkin_install_python(PROGRAMS scripts/moveit_fk_demo.py scripts/moveit_ik_demo.py scripts/moveit_line_demo.py scripts/moveit_cycle_demo.py DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} )
|
根据关节角度控制机械臂
moveit_fk_demo.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
import rospy, sys import moveit_commander from control_msgs.msg import GripperCommand
class MoveItFkDemo: def __init__(self): moveit_commander.roscpp_initialize(sys.argv)
rospy.init_node('moveit_fk_demo', anonymous=True) arm = moveit_commander.MoveGroupCommander('arm_group') gripper = moveit_commander.MoveGroupCommander('hand_group') arm.set_goal_joint_tolerance(0.001) gripper.set_goal_joint_tolerance(0.001) arm.set_named_target('zero_pose') arm.go() rospy.sleep(2) joint_positions = [3.14, 0.90, 0.84, 0, 1.59, 1.52] arm.set_joint_value_target(joint_positions) arm.go() rospy.sleep(1) gripper.set_joint_value_target([0.03, -0.03]) gripper.go() rospy.sleep(1) moveit_commander.roscpp_shutdown() moveit_commander.os._exit(0)
if __name__ == "__main__": try: MoveItFkDemo() except rospy.ROSInterruptException: pass
|
根据末端位姿控制机械臂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
import rospy, sys import moveit_commander from moveit_msgs.msg import RobotTrajectory from trajectory_msgs.msg import JointTrajectoryPoint
from geometry_msgs.msg import PoseStamped, Pose from tf.transformations import euler_from_quaternion, quaternion_from_euler
class MoveItIkDemo: def __init__(self): moveit_commander.roscpp_initialize(sys.argv)
rospy.init_node('moveit_ik_demo') arm = moveit_commander.MoveGroupCommander('arm_group') end_effector = arm.get_end_effector_link() reference_frame = "base_link" 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.set_named_target("zero_pose") arm.go() rospy.sleep(2) target_pose = PoseStamped() target_pose.header.frame_id = reference_frame target_pose.header.stamp = rospy.Time.now() target_pose.pose.position.x = 0.5 target_pose.pose.position.y = 0.5 target_pose.pose.position.z = 0.5 target_pose.pose.orientation.x = 0.911822 target_pose.pose.orientation.y = -0.0269758 target_pose.pose.orientation.z = 0.285694 target_pose.pose.orientation.w = -0.293653 arm.set_start_state_to_current_state() arm.set_pose_target(target_pose, end_effector) plan_success,traj,planning_time,error_code=arm.plan() arm.execute(traj) rospy.sleep(1) arm.shift_pose_target(1, -0.05, end_effector) arm.go() rospy.sleep(1) arm.shift_pose_target(3, -1.57, end_effector) arm.go() rospy.sleep(1) arm.set_named_target('zero_pose') arm.go()
moveit_commander.roscpp_shutdown() moveit_commander.os._exit(0)
if __name__ == "__main__": try: MoveItIkDemo() except rospy.ROSInterruptException: pass
|
画正方形
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
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): moveit_commander.roscpp_initialize(sys.argv)
rospy.init_node('moveit_line_demo', anonymous=True) arm = MoveGroupCommander('arm_group') 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) end_effector_link = arm.get_end_effector_link()
arm.set_named_target('zero_pose') arm.go() rospy.sleep(1) start_pose = arm.get_current_pose(end_effector_link).pose waypoints = []
wpose = deepcopy(start_pose) wpose.position.x += 0.20 waypoints.append(deepcopy(wpose)) wpose.position.z -= 0.2 waypoints.append(deepcopy(wpose)) wpose.position.x += 0.20 waypoints.append(deepcopy(wpose)) wpose.position.y += 0.20 waypoints.append(deepcopy(wpose)) wpose.position.x -= 0.20 waypoints.append(deepcopy(wpose)) wpose.position.y -= 0.20 waypoints.append(deepcopy(wpose)) wpose.position.z += 0.2 waypoints.append(deepcopy(wpose)) wpose.position.x -= 0.20 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, 0.01, 0.0, True) attempts += 1 if attempts % 10 == 0: rospy.loginfo("Still trying after " + str(attempts) + " attempts...") 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('zero_pose') arm.go() rospy.sleep(2) moveit_commander.roscpp_shutdown() moveit_commander.os._exit(0)
if __name__ == "__main__": try: MoveItCartesianDemo() except rospy.ROSInterruptException: pass
|

画圆形
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
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): moveit_commander.roscpp_initialize(sys.argv)
rospy.init_node('moveit_cycle_demo', anonymous=True)
arm = MoveGroupCommander('arm_group') 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) end_effector_link = arm.get_end_effector_link()
arm.set_named_target('zero_pose') 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.5 target_pose.pose.position.y = 0 target_pose.pose.position.z = 0.5 target_pose.pose.orientation.x = 0.707 target_pose.pose.orientation.y = 0.7071 target_pose.pose.orientation.z = 0 target_pose.pose.orientation.w = 0 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 + 1.5*radius * math.cos(th) target_pose.pose.position.z = centerB + 1.5*radius * math.sin(th) wpose = deepcopy(target_pose.pose) waypoints.append(deepcopy(wpose)) wpose.position.z += 0.2 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, 0.01, 0.0, True) attempts += 1 if attempts % 10 == 0: rospy.loginfo("Still trying after " + str(attempts) + " attempts...") 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('zero_pose') arm.go() rospy.sleep(1) moveit_commander.roscpp_shutdown() moveit_commander.os._exit(0)
if __name__ == "__main__": try: MoveItCartesianDemo() except rospy.ROSInterruptException: pass
|

还可以根据自己的喜好,让机械臂在笛卡尔空间作不同的运动。
