forked from blksail-edu/python-refresher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphysics.py
More file actions
293 lines (237 loc) · 9.08 KB
/
physics.py
File metadata and controls
293 lines (237 loc) · 9.08 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import numpy as np
import matplotlib.pyplot as plt
g = 9.81
base_atmospheric_pressure = 101325
def calculate_buoyancy(V, density_fluid):
"""Calculate buoyant force.
V -- volume of fluid |
density_fluid -- density of fluid
"""
if V <= 0 or density_fluid <= 0:
raise ValueError("Volume or density cannot be <= 0.")
buoyant_force = density_fluid * V * g
return buoyant_force
def will_it_float(V, mass):
"""Checks whether object with given mass floating in a fluid of given volume (by Archimedes' principle) will float.
V -- volume of object |
mass -- mass of object
"""
if V <= 0 or mass <= 0:
raise ValueError("Volume or mass cannot be <= 0.")
buoyant_force = V * 1000 * g
weight = mass * g
return buoyant_force > weight
def calculate_pressure(depth):
"""Calculates pressure at given depth.
depth -- depth of object in water
"""
if depth <= 0:
raise ValueError("Depth must be positive value.")
pressure = 1000 * g * depth + base_atmospheric_pressure
return pressure
def calculate_acceleration(F, mass):
"""
Calculates acceleration given force and mass.
F - force applied on object |
mass - mass of object
"""
if mass <= 0:
raise ValueError("Mass must be positive.")
acceleration = F / mass
return acceleration
def calculate_angular_acceleration(tau, I):
"""
Calculates angular acceleration given torque and moment of inertia.
tau - torque applied on object |
I - moment of inertia of object
"""
if I <= 0:
raise ValueError("Moment of inertia must be positive.")
angular_acceleration = tau / I
return angular_acceleration
def calculate_torque(F_magnitude, F_direction, r):
"""
Calculates torque given magnitude and direction of force and distance from center of mass .
F_magnitude - magnitude of force applied on object |
F_direction - direction of force applied on object |
r - distance of force applied from center of mass
"""
if F_magnitude < 0 or r < 0:
raise ValueError(
"Magnitude of force and distance to center of mass must be positive"
)
torque = F_magnitude * np.sin(np.radians(F_direction)) * r
return torque
def calculate_moment_of_inertia(m, r):
"""
Calculates moment of inertia given mass and distance from axis of rotation to center of mass.
m - mass of object |
r - distance from axis of rotation to center of mass
"""
if m <= 0 or r < 0:
raise ValueError(
"Mass must be positive and distance to center of mass must be nonnegative."
)
moment_of_inertia = m * pow(r, 2)
return moment_of_inertia
def calculate_auv_acceleration(
F_magnitude, F_angle, mass=100, volume=0.1, thruster_distance=0.5
):
"""
Calculates acceleration of AUV given magnitude of force exerted by thruster and mass of AUV.
F_magnitude - Magnitude of force exerted by thruster |
F_angle - Angle of force applied by the thruster in degrees
mass - (optional), mass of the AUV |
volume - (optional), volume of the AUV |
thruster_distance - (optional), distance of thruster from center of mass of AUV
"""
if F_magnitude < 0 or mass <= 0:
raise ValueError(
"Magnitude of force must be positive and mass must be positive."
)
auv_acceleration = np.array(
[
F_magnitude * np.cos(np.radians(F_angle)) / mass,
F_magnitude * np.sin(np.radians(F_angle)) / mass,
],
dtype=float,
)
return auv_acceleration
def calculate_auv_angular_acceleration(
F_magnitude, F_angle, inertia=1, thruster_distance=0.5
):
"""
Calculates angular acceleration of AUV given magnitude of force exerted by thruster and angle of force respective to center of mass.
F_magnitude - Magnitude of force exerted by thruster |
F_angle - Angle of thruster exerting force on AUV |
inertia - (optional), moment of inertia of AUV |
thruster_distance (optional), distance of thruster from center of mass from AUV
"""
if F_magnitude < 0:
raise ValueError("Magnitude of force must be positive.")
torque = F_magnitude * np.sin(np.radians(F_angle)) * thruster_distance
auv_angular_acceleration = calculate_angular_acceleration(torque, inertia)
return auv_angular_acceleration
def calculate_auv2_acceleration(T, alpha, theta, mass=100):
"""
Calculates linear acceleration of AUV given magnitude of force exerted by all 4 thrusters, their angles, and mass of AUV.
T - Forces exerted by each of the 4 thrusters
alpha - Angle of each thruster |
theta - Rotation of AUV |
mass - Mass of AUV
"""
if len(T) != 4:
raise ValueError("Must be exactly 4 thrusters present.")
if mass <= 0:
raise ValueError("Mass must be positive.")
cos_angle = np.cos(alpha)
sin_angle = np.sin(alpha)
signs_matrix = np.array(
[
cos_angle,
cos_angle,
-cos_angle,
-cos_angle,
sin_angle,
-sin_angle,
-sin_angle,
sin_angle,
],
).reshape(2, 4)
forces_matrix = np.dot(signs_matrix, T)
cos_theta = np.cos(theta)
sin_theta = np.sin(theta)
rotation_matrix = np.array([cos_theta, -sin_theta, sin_theta, cos_theta]).reshape(
2, 2
)
acceleration_matrix = np.dot(rotation_matrix, forces_matrix) / mass
return acceleration_matrix
def calculate_auv2_angular_acceleration(T, alpha, L, l, inertia=100):
"""
Calculates angular acceleration of AUV given magnitude of force exerted by thrusters, angle of thrusters, and dimensions of AUV.
T - Magnitude of force exerted by thruster |
alpha - angle of thruster with respect to x-axis |
L - half of the length of the AUV |
l - half of the width of the AUV |
inertia - (optional), moment of inertia of AUV
"""
if len(T) != 4:
raise ValueError("Must be exactly 4 thrusters present.")
if inertia <= 0:
raise ValueError("Moment of inertia must be positive.")
if L <= 0 or l <= 0:
raise ValueError("Dimensions of AUV must be positive")
sin_angle = np.sin(alpha)
cos_angle = np.cos(alpha)
T = np.array([T])
net_torque = np.sum(
np.dot(T, np.array([[1, -1, 1, -1]]).T) * (sin_angle * L + cos_angle * l)
)
# for i in range(4):
# sin_angle = np.sin(alpha)
# cos_angle = np.cos(alpha)
# if i % 2 == 0:
# net_torque += T[i] * (sin_angle * L + cos_angle * l)
# else:
# net_torque -= T[i] * (sin_angle * L + cos_angle * l)
angular_acceleration = calculate_angular_acceleration(net_torque, inertia)
return angular_acceleration
def simulate_auv2_motion(
T, alpha, L, l, mass=100, inertia=100, dt=0.1, t_final=10, x0=0, y0=0, theta0=0
) -> tuple:
"""
Simulates the motion of the AUV in a 2D plane.
Arguments:
T: an np.ndarray of the magnitudes of the forces applied by the thrusters in Newtons.
alpha: the angle of the thrusters in radians.
L: the distance from the center of mass of the AUV to the thrusters in meters.
l: the distance from the center of mass of the AUV to the thrusters in meters.
inertia (optional): the moment of inertia of the AUV
dt (optional): the time step of the simulation in seconds.
t_final (optional): the final time of the simulation in seconds
x0 (optional): the initial x-position of the AUV in meters.
y0 (optional): the initial y-position of the AUV in meters.
theta0 (optional): the initial angle of the AUV in radians
"""
if len(T) != 4:
raise ValueError("Must be exactly 4 thrusters present.")
if L <= 0 or l <= 0:
raise ValueError("Dimensions must be positive.")
t = np.arange(0, t_final, dt)
x = np.zeros_like(t)
y = np.zeros_like(t)
v_x = np.zeros_like(t)
v_y = np.zeros_like(t)
a_x = np.zeros_like(t)
a_y = np.zeros_like(t)
omega = np.zeros_like(t)
theta = np.zeros_like(t)
x[0] = x0
y[0] = y0
theta[0] = theta0
a_x[0] = calculate_auv2_acceleration(T, alpha, theta0)[0]
a_y[0] = calculate_auv2_acceleration(T, alpha, theta0)[1]
for i in range(1, len(t)):
omega[i] = (
calculate_auv2_angular_acceleration(T, alpha, L, l, inertia)
) * dt + omega[i - 1]
theta[i] = theta[i - 1] + omega[i] * dt
a_x[i] = calculate_auv2_acceleration(T, alpha, theta[i], mass)[0]
a_y[i] = calculate_auv2_acceleration(T, alpha, theta[i], mass)[1]
v_x[i] = a_x[i] * dt + v_x[i - 1]
v_y[i] = a_y[i] * dt + v_y[i - 1]
x[i] = v_x[i] * dt + x[i - 1]
y[i] = v_y[i] * dt + y[i - 1]
v = []
a = []
for i in range(len(t)):
v.append([v_x[i], v_y[i]])
a.append([a_x[i], a_y[i]])
return (t, x, y, theta, v, omega, a)
def plot_auv2_motion(auv_motion):
plt.plot(auv_motion[0], auv_motion[1], label="X-Position")
plt.xlabel("Time (s)")
plt.ylabel("X-Position (m)")
plt.show()
tup = simulate_auv2_motion([40, 80, 120, 160], np.pi / 3, 3, 2, 100, 100, 0.1, 0.4)
print(np.array(tup[1]))