Skip to content

Commit 377aaab

Browse files
committed
Keyboard humanoid demos
1 parent 2560bf0 commit 377aaab

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

agent_zoo/demo_keyboard_humanoid1.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import gym, roboschool, sys, os
2+
import numpy as np
3+
import pyglet, pyglet.window as pw, pyglet.window.key as pwk
4+
from pyglet import gl
5+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
6+
import tensorflow as tf
7+
8+
#
9+
# This opens a test window (not chase camera), allows to control humanoid using keyboard.
10+
#
11+
12+
def demo_run():
13+
env = gym.make("RoboschoolHumanoidFlagrun-v1")
14+
15+
config = tf.ConfigProto(
16+
inter_op_parallelism_threads=1,
17+
intra_op_parallelism_threads=1,
18+
device_count = { "GPU": 0 } )
19+
sess = tf.InteractiveSession(config=config)
20+
21+
from RoboschoolHumanoidFlagrunHarder_v1_2017jul import ZooPolicyTensorflow
22+
pi = ZooPolicyTensorflow("humanoid1", env.observation_space, env.action_space)
23+
24+
class TestKeyboardControl:
25+
def __init__(self):
26+
self.keys = {}
27+
self.control = np.zeros(2)
28+
def key(self, event_type, key, modifiers):
29+
self.keys[key] = +1 if event_type==6 else 0
30+
#print ("event_type", event_type, "key", key, "modifiers", modifiers)
31+
self.control[0] = self.keys.get(0x1000014, 0) - self.keys.get(0x1000012, 0)
32+
self.control[1] = self.keys.get(0x1000013, 0) - self.keys.get(0x1000015, 0)
33+
34+
obs = env.reset()
35+
eu = env.unwrapped
36+
37+
still_open = env.render("human") # This creates window to set callbacks on
38+
ctrl = TestKeyboardControl()
39+
eu.scene.cpp_world.set_key_callback(ctrl.key)
40+
41+
while 1:
42+
a = pi.act(obs, env)
43+
44+
if (ctrl.control != 0).any():
45+
eu.walk_target_x = eu.body_xyz[0] + 2.0*ctrl.control[0]
46+
eu.walk_target_y = eu.body_xyz[1] + 2.0*ctrl.control[1]
47+
eu.flag = eu.scene.cpp_world.debug_sphere(eu.walk_target_x, eu.walk_target_y, 0.2, 0.2, 0xFF8080)
48+
eu.flag_timeout = 100500
49+
50+
obs, r, done, _ = env.step(a)
51+
still_open = env.render("human")
52+
if still_open==False:
53+
return
54+
55+
if __name__=="__main__":
56+
demo_run()

agent_zoo/demo_keyboard_humanoid2.py

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import gym, roboschool, sys, os
2+
import numpy as np
3+
import pyglet, pyglet.window as pw, pyglet.window.key as pwk
4+
from pyglet import gl
5+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
6+
import tensorflow as tf
7+
8+
#
9+
# This opens a third-party window (not test window), shows rendered chase camera, allows to control humanoid
10+
# using keyboard (in a different way)
11+
#
12+
13+
class PygletInteractiveWindow(pw.Window):
14+
def __init__(self, env):
15+
pw.Window.__init__(self, width=600, height=400, vsync=False, resizable=True)
16+
self.theta = 0
17+
self.still_open = True
18+
19+
@self.event
20+
def on_close():
21+
self.still_open = False
22+
23+
@self.event
24+
def on_resize(width, height):
25+
self.win_w = width
26+
self.win_h = height
27+
28+
self.keys = {}
29+
self.human_pause = False
30+
self.human_done = False
31+
32+
def imshow(self, arr):
33+
H, W, C = arr.shape
34+
assert C==3
35+
image = pyglet.image.ImageData(W, H, 'RGB', arr.tobytes(), pitch=W*-3)
36+
self.clear()
37+
self.switch_to()
38+
self.dispatch_events()
39+
texture = image.get_texture()
40+
gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST)
41+
texture.width = W
42+
texture.height = H
43+
texture.blit(0, 0, width=self.win_w, height=self.win_h)
44+
self.flip()
45+
46+
def on_key_press(self, key, modifiers):
47+
self.keys[key] = +1
48+
if key==pwk.ESCAPE: self.still_open = False
49+
50+
def on_key_release(self, key, modifiers):
51+
self.keys[key] = 0
52+
53+
def each_frame(self):
54+
self.theta += 0.05 * (self.keys.get(pwk.LEFT, 0) - self.keys.get(pwk.RIGHT, 0))
55+
56+
def demo_run():
57+
env = gym.make("RoboschoolHumanoidFlagrun-v1")
58+
59+
config = tf.ConfigProto(
60+
inter_op_parallelism_threads=1,
61+
intra_op_parallelism_threads=1,
62+
device_count = { "GPU": 0 } )
63+
sess = tf.InteractiveSession(config=config)
64+
65+
from RoboschoolHumanoidFlagrunHarder_v1_2017jul import ZooPolicyTensorflow
66+
pi = ZooPolicyTensorflow("humanoid1", env.observation_space, env.action_space)
67+
68+
control_me = PygletInteractiveWindow(env.unwrapped)
69+
70+
env.reset()
71+
eu = env.unwrapped
72+
73+
obs = env.reset()
74+
75+
while 1:
76+
a = pi.act(obs, env)
77+
78+
x, y, z = eu.body_xyz
79+
eu.walk_target_x = x + 1.1*np.cos(control_me.theta) # 1.0 or less will trigger flag reposition by env itself
80+
eu.walk_target_y = y + 1.1*np.sin(control_me.theta)
81+
eu.flag = eu.scene.cpp_world.debug_sphere(eu.walk_target_x, eu.walk_target_y, 0.2, 0.2, 0xFF8080)
82+
eu.flag_timeout = 100500
83+
84+
obs, r, done, _ = env.step(a)
85+
img = env.render("rgb_array")
86+
control_me.imshow(img)
87+
control_me.each_frame()
88+
if control_me.still_open==False: break
89+
90+
if __name__=="__main__":
91+
demo_run()

roboschool/cpp-household/roboschool.files

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ python-binding.cpp
3636
../../agent_zoo/demo_race1.py
3737
../../agent_zoo/demo_race2.py
3838
../../agent_zoo/demo_pong.py
39+
../../agent_zoo/demo_keyboard_humanoid1.py
40+
../../agent_zoo/demo_keyboard_humanoid2.py
3941

4042
glsl/common.h
4143
glsl/displaytex.frag.glsl

0 commit comments

Comments
 (0)