12
12
from std_msgs .msg import Float64
13
13
from std_msgs .msg import Empty
14
14
from trac_ik_python .trac_ik import IK
15
+ import camera_forward_kinematics
16
+ import tf
15
17
16
18
GRIPPER_LINK = "gripper_link"
17
- ARM_BASE_LINK = "arm_base_link "
19
+ ARM_BASE_LINK = "bottom_plate "
18
20
MOVE_GROUP_NAME = 'arm'
19
21
20
22
ROSTOPIC_SET_ARM_JOINT = '/goal_dynamixel_position'
24
26
ROSTOPIC_CLOSE_GRIPPER = '/gripper/close'
25
27
26
28
IK_POSITION_TOLERANCE = 0.01
27
- IK_ORIENTATION_TOLERANCE = np .pi / 9
29
+ IK_ORIENTATION_TOLERANCE = np .pi / 6
28
30
current_joint_state = None
29
31
30
32
@@ -33,17 +35,18 @@ def home_arm(pub):
33
35
set_arm_joint (pub , np .zeros (5 ))
34
36
rospy .sleep (5 )
35
37
36
- def set_camera_pan ( pub , pan_rad ):
38
+ def set_camera_angles ( pan_pub , tilt_pub , pan_rad , tilt_rad , ck ):
37
39
pan_msg = Float64 ()
38
40
pan_msg .data = pan_rad
39
41
rospy .loginfo ('Going to camera pan: {} rad' .format (pan_rad ))
40
- pub .publish (pan_msg )
42
+ pan_pub .publish (pan_msg )
41
43
42
- def set_camera_tilt (pub , tilt_rad ):
43
44
tilt_msg = Float64 ()
44
45
tilt_msg .data = tilt_rad
45
46
rospy .loginfo ('Going to camera tilt: {} rad' .format (tilt_rad ))
46
- pub .publish (tilt_msg )
47
+ tilt_pub .publish (tilt_msg )
48
+
49
+ print ("Camera Extrinsics" , ck .cameraForwardKinematics (np .array ([[pan_rad , tilt_rad , 0. ]])))
47
50
48
51
def set_arm_joint (pub , joint_target ):
49
52
joint_state = JointState ()
@@ -52,8 +55,8 @@ def set_arm_joint(pub, joint_target):
52
55
53
56
def get_joint_state (data ):
54
57
global current_joint_state
55
- if len (data .position ) == 11 :
56
- current_joint_state = data .position [4 : 9 ]
58
+ # if len(data.position) == 9 :
59
+ current_joint_state = data .position [0 : 5 ]
57
60
58
61
def open_gripper (pub ):
59
62
empty_msg = Empty ()
@@ -101,36 +104,72 @@ def compute_ik(ik_solver, target_pose, current_joint):
101
104
102
105
def main ():
103
106
rospy .init_node ('IK_Control' , anonymous = True )
104
-
105
- #target_poses = [Pose(Point(0.279, 0.176, 0.217),
106
- # Quaternion(-0.135, 0.350, 0.329, 0.866)),
107
- # Pose(Point(0.339, 0.0116, 0.255),
108
- # Quaternion(0.245, 0.613, -0.202, 0.723))]
109
-
110
- target_poses = [Pose (Point (0.279 , 0.176 , 0.217 ),
111
- Quaternion (0 , 0 , 0 , 1 )),
112
- Pose (Point (0.239 , 0.0116 , 0.05 ),
113
- Quaternion (0 , 0.8509035 , 0 , 0.525322 ))]
107
+ global current_joint_state
108
+
109
+ rad_from_deg = np .pi / 180.
114
110
115
111
ik_solver = IK (ARM_BASE_LINK , GRIPPER_LINK )
116
-
112
+ ck = camera_forward_kinematics .camerakinematics ()
113
+
114
+ # Describing the publisher subscribers
117
115
rospy .Subscriber ('/joint_states' , JointState , get_joint_state )
118
116
119
117
arm_pub = rospy .Publisher (ROSTOPIC_SET_ARM_JOINT , JointState , queue_size = 1 )
120
118
pan_pub = rospy .Publisher (ROSTOPIC_SET_PAN_JOINT , Float64 , queue_size = 1 )
121
119
tilt_pub = rospy .Publisher (ROSTOPIC_SET_TILT_JOINT , Float64 , queue_size = 1 )
122
120
gripper_open_pub = rospy .Publisher (ROSTOPIC_OPEN_GRIPPER , Empty , queue_size = 1 )
123
121
gripper_close_pub = rospy .Publisher (ROSTOPIC_CLOSE_GRIPPER , Empty , queue_size = 1 )
122
+ tf_listener = tf .TransformListener ()
124
123
rospy .sleep (2 )
125
124
126
- home_arm ( arm_pub )
125
+ # Homing of all servos
127
126
close_gripper (gripper_close_pub )
127
+ set_camera_angles (pan_pub , tilt_pub , rad_from_deg * 0. , rad_from_deg * 40.0 , ck )
128
+
129
+ rospy .sleep (5 )
130
+ # while not rospy.is_shutdown():
131
+ # try:
132
+ # tf_listener.waitForTransform("/camera_depth_frame", "/icp_cuboid_frame", rospy.Time.now(), rospy.Duration(4.0))
133
+ # position, quaternion = tf_listener.lookupTransform("/camera_depth_frame", "/icp_cuboid_frame", rospy.Time(0))
134
+ # print("Box in depth frame")
135
+ # print(position, quaternion)
136
+ # break
137
+ # except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException) as e:
138
+ # print(e)
139
+ # # return
140
+
141
+ # # for pose in target_poses:
142
+ # O = tf.transformations.euler_from_quaternion(quaternion)
143
+ # R, T = ck.getTransformedPose(np.array([O[0], O[1], O[2]]), np.array([position[0], position[1], position[2]]))
144
+ # print("Box in bottom plate frame" ,R, T)
145
+ # q = tf.transformations.quaternion_from_euler(R[0], R[1], R[2])
146
+ # poses = [Pose(Point(T[0], T[1], T[2]+0.1), Quaternion(q[0], q[1], q[2], q[3])),
147
+ # Pose(Point(T[0], T[1], T[2]), Quaternion(q[0], q[1], q[2], q[3]))]
148
+ # rospy.loginfo('Commanding arm to pose {}'.format(poses))
149
+
150
+ # q = tf.transformations.quaternion_from_euler(0, 90, 0)
151
+ # poses = [Pose(Point(0.3, 0.000, 0.25), Quaternion(q[0], q[1], q[2], q[3])),
152
+ # Pose(Point(0.3, 0.000, 0.10), Quaternion(q[0], q[1], q[2], q[3]))]
153
+
154
+ while not rospy .is_shutdown ():
155
+ try :
156
+ tf_listener .waitForTransform ("/bottom_plate" , "/icp_cuboid_frame" , rospy .Time .now (), rospy .Duration (4.0 ))
157
+ P , Q = tf_listener .lookupTransform ("/bottom_plate" , "/icp_cuboid_frame" , rospy .Time (0 ))
158
+ print ("Box in bottom plate frame" )
159
+ print (P , Q )
160
+ break
161
+ except (tf .LookupException , tf .ConnectivityException , tf .ExtrapolationException ) as e :
162
+ print (e )
163
+
164
+ poses = [Pose (Point (P [0 ], P [1 ], P [2 ]+ 0.1 ), Quaternion (Q [0 ], Q [1 ], Q [2 ], Q [3 ])),
165
+ Pose (Point (P [0 ], P [1 ], P [2 ]), Quaternion (Q [0 ], Q [1 ], Q [2 ], Q [3 ]))]
128
166
129
- for pose in target_poses :
167
+ target_joint = None
130
168
131
- rospy . loginfo ( 'Commanding arm to pose {}' . format ( pose ) )
132
-
169
+ home_arm ( arm_pub )
170
+ for pose in poses :
133
171
print (current_joint_state )
172
+ print ("Reaching a joint state" )
134
173
if current_joint_state :
135
174
target_joint = compute_ik (
136
175
ik_solver , pose , current_joint_state )
@@ -139,7 +178,8 @@ def main():
139
178
set_arm_joint (arm_pub , target_joint )
140
179
rospy .sleep (5 )
141
180
142
- open_gripper (gripper_open_pub )
181
+ open_gripper (gripper_open_pub )
182
+ rospy .sleep (4 )
143
183
144
184
raw_input ("Robot ready to close gripper. Press Enter to continue." )
145
185
print ("Robot moving. Please wait." )
@@ -151,3 +191,10 @@ def main():
151
191
152
192
if __name__ == "__main__" :
153
193
main ()
194
+
195
+
196
+ # Some values
197
+ # target_poses = [Pose(Point(0.279, 0.176, 0.217),
198
+ # Quaternion(0, 0, 0, 1)),
199
+ # Pose(Point(0.239, 0.0116, 0.05),
200
+ # Quaternion(0, 0.8509035, 0, 0.525322))]
0 commit comments