-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
executable file
·222 lines (193 loc) · 6.6 KB
/
utils.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import numpy as np
import math
import json
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def transformationMatrix(phi, theta, psi):
J1_n2 = np.array(
[
[
math.cos(psi) * math.cos(theta),
-math.sin(psi) * math.cos(phi)
+ math.cos(psi) * math.sin(theta) * math.sin(phi),
math.sin(psi) * math.sin(phi)
+ math.cos(psi) * math.cos(phi) * math.sin(theta),
],
[
math.sin(psi) * math.cos(theta),
math.cos(psi) * math.cos(phi)
+ math.sin(phi) * math.sin(theta) * math.sin(psi),
-math.cos(psi) * math.sin(phi)
+ math.sin(theta) * math.sin(psi) * math.cos(phi),
],
[
-math.sin(theta),
math.cos(theta) * math.sin(phi),
math.cos(theta) * math.cos(phi),
],
]
)
J2_n2 = np.array(
[
[1, math.sin(theta) * math.tan(theta), math.cos(phi) * math.tan(theta)],
[0, math.cos(phi), -math.sin(phi)],
[0, math.sin(phi) / math.cos(theta), math.cos(phi) / math.cos(theta)],
]
)
return J1_n2, J2_n2
def unit_vecs(r):
return np.array(
[[0, -r[2][0], r[1][0]], [r[2][0], 0, -r[0][0]], [-r[1][0], r[0][0], 0]]
) # Skew-symmetric matrix
def constants():
g = 9.816
I3 = np.eye(3)
Z3 = np.zeros(3)
i_hat = np.array([[1, 0, 0]]).transpose()
j_hat = np.array([[0, 1, 0]]).transpose()
k_hat = np.array([[0, 0, 1]]).transpose()
return g, I3, Z3, i_hat, j_hat, k_hat
def save_json(vars, path="vars/2d_glider_variables.json"):
with open(path, "w", encoding="utf-8") as file:
json.dump(vars, file, separators=(",", ":"), sort_keys=True, indent=4)
def load_json(path="vars/2d_glider_variables.json"):
file = open(path)
return json.load(file)
def PID(kp, ki, kd, setpoint, measured, integral, dt, derivative):
error = setpoint - measured
P = kp * error
integral = dt * error + integral
I = ki * integral
D = kd * derivative
pid = P + I + D
return pid, integral, error
def plots(t, x, plot):
vel = []
phi = []
theta = []
psi = []
for i in range(len(t)):
v = math.sqrt(math.pow(x[6][i], 2) + math.pow(x[8][i], 2))
phi_angle = math.degrees(x[-3][i])
theta_angle = math.degrees(x[-2][i])
psi_angle = math.degrees(x[-1][i]) % 360
vel.append(v)
phi.append(phi_angle)
theta.append(theta_angle)
psi.append(psi_angle)
vel = np.array(vel)
phi = np.array(phi)
theta = np.array(theta)
psi = np.array(psi)
if plot == ["3D"]:
ax = plt.axes(projection="3d")
ax.plot3D(x[0], x[1], x[2], "gray")
x1, y1, z1 = [0, 200], [0, 70], [0, 70]
ax.plot(x1, y1, z1, color="red")
ax.set_xlabel("x (m)")
ax.set_ylabel("y (m)")
ax.set_zlabel("z (m)")
ax.invert_zaxis()
plt.show()
elif plot == ["all"] or plot == "all":
fig = plt.figure()
ax = fig.add_subplot(2, 2, 1, projection="3d")
ax.plot3D(x[0], x[1], x[2], "gray")
ax.set_xlabel("x (m)")
ax.set_ylabel("y (m)")
ax.set_zlabel("z (m)")
ax.invert_zaxis()
ax = fig.add_subplot(2, 2, 2)
ax.plot(x[0], x[2])
ax.set(xlabel="x (m)", ylabel="z (m)")
ax = fig.add_subplot(2, 2, 3)
ax.plot(x[1], x[2])
ax.set(xlabel="y (m)", ylabel="z (m)")
ax = fig.add_subplot(2, 2, 4)
ax.plot(x[1], x[0])
ax.set(xlabel="y (m)", ylabel="x (m)")
plt.show()
fig, ax = plt.subplots(3, 2)
ax[0, 0].plot(t, x[0])
ax[0, 0].set(xlabel="time (s)", ylabel="x (m)")
ax[1, 0].plot(t, x[1])
ax[1, 0].set(xlabel="time (s)", ylabel="y (m)")
ax[2, 0].plot(t, x[2])
ax[2, 0].set(xlabel="time (s)", ylabel="z (m)")
ax[0, 1].plot(t, phi)
ax[0, 1].set(xlabel="time (s)", ylabel="phi (deg)")
ax[1, 1].plot(t, theta)
ax[1, 1].set(xlabel="time (s)", ylabel="theta (deg)")
ax[2, 1].plot(t, psi)
ax[2, 1].set(xlabel="time (s)", ylabel="psi (deg)")
plt.show()
fig = plt.figure()
ax = fig.add_subplot(4, 2, 1)
ax.plot(t, x[3])
ax.set(xlabel="time (s)", ylabel="Omega1 (rad/s)")
ax = fig.add_subplot(4, 2, 3)
ax.plot(t, x[4])
ax.set(xlabel="time (s)", ylabel="Omega2 (rad/s)")
ax = fig.add_subplot(4, 2, 5)
ax.plot(t, x[5])
ax.set(xlabel="time (s)", ylabel="Omega3 (rad/s)")
ax = fig.add_subplot(4, 2, 2)
ax.plot(t, x[6])
ax.set(xlabel="time (s)", ylabel="v1 (m/s)")
ax = fig.add_subplot(4, 2, 4)
ax.plot(t, x[7])
ax.set(xlabel="time (s)", ylabel="v2 (m/s)")
ax = fig.add_subplot(4, 2, 6)
ax.plot(t, x[8])
ax.set(xlabel="time (s)", ylabel="v3 (m/s)")
ax = fig.add_subplot(4, 1, 4)
ax.plot(t, vel)
ax.set(xlabel="time (s)", ylabel="velocity (m/s)")
plt.show()
fig, ax = plt.subplots(2, 2)
ax[0, 0].plot(t, x[9])
ax[0, 0].set(xlabel="time (s)", ylabel="rp1 (m)")
ax[1, 0].plot(t, x[10])
ax[1, 0].set(xlabel="time (s)", ylabel="rp2 (m)")
ax[0, 1].plot(t, x[11])
ax[0, 1].set(xlabel="time (s)", ylabel="rp3 (m)")
ax[1, 1].plot(t, x[21])
ax[1, 1].set(xlabel="time (s)", ylabel="mb (kg)")
plt.show()
else:
for p in plot:
if p == "x":
plt.plot(t, x[0])
elif p == "y":
plt.plot(t, x[1])
elif p == "z":
plt.plot(t, x[2])
elif p == "omega1":
plt.plot(t, x[3])
elif p == "omega2":
plt.plot(t, x[4])
elif p == "omega3":
plt.plot(t, x[5])
elif p == "v1":
plt.plot(t, x[6])
elif p == "v2":
plt.plot(t, x[7])
elif p == "v3":
plt.plot(t, x[8])
elif p == "vel":
plt.plot(t, vel)
elif p == "rp1":
plt.plot(t, x[9])
elif p == "rp2":
plt.plot(t, x[10])
elif p == "rp3":
plt.plot(t, x[11])
elif p == "mb":
plt.plot(t, x[21])
elif p == "phi":
plt.plot(t, phi)
elif p == "theta":
plt.plot(t, theta)
elif p == "psi":
plt.plot(t, psi)
plt.show()