forked from botprof/agv-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusv_demo_3dof.py
100 lines (72 loc) · 2.31 KB
/
usv_demo_3dof.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
"""
Example usv_demo_3dof.py
Author: Joshua A. Marshall <[email protected]>
Thomas M. C. Sears <[email protected]>
GitHub: https://github.com/botprof/agv-examples
This code demonstrates basic use of the 'models.LongitudinalUSV' class
to simulate a 3DOF (surge [x], heave [z], pitch).
Run me in interactive Python for an animation!
"""
# %%
# SIMULATION SETUP
import numpy as np
import matplotlib.pyplot as plt
from mobotpy.models import LongitudinalUSV
# Set the simulation time [s] and the sample period [s]
SIM_TIME = 5.0
T = 0.02
# Create an array of time values [s]
t = np.arange(0, SIM_TIME, T)
N = np.size(t)
# %%
# FUNCTION DEFINITIONS
# Create a space-time wave model (simple wave)
def wave(x, t):
# Define the parameters of the sinusoidal array
amplitude = 1.0 # [m]
phase = np.pi / 2 # [rad]
period = 1 # [s]
time_frequency = 1 / period # [1/s]
wavelength = 10 # [m]
space_frequency = 1 / wavelength # [1/m]
# Find height of wave
height = amplitude * \
np.sin(2*np.pi*(time_frequency*t + space_frequency*x) + phase)
# Find slope of wave in space
slope_x = 2*np.pi*space_frequency*amplitude * \
np.cos(2*np.pi*(time_frequency*t + space_frequency*x) + phase)
wave_angle = np.arctan2(slope_x, 1)
return height, wave_angle
# %%
# BOAT STATE (SURGE, HEAVE, PITCH)
# Start with all zeros
x = np.zeros((3, N))
# Keep surge (x) at zero. Set heave and pitch to wave
# height and slope respectively
x[1, :], x[2, :] = wave(x[0, :], t)
# %%
# WAVE SIMULATION
# Define spatial range to plot wave
x_range = np.arange(-20, 20, 0.1)
N_x = np.size(x_range)
# Initialize array to store wave heights for all N time steps
wave_heights = np.zeros((N_x, N))
for i in range(N):
wave_heights[:, i], _ = wave(x_range, t[i])
# %%
# MAKE AN ANIMATION
# Set the side length of the vehicle [m]
LENGTH = 2.0
# Use the LongitudinalUSV class to create an animation
vehicle = LongitudinalUSV(LENGTH)
# Create and save the animation
ani = vehicle.animate(x, T,
wave_positions=x_range, wave_data=wave_heights,
relative=False)
# %%
# Show all the plots to the screen
plt.show()
# Show animation in HTML output if you are using IPython or Jupyter notebooks
plt.rc('animation', html='jshtml')
display(ani)
plt.close()