-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_sim3d.py
More file actions
85 lines (65 loc) · 2 KB
/
example_sim3d.py
File metadata and controls
85 lines (65 loc) · 2 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
import gymnasium as gym
import matplotlib.pyplot as plt
import numpy as np
import threading
import time
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as animation
import env_wrapper
env_wrapper.register_env()
env = gym.make("RocketSim-v0")
observation, _ = env.reset()
# lists to store historical data
x = []
y = []
z = []
# function to update the plot
def create_plot():
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel('Time')
ax.set_ylabel('Altitude')
ax.set_zlabel('Displacement')
plt.title("Rocket Simulation")
# Plot the ground as a flat plane at z=0
X = np.arange(-0.2, 0.2, 0.25)
Y = np.arange(-0.2, 0.2, 0.25)
X, Y = np.meshgrid(X, Y)
Z = np.zeros(X.shape)
ax.plot_surface(X, Y, Z, alpha=0.5)
create_plot()
fig = plt.gcf()
def animate(i):
plt.cla()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.title("SATURN Rocket Simulation")
ax.plot(x, y, z)
# ground as a flat plane z=0
X = np.arange(-0.2, 0.2, 0.25)
Y = np.arange(-0.2, 0.2, 0.25)
X, Y = np.meshgrid(X, Y)
Z = np.zeros(X.shape)
ax.plot_surface(X, Y, Z, alpha=0.5)
ani = animation.FuncAnimation(fig, animate, interval=1000)
def data_collection():
while True:
action = env.action_space.sample()
observation, reward, terminated, truncated, _ = env.step(action)
x.append(observation["displacement"][0])
y.append(observation["displacement"][1])
z.append(observation["displacement"][2])
# ADD YOUR RL CODE HERE
time.sleep(1)
if terminated or truncated:
print()
print("----------------- Env Reset -----------------")
observation, _ = env.reset()
break
# data collection in a separate thread to plot
data_thread = threading.Thread(target=data_collection)
data_thread.daemon = True
data_thread.start()
plt.show()