Skip to content

Commit 52e0b9a

Browse files
committed
Semi working machine.py
1 parent 1c0e7d2 commit 52e0b9a

File tree

3 files changed

+71
-20
lines changed

3 files changed

+71
-20
lines changed

scripts/controller.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ def __init__(self):
3030
self.ik_solver = IK(self.BOTTOM_PLATE_LINK, self.GRIPPER_LINK)
3131

3232
# predefined variables
33+
self.HOME_POS_MANIPULATOR_00 = [0.0, 0.0, 0.0, 0.0, 0.0]
3334
self.HOME_POS_MANIPULATOR_01 = [0.004601942375302315, -0.4218447208404541, 1.6260197162628174, -0.1426602154970169, 0.010737866163253784]
34-
self.HOME_POS_MANIPULATOR_02 = [0.0, 0.0, 1.22, -0.142, 0.0]
35+
self.HOME_POS_MANIPULATOR_02 = [0.0, 0.0, 1.22, -1.57, 1.57]
3536
self.HOME_POS_CAMERA_01 = [0.0, 0.698]
3637
self.HOME_POS_CAMERA_02 = [-0.523, -0.523]
3738
self.IK_POSITION_TOLERANCE = 0.01
@@ -40,20 +41,22 @@ def __init__(self):
4041

4142
def set_camera_angles(self, angles):
4243
pan_msg = Float64()
43-
pan_msg.data = angles[0]
44+
pan_msg.data = float(angles[0])
4445
rospy.loginfo('Going to camera pan: {} rad'.format(angles[0]))
4546
self.pan_pub.publish(pan_msg)
4647

4748
tilt_msg = Float64()
48-
tilt_msg.data = angles[1]
49+
tilt_msg.data = float(angles[1])
4950
rospy.loginfo('Going to camera tilt: {} rad'.format(angles[1]))
5051
self.tilt_pub.publish(tilt_msg)
5152
rospy.sleep(4)
5253

5354
def set_arm_joint_angles(self, joint_target):
5455
joint_state = JointState()
5556
joint_state.position = tuple(joint_target)
57+
rospy.loginfo('Going to arm joint 0: {} rad'.format(joint_target[0]))
5658
self.arm_pub.publish(joint_state)
59+
rospy.sleep(4)
5760

5861
def get_joint_state(self, data):
5962
self.current_joint_state = data.position[0:5]
@@ -106,7 +109,7 @@ def compute_ik(self, target_pose):
106109
return result
107110

108111
# Goes to the position given by FRAME and grabs the object from the top
109-
def go_to_pose(self, FRAME):
112+
def go_to_grasp_pose(self, FRAME):
110113
try:
111114
self.tf_listener.waitForTransform(self.BOTTOM_PLATE_LINK, FRAME, rospy.Time.now(), rospy.Duration(5.0))
112115
P, Q = self.tf_listener.lookupTransform(self.BOTTOM_PLATE_LINK, FRAME, rospy.Time(0))
@@ -141,7 +144,41 @@ def check_grasp(self):
141144
if self.current_gripper_state:
142145
if(np.abs(self.current_gripper_state[0]) < self.MIN_CLOSING_GAP \
143146
and np.abs(self.current_gripper_state[1] < self.MIN_CLOSING_GAP)):
147+
print("Coundn't grasp the object")
144148
return False
149+
else:
150+
return True
145151
else:
146152
print("Joint State Subscriber not working")
147153
return False
154+
155+
# Goes to the position given by FRAME and grabs the object from the top
156+
def go_to_handover_pose(self, pose):
157+
# print(pose)
158+
try:
159+
self.tf_listener.waitForTransform(self.BOTTOM_PLATE_LINK, pose.header.frame_id, rospy.Time.now(), rospy.Duration(5.0))
160+
P, Q = self.tf_listener.lookupTransform(self.BOTTOM_PLATE_LINK, pose.header.frame_id, rospy.Time(0))
161+
except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException) as e:
162+
rospy.logwarn(e)
163+
return False
164+
165+
O = tf.transformations.euler_from_quaternion(Q)
166+
Q = tf.transformations.quaternion_from_euler(0, -np.pi/3, O[2])
167+
168+
poses = [Pose(Point(P[0], P[1], P[2]-0.45), Quaternion(Q[0], Q[1], Q[2], Q[3]))]
169+
170+
target_joint = None
171+
while target_joint is not None:
172+
if self.current_joint_state:
173+
target_joint = self.compute_ik(pose)
174+
else:
175+
print("Joint State Subscriber not working")
176+
return False
177+
178+
if target_joint:
179+
self.set_arm_joint_angles(target_joint)
180+
rospy.sleep(8)
181+
else:
182+
self.IK_POSITION_TOLERANCE += 0.1
183+
return True
184+

scripts/ik_control.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def main():
132132
# home_joint = [0.0, 0.0, 1.22, -0.142, 0.0]
133133
set_arm_joint(arm_pub, home_joint)
134134
close_gripper(gripper_close_pub)
135-
set_camera_angles(pan_pub, tilt_pub, rad_from_deg*0., rad_from_deg*40.0) #, ck)
135+
set_camera_angles(pan_pub, tilt_pub, rad_from_deg*0., rad_from_deg*20.0) #, ck)
136136

137137
rospy.sleep(10)
138138

scripts/machine.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from controller import Controller
99
from geometry_msgs.msg import PoseStamped
1010
from dynamixel_workbench_msgs.srv import SetPID
11+
from object_detection.srv import ObjectDetection
1112

1213
# Initialize controller
1314
ctrl = Controller()
@@ -18,13 +19,14 @@ def __init__(self):
1819

1920
def execute(self, userdata):
2021
userdata.tool_id = -1
22+
ctrl.open_gripper()
2123
ctrl.set_camera_angles(ctrl.HOME_POS_CAMERA_01)
2224
ctrl.set_arm_joint_angles(ctrl.HOME_POS_MANIPULATOR_01)
2325
rospy.loginfo('Executing state IDLE')
2426
while(True):
2527
# TODO: Ask for Input
2628
userdata.tool_id = input("Enter Tool ID you want:")
27-
if(isinstance(userdata.tool_id, int) and userdata.tool_id>0 and userdata.tool_id<4):
29+
if(isinstance(userdata.tool_id, int) and userdata.tool_id>0 and userdata.tool_id<5):
2830
break
2931

3032
# Return success
@@ -35,24 +37,35 @@ def execute(self, userdata):
3537
# define state FindTool
3638
class FindTool(smach.State):
3739
def __init__(self):
38-
smach.State.__init__(self, outcomes=['foundTool'], input_keys=['tool_id'], output_keys=['frame_name'])
40+
smach.State.__init__(self, outcomes=['foundTool', 'notfoundTool'], input_keys=['tool_id'], output_keys=['frame_name'])
3941

4042
def execute(self, userdata):
4143
rospy.loginfo('Executing state FINDTOOL')
42-
print(userdata.tool_id)
43-
## TODO: change the name of the variable frame_name
44-
userdata.frame_name = "random"
45-
return 'foundTool'
44+
rospy.wait_for_service('detect_object')
45+
try:
46+
get_success = rospy.ServiceProxy('detect_object', ObjectDetection)
47+
success = get_success(tool_id)
48+
if success:
49+
userdata.frame_name = "/icp_cuboid_frame"
50+
return 'foundTool'
51+
else:
52+
rospy.logwarn("Service returned false")
53+
return 'notfoundTool'
54+
except: rospy.ServiceException as e:
55+
rospy.logwarn("Service called failed:{0}".format(e))
56+
return 'notfoundTool'
4657

4758
# define state IK1
4859
class IK1(smach.State):
4960
def __init__(self):
5061
smach.State.__init__(self, outcomes=['noIK','foundIK'], input_keys=['frame_name'])
5162

5263
def execute(self, userdata):
53-
success = ctrl.go_to_pose(userdata.frame_name)
64+
ctrl.set_arm_joint_angles(ctrl.HOME_POS_MANIPULATOR_00)
5465
rospy.loginfo('Executing state IK1')
66+
success = ctrl.go_to_grasp_pose(userdata.frame_name)
5567
return 'foundIK' if success else 'noIK'
68+
# return 'foundIK'
5669

5770
# define state Move
5871
# class Move(smach.State):
@@ -66,7 +79,7 @@ def execute(self, userdata):
6679
# define state Grab
6780
class Grab(smach.State):
6881
def __init__(self):
69-
smach.State.__init__(self, outcomes=['grabSuccess','grabFailure'])
82+
smach.State.__init__(self, outcomes=['grabSuccess', 'grabFailure'])
7083

7184
def execute(self, userdata):
7285
ctrl.close_gripper()
@@ -104,7 +117,7 @@ def __init__(self):
104117

105118
def execute(self, userdata):
106119
## Call service to get the frame id of the hand centroid
107-
posestamped = rospy.wait_for_message("/openface2/head_pose", PoseStamped)
120+
userdata.head_pose = rospy.wait_for_message("/openface2/head_pose", PoseStamped)
108121
rospy.loginfo('Executing state FindAttention')
109122
return 'giveTool'
110123

@@ -117,7 +130,7 @@ def execute(self, userdata):
117130
## Wait till IK not found. Change tolerance and call compute IK again
118131
## Break go to pose into find pose and compute IK functions
119132
## Make a different function with gripper pointing upwards and call it from here
120-
success = ctrl.go_to_pose(userdata.head_pose)
133+
success = ctrl.go_to_handover_pose(userdata.head_pose)
121134
# if not success
122135
rospy.loginfo('Executing state IK2')
123136
return 'foundIK'
@@ -134,7 +147,7 @@ def execute(self, userdata):
134147
# define state ChangePID
135148
class ChangePID(smach.State):
136149
def __init__(self):
137-
smach.State.__init__(self, outcomes=['changed', 'not_changed'], input_keys=['joint_nums','PID'])
150+
smach.State.__init__(self, outcomes=['changed', 'notchanged'], input_keys=['joint_nums','PID'])
138151

139152
def execute(self, userdata):
140153
rospy.wait_for_service('SetPID')
@@ -146,7 +159,7 @@ def execute(self, userdata):
146159
return 'changed'
147160
except rospy.ServiceException as e:
148161
rospy.logwarn("Service call failed:{0}".format(e))
149-
return 'not_changed'
162+
return 'notchanged'
150163

151164
# define state OpenGripper
152165
class OpenGripper(smach.State):
@@ -155,7 +168,7 @@ def __init__(self):
155168

156169
def execute(self, userdata):
157170
## Detect when to Open gripper
158-
self.open_gripper()
171+
ctrl.open_gripper()
159172
rospy.loginfo('Executing state OpenGripper')
160173
return 'opened'
161174

@@ -166,17 +179,18 @@ def main():
166179
# Create a SMACH state machine
167180
sm = smach.StateMachine(outcomes=['stop'])
168181
sm.userdata.tool_id = -1
169-
sm.userdata.joint_nums = [1,2,3]
182+
sm.userdata.joint_nums = [1,6]
170183
sm.userdata.PID = [0,0,0]
171184
sis = smach_ros.IntrospectionServer('server_name', sm, '/SM_ROOT')
172185
sis.start()
186+
rospy.sleep(5)
173187
# Open the container
174188
with sm:
175189
# Add states to the container
176190
smach.StateMachine.add('IDLE', Idle(),
177191
transitions={'gotToolInput':'FINDTOOL'})
178192
smach.StateMachine.add('FINDTOOL', FindTool(),
179-
transitions={'foundTool':'IK1'})
193+
transitions={'foundTool':'IK1', 'notfoundTool':'IDLE'})
180194
smach.StateMachine.add('IK1', IK1(),
181195
transitions={'noIK':'stop','foundIK':'GRAB'})
182196
# smach.StateMachine.add('MOVE', Move(),

0 commit comments

Comments
 (0)