5 import moveit_commander
9 from geometry_msgs.msg
import Point, Pose
10 from gazebo_msgs.msg
import ModelStates
11 from control_msgs.msg
import GripperCommandAction, GripperCommandGoal
12 from tf.transformations
import quaternion_from_euler, euler_from_quaternion
14 gazebo_model_states = ModelStates()
17 global gazebo_model_states
18 gazebo_model_states = msg
23 euler = euler_from_quaternion(
24 (object_orientation.x, object_orientation.y,
25 object_orientation.z, object_orientation.w))
31 global gazebo_model_states
33 OBJECT_NAME =
"wood_cube_5cm" 42 Point(0.0, -0.3, 0.0),
44 Point(0.2, -0.2, 0.0)]
46 sub_model_states = rospy.Subscriber(
"gazebo/model_states", ModelStates, callback, queue_size=1)
48 arm = moveit_commander.MoveGroupCommander(
"arm")
49 arm.set_max_velocity_scaling_factor(0.4)
50 arm.set_max_acceleration_scaling_factor(1.0)
52 gripper.wait_for_server()
53 gripper_goal = GripperCommandGoal()
54 gripper_goal.command.max_effort = 4.0
60 gripper_goal.command.position = GRIPPER_OPEN
61 gripper.send_goal(gripper_goal)
62 gripper.wait_for_result(rospy.Duration(1.0))
65 arm.set_named_target(
"home")
72 print(
"Wait " + str(sleep_time) +
" secs.")
73 rospy.sleep(sleep_time)
77 if OBJECT_NAME
in gazebo_model_states.name:
78 object_index = gazebo_model_states.name.index(OBJECT_NAME)
80 object_position = gazebo_model_states.pose[object_index].position
81 object_orientation = gazebo_model_states.pose[object_index].orientation
82 object_yaw =
yaw_of(object_orientation)
86 target_pose.position.x = object_position.x
87 target_pose.position.y = object_position.y
88 target_pose.position.z = APPROACH_Z
89 q = quaternion_from_euler(-math.pi, 0.0, object_yaw)
90 target_pose.orientation.x = q[0]
91 target_pose.orientation.y = q[1]
92 target_pose.orientation.z = q[2]
93 target_pose.orientation.w = q[3]
94 arm.set_pose_target(target_pose)
96 print(
"Failed to approach an object.")
101 target_pose.position.z = PICK_Z
102 arm.set_pose_target(target_pose)
103 if arm.go()
is False:
104 print(
"Failed to grip an object.")
107 gripper_goal.command.position = GRIPPER_CLOSE
108 gripper.send_goal(gripper_goal)
109 gripper.wait_for_result(rospy.Duration(1.0))
112 target_pose.position.z = LEAVE_Z
113 arm.set_pose_target(target_pose)
114 if arm.go()
is False:
115 print(
"Failed to pick up an object.")
120 place_position = random.choice(PLACE_POSITIONS)
121 target_pose.position.x = place_position.x
122 target_pose.position.y = place_position.y
123 q = quaternion_from_euler(-math.pi, 0.0, -math.pi/2.0)
124 target_pose.orientation.x = q[0]
125 target_pose.orientation.y = q[1]
126 target_pose.orientation.z = q[2]
127 target_pose.orientation.w = q[3]
128 arm.set_pose_target(target_pose)
129 if arm.go()
is False:
130 print(
"Failed to approach target position.")
135 target_pose.position.z = PICK_Z
136 arm.set_pose_target(target_pose)
137 if arm.go()
is False:
138 print(
"Failed to place an object.")
141 gripper_goal.command.position = GRIPPER_OPEN
142 gripper.send_goal(gripper_goal)
143 gripper.wait_for_result(rospy.Duration(1.0))
146 target_pose.position.z = LEAVE_Z
147 arm.set_pose_target(target_pose)
148 if arm.go()
is False:
149 print(
"Failed to leave from an object.")
154 arm.set_named_target(
"home")
155 if arm.go()
is False:
156 print(
"Failed to go back to home pose.")
166 if __name__ ==
'__main__':
167 rospy.init_node(
"pick_and_place_in_gazebo_example")
170 if not rospy.is_shutdown():
172 except rospy.ROSInterruptException:
def yaw_of(object_orientation)