Skip to content

Commit

Permalink
changed as per suggestions from Jason and Timo
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter230655 committed Sep 19, 2024
1 parent 84cf308 commit 08fb6ca
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions examples-gallery/plot_parallel_park.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# %%
"""
Parallel Park a Car
===================
Expand Down Expand Up @@ -174,7 +173,8 @@
prob.add_option('nlp_scaling_method', 'gradient-based')

# %%
# Give some rough estimates for the x and y trajectories.
# Give some rough estimates for the x and y trajectories. They were used to
# generate the initial guess, now stored in the file parallel_park_solution.npy.
x_guess = 3.0/duration*2.0*time
x_guess[num_nodes//2:] = 6.0 - 3.0/duration*2.0*time[num_nodes//2:]
y_guess = 2.0/duration*time
Expand All @@ -185,15 +185,17 @@
prob.plot_trajectories(initial_guess)

# %%
# Find the optimal solution.
# Solve the optimisation, using the initial guess stored in the file
# parallel_park_solution.npy.
initial_guess = np.load('parallel_park_solution.npy')
solution, info = prob.solve(initial_guess)
print(info['status_msg'])
print(info['obj_val'])

# %%
#Improved initial_guess is stored like this
#Improved initial_guess may be saved and stored like this:
# ```np.save('parallel_park_solution.npy', solution)```

# %%
# Plot the optimal state and input trajectories.
prob.plot_trajectories(solution)
Expand Down Expand Up @@ -230,17 +232,6 @@
coords.append(eval_point_coords(xi, ui, list(par_map.values())))
coords = np.array(coords) # shape(501, 3, 5)

coords1 = []
time1 = []
for i in range(coords.shape[0]):
if i % 3 == 0:
coords1.append(coords[i, :, :])
time1.append(time[i])
coords1.append(coords[-1, :, :])
time1.append(time[-1])
time = np.array(time1)
coords = np.array(coords1)

def frame(i):

fig, ax = plt.subplots()
Expand All @@ -267,6 +258,8 @@ def frame(i):

fig, title_text, lines, Pr_path, Pf_path = frame(0)

time = list(time)
time[-3:] = [time[-1]]*3

def animate(i):
title_text.set_text('Time = {:1.2f} s'.format(time[i]))
Expand All @@ -275,13 +268,13 @@ def animate(i):
Pf_path.set_data(coords[:i, 0, 2], coords[:i, 1, 2])


ani = animation.FuncAnimation(fig, animate, len(time),
interval=int(interval_value*1000*1))
ani = animation.FuncAnimation(fig, animate, range(0, len(time), 3),
interval=int(interval_value*1000*2))

# %%
# A frame from the animation.

# sphinx_gallery_thumbnail_number = 7
frame(140)
frame(450)

plt.show()

0 comments on commit 08fb6ca

Please sign in to comment.