Example 2: Multiple Corridors
This example demonstrates how to use Arena with a multiple-corridors scenario.
1from math import sin, cos, pi
2import arena
3import matplotlib.pylab as plt
4"""
5Example: Analytical motion planning for a unicycle model in a multi-corridor environment.
6
7This example demonstrates:
8 • How to construct multiple corridors using `get_corridor_from_vector`.
9 • How to load a unicycle vehicle model from the `unicycle_library`.
10 • How to modify vehicle parameters after instantiation.
11 • How to compute start and end poses using `compute_start_pose` and `compute_end_pose`.
12 • How to generate and visualize the resulting analytical trajectory.
13"""
14
15
16### Define corridors ###
17width1 =1
18height1 = 4
19phi1 = pi/6
20
21width2 = width1
22height2 = 3
23phi2 = pi/2
24add_height2 = 0.7
25
26width3 = width2
27height3 = height1
28phi3 = -pi/6
29add_height3 = add_height2
30
31width4 = width3
32height4 = 3
33phi4 = -pi/2 - 0.2
34add_height4 = add_height3
35
36width5 = width4
37height5 = height1
38phi5 = 0
39add_height5 = add_height4
40
41# Define corridor1
42corridor1 = arena.CorridorWorld(width = width1, height = height1, center = [0, 0], tilt = phi1)
43
44# Define corridor2
45tail_vector2 = corridor1.head
46head_vector2 = [corridor1.head[0] + height2 * cos(phi2), corridor1.head[1] + height2 * sin(phi2)]
47corridor2 = arena.get_corridor_from_vector(tail_vector2, head_vector2, width2, add_height = add_height2)
48
49# Define corridor3
50tail_vector3 = corridor2.head
51head_vector3 = [corridor2.head[0] + height3 * cos(phi3), corridor2.head[1] + height3 * sin(phi3)]
52corridor3 = arena.get_corridor_from_vector(tail_vector3, head_vector3, width3, add_height = add_height3)
53
54# Define corridor4
55tail_vector4 = corridor3.head
56head_vector4 = [corridor3.head[0] + height4 * cos(phi4), corridor3.head[1] + height4 * sin(phi4)]
57corridor4 = arena.get_corridor_from_vector(tail_vector4, head_vector4, width4, add_height = add_height4)
58
59# Define corridor5
60tail_vector5 = corridor4.head
61head_vector5 = [corridor4.head[0] + height5 * cos(phi5), corridor4.head[1] + height5 * sin(phi5)]
62corridor5 = arena.get_corridor_from_vector(tail_vector5, head_vector5, width5, add_height = add_height5)
63
64corridor_list = [corridor1, corridor2, corridor3, corridor4, corridor5]
65
66### Define Unicycle vehicle ###
67unicycle = arena.Unicycle(model = 'Rosbot circular')
68# Modify vehicle parameters
69unicycle.update(v_max = 0.8)
70unicycle.update(omega_max = 1)
71
72### Define initial pose and final pose ###
73initial_pose = arena.compute_start_pose(corridor1, unicycle, 0.5)
74final_pose = arena.compute_end_pose(corridor5, unicycle, 0.5)
75
76### Define Motion Planner ###
77mp = arena.MotionPlanner(unicycle, corridor_list, start_pose=initial_pose, end_pose=final_pose)
78
79### Compute analytical trajectory ###
80analytical_trajectory = mp.compute_trajectory_analytical()
81
82### Plot results ###
83figure = mp.plot_planner_inputs()
84plt.title('Analytical Motion Planner - Unicycle Within Multiple Corridors')
85arena.plot_analytical_trajectory(analytical_trajectory, figure = figure)
86arena.plot_velocity_profiles(analytical_trajectory, unicycle)
87plt.show(block = True)
Step 1 — Corridors definition
We first define five corridors through the function get_corridor_from_vector, particularly useful for longer sequences of corridors.
### Define corridors ###
width1 =1
height1 = 4
phi1 = pi/6
width2 = width1
height2 = 3
phi2 = pi/2
add_height2 = 0.7
width3 = width2
height3 = height1
phi3 = -pi/6
add_height3 = add_height2
width4 = width3
height4 = 3
phi4 = -pi/2 - 0.2
add_height4 = add_height3
width5 = width4
height5 = height1
phi5 = 0
add_height5 = add_height4
# Define corridor1
corridor1 = arena.CorridorWorld(width = width1, height = height1, center = [0, 0], tilt = phi1)
# Define corridor2
tail_vector2 = corridor1.head
head_vector2 = [corridor1.head[0] + height2 * cos(phi2), corridor1.head[1] + height2 * sin(phi2)]
corridor2 = arena.get_corridor_from_vector(tail_vector2, head_vector2, width2, add_height = add_height2)
# Define corridor3
tail_vector3 = corridor2.head
head_vector3 = [corridor2.head[0] + height3 * cos(phi3), corridor2.head[1] + height3 * sin(phi3)]
corridor3 = arena.get_corridor_from_vector(tail_vector3, head_vector3, width3, add_height = add_height3)
# Define corridor4
tail_vector4 = corridor3.head
head_vector4 = [corridor3.head[0] + height4 * cos(phi4), corridor3.head[1] + height4 * sin(phi4)]
corridor4 = arena.get_corridor_from_vector(tail_vector4, head_vector4, width4, add_height = add_height4)
# Define corridor5
tail_vector5 = corridor4.head
head_vector5 = [corridor4.head[0] + height5 * cos(phi5), corridor4.head[1] + height5 * sin(phi5)]
corridor5 = arena.get_corridor_from_vector(tail_vector5, head_vector5, width5, add_height = add_height5)
corridor_list = [corridor1, corridor2, corridor3, corridor4, corridor5]
Step 2 — Unicycle vehicle definition
Next, we instantiate a Unicycle object by loading a predefined model from Arena’s unicycle library.
In this case, the user does not need to manually specify the vehicle parameters.
If a parameter needs to be adjusted, the update() method of the Unicycle object can be used, as shown below.
### Define Unicycle vehicle ###
unicycle = arena.Unicycle(model = 'Rosbot circular')
# Modify vehicle parameters
unicycle.update(v_max = 0.8)
unicycle.update(omega_max = 1)
Step 3 — Initial and final pose definition
The initial and final poses can be conveniently obtained using the functions
compute_start_pose() and compute_end_pose().
These functions place the vehicle along the centerline of the corresponding corridor while
automatically enforcing a collision-free positioning.
compute_start_pose()positions the vehicle in the first corridor. The pose is computed by taking the midpoint of the back face of the corridor and shifting it forward by half the vehicle length plus an optional safety margin. The orientation is aligned with the corridor direction.compute_end_pose()performs the analogous operation in the last corridor, starting from the midpoint of the front face and shifting backward by the same offset.
Using these helper functions ensures that the start and goal configurations always lie inside their respective corridors.
### Define initial pose and final pose ###
initial_pose = arena.compute_start_pose(corridor1, unicycle, 0.5)
final_pose = arena.compute_end_pose(corridor5, unicycle, 0.5)
Step 4 — Create the Motion Planner and compute the trajectory
Now we instantiate the MotionPlanner object and use the compute_trajectory_analytical() method to find the solution trajectory.
### Define Motion Planner ###
mp = arena.MotionPlanner(unicycle, corridor_list, start_pose=initial_pose, end_pose=final_pose)
### Compute analytical trajectory ###
analytical_trajectory = mp.compute_trajectory_analytical()
Step 5 — Plot the results
Finally we plot the resulting path and control profiles.
### Plot results ###
figure = mp.plot_planner_inputs()
plt.title('Analytical Motion Planner - Unicycle Within Multiple Corridors')
arena.plot_analytical_trajectory(analytical_trajectory, figure = figure)
arena.plot_velocity_profiles(analytical_trajectory, unicycle)
plt.show(block = True)