Skip to content

Commit 784fdc6

Browse files
committed
Added camera and gripper control
1 parent f24f2e2 commit 784fdc6

File tree

3 files changed

+138
-43
lines changed

3 files changed

+138
-43
lines changed

scripts/Assignment3_Camera_Calibration.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,26 @@
1010
from sensor_msgs.msg import (JointState,
1111
Image,
1212
CameraInfo)
13+
from std_msgs.msg import Float64
1314
from cv_bridge import CvBridge, CvBridgeError
1415
from ar_track_alvar_msgs.msg import AlvarMarkers
1516
import forward_kinematics_extrinsics
1617

1718
ROSTOPIC_SET_ARM_JOINT = '/goal_dynamixel_position'
18-
#flag = 0
19+
ROSTOPIC_SET_PAN_JOINT = '/pan/command'
20+
ROSTOPIC_SET_TILT_JOINT = '/tilt/command'
21+
22+
def set_camera_pan(pub, pan_rad):
23+
pan_msg = Float64()
24+
pan_msg.data = pan_rad
25+
rospy.loginfo('Going to camera pan: {} rad'.format(pan_rad))
26+
pub.publish(pan_msg)
27+
28+
def set_camera_tilt(pub, tilt_rad):
29+
tilt_msg = Float64()
30+
tilt_msg.data = tilt_rad
31+
rospy.loginfo('Going to camera tilt: {} rad'.format(tilt_rad))
32+
pub.publish(tilt_msg)
1933

2034
def home_arm(pub):
2135
set_arm_joint(pub, np.zeros(5))
@@ -45,6 +59,9 @@ def main(noiter=15):
4559

4660
bridge = CvBridge()
4761
pub = rospy.Publisher(ROSTOPIC_SET_ARM_JOINT, JointState, queue_size=1)
62+
pan_pub = rospy.Publisher(ROSTOPIC_SET_PAN_JOINT, Float64, queue_size=1)
63+
tilt_pub = rospy.Publisher(ROSTOPIC_SET_TILT_JOINT, Float64, queue_size=1)
64+
4865
rospy.sleep(2)
4966
Calib = rospy.wait_for_message("/camera/color/camera_info", CameraInfo)
5067
K_matrix = np.array(Calib.K)
@@ -60,6 +77,9 @@ def main(noiter=15):
6077
raw_input("Robot ready to move to HOME POSITION. Press Enter to continue.")
6178
print("Robot moving. Please wait.")
6279
home_arm(pub)
80+
set_camera_pan(pan_pub, rad_from_deg*0.)
81+
set_camera_tilt(tilt_pub, rad_from_deg*0.)
82+
rospy.sleep(4)
6383

6484
def imgcallback(i):
6585
data = rospy.wait_for_message("/camera/color/image_raw", Image, timeout=5)
@@ -168,6 +188,6 @@ def get_P(end_effector_position):
168188
return point_3D_world_frame
169189

170190
if __name__ == "__main__":
171-
# main(50)
172-
calib_info = np.load("../data/calibration/calibration_info.npz")
173-
calculate_extrinsic(calib_info['position'], calib_info['camerainfo'], calib_info['forward_kinematics'], 20)
191+
main(50)
192+
# calib_info = np.load("../data/calibration/calibration_info.npz")
193+
# calculate_extrinsic(calib_info['position'], calib_info['camerainfo'], calib_info['forward_kinematics'], 20)

scripts/ik_control.py

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,22 @@
99
import rospy
1010
from geometry_msgs.msg import Pose, Point, Quaternion
1111
from sensor_msgs.msg import JointState
12+
from std_msgs.msg import Float64
13+
from std_msgs.msg import Empty
1214
from trac_ik_python.trac_ik import IK
1315

1416
GRIPPER_LINK = "gripper_link"
1517
ARM_BASE_LINK = "arm_base_link"
1618
MOVE_GROUP_NAME = 'arm'
19+
1720
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+
1826
IK_POSITION_TOLERANCE = 0.01
19-
IK_ORIENTATION_TOLERANCE = np.pi/6
27+
IK_ORIENTATION_TOLERANCE = np.pi/9
2028
current_joint_state = None
2129

2230

@@ -25,6 +33,38 @@ def home_arm(pub):
2533
set_arm_joint(pub, np.zeros(5))
2634
rospy.sleep(5)
2735

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+
2868

2969
def compute_ik(ik_solver, target_pose, current_joint):
3070
"""
@@ -59,17 +99,6 @@ def compute_ik(ik_solver, target_pose, current_joint):
5999
rospy.logerr('No IK solution found')
60100
return result
61101

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-
73102
def main():
74103
rospy.init_node('IK_Control', anonymous=True)
75104

@@ -79,33 +108,45 @@ def main():
79108
# Quaternion(0.245, 0.613, -0.202, 0.723))]
80109

81110
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),
84113
Quaternion(0, 0.8509035, 0, 0.525322))]
85114

86115
ik_solver = IK(ARM_BASE_LINK, GRIPPER_LINK)
87116

88117
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)
91124
rospy.sleep(2)
92-
home_arm(pub)
93125

126+
home_arm(arm_pub)
127+
close_gripper(gripper_close_pub)
94128

95129
for pose in target_poses:
96130

97131
rospy.loginfo('Commanding arm to pose {}'.format(pose))
98132

99-
print(current_joint_state)
100-
if current_joint_state:
133+
print(current_joint_state)
134+
if current_joint_state:
101135
target_joint = compute_ik(
102136
ik_solver, pose, current_joint_state)
103137

104138
if target_joint:
105-
set_arm_joint(pub, target_joint)
139+
set_arm_joint(arm_pub, target_joint)
106140
rospy.sleep(5)
107141

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)
109150

110151

111152
if __name__ == "__main__":

scripts/lab2_motion_demo.py

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import rospy
1010

1111
from sensor_msgs.msg import JointState
12+
import PRM
1213

1314

1415
ROSTOPIC_SET_ARM_JOINT = '/goal_dynamixel_position'
@@ -28,34 +29,67 @@ def set_arm_joint(pub, joint_target):
2829
def main():
2930
rospy.init_node('command_joints_example', anonymous=True)
3031

31-
rad_from_deg = np.pi/180.
32-
start_joints_deg = [ 0., 20., 50., -80., 0.]
33-
final_joints_deg = [135., 20., 50., -80., 0.]
34-
3532
pub = rospy.Publisher(ROSTOPIC_SET_ARM_JOINT,
3633
JointState, queue_size=1)
3734
rospy.sleep(2)
38-
39-
raw_input("Robot ready to move to HOME POSITION. Press Enter to continue.")
40-
print("Robot moving. Please wait.")
41-
home_arm(pub)
42-
rospy.sleep(5)
4335

44-
raw_input("Robot ready to move to START POSITION. Press Enter to continue.")
45-
print("Robot moving. Please wait.")
46-
set_arm_joint(pub, rad_from_deg*np.array(start_joints_deg))
47-
rospy.sleep(5)
48-
49-
raw_input("Robot ready to move to FINAL POSITION. Press Enter to continue.")
50-
print("Robot moving. Please wait.")
51-
set_arm_joint(pub, rad_from_deg*np.array(final_joints_deg))
52-
rospy.sleep(10)
36+
rad_from_deg = np.pi/180.
5337

5438
raw_input("Robot ready to move to HOME POSITION. Press Enter to continue.")
5539
print("Robot moving. Please wait.")
5640
home_arm(pub)
5741
rospy.sleep(5)
5842

43+
start = np.array([[ 0., 20., 50., -80., 0.]])
44+
goal = np.array([[85., 20., 50., -80., 0.]])
45+
46+
# Create PRM variable and get path from start to goal
47+
obstacles_cuboids = np.array([[[0.0604, 0.1854, 0.214], [0, 0, 0], [0.1, 0.125, 0.225]], \
48+
[[0.0604, 0.1854, 0.349], [0, 0, 0], [0.125, 0.225, 0.1]]])
49+
50+
prm = PRM.ProbabilisticRoadMap(obstacles_cuboids)
51+
52+
path = prm.makePlan(start, goal)
53+
54+
# # Send nodes of the path as commands to controller
55+
for joint_angles in path:
56+
print("Robot moving. Please wait.")
57+
joint_angles = (joint_angles*rad_from_deg).tolist()
58+
set_arm_joint(pub, np.array(joint_angles))
59+
print(joint_angles)
60+
rospy.sleep(4)
61+
62+
63+
path = prm.makePlan(goal, start)
64+
65+
# # Send nodes of the path as commands to controller
66+
for joint_angles in path:
67+
print("Robot moving. Please wait.")
68+
joint_angles = (joint_angles*rad_from_deg).tolist()
69+
set_arm_joint(pub, np.array(joint_angles))
70+
print(joint_angles)
71+
rospy.sleep(4)
72+
73+
# raw_input("Robot ready to move to HOME POSITION. Press Enter to continue.")
74+
# print("Robot moving. Please wait.")
75+
# home_arm(pub)
76+
#
77+
78+
# raw_input("Robot ready to move to START POSITION. Press Enter to continue.")
79+
# print("Robot moving. Please wait.")
80+
# set_arm_joint(pub, rad_from_deg*np.array(start_joints_deg))
81+
# rospy.sleep(5)
82+
83+
# raw_input("Robot ready to move to FINAL POSITION. Press Enter to continue.")
84+
# print("Robot moving. Please wait.")
85+
# set_arm_joint(pub, rad_from_deg*np.array(final_joints_deg))
86+
# rospy.sleep(10)
87+
88+
# raw_input("Robot ready to move to HOME POSITION. Press Enter to continue.")
89+
# print("Robot moving. Please wait.")
90+
# home_arm(pub)
91+
# rospy.sleep(5)
92+
5993

6094
if __name__ == "__main__":
6195
main()

0 commit comments

Comments
 (0)