-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.py
executable file
·91 lines (79 loc) · 2.37 KB
/
render.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
from __future__ import absolute_import, print_function, division
from rotary_pendulum_pybullet import RotaryPendulumPyBulletEnv
from rotary_pendulum import RotaryPendulumEnv, RotaryPendulumSwingupEnv
from gym_brt.control import (
NoControl,
RandomControl,
QubeFlipUpControl,
QubeHoldControl,
) # , \
# QubeDampenControl
import numpy as np
import argparse
import gym
import time
def main():
# Get arguments to quickly test the environments
parser = argparse.ArgumentParser(
description="Renders a Gym environment for quick inspection."
)
parser.add_argument(
"--pybullet",
"-pb",
action="store_true",
help="Whether to use Mujoco (default) or PyBullet.",
)
parser.add_argument(
"--begin_down",
"-bd",
action="store_true",
help="Whether to start the pendulum at the top (default) or at the bottom",
)
parser.add_argument(
"--controller",
"-c",
type=str,
default="flip",
choices=["none", "rand", "flip", "hold"],
help="The controller to use.",
)
parser.add_argument(
"--frequency",
"-f",
default="1000",
type=float,
help="The frequency of the simulation.",
)
args = parser.parse_args()
# A list of available classical controllers
controllers_dict = {
"none": NoControl,
"rand": RandomControl,
"flip": QubeFlipUpControl,
"hold": QubeHoldControl,
}
controller = controllers_dict[args.controller]
# Select Mujoco or PyBullet to run the simulation
if args.pybullet:
env = RotaryPendulumPyBulletEnv() # PyBullet Env
else:
if args.begin_down:
env = RotaryPendulumSwingupEnv() # Mujoco env
else:
env = RotaryPendulumEnv() # Mujoco env
run_simulation(env, controller)
def run_simulation(env, controller):
env.render(mode="human") # Needed for pybullet for some reason...
ctrl_sys = controller(env, frequency=args.frequency) # Set the controller
obs = env.reset() # Get the initial observation
try:
while True:
action = ctrl_sys.action(obs)
obs, reward, done, _ = env.step(action)
env.render("human")
if done:
obs = env.reset()
finally:
env.close()
if __name__ == "__main__":
main()