-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathshowcase.py
More file actions
217 lines (193 loc) · 6.02 KB
/
Copy pathshowcase.py
File metadata and controls
217 lines (193 loc) · 6.02 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
"""
API showcase of the pymaneuvering library, demonstrating the
simulation of vessel trajectories one for an inland vessel and
one for an ocean vessel.
The trajectories are plotted with rotated transparent ship
footprints to visualize the vessel's orientation and size along the path.
The inland vessel uses the GMS-like model, while the ocean
vessel uses the KVLCC2 L64 model.
Both vessels are subjected to a constant rudder angle of 10° for
800 seconds, and their trajectories are compared in a single plot.
Usage instructions:
1. Ensure you have pymaneuvering installed in your Python environment.
2. Run this script to generate the plot of the vessel trajectories.
3. The plot will be saved as "vessel_trajectories.png" in the current working directory.
"""
import math
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib import transforms
from pymaneuvering import Vessel, VTYPE, IntegrationMode
# Load a pre-calibrated vessel
vessel_inland = Vessel(new_from=VTYPE.GMS_LIKE)
vessel_ocean = Vessel(new_from=VTYPE.KVLCC2_L64)
# Let the vessel drive with a rudder angle
# of 10° for 1000 seconds
# -------------------------------------
# Inital position
pos_inland = [0, 0] # x,y [m]
pos_ocean = [0, 0] # x,y [m]
# Initial heading
psi_i = 0 # [rad]
psi_o = 0 # [rad]
# Unpack initial values
uvr_i = np.array([2.777, 0, 0.0]) # u,v,r [m/s, m/s, rad/s]
uvr_o = np.array([2.777, 0, 0.0]) # u,v,r [m/s, m/s, rad/s]
positions_i = []
positions_o = []
for _ in range(800):
# Simulate inland vessel
uvr_i, eta_i = vessel_inland.pstep(
X=uvr_i,
pos=pos_inland,
dT=1, # 1 second time step
delta=10 * (math.pi / 180), # Convert to radians
psi=psi_i, # Heading
water_depth=15, # 15 m water depth
fl_psi=0, # 0° current angle
fl_vel=None, # No current velocity
mode=IntegrationMode.TRAPEZOIDAL,
)
x_i, y_i, psi_i = eta_i # Unpack new position and heading
positions_i.append([x_i, y_i, psi_i]) # Store the new position and heading
pos_inland = [x_i, y_i] # Update the position
# Simulate ocean vessel
uvr_o, eta_o = vessel_ocean.pstep(
X=uvr_o,
pos=pos_ocean,
dT=1, # 1 second time step
delta=10 * (math.pi / 180), # Convert to radians
psi=psi_o, # Heading
nps=5,
water_depth=15, # 15 m water depth
fl_psi=0, # 0° current angle
fl_vel=None, # No current velocity
w_vel=None, # No wind velocity
beta_w=None, # No wind angle
)
x_o, y_o, psi_o = eta_o # Unpack new position and heading
positions_o.append([x_o, y_o, psi_o]) # Store the new position and heading
pos_ocean = [x_o, y_o] # Update the position
# --------------------------------------------------------
# Plot the trajectories with ship footprints
# --------------------------------------------------------
ps = list(zip(*positions_i))
plt.rcParams.update(
{
"figure.dpi": 150,
"axes.facecolor": "white",
"axes.edgecolor": "black",
"axes.linewidth": 1.0,
"axes.labelsize": 12,
"axes.titlesize": 13,
"xtick.labelsize": 10,
"ytick.labelsize": 10,
"legend.fontsize": 9,
}
)
inland_color = "#1f77b4"
ocean_color = "#d62728"
fig, ax = plt.subplots(figsize=(12, 8))
ax.plot(ps[0], ps[1], color=inland_color, linewidth=2.2, label="Inland trajectory")
# Quick plot of the trajectory for ocean vessel
ps_o = list(zip(*positions_o))
ax.plot(ps_o[0], ps_o[1], color=ocean_color, linewidth=2.2, label="Ocean trajectory")
# Plot ship footprint every n steps
n = 40
x_sub = ps[0][::n]
y_sub = ps[1][::n]
psi_sub = ps[2][::n]
ship_length_i = VTYPE.GMS_LIKE.value["L"]
ship_beam_i = VTYPE.GMS_LIKE.value["B"]
for idx, (x, y, psi) in enumerate(zip(x_sub, y_sub, psi_sub)):
ship = Rectangle(
(-ship_length_i / 2, -ship_beam_i / 2),
ship_length_i,
ship_beam_i,
facecolor=inland_color,
edgecolor="black",
alpha=0.24,
linewidth=0.9,
label="Inland ship footprint" if idx == 0 else None,
)
tr = transforms.Affine2D().rotate_around(0, 0, -psi).translate(x, y) + ax.transData
ship.set_transform(tr)
ax.add_patch(ship)
x_sub_o = ps_o[0][::n]
y_sub_o = ps_o[1][::n]
psi_sub_o = ps_o[2][::n]
ship_length_o = VTYPE.KVLCC2_L64.value["Lpp"]
ship_beam_o = VTYPE.KVLCC2_L64.value["B"]
for idx, (x, y, psi) in enumerate(zip(x_sub_o, y_sub_o, psi_sub_o)):
ship = Rectangle(
(-ship_length_o / 2, -ship_beam_o / 2),
ship_length_o,
ship_beam_o,
facecolor=ocean_color,
edgecolor="black",
alpha=0.18,
linewidth=0.9,
label="Ocean ship footprint" if idx == 0 else None,
)
tr = transforms.Affine2D().rotate_around(0, 0, psi).translate(x, y) + ax.transData
ship.set_transform(tr)
ax.add_patch(ship)
# Start and end markers
ax.scatter(
ps[0][0],
ps[1][0],
color=inland_color,
edgecolors="black",
marker="o",
s=52,
linewidths=0.6,
label="Inland start",
)
ax.scatter(
ps[0][-1],
ps[1][-1],
color=inland_color,
edgecolors="black",
marker="X",
s=64,
linewidths=0.7,
label="Inland end",
)
ax.scatter(
ps_o[0][0],
ps_o[1][0],
color=ocean_color,
edgecolors="black",
marker="o",
s=52,
linewidths=0.6,
label="Ocean start",
)
ax.scatter(
ps_o[0][-1],
ps_o[1][-1],
color=ocean_color,
edgecolors="black",
marker="X",
s=64,
linewidths=0.7,
label="Ocean end",
)
ax.axis("equal")
ax.set_title("Vessel trajectories with rotated transparent ship footprints")
ax.set_xlabel("East [m]")
ax.set_ylabel("North [m]")
ax.grid(True, linestyle="-", linewidth=0.6, color="#b0b0b0", alpha=0.55)
ax.minorticks_on()
ax.grid(which="minor", linestyle="-", linewidth=0.3, color="#d0d0d0", alpha=0.45)
ax.legend(
loc="best",
frameon=True,
facecolor="white",
edgecolor="black",
framealpha=0.96,
ncol=2,
)
fig.tight_layout()
plt.savefig("vessel_trajectories.png", dpi=300)