-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtest_manual.py
More file actions
121 lines (107 loc) · 4.71 KB
/
test_manual.py
File metadata and controls
121 lines (107 loc) · 4.71 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
import gym, gym.spaces, gym.utils, gym.utils.seeding
import numpy as np
import sys
from gym.envs.mujoco.mujoco_env import MujocoEnv
# From https://raw.githubusercontent.com/openai/roboschool/master/roboschool/test_manual.py
#
# Run this file to test environments using manual control:
#
# python test_manual.py RoboschoolHopper-v0
#
class TestKeyboardControl:
def __init__(self):
self.keys = {}
self.control = np.zeros(9)
self.human_pause = False
self.human_done = False
def key(self, event_type, key, modifiers):
self.keys[key] = +1 if event_type==6 else 0
#print ("event_type", event_type, "key", key, "modifiers", modifiers)
self.control[0] = self.keys.get(0x1000014, 0) - self.keys.get(0x1000012, 0)
self.control[1] = self.keys.get(0x1000013, 0) - self.keys.get(0x1000015, 0)
self.control[2] = self.keys.get(ord('A'), 0) - self.keys.get(ord('Z'), 0)
self.control[3] = self.keys.get(ord('S'), 0) - self.keys.get(ord('X'), 0)
self.control[4] = self.keys.get(ord('D'), 0) - self.keys.get(ord('C'), 0)
self.control[5] = self.keys.get(ord('F'), 0) - self.keys.get(ord('V'), 0)
self.control[6] = self.keys.get(ord('G'), 0) - self.keys.get(ord('B'), 0)
self.control[7] = self.keys.get(ord('H'), 0) - self.keys.get(ord('N'), 0)
self.control[8] = self.keys.get(ord('J'), 0) - self.keys.get(ord('M'), 0)
if event_type==6 and key==32: # press Space to pause
self.human_pause = 1 - self.human_pause
if event_type==6 and key==0x1000004: # press Enter to restart
self.human_done = True
class TestKeyboardControlMuj:
def __init__(self):
self.keys = {}
self.control = np.zeros(9)
self.human_pause = False
self.human_done = False
def key(self, window, key, scancode, event_type, modifiers):
self.keys[key] = +1 if event_type==1 else 0
# print(key)
#print ("event_type", event_type, "key", key, "modifiers", modifiers)
self.control[0] = self.keys.get(265, 0) - self.keys.get(264, 0)
self.control[1] = self.keys.get(262, 0) - self.keys.get(263, 0)
self.control[2] = self.keys.get(ord('A'), 0) - self.keys.get(ord('Z'), 0)
self.control[3] = self.keys.get(ord('S'), 0) - self.keys.get(ord('X'), 0)
self.control[4] = self.keys.get(ord('D'), 0) - self.keys.get(ord('C'), 0)
self.control[5] = self.keys.get(ord('F'), 0) - self.keys.get(ord('V'), 0)
self.control[6] = self.keys.get(ord('G'), 0) - self.keys.get(ord('B'), 0)
self.control[7] = self.keys.get(ord('H'), 0) - self.keys.get(ord('N'), 0)
self.control[8] = self.keys.get(ord('J'), 0) - self.keys.get(ord('M'), 0)
if event_type==1 and key==32: # press Space to pause
self.human_pause = 1 - self.human_pause
if event_type==1 and key==257: # press Enter to restart
self.human_done = True
usage = """
This is manual test. Usage:
%s <env_id>
Keyboard shortcuts:
* F1 toggle slow motion
* F2 toggle captions
* F3 toggle HUD: observations, actions, reward
* ENTER to restart episode (works only in this test)
* SPACE to pause (works only in this test)
* Up/down, left/right, a/z, s/x, d/c, f/v, g/b, h/n, j/m to control robot (works only in this test)
"""
def test(env_id):
print(usage % sys.argv[0])
env = gym.make(env_id)
# import pdb; pdb.set_trace()
env.reset() # This creates default single player scene
if isinstance(env.unwrapped, MujocoEnv):
ctrl = TestKeyboardControlMuj()
from mujoco_py.glfw import set_key_callback
set_key_callback(env.unwrapped._get_viewer().window, ctrl.key)
else:
raise NotImplementedError
a = np.zeros(env.action_space.shape)
copy_n = min(len(a), len(ctrl.control))
ctrl.human_pause = False
while 1:
ctrl.human_done = False
sn = env.reset()
frame = 0
reward = 0.0
episode_over = False
while 1:
s = sn
a[:copy_n] = ctrl.control[:copy_n]
# import pdb; pdb.set_trace()
sn, rplus, done, info = env.step(a)
reward += rplus
#env.render("rgb_array")
episode_over |= done
still_visible = True
# import pdb; pdb.set_trace()
while True:
env.render("human")
#env.unwrapped.camera.test_window()
if not ctrl.human_pause: break
if ctrl.human_done: break
if not still_visible: break
frame += 1
if not still_visible: break
if __name__=="__main__":
env_id = "RoboschoolHumanoid-v0" if len(sys.argv) <= 1 else sys.argv[1]
test(env_id)