forked from ARISE-Initiative/robosuite
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpanda_ik_controller.py
More file actions
303 lines (243 loc) · 10.8 KB
/
panda_ik_controller.py
File metadata and controls
303 lines (243 loc) · 10.8 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"""
NOTE: requires pybullet module.
Run `pip install pybullet==1.9.5`.
"""
import numpy as np
try:
import pybullet as p
except ImportError:
raise Exception(
"Please make sure pybullet is installed. Run `pip install pybullet==1.9.5`"
)
from os.path import join as pjoin
import robosuite.utils.transform_utils as T
from robosuite.controllers import Controller
class PandaIKController(Controller):
"""
Inverse kinematics for the Panda robot, using Pybullet and the urdf description
files. Loads a panda robot into an internal Pybullet simulation, and uses it to
do inverse kinematics computations.
"""
def __init__(self, bullet_data_path, robot_jpos_getter):
"""
Args:
bullet_data_path (str): base path to bullet data.
robot_jpos_getter (function): function that returns the joint positions of
the robot to be controlled as a numpy array.
"""
# path to data folder of bullet repository
self.bullet_data_path = bullet_data_path
# returns current robot joint positions
self.robot_jpos_getter = robot_jpos_getter
# Do any setup needed for Inverse Kinematics.
self.setup_inverse_kinematics()
# Should be in (0, 1], smaller values mean less sensitivity.
self.user_sensitivity = .3
self.sync_state()
def get_control(self, dpos, rotation):
"""
Returns joint velocities to control the robot after the target end effector
position and orientation are updated from arguments @dpos and @rotation.
Args:
dpos (numpy array): a 3 dimensional array corresponding to the desired
change in x, y, and z end effector position.
rotation (numpy array): a rotation matrix of shape (3, 3) corresponding
to the desired orientation of the end effector.
Returns:
velocities (numpy array): a flat array of joint velocity commands to apply
to try and achieve the desired input control.
"""
# Sync joint positions for IK.
self.sync_ik_robot(self.robot_jpos_getter())
self.commanded_joint_positions = self.joint_positions_for_eef_command(
dpos, rotation
)
# P controller from joint positions (from IK) to velocities
velocities = np.zeros(7)
deltas = self._get_current_error(
self.robot_jpos_getter(), self.commanded_joint_positions
)
for i, delta in enumerate(deltas):
velocities[i] = -2. * delta # -2. * delta
velocities = self.clip_joint_velocities(velocities)
self.commanded_joint_velocities = velocities
return velocities
def sync_state(self):
"""
Syncs the internal Pybullet robot state to the joint positions of the
robot being controlled.
"""
# sync IK robot state to the current robot joint positions
self.sync_ik_robot(self.robot_jpos_getter())
# make sure target pose is up to date
self.ik_robot_target_pos, self.ik_robot_target_orn = (
self.ik_robot_eef_joint_cartesian_pose()
)
def setup_inverse_kinematics(self):
"""
This function is responsible for doing any setup for inverse kinematics.
Inverse Kinematics maps end effector (EEF) poses to joint angles that
are necessary to achieve those poses.
"""
# Set up a connection to the PyBullet simulator.
p.connect(p.DIRECT)
p.resetSimulation()
# get paths to urdfs
self.robot_urdf = pjoin(
self.bullet_data_path, "panda_description/urdf/panda_arm.urdf"
)
# load the urdfs
self.ik_robot = p.loadURDF(self.robot_urdf, (0, 0, 0.9), useFixedBase=1)
# Simulation will update as fast as it can in real time, instead of waiting for
# step commands like in the non-realtime case.
p.setRealTimeSimulation(1)
def sync_ik_robot(self, joint_positions, simulate=False, sync_last=True):
"""
Force the internal robot model to match the provided joint angles.
Args:
joint_positions (list): a list or flat numpy array of joint positions.
simulate (bool): If True, actually use physics simulation, else
write to physics state directly.
sync_last (bool): If False, don't sync the last joint angle. This
is useful for directly controlling the roll at the end effector.
"""
num_joints = len(joint_positions)
if not sync_last:
num_joints -= 1
for i in range(num_joints):
if simulate:
p.setJointMotorControl2(
self.ik_robot,
i,
p.POSITION_CONTROL,
targetVelocity=0,
targetPosition=joint_positions[i],
force=500,
positionGain=0.5,
velocityGain=1.,
)
else:
p.resetJointState(self.ik_robot, i, joint_positions[i], 0)
def ik_robot_eef_joint_cartesian_pose(self):
"""
Returns the current cartesian pose of the last joint of the ik robot with respect to the base frame as
a (pos, orn) tuple where orn is a x-y-z-w quaternion
"""
eef_pos_in_world = np.array(p.getLinkState(self.ik_robot, 6)[0])
eef_orn_in_world = np.array(p.getLinkState(self.ik_robot, 6)[1])
eef_pose_in_world = T.pose2mat((eef_pos_in_world, eef_orn_in_world))
base_pos_in_world = np.array(p.getBasePositionAndOrientation(self.ik_robot)[0])
base_orn_in_world = np.array(p.getBasePositionAndOrientation(self.ik_robot)[1])
base_pose_in_world = T.pose2mat((base_pos_in_world, base_orn_in_world))
world_pose_in_base = T.pose_inv(base_pose_in_world)
eef_pose_in_base = T.pose_in_A_to_pose_in_B(
pose_A=eef_pose_in_world, pose_A_in_B=world_pose_in_base
)
return T.mat2pose(eef_pose_in_base)
def inverse_kinematics(self, target_position, target_orientation, rest_poses=None):
"""
Helper function to do inverse kinematics for a given target position and
orientation in the PyBullet world frame.
Args:
target_position: A tuple, list, or numpy array of size 3 for position.
target_orientation: A tuple, list, or numpy array of size 4 for
a orientation quaternion.
rest_poses: (optional) A list of size @num_joints to favor ik solutions close by.
Returns:
A list of size @num_joints corresponding to the joint angle solution.
"""
if rest_poses is None:
ik_solution = list(
p.calculateInverseKinematics(
self.ik_robot,
7,
target_position,
targetOrientation=target_orientation,
restPoses=rest_poses,
jointDamping=[0.1] * 8,
)
)
else:
ik_solution = list(
p.calculateInverseKinematics(
self.ik_robot,
7,
target_position,
targetOrientation=target_orientation,
lowerLimits=[-2.8973, -1.7628, -2.8973, -3.0718, -2.8973, -0.0175, -2.8973],
upperLimits=[2.8973, 1.7628, 2.8973, -0.0698, 2.8973, 3.7525, 2.8973],
jointRanges=[5.8, 3.5, 5.8, 3.1, 5.8, 3.8, 5.8],
restPoses=rest_poses,
jointDamping=[0.1] * 8,
)
)
return ik_solution
def bullet_base_pose_to_world_pose(self, pose_in_base):
"""
Convert a pose in the base frame to a pose in the world frame.
Args:
pose_in_base: a (pos, orn) tuple.
Returns:
pose_in world: a (pos, orn) tuple.
"""
pose_in_base = T.pose2mat(pose_in_base)
base_pos_in_world = np.array(p.getBasePositionAndOrientation(self.ik_robot)[0])
base_orn_in_world = np.array(p.getBasePositionAndOrientation(self.ik_robot)[1])
base_pose_in_world = T.pose2mat((base_pos_in_world, base_orn_in_world))
pose_in_world = T.pose_in_A_to_pose_in_B(
pose_A=pose_in_base, pose_A_in_B=base_pose_in_world
)
return T.mat2pose(pose_in_world)
def joint_positions_for_eef_command(self, dpos, rotation):
"""
This function runs inverse kinematics to back out target joint positions
from the provided end effector command.
Same arguments as @get_control.
Returns:
A list of size @num_joints corresponding to the target joint angles.
"""
self.ik_robot_target_pos += dpos * self.user_sensitivity
# this rotation accounts for offsetting the rotation between the final joint and
# the hand link, since the pybullet eef frame is the final joint
# of robot arm, whereas the mujoco eef frame is the hand link, which is rotated
# from the final joint by the 'quat' value for 'right_hand' specified in panda/robot.xml
rotation = rotation.dot(
T.rotation_matrix(angle=-np.pi/4, direction=[0., 0., 1.], point=None)[
:3, :3
]
)
self.ik_robot_target_orn = T.mat2quat(rotation)
# convert from target pose in base frame to target pose in bullet world frame
world_targets = self.bullet_base_pose_to_world_pose(
(self.ik_robot_target_pos, self.ik_robot_target_orn)
)
# Set default rest pose as a neutral down-position over the center of the table
rest_poses = [0, np.pi/6, 0.00, -(np.pi - 2*np.pi/6), 0.00, (np.pi - np.pi/6), np.pi/4]
for bullet_i in range(100):
arm_joint_pos = self.inverse_kinematics(
world_targets[0], world_targets[1], rest_poses=rest_poses
)
self.sync_ik_robot(arm_joint_pos, sync_last=True)
return arm_joint_pos
def _get_current_error(self, current, set_point):
"""
Returns an array of differences between the desired joint positions and current
joint positions. Useful for PID control.
Args:
current: the current joint positions.
set_point: the joint positions that are desired as a numpy array.
Returns:
the current error in the joint positions.
"""
error = current - set_point
return error
def clip_joint_velocities(self, velocities):
"""
Clips joint velocities into a valid range.
"""
for i in range(len(velocities)):
if velocities[i] >= 1.0:
velocities[i] = 1.0
elif velocities[i] <= -1.0:
velocities[i] = -1.0
return velocities