pick_and_place_in_gazebo_example.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 import rospy
5 import moveit_commander
6 import actionlib
7 import math
8 import random
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
13 
14 gazebo_model_states = ModelStates()
15 
16 def callback(msg):
17  global gazebo_model_states
18  gazebo_model_states = msg
19 
20 
21 def yaw_of(object_orientation):
22  # クォータニオンをオイラー角に変換しyaw角度を返す
23  euler = euler_from_quaternion(
24  (object_orientation.x, object_orientation.y,
25  object_orientation.z, object_orientation.w))
26 
27  return euler[2]
28 
29 
30 def main():
31  global gazebo_model_states
32 
33  OBJECT_NAME = "wood_cube_5cm" # 掴むオブジェクトの名前
34  GRIPPER_OPEN = 1.2 # 掴む時のハンド開閉角度
35  GRIPPER_CLOSE = 0.42 # 設置時のハンド開閉角度
36  APPROACH_Z = 0.15 # 接近時のハンドの高さ
37  LEAVE_Z = 0.20 # 離れる時のハンドの高さ
38  PICK_Z = 0.12 # 掴む時のハンドの高さ
39  PLACE_POSITIONS = [ # オブジェクトの設置位置 (ランダムに設置する)
40  Point(0.4, 0.0, 0.0),
41  Point(0.0, 0.3, 0.0),
42  Point(0.0, -0.3, 0.0),
43  Point(0.2, 0.2, 0.0),
44  Point(0.2, -0.2, 0.0)]
45 
46  sub_model_states = rospy.Subscriber("gazebo/model_states", ModelStates, callback, queue_size=1)
47 
48  arm = moveit_commander.MoveGroupCommander("arm")
49  arm.set_max_velocity_scaling_factor(0.4)
50  arm.set_max_acceleration_scaling_factor(1.0)
51  gripper = actionlib.SimpleActionClient("crane_x7/gripper_controller/gripper_cmd", GripperCommandAction)
52  gripper.wait_for_server()
53  gripper_goal = GripperCommandGoal()
54  gripper_goal.command.max_effort = 4.0
55 
56  rospy.sleep(1.0)
57 
58  while True:
59  # 何かを掴んでいた時のためにハンドを開く
60  gripper_goal.command.position = GRIPPER_OPEN
61  gripper.send_goal(gripper_goal)
62  gripper.wait_for_result(rospy.Duration(1.0))
63 
64  # SRDFに定義されている"home"の姿勢にする
65  arm.set_named_target("home")
66  arm.go()
67  rospy.sleep(1.0)
68 
69  # 一定時間待機する
70  # この間に、ユーザがgazebo上のオブジェクト姿勢を変更しても良い
71  sleep_time = 3.0
72  print("Wait " + str(sleep_time) + " secs.")
73  rospy.sleep(sleep_time)
74  print("Start")
75 
76  # オブジェクトがgazebo上に存在すれば、pick_and_placeを実行する
77  if OBJECT_NAME in gazebo_model_states.name:
78  object_index = gazebo_model_states.name.index(OBJECT_NAME)
79  # オブジェクトの姿勢を取得
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)
83 
84  # オブジェクトに接近する
85  target_pose = Pose()
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)
95  if arm.go() is False:
96  print("Failed to approach an object.")
97  continue
98  rospy.sleep(1.0)
99 
100  # 掴みに行く
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.")
105  continue
106  rospy.sleep(1.0)
107  gripper_goal.command.position = GRIPPER_CLOSE
108  gripper.send_goal(gripper_goal)
109  gripper.wait_for_result(rospy.Duration(1.0))
110 
111  # 持ち上げる
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.")
116  continue
117  rospy.sleep(1.0)
118 
119  # 設置位置に移動する
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.")
131  continue
132  rospy.sleep(1.0)
133 
134  # 設置する
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.")
139  continue
140  rospy.sleep(1.0)
141  gripper_goal.command.position = GRIPPER_OPEN
142  gripper.send_goal(gripper_goal)
143  gripper.wait_for_result(rospy.Duration(1.0))
144 
145  # ハンドを上げる
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.")
150  continue
151  rospy.sleep(1.0)
152 
153  # SRDFに定義されている"home"の姿勢にする
154  arm.set_named_target("home")
155  if arm.go() is False:
156  print("Failed to go back to home pose.")
157  continue
158  rospy.sleep(1.0)
159 
160  print("Done")
161 
162  else:
163  print("No objects")
164 
165 
166 if __name__ == '__main__':
167  rospy.init_node("pick_and_place_in_gazebo_example")
168 
169  try:
170  if not rospy.is_shutdown():
171  main()
172  except rospy.ROSInterruptException:
173  pass


crane_x7_examples
Author(s): Daisuke Sato , Hiroyuki Nomura
autogenerated on Mon Mar 1 2021 03:18:34