Skip to content

Commit 24edc9c

Browse files
committed
Last minute testing
1 parent 3936f23 commit 24edc9c

File tree

3 files changed

+45
-29
lines changed

3 files changed

+45
-29
lines changed

launch/camera_calibration.launch

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<launch>
22
<include file="position_control.launch"/>
33
<include file="$(find openface2_ros)/launch/openface2_ros.launch"/>
4+
<include file="$(find object_detection)/launch/object_detection.launch"/>
45
<include file="$(find realsense2_camera)/launch/rs_camera.launch">
56
<arg name="enable_pointcloud" value="true"/>
67
</include>

scripts/controller.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def __init__(self):
2525
# global variables
2626
self.current_joint_state = None
2727
self.current_gripper_state = None
28+
self.current_target_state = None
2829

2930
# global frames
3031
self.GRIPPER_LINK = "gripper_link"
@@ -38,10 +39,11 @@ def __init__(self):
3839
self.HOME_POS_MANIPULATOR_01 = [0.004601942375302315, -0.4218447208404541, 1.6260197162628174, -0.1426602154970169, 0.010737866163253784]
3940
self.HOME_POS_MANIPULATOR_02 = [0.0, 0.0, 1.22, -1.57, 1.57]
4041
self.HOME_POS_CAMERA_01 = [0.0, 0.698]
41-
self.HOME_POS_CAMERA_02 = [-0.523, -0.523]
42+
self.HOME_POS_CAMERA_02 = [-0.452, -0.452]
4243
self.IK_POSITION_TOLERANCE = 0.01
43-
self.IK_ORIENTATION_TOLERANCE = np.pi/9
44+
self.IK_ORIENTATION_TOLERANCE = np.pi/7
4445
self.MIN_CLOSING_GAP = 0.002
46+
self.MOVEABLE_JOINTS = [0,4]
4547

4648
def set_camera_angles(self, angles):
4749
pan_msg = Float64()
@@ -53,25 +55,24 @@ def set_camera_angles(self, angles):
5355
tilt_msg.data = float(angles[1])
5456
rospy.loginfo('Going to camera tilt: {} rad'.format(angles[1]))
5557
self.tilt_pub.publish(tilt_msg)
56-
rospy.sleep(4)
5758

5859
def set_arm_joint_angles(self, joint_target):
5960
joint_state = JointState()
6061
joint_state.position = tuple(joint_target)
6162
rospy.loginfo('Going to arm joint 0: {} rad'.format(joint_target[0]))
6263
self.arm_pub.publish(joint_state)
63-
rospy.sleep(4)
64+
rospy.sleep(6)
6465

6566
def get_joint_state(self, data):
6667
# TODO: Change this when required
67-
moveable_joints = [1,2,3]
68+
6869
# Add timestamp
6970
self.history['timestamp'].append(time.time())
7071
if(len(self.history['timestamp']) > 20):
7172
self.history['timestamp'].popleft()
7273

7374
# Add Joint Feedback
74-
joint_angles = np.array(data.position)[moveable_joints]
75+
joint_angles = np.array(data.position)[self.MOVEABLE_JOINTS]
7576
self.history['joint_feedback'].append(joint_angles)
7677
if(len(self.history['joint_feedback']) > 20):
7778
self.history['joint_feedback'].popleft()
@@ -137,8 +138,8 @@ def go_to_grasp_pose(self, FRAME):
137138
O = tf.transformations.euler_from_quaternion(Q)
138139
Q = tf.transformations.quaternion_from_euler(0, np.pi/2, O[2])
139140

140-
poses = [Pose(Point(P[0], P[1], P[2]+0.20), Quaternion(Q[0], Q[1], Q[2], Q[3])),
141-
Pose(Point(P[0], P[1], P[2]+0.15), Quaternion(Q[0], Q[1], Q[2], Q[3]))]
141+
poses = [Pose(Point(P[0], P[1], P[2]+0.15), Quaternion(Q[0], Q[1], Q[2], Q[3])),
142+
Pose(Point(P[0], P[1], P[2]+0.10), Quaternion(Q[0], Q[1], Q[2], Q[3]))]
142143

143144
target_joint = None
144145
self.open_gripper()
@@ -151,7 +152,7 @@ def go_to_grasp_pose(self, FRAME):
151152

152153
if target_joint:
153154
self.set_arm_joint_angles(target_joint)
154-
rospy.sleep(8)
155+
self.current_target_state = np.array(target_joint)[self.MOVEABLE_JOINTS]
155156
else:
156157
return False
157158
return True
@@ -171,7 +172,7 @@ def check_grasp(self):
171172

172173
# Goes to the position given by FRAME and grabs the object from the top
173174
def go_to_handover_pose(self, pose):
174-
# print(pose)
175+
print(pose)
175176
try:
176177
self.tf_listener.waitForTransform(self.BOTTOM_PLATE_LINK, pose.header.frame_id, rospy.Time.now(), rospy.Duration(5.0))
177178
P, Q = self.tf_listener.lookupTransform(self.BOTTOM_PLATE_LINK, pose.header.frame_id, rospy.Time(0))
@@ -182,20 +183,23 @@ def go_to_handover_pose(self, pose):
182183
O = tf.transformations.euler_from_quaternion(Q)
183184
Q = tf.transformations.quaternion_from_euler(0, -np.pi/3, O[2])
184185

185-
poses = [Pose(Point(P[0], P[1], P[2]-0.45), Quaternion(Q[0], Q[1], Q[2], Q[3]))]
186+
poses = Pose(Point(P[0], P[1], P[2]-0.45), Quaternion(Q[0], Q[1], Q[2], Q[3]))
186187

187188
target_joint = None
188-
while target_joint is not None:
189+
while target_joint is None:
189190
if self.current_joint_state:
190-
target_joint = self.compute_ik(pose)
191+
target_joint = self.compute_ik(poses)
191192
else:
192193
print("Joint State Subscriber not working")
193194
return False
194195

195196
if target_joint:
197+
print(target_joint)
196198
self.set_arm_joint_angles(target_joint)
197-
rospy.sleep(8)
199+
self.current_target_state = np.array(target_joint)[self.MOVEABLE_JOINTS]
198200
else:
199-
self.IK_POSITION_TOLERANCE += 0.1
201+
rospy.logwarn("No Solution was found. Current Tolerance "+ str(self.IK_POSITION_TOLERANCE))
202+
self.IK_POSITION_TOLERANCE += 0.05
203+
print(target_joint)
200204
return True
201205

scripts/machine.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from geometry_msgs.msg import PoseStamped
1111
from dynamixel_workbench_msgs.srv import SetPID
1212
from object_detection.srv import ObjectDetection
13+
import pdb
1314

1415
# Initialize controller
1516
ctrl = Controller()
@@ -43,17 +44,17 @@ def __init__(self):
4344

4445
def execute(self, userdata):
4546
rospy.loginfo('Executing state FINDTOOL')
46-
rospy.wait_for_service('detect_object')
47+
rospy.wait_for_service('object_pose_detection/detect_objects')
4748
try:
48-
get_success = rospy.ServiceProxy('detect_object', ObjectDetection)
49-
success = get_success(tool_id)
49+
get_success = rospy.ServiceProxy('object_pose_detection/detect_objects', ObjectDetection)
50+
success = get_success(userdata.tool_id)
5051
if success:
51-
userdata.frame_name = "/icp_cuboid_frame"
52+
userdata.frame_name = "/object_frame"
5253
return 'foundTool'
5354
else:
5455
rospy.logwarn("Service returned false")
5556
return 'notfoundTool'
56-
except: rospy.ServiceException as e:
57+
except rospy.ServiceException as e:
5758
rospy.logwarn("Service called failed:{0}".format(e))
5859
return 'notfoundTool'
5960

@@ -126,15 +127,17 @@ def execute(self, userdata):
126127
# define state IK2
127128
class IK2(smach.State):
128129
def __init__(self):
129-
smach.State.__init__(self, outcomes=['foundIK'], input_keys=['head_pose'])
130+
smach.State.__init__(self, outcomes=['foundIK', 'noIK'], input_keys=['head_pose'])
130131

131132
def execute(self, userdata):
132133
## Wait till IK not found. Change tolerance and call compute IK again
133134
## Break go to pose into find pose and compute IK functions
134135
## Make a different function with gripper pointing upwards and call it from here
135-
success = ctrl.go_to_handover_pose(userdata.head_pose)
136-
# if not success
137136
rospy.loginfo('Executing state IK2')
137+
success = ctrl.go_to_handover_pose(userdata.head_pose)
138+
rospy.sleep(8)
139+
if not success:
140+
return 'noIK'
138141
return 'foundIK'
139142

140143
# # define state MoveGive
@@ -158,6 +161,7 @@ def execute(self, userdata):
158161
P,I,D = userdata.PID
159162
for joint_num in userdata.joint_nums:
160163
response = set_PID(joint_num, P, I, D)
164+
rospy.sleep(10)
161165
return 'changed'
162166
except rospy.ServiceException as e:
163167
rospy.logwarn("Service call failed:{0}".format(e))
@@ -175,10 +179,12 @@ def execute(self, userdata):
175179
joint_len = len(ctrl.history['joint_feedback'][0])
176180
joint_sum = np.zeros(ctrl.history['joint_feedback'][0].shape)
177181
for joint_feedback in ctrl.history['joint_feedback']:
178-
joint_sum += joint_feedback
182+
# pdb.set_trace()
183+
joint_sum += joint_feedback - ctrl.current_target_state
179184
avg_joint_sum = joint_sum/joint_len
180-
if(np.sum(avg_joint_sum) < MIN_JOINT_MOTION_FOR_HAND_OVER):
185+
if(np.sum(avg_joint_sum) > MIN_JOINT_MOTION_FOR_HAND_OVER):
181186
ctrl.open_gripper()
187+
rospy.sleep(1)
182188
return 'opened'
183189

184190

@@ -190,7 +196,8 @@ def main():
190196
sm = smach.StateMachine(outcomes=['stop'])
191197
sm.userdata.tool_id = -1
192198
sm.userdata.joint_nums = [1,6]
193-
sm.userdata.PID = [0,0,0]
199+
sm.userdata.PID_Final = [0,0,0]
200+
sm.userdata.PID_Initial = [640,5,50]
194201
sis = smach_ros.IntrospectionServer('server_name', sm, '/SM_ROOT')
195202
sis.start()
196203
rospy.sleep(5)
@@ -214,14 +221,18 @@ def main():
214221
smach.StateMachine.add('ATTENTIONSEEKER', FindAttention(),
215222
transitions={'giveTool':'IK2'})
216223
smach.StateMachine.add('IK2', IK2(),
217-
transitions={'foundIK':'CHANGEPID'})
224+
transitions={'foundIK':'CHANGEPID', 'noIK':'stop'})
218225
# smach.StateMachine.add('MOVEGIVE', MoveGive(),
219226
# transitions={'reached':'CHANGEPID'})
220227

221228
smach.StateMachine.add('CHANGEPID', ChangePID(),
222-
transitions={'changed':'OPENGRIPPER', 'notchanged': 'CHANGEPID'})
229+
transitions={'changed':'OPENGRIPPER', 'notchanged': 'CHANGEPID'},
230+
remapping={'PID':'PID_Final'})
223231
smach.StateMachine.add('OPENGRIPPER', OpenGripper(),
224-
transitions={'opened':'IDLE'})
232+
transitions={'opened':'CHANGEBACKPID'})
233+
smach.StateMachine.add('CHANGEBACKPID', ChangePID(),
234+
transitions={'changed':'IDLE', 'notchanged': 'CHANGEPID'},
235+
remapping={'PID':'PID_Initial'})
225236

226237

227238

0 commit comments

Comments
 (0)