-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathreal_env.py
82 lines (70 loc) · 2.82 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
# Author: Jimmy Wu
# Date: October 2024
from cameras import KinovaCamera, LogitechCamera
from constants import BASE_RPC_HOST, BASE_RPC_PORT, ARM_RPC_HOST, ARM_RPC_PORT, RPC_AUTHKEY
from constants import BASE_CAMERA_SERIAL
from arm_server import ArmManager
from base_server import BaseManager
class RealEnv:
def __init__(self):
# RPC server connection for base
base_manager = BaseManager(address=(BASE_RPC_HOST, BASE_RPC_PORT), authkey=RPC_AUTHKEY)
try:
base_manager.connect()
except ConnectionRefusedError as e:
raise Exception('Could not connect to base RPC server, is base_server.py running?') from e
# RPC server connection for arm
arm_manager = ArmManager(address=(ARM_RPC_HOST, ARM_RPC_PORT), authkey=RPC_AUTHKEY)
try:
arm_manager.connect()
except ConnectionRefusedError as e:
raise Exception('Could not connect to arm RPC server, is arm_server.py running?') from e
# RPC proxy objects
self.base = base_manager.Base(max_vel=(0.5, 0.5, 1.57), max_accel=(0.5, 0.5, 1.57))
self.arm = arm_manager.Arm()
# Cameras
self.base_camera = LogitechCamera(BASE_CAMERA_SERIAL)
self.wrist_camera = KinovaCamera()
def get_obs(self):
obs = {}
obs.update(self.base.get_state())
obs.update(self.arm.get_state())
obs['base_image'] = self.base_camera.get_image()
obs['wrist_image'] = self.wrist_camera.get_image()
return obs
def reset(self):
print('Resetting base...')
self.base.reset()
print('Resetting arm...')
self.arm.reset()
print('Robot has been reset')
def step(self, action):
# Note: We intentionally do not return obs here to prevent the policy from using outdated data
self.base.execute_action(action) # Non-blocking
self.arm.execute_action(action) # Non-blocking
def close(self):
self.base.close()
self.arm.close()
self.base_camera.close()
self.wrist_camera.close()
if __name__ == '__main__':
import time
import numpy as np
from constants import POLICY_CONTROL_PERIOD
env = RealEnv()
try:
while True:
env.reset()
for _ in range(100):
action = {
'base_pose': 0.1 * np.random.rand(3) - 0.05,
'arm_pos': 0.1 * np.random.rand(3) + np.array([0.55, 0.0, 0.4]),
'arm_quat': np.random.rand(4),
'gripper_pos': np.random.rand(1),
}
env.step(action)
obs = env.get_obs()
print([(k, v.shape) if v.ndim == 3 else (k, v) for (k, v) in obs.items()])
time.sleep(POLICY_CONTROL_PERIOD) # Note: Not precise
finally:
env.close()