9
9
import rospy
10
10
from geometry_msgs .msg import Pose , Point , Quaternion
11
11
from sensor_msgs .msg import JointState
12
+ from std_msgs .msg import Float64
13
+ from std_msgs .msg import Empty
12
14
from trac_ik_python .trac_ik import IK
13
15
14
16
GRIPPER_LINK = "gripper_link"
15
17
ARM_BASE_LINK = "arm_base_link"
16
18
MOVE_GROUP_NAME = 'arm'
19
+
17
20
ROSTOPIC_SET_ARM_JOINT = '/goal_dynamixel_position'
21
+ ROSTOPIC_SET_PAN_JOINT = '/pan/command'
22
+ ROSTOPIC_SET_TILT_JOINT = '/tilt/command'
23
+ ROSTOPIC_OPEN_GRIPPER = '/gripper/open'
24
+ ROSTOPIC_CLOSE_GRIPPER = '/gripper/close'
25
+
18
26
IK_POSITION_TOLERANCE = 0.01
19
- IK_ORIENTATION_TOLERANCE = np .pi / 6
27
+ IK_ORIENTATION_TOLERANCE = np .pi / 9
20
28
current_joint_state = None
21
29
22
30
@@ -25,6 +33,38 @@ def home_arm(pub):
25
33
set_arm_joint (pub , np .zeros (5 ))
26
34
rospy .sleep (5 )
27
35
36
+ def set_camera_pan (pub , pan_rad ):
37
+ pan_msg = Float64 ()
38
+ pan_msg .data = pan_rad
39
+ rospy .loginfo ('Going to camera pan: {} rad' .format (pan_rad ))
40
+ pub .publish (pan_msg )
41
+
42
+ def set_camera_tilt (pub , tilt_rad ):
43
+ tilt_msg = Float64 ()
44
+ tilt_msg .data = tilt_rad
45
+ rospy .loginfo ('Going to camera tilt: {} rad' .format (tilt_rad ))
46
+ pub .publish (tilt_msg )
47
+
48
+ def set_arm_joint (pub , joint_target ):
49
+ joint_state = JointState ()
50
+ joint_state .position = tuple (joint_target )
51
+ pub .publish (joint_state )
52
+
53
+ def get_joint_state (data ):
54
+ global current_joint_state
55
+ if len (data .position ) == 11 :
56
+ current_joint_state = data .position [4 :9 ]
57
+
58
+ def open_gripper (pub ):
59
+ empty_msg = Empty ()
60
+ rospy .loginfo ('Opening gripper' )
61
+ pub .publish (empty_msg )
62
+
63
+ def close_gripper (pub ):
64
+ empty_msg = Empty ()
65
+ rospy .loginfo ('Closing gripper' )
66
+ pub .publish (empty_msg )
67
+
28
68
29
69
def compute_ik (ik_solver , target_pose , current_joint ):
30
70
"""
@@ -59,17 +99,6 @@ def compute_ik(ik_solver, target_pose, current_joint):
59
99
rospy .logerr ('No IK solution found' )
60
100
return result
61
101
62
-
63
- def set_arm_joint (pub , joint_target ):
64
- joint_state = JointState ()
65
- joint_state .position = tuple (joint_target )
66
- pub .publish (joint_state )
67
-
68
- def get_joint_state (data ):
69
- global current_joint_state
70
- if len (data .position ) == 11 :
71
- current_joint_state = data .position [4 :9 ]
72
-
73
102
def main ():
74
103
rospy .init_node ('IK_Control' , anonymous = True )
75
104
@@ -79,33 +108,45 @@ def main():
79
108
# Quaternion(0.245, 0.613, -0.202, 0.723))]
80
109
81
110
target_poses = [Pose (Point (0.279 , 0.176 , 0.217 ),
82
- Quaternion (- 0.4940158 , - 0.4228743 , - 0.4940158 , 0.5771257 )),
83
- Pose (Point (0.239 , 0.0116 , 0.2 ),
111
+ Quaternion (0 , 0 , 0 , 1 )),
112
+ Pose (Point (0.239 , 0.0116 , 0.05 ),
84
113
Quaternion (0 , 0.8509035 , 0 , 0.525322 ))]
85
114
86
115
ik_solver = IK (ARM_BASE_LINK , GRIPPER_LINK )
87
116
88
117
rospy .Subscriber ('/joint_states' , JointState , get_joint_state )
89
- pub = rospy .Publisher (ROSTOPIC_SET_ARM_JOINT ,
90
- JointState , queue_size = 1 )
118
+
119
+ arm_pub = rospy .Publisher (ROSTOPIC_SET_ARM_JOINT , JointState , queue_size = 1 )
120
+ pan_pub = rospy .Publisher (ROSTOPIC_SET_PAN_JOINT , Float64 , queue_size = 1 )
121
+ tilt_pub = rospy .Publisher (ROSTOPIC_SET_TILT_JOINT , Float64 , queue_size = 1 )
122
+ gripper_open_pub = rospy .Publisher (ROSTOPIC_OPEN_GRIPPER , Empty , queue_size = 1 )
123
+ gripper_close_pub = rospy .Publisher (ROSTOPIC_CLOSE_GRIPPER , Empty , queue_size = 1 )
91
124
rospy .sleep (2 )
92
- home_arm (pub )
93
125
126
+ home_arm (arm_pub )
127
+ close_gripper (gripper_close_pub )
94
128
95
129
for pose in target_poses :
96
130
97
131
rospy .loginfo ('Commanding arm to pose {}' .format (pose ))
98
132
99
- print (current_joint_state )
100
- if current_joint_state :
133
+ print (current_joint_state )
134
+ if current_joint_state :
101
135
target_joint = compute_ik (
102
136
ik_solver , pose , current_joint_state )
103
137
104
138
if target_joint :
105
- set_arm_joint (pub , target_joint )
139
+ set_arm_joint (arm_pub , target_joint )
106
140
rospy .sleep (5 )
107
141
108
- home_arm (pub )
142
+ open_gripper (gripper_open_pub )
143
+
144
+ raw_input ("Robot ready to close gripper. Press Enter to continue." )
145
+ print ("Robot moving. Please wait." )
146
+ close_gripper (gripper_close_pub )
147
+ rospy .sleep (4 )
148
+
149
+ home_arm (arm_pub )
109
150
110
151
111
152
if __name__ == "__main__" :
0 commit comments