forked from MarkFzp/mobile-aloha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreal_env.py
277 lines (229 loc) · 12.4 KB
/
real_env.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
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
import time
import numpy as np
import collections
import matplotlib.pyplot as plt
import dm_env
from pyquaternion import Quaternion
from constants import DT, START_ARM_POSE, MASTER_GRIPPER_JOINT_NORMALIZE_FN, PUPPET_GRIPPER_JOINT_UNNORMALIZE_FN
from constants import PUPPET_GRIPPER_POSITION_NORMALIZE_FN, PUPPET_GRIPPER_VELOCITY_NORMALIZE_FN
from constants import PUPPET_GRIPPER_JOINT_OPEN, PUPPET_GRIPPER_JOINT_CLOSE
from aloha_scripts.robot_utils import Recorder, ImageRecorder
from aloha_scripts.robot_utils import setup_master_bot, setup_puppet_bot, move_arms, move_grippers
from interbotix_xs_modules.arm import InterbotixManipulatorXS
from interbotix_xs_msgs.msg import JointSingleCommand
import pyrealsense2 as rs
import pyagxrobots
from dynamixel_client import DynamixelClient
import IPython
e = IPython.embed
class RealEnv:
"""
Environment for real robot bi-manual manipulation
Action space: [left_arm_qpos (6), # absolute joint position
left_gripper_positions (1), # normalized gripper position (0: close, 1: open)
right_arm_qpos (6), # absolute joint position
right_gripper_positions (1),] # normalized gripper position (0: close, 1: open)
Observation space: {"qpos": Concat[ left_arm_qpos (6), # absolute joint position
left_gripper_position (1), # normalized gripper position (0: close, 1: open)
right_arm_qpos (6), # absolute joint position
right_gripper_qpos (1)] # normalized gripper position (0: close, 1: open)
"qvel": Concat[ left_arm_qvel (6), # absolute joint velocity (rad)
left_gripper_velocity (1), # normalized gripper velocity (pos: opening, neg: closing)
right_arm_qvel (6), # absolute joint velocity (rad)
right_gripper_qvel (1)] # normalized gripper velocity (pos: opening, neg: closing)
"images": {"cam_high": (480x640x3), # h, w, c, dtype='uint8'
"cam_low": (480x640x3), # h, w, c, dtype='uint8'
"cam_left_wrist": (480x640x3), # h, w, c, dtype='uint8'
"cam_right_wrist": (480x640x3)} # h, w, c, dtype='uint8'
"""
def __init__(self, init_node, setup_robots=True, setup_base=False):
self.puppet_bot_left = InterbotixManipulatorXS(robot_model="vx300s", group_name="arm", gripper_name="gripper",
robot_name=f'puppet_left', init_node=init_node)
self.puppet_bot_right = InterbotixManipulatorXS(robot_model="vx300s", group_name="arm", gripper_name="gripper",
robot_name=f'puppet_right', init_node=False)
if setup_robots:
self.setup_robots()
if setup_base:
self.setup_base()
# self.setup_t265()
self.setup_dxl()
self.recorder_left = Recorder('left', init_node=False)
self.recorder_right = Recorder('right', init_node=False)
self.image_recorder = ImageRecorder(init_node=False)
self.gripper_command = JointSingleCommand(name="gripper")
def setup_t265(self):
self.pipeline = rs.pipeline()
cfg = rs.config()
# if only pose stream is enabled, fps is higher (202 vs 30)
cfg.enable_stream(rs.stream.pose)
self.pipeline.start(cfg)
def setup_dxl(self):
self.dxl_client = DynamixelClient([1, 2], port='/dev/ttyDXL_wheels', lazy_connect=True)
self.wheel_r = 0.101 / 2 # 101 mm is the diameter
self.base_r = 0.622 # 622 mm is the distance between the two wheels
def setup_base(self):
self.tracer = pyagxrobots.pysdkugv.TracerBase()
self.tracer.EnableCAN()
def setup_robots(self):
setup_puppet_bot(self.puppet_bot_left)
setup_puppet_bot(self.puppet_bot_right)
def get_qpos(self):
left_qpos_raw = self.recorder_left.qpos
right_qpos_raw = self.recorder_right.qpos
left_arm_qpos = left_qpos_raw[:6]
right_arm_qpos = right_qpos_raw[:6]
left_gripper_qpos = [PUPPET_GRIPPER_POSITION_NORMALIZE_FN(left_qpos_raw[7])] # this is position not joint
right_gripper_qpos = [PUPPET_GRIPPER_POSITION_NORMALIZE_FN(right_qpos_raw[7])] # this is position not joint
return np.concatenate([left_arm_qpos, left_gripper_qpos, right_arm_qpos, right_gripper_qpos])
def get_qvel(self):
left_qvel_raw = self.recorder_left.qvel
right_qvel_raw = self.recorder_right.qvel
left_arm_qvel = left_qvel_raw[:6]
right_arm_qvel = right_qvel_raw[:6]
left_gripper_qvel = [PUPPET_GRIPPER_VELOCITY_NORMALIZE_FN(left_qvel_raw[7])]
right_gripper_qvel = [PUPPET_GRIPPER_VELOCITY_NORMALIZE_FN(right_qvel_raw[7])]
return np.concatenate([left_arm_qvel, left_gripper_qvel, right_arm_qvel, right_gripper_qvel])
def get_effort(self):
left_effort_raw = self.recorder_left.effort
right_effort_raw = self.recorder_right.effort
left_robot_effort = left_effort_raw[:7]
right_robot_effort = right_effort_raw[:7]
return np.concatenate([left_robot_effort, right_robot_effort])
def get_images(self):
return self.image_recorder.get_images()
def get_base_vel_t265(self):
raise NotImplementedError
frames = self.pipeline.wait_for_frames()
pose_frame = frames.get_pose_frame()
pose = pose_frame.get_pose_data()
q1 = Quaternion(w=pose.rotation.w, x=pose.rotation.x, y=pose.rotation.y, z=pose.rotation.z)
rotation = -np.array(q1.yaw_pitch_roll)[0]
rotation_vec = np.array([np.cos(rotation), np.sin(rotation)])
linear_vel_vec = np.array([pose.velocity.z, pose.velocity.x])
is_forward = rotation_vec.dot(linear_vel_vec) > 0
base_linear_vel = np.sqrt(pose.velocity.z ** 2 + pose.velocity.x ** 2) * (1 if is_forward else -1)
base_angular_vel = pose.angular_velocity.y
return np.array([base_linear_vel, base_angular_vel])
def get_base_vel(self):
left_vel, right_vel = self.dxl_client.read_pos_vel_cur()[1]
right_vel = -right_vel # right wheel is inverted
base_linear_vel = (left_vel + right_vel) * self.wheel_r / 2
base_angular_vel = (right_vel - left_vel) * self.wheel_r / self.base_r
return np.array([base_linear_vel, base_angular_vel])
def get_tracer_vel(self):
linear_vel, angular_vel = self.tracer.GetLinearVelocity(), self.tracer.GetAngularVelocity()
return np.array([linear_vel, angular_vel])
def set_gripper_pose(self, left_gripper_desired_pos_normalized, right_gripper_desired_pos_normalized):
left_gripper_desired_joint = PUPPET_GRIPPER_JOINT_UNNORMALIZE_FN(left_gripper_desired_pos_normalized)
self.gripper_command.cmd = left_gripper_desired_joint
self.puppet_bot_left.gripper.core.pub_single.publish(self.gripper_command)
right_gripper_desired_joint = PUPPET_GRIPPER_JOINT_UNNORMALIZE_FN(right_gripper_desired_pos_normalized)
self.gripper_command.cmd = right_gripper_desired_joint
self.puppet_bot_right.gripper.core.pub_single.publish(self.gripper_command)
def _reset_joints(self):
reset_position = START_ARM_POSE[:6]
move_arms([self.puppet_bot_left, self.puppet_bot_right], [reset_position, reset_position], move_time=1)
def _reset_gripper(self):
"""Set to position mode and do position resets: first open then close. Then change back to PWM mode"""
move_grippers([self.puppet_bot_left, self.puppet_bot_right], [PUPPET_GRIPPER_JOINT_OPEN] * 2, move_time=0.5)
move_grippers([self.puppet_bot_left, self.puppet_bot_right], [PUPPET_GRIPPER_JOINT_CLOSE] * 2, move_time=1)
def get_observation(self, get_tracer_vel=False):
obs = collections.OrderedDict()
obs['qpos'] = self.get_qpos()
obs['qvel'] = self.get_qvel()
obs['effort'] = self.get_effort()
obs['images'] = self.get_images()
# obs['base_vel_t265'] = self.get_base_vel_t265()
obs['base_vel'] = self.get_base_vel()
if get_tracer_vel:
obs['tracer_vel'] = self.get_tracer_vel()
return obs
def get_reward(self):
return 0
def reset(self, fake=False):
if not fake:
# Reboot puppet robot gripper motors
self.puppet_bot_left.dxl.robot_reboot_motors("single", "gripper", True)
self.puppet_bot_right.dxl.robot_reboot_motors("single", "gripper", True)
self._reset_joints()
self._reset_gripper()
return dm_env.TimeStep(
step_type=dm_env.StepType.FIRST,
reward=self.get_reward(),
discount=None,
observation=self.get_observation())
def step(self, action, base_action=None, get_tracer_vel=False, get_obs=True):
state_len = int(len(action) / 2)
left_action = action[:state_len]
right_action = action[state_len:]
self.puppet_bot_left.arm.set_joint_positions(left_action[:6], blocking=False)
self.puppet_bot_right.arm.set_joint_positions(right_action[:6], blocking=False)
self.set_gripper_pose(left_action[-1], right_action[-1])
if base_action is not None:
# linear_vel_limit = 1.5
# angular_vel_limit = 1.5
# base_action_linear = np.clip(base_action[0], -linear_vel_limit, linear_vel_limit)
# base_action_angular = np.clip(base_action[1], -angular_vel_limit, angular_vel_limit)
base_action_linear, base_action_angular = base_action
self.tracer.SetMotionCommand(linear_vel=base_action_linear, angular_vel=base_action_angular)
# time.sleep(DT)
if get_obs:
obs = self.get_observation(get_tracer_vel)
else:
obs = None
return dm_env.TimeStep(
step_type=dm_env.StepType.MID,
reward=self.get_reward(),
discount=None,
observation=obs)
def get_action(master_bot_left, master_bot_right):
action = np.zeros(14) # 6 joint + 1 gripper, for two arms
# Arm actions
action[:6] = master_bot_left.dxl.joint_states.position[:6]
action[7:7+6] = master_bot_right.dxl.joint_states.position[:6]
# Gripper actions
action[6] = MASTER_GRIPPER_JOINT_NORMALIZE_FN(master_bot_left.dxl.joint_states.position[6])
action[7+6] = MASTER_GRIPPER_JOINT_NORMALIZE_FN(master_bot_right.dxl.joint_states.position[6])
return action
# def get_base_action():
def make_real_env(init_node, setup_robots=True, setup_base=False):
env = RealEnv(init_node, setup_robots, setup_base)
return env
def test_real_teleop():
"""
Test bimanual teleoperation and show image observations onscreen.
It first reads joint poses from both master arms.
Then use it as actions to step the environment.
The environment returns full observations including images.
An alternative approach is to have separate scripts for teleoperation and observation recording.
This script will result in higher fidelity (obs, action) pairs
"""
onscreen_render = True
render_cam = 'cam_left_wrist'
# source of data
master_bot_left = InterbotixManipulatorXS(robot_model="wx250s", group_name="arm", gripper_name="gripper",
robot_name=f'master_left', init_node=True)
master_bot_right = InterbotixManipulatorXS(robot_model="wx250s", group_name="arm", gripper_name="gripper",
robot_name=f'master_right', init_node=False)
setup_master_bot(master_bot_left)
setup_master_bot(master_bot_right)
# setup the environment
env = make_real_env(init_node=False)
ts = env.reset(fake=True)
episode = [ts]
# setup visualization
if onscreen_render:
ax = plt.subplot()
plt_img = ax.imshow(ts.observation['images'][render_cam])
plt.ion()
for t in range(1000):
action = get_action(master_bot_left, master_bot_right)
ts = env.step(action)
episode.append(ts)
if onscreen_render:
plt_img.set_data(ts.observation['images'][render_cam])
plt.pause(DT)
else:
time.sleep(DT)
if __name__ == '__main__':
test_real_teleop()