forked from simon0793/pr2_simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation_one_cup_motion_queue.py
241 lines (200 loc) · 7.32 KB
/
simulation_one_cup_motion_queue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import arm
import planner
import numpy as np
import rospy
import roslib
import actionlib
roslib.load_manifest("pr2_controllers_msgs")
roslib.load_manifest("move_base_msgs")
#from pr2_controllers_msgs.msg import JointTrajectoryAction, JointTrajectoryGoal
import trajectory_msgs.msg as tm
import sensor_msgs.msg as sm
import pr2_controllers_msgs.msg as pcm
#import control_msgs.msg as pcm
roslib.load_manifest('tfx')
import tfx
import tf.transformations as tft
import openravepy as rave
'''EXPLICIT LOCATIONS USED'''
RED = [0.701716, 0.400784, 0.83095]
BLUE = [0.701716, 0.000784, 0.83095]
GREEN = [0.701716, -0.400784, 0.83095]
RIGHT_UP = [0.5, -0.2, 1.25]
LEFT_UP = [0.5, 0.2, 1.25]
TARGET_GREEN = [0.6, -0.405, 0.80]
ALIGN_GRASP_GREEN = [0.7, -0.405, 0.80]
RAISED_GREEN = [0.7, -0.405, 1.0]
RAISED_DEST_GREEN = [0.7, 0.0, 1.0]
DEST_GREEN = [0.7, 0.0, 0.81]
DEST_GREEN_REMOVED = [0.45, 0.0, 1.0]
'''OUTPUT FILE TO WRITE TO'''
FILENAME = '/home/simon/output.txt'
FILE = None
class SimpleArmState:
''' Class for representing just the state of PR2 arms (no additional objects)
'''
def __init__(self):
'''
'''
self.rightArm = arm.Arm('right')
self.leftArm = arm.Arm('left')
self.rightArm.sim.env.SetViewer('qtcoin')
self.valid = True
self.graspPlanner = planner.Planner('right')
rightArmPose = self.rightArm.get_pose()
self.currentGoal = rightArmPose.position.array # Set goal as default to current starting position
def setCurrentGoalPosition(self, newGoalPosition):
self.currentGoal = newGoalPosition
def isGoalState(self, acceptableAbsError = 0.1):
''' Returns true when goal state is reached
by default always returns true
'''
''' ------ CHANGE CODE BELOW THIS POINT ------ '''
rightArmPose = self.rightArm.get_pose()
targetX, targetY, targetZ = self.currentGoal
currentX, currentY, currentZ = rightArmPose.position.array
return (abs(targetX - currentX) <= acceptableAbsError and abs(targetY - currentY) <= acceptableAbsError and abs(targetZ - currentZ) <= acceptableAbsError) # Modify to check the current state
''' ------ CHANGE CODE ABOVE THIS POINT ------ '''
def successorState(self):
''' Updates current state to the next successor state
by default it just returns current state
'''
if self.valid:
newPosition = self.currentGoal
else:
print "The last successor state was invalid"
print "Enter a custom pose x y z"
coord = raw_input().split()
newPosition = [float(x) for x in coord]
newRightArmPose = tfx.pose(self.rightArm.get_pose()) # only want to change position not angles
newRightArmPose.position = newPosition
newRightJointsList = self.graspPlanner.get_grasp_joint_trajectory(self.rightArm.get_joints(), newRightArmPose.position, n_steps=10)
print newRightJointsList
if newRightJointsList is None:
rospy.loginfo('Invalid Right Arm Pose')
print newPosition
self.valid = False
else:
for newRightJoints in newRightJointsList:
writeToOutput(newRightJoints)
self.rightArm.execute_joint_trajectory(newRightJointsList)
self.valid = True
rospy.sleep(.01)
return self # Modify to generate a valid successor states, does not update state if pose is invalid and instead reverts to last valid state
def grasp(self):
self.rightArm.close_gripper()
print "Closing Gripper..."
rospy.sleep(2)
def release(self):
self.rightArm.open_gripper()
print "Opening Gripper..."
rospy.sleep(10)
def clear_arms(self):
self.raiseArms()
self.rightArm.go_to_posture('side')
self.leftArm.go_to_posture('side')
self.alignGrippers()
def alignGrippers(self):
rightArmPose = self.rightArm.get_pose()
leftArmPose = self.leftArm.get_pose()
newLeftArmPose = tfx.pose(leftArmPose.position, tfx.tb_angles(0.0, 0.0, 0.0)) # Create new pose
newRightArmPose = tfx.pose(rightArmPose.position, tfx.tb_angles(0.0, 0.0, 0.0))
newRightJoints = self.rightArm.ik(newRightArmPose) # Updates arm pose if valid
newLeftJoints = self.leftArm.ik(newLeftArmPose)
if newRightJoints is not None:
self.rightArm.go_to_joints(newRightJoints)
print('new_pose: {0}'.format(newRightArmPose))
print "Aligning right gripper..."
self.rightArm.open_gripper()
self.valid = True
else:
rospy.loginfo('Invalid Right Arm Pose')
print "RIGHT ALIGNMENT FAILED"
self.valid = False
return self
rospy.sleep(0.5)
if newLeftJoints is not None:
self.leftArm.go_to_joints(newLeftJoints)
print "Aligning left gripper..."
self.leftArm.open_gripper()
else:
rospy.loginfo('Invalid Left Arm Pose')
print "LEFT ALIGNMENT FAILED"
rospy.sleep(0.5)
def raiseArms(self):
rightArmPose = self.rightArm.get_pose()
leftArmPose = self.leftArm.get_pose()
newRightArmPose = tfx.pose(rightArmPose) # Create new pose
newLeftArmPose = tfx.pose(leftArmPose) # Create new pose
newRightArmPose.position = RIGHT_UP # either update by adding to xyz offset array "+ [0, -pos_step, 0]" or assign new position, defualt keeps position the same
newLeftArmPose.position = LEFT_UP
newRightJoints = self.rightArm.ik(newRightArmPose) # Updates arm pose if valid
newLeftJoints = self.leftArm.ik(newLeftArmPose)
if newRightJoints is not None:
self.rightArm.go_to_joints(newRightJoints)
print('new_pose: {0}'.format(newRightArmPose))
self.valid = True
else:
rospy.loginfo('Invalid Right Arm Pose')
print newRightArmPose
self.valid = False
return self
rospy.sleep(.01)
if newLeftJoints is not None:
self.leftArm.go_to_joints(newLeftJoints)
else:
rospy.loginfo('Invalid Left Arm Pose')
rospy.sleep(.01)
return self
class queuePlanner():
''' Class for planning motion using a simple queue of positions
'''
def __init__(self, positionQueue = []):
self.state = SimpleArmState()
self.goalPositionQueue = positionQueue
self.index = 0
def _pop(self):
if self.index < len(self.goalPositionQueue):
self.index += 1
return self.goalPositionQueue[self.index - 1]
else:
return None
def run(self):
currentGoal = self._pop();
while currentGoal is not None:
if isinstance(currentGoal, str):
if currentGoal == "CLEAR":
self.state.clear_arms()
elif currentGoal == "GRASP":
self.state.grasp()
elif currentGoal == "RELEASE":
self.state.release()
else:
print "ERROR INVALID COMMAND EXITING"
return -1
else:
self.state.setCurrentGoalPosition(currentGoal)
while not self.state.isGoalState():
print 'Getting Next State...'
self.state = self.state.successorState()
currentGoal = self._pop()
print 'Goal Reached'
return 0
# def step(self):
# def next(self):
# def skipStep(self):
# def skipNext(self):
# def setBreak(self):
def main():
global FILE
FILE = open(FILENAME, 'w')
goalPositions = ["CLEAR", TARGET_GREEN, ALIGN_GRASP_GREEN, "GRASP", RAISED_GREEN, RAISED_DEST_GREEN, DEST_GREEN, "RELEASE", DEST_GREEN_REMOVED, "CLEAR"]
qPlan = queuePlanner(goalPositions)
qPlan.run()
FILE.close()
def writeToOutput(data):
if FILE is not None:
FILE.write(str(data) + '\n')
if __name__ == '__main__':
rospy.init_node('test_arm', anonymous=True)
main()