-
Notifications
You must be signed in to change notification settings - Fork 3
/
pid_test.py
58 lines (47 loc) · 1.39 KB
/
pid_test.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
from mujoco_py import load_model_from_path, MjSim, MjViewer, cymj
import numpy as np
import matplotlib.pyplot as plt
def mj_viewer_setup(sim):
viewer = MjViewer(sim)
viewer.cam.azimuth = 0
sim.forward()
viewer.cam.distance = 1
viewer._run_speed = 0.25
return viewer
def mj_render(viewer):
viewer.render()
visualize = False
# ---------------- Model to select ----------------
# model = load_model_from_path("position_control/env_position_control.xml")
# model = load_model_from_path("P_control/env_P_control.xml")
model = load_model_from_path("PID_control/env_PID_control.xml")
# ---------------- Program ----------------
sim = MjSim(model)
cymj.set_pid_control(sim.model, sim.data)
if visualize:
viewer = mj_viewer_setup(sim)
dt = sim.model.opt.timestep
sim.reset()
sim.data.ctrl[:] = 0
points = []
reference = []
for t in range(800): # 200 is maximal episode steps count
if t > 50:
sim.data.ctrl[9] = 0.3
if t > 250:
sim.data.ctrl[9] = 0.6
if t > 500:
sim.data.ctrl[9] = 0.3
sim.step()
points.append(sim.data.qpos[9])
reference.append(sim.data.ctrl[9])
if visualize:
mj_render(viewer)
fig, ax = plt.subplots()
x = np.array(range(1, len(points) + 1)) * dt
ax.plot(x, points, label="output")
ax.plot(x, reference, label="reference")
ax.set(xlabel='time [s]', ylabel='joint position [rad]')
ax.grid()
ax.legend()
plt.show()