-
Notifications
You must be signed in to change notification settings - Fork 25
/
manual_control.py
executable file
·142 lines (112 loc) · 3.81 KB
/
manual_control.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
#!/usr/bin/env python
# manual
"""
This script allows you to manually control the simulator or Duckiebot
using the keyboard arrows.
"""
from PIL import Image
import argparse
import sys
import gym
import numpy as np
import pyglet
from pyglet.window import key
from gym_duckietown.envs import DuckietownEnv
# from experiments.utils import save_img
parser = argparse.ArgumentParser()
parser.add_argument("--env-name", default=None)
parser.add_argument("--map-name", default="udem1")
parser.add_argument("--distortion", default=False, action="store_true")
parser.add_argument("--camera_rand", default=False, action="store_true")
parser.add_argument("--draw-curve", action="store_true", help="draw the lane following curve")
parser.add_argument("--draw-bbox", action="store_true", help="draw collision detection bounding boxes")
parser.add_argument("--domain-rand", action="store_true", help="enable domain randomization")
parser.add_argument("--dynamics_rand", action="store_true", help="enable dynamics randomization")
parser.add_argument("--frame-skip", default=1, type=int, help="number of frames to skip")
parser.add_argument("--seed", default=1, type=int, help="seed")
args = parser.parse_args()
if args.env_name and args.env_name.find("Duckietown") != -1:
env = DuckietownEnv(
seed=args.seed,
map_name=args.map_name,
draw_curve=args.draw_curve,
draw_bbox=args.draw_bbox,
domain_rand=args.domain_rand,
frame_skip=args.frame_skip,
distortion=args.distortion,
camera_rand=args.camera_rand,
dynamics_rand=args.dynamics_rand,
)
else:
env = gym.make(args.env_name)
env.reset()
env.render()
@env.unwrapped.window.event
def on_key_press(symbol, modifiers):
"""
This handler processes keyboard commands that
control the simulation
"""
if symbol == key.BACKSPACE or symbol == key.SLASH:
print("RESET")
env.reset()
env.render()
elif symbol == key.PAGEUP:
env.unwrapped.cam_angle[0] = 0
elif symbol == key.ESCAPE:
env.close()
sys.exit(0)
# Take a screenshot
# UNCOMMENT IF NEEDED - Skimage dependency
# elif symbol == key.RETURN:
# print('saving screenshot')
# img = env.render('rgb_array')
# save_img('screenshot.png', img)
# Register a keyboard handler
key_handler = key.KeyStateHandler()
env.unwrapped.window.push_handlers(key_handler)
def update(dt):
"""
This function is called at every frame to handle
movement/stepping and redrawing
"""
wheel_distance = 0.102
min_rad = 0.08
action = np.array([0.0, 0.0])
if key_handler[key.UP]:
action += np.array([0.44, 0.0])
if key_handler[key.DOWN]:
action -= np.array([0.44, 0])
if key_handler[key.LEFT]:
action += np.array([0, 1])
if key_handler[key.RIGHT]:
action -= np.array([0, 1])
if key_handler[key.SPACE]:
action = np.array([0, 0])
v1 = action[0]
v2 = action[1]
# Limit radius of curvature
if v1 == 0 or abs(v2 / v1) > (min_rad + wheel_distance / 2.0) / (min_rad - wheel_distance / 2.0):
# adjust velocities evenly such that condition is fulfilled
delta_v = (v2 - v1) / 2 - wheel_distance / (4 * min_rad) * (v1 + v2)
v1 += delta_v
v2 -= delta_v
action[0] = v1
action[1] = v2
# Speed boost
if key_handler[key.LSHIFT]:
action *= 1.5
obs, reward, done, info = env.step(action)
print("step_count = %s, reward=%.3f" % (env.unwrapped.step_count, reward))
if key_handler[key.RETURN]:
im = Image.fromarray(obs)
im.save("screen.png")
if done:
print("done!")
env.reset()
env.render()
env.render()
pyglet.clock.schedule_interval(update, 1.0 / env.unwrapped.frame_rate)
# Enter main event loop
pyglet.app.run()
env.close()