Example 2: Bicycle Model

This example demonstrates how to use Arena with a two-corridors scenario and bicycle vehicle.

Example of a two-corridor setup
example_bicycle.py
 1from math import sin, cos, pi
 2import matplotlib.pyplot as plt
 3import arena
 4"""
 5Example: Analytical motion planning for a bicycle model in a two-corridor environment.
 6
 7This example demonstrates:
 8  • How to construct corridors using `get_corridor_from_vector`.
 9  • How to specify start and end poses in the shrunken corridor reference frames.
10  • How to instantiate and configure a bicycle vehicle model.
11  • How to compute and visualize the resulting analytical trajectory.
12"""
13
14### Define corridors ###
15width1, width2 = 3, 3
16height1, height2 = 6, 4
17phi1, phi2 = pi/2, pi/6
18
19# Instantiate two corridors by defining two consecutive vectors. 
20# Tail point corridor1
21start_point1 = [0, 0] 
22# Head point corridor1
23end_point1   = [start_point1[0] + height1 * cos(phi1),
24                start_point1[1] + height1 * sin(phi1)] # Head point
25# Tail point corridor2
26start_point2 = [end_point1[0],
27                end_point1[1]]
28# Head point corridor2
29end_point2   = [start_point2[0] + height2 * cos(phi2),
30                start_point2[1] + height2 * sin(phi2)]
31
32# Instantiate corridors with get_corridor_from_vector method
33corridor1 = arena.get_corridor_from_vector(
34    start_point1,
35    end_point1,
36    width1,
37    add_height = 0.2 * height1)
38
39corridor2 = arena.get_corridor_from_vector(
40    start_point2,
41    end_point2,
42    width2,
43    add_height = 0.2 * height2)
44
45corridor_list = [corridor1, corridor2]
46
47### Define initial pose and final pose ###
48# Define relative poses wrt the center of shrunken corridors
49x_percentage_initial = 0.8
50y_percentage_initial = -0.7
51theta0_relative = -pi/2
52x_percentage_final = -0.4
53y_percentage_final = 0.8
54thetaf_relative = 5 * pi/4
55
56relative_start_pose = [x_percentage_initial,
57                         y_percentage_initial,
58                         theta0_relative]
59
60relative_end_pose = [x_percentage_final,
61                       y_percentage_final,
62                       thetaf_relative]
63
64### Define Bicycle vehicle ###
65vehicle_width = 0.430 
66vehicle_length = 0.508
67vehicle_wheelbase = 0.4
68vehicle_vmax = 1
69vehicle_deltamax = 0.5
70
71bicycle = arena.Bicycle(
72    [0,0,0],
73    width = vehicle_width,
74    length = vehicle_length,
75    wheelbase = vehicle_wheelbase, 
76    v_max = vehicle_vmax,
77    v_min = -vehicle_vmax,
78    delta_max = vehicle_deltamax, 
79    delta_min = -vehicle_deltamax)
80
81### Define Motion Planner ###
82mp = arena.MotionPlanner(bicycle,
83                         corridor_list,
84                         relative_start_pose=relative_start_pose,
85                         relative_end_pose=relative_end_pose)
86
87### Compute analytical trajectory ###
88analytical_trajectory = mp.compute_trajectory_analytical()
89
90### Plot results ###
91figure = mp.plot_planner_inputs()
92plt.title('Analytical Motion Planner - Bicycle in Two Corridors')
93arena.plot_analytical_trajectory(analytical_trajectory, figure)
94arena.plot_velocity_profiles(analytical_trajectory, bicycle)
95
96plt.show(block = True)
97

Step 1 — Corridors definition

We define the two corridors using the function get_corridor_from_vector. This helper function simplifies corridor placement by constructing a corridor directly from a vector: given the corridor width and the tail and head points of a vector, it creates a corridor that is aligned with the vector and has the same length (optionally extended by a margin). This makes it straightforward to build consecutive corridors.

### Define corridors ###
width1, width2 = 3, 3
height1, height2 = 6, 4
phi1, phi2 = pi/2, pi/6

# Instantiate two corridors by defining two consecutive vectors. 
# Tail point corridor1
start_point1 = [0, 0] 
# Head point corridor1
end_point1   = [start_point1[0] + height1 * cos(phi1),
                start_point1[1] + height1 * sin(phi1)] # Head point
# Tail point corridor2
start_point2 = [end_point1[0],
                end_point1[1]]
# Head point corridor2
end_point2   = [start_point2[0] + height2 * cos(phi2),
                start_point2[1] + height2 * sin(phi2)]

# Instantiate corridors with get_corridor_from_vector method
corridor1 = arena.get_corridor_from_vector(
    start_point1,
    end_point1,
    width1,
    add_height = 0.2 * height1)

corridor2 = arena.get_corridor_from_vector(
    start_point2,
    end_point2,
    width2,
    add_height = 0.2 * height2)

corridor_list = [corridor1, corridor2]

Step 2 — Initial and final pose definition

We define the initial and final poses in the local reference frames of the shrunken corridors. Each corridor has a corresponding shrunken version, with the same center and orientation but reduced width and length by 2r, where r is the robot footprint radius.

In this local frame: - x and y range from -1 to 1 inside the corridor bounds - θ = π/2 corresponds to an orientation aligned with the corridor

Defining poses in these normalized coordinates ensures that the start and goal always lie inside the feasible corridor region, making the specification intuitive and preventing invalid pose definitions. Then these poses are passed to the motion planner directly.

### Define initial pose and final pose ###
# Define relative poses wrt the center of shrunken corridors
x_percentage_initial = 0.8
y_percentage_initial = -0.7
theta0_relative = -pi/2
x_percentage_final = -0.4
y_percentage_final = 0.8
thetaf_relative = 5 * pi/4

relative_start_pose = [x_percentage_initial,
                         y_percentage_initial,
                         theta0_relative]

relative_end_pose = [x_percentage_final,
                       y_percentage_final,
                       thetaf_relative]

Step 3 — Bicycle vehicle definition

Next, we instantiate a Bicycle object using the user-defined vehicle parameters.

### Define Bicycle vehicle ###
vehicle_width = 0.430 
vehicle_length = 0.508
vehicle_wheelbase = 0.4
vehicle_vmax = 1
vehicle_deltamax = 0.5

bicycle = arena.Bicycle(
    [0,0,0],
    width = vehicle_width,
    length = vehicle_length,
    wheelbase = vehicle_wheelbase, 
    v_max = vehicle_vmax,
    v_min = -vehicle_vmax,
    delta_max = vehicle_deltamax, 
    delta_min = -vehicle_deltamax)

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. Note that the relative poses are provided via the keywords relative_start_pose and relative_end_pose to the MotionPlanner object.

### Define Motion Planner ###
mp = arena.MotionPlanner(bicycle,
                         corridor_list,
                         relative_start_pose=relative_start_pose,
                         relative_end_pose=relative_end_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 - Bicycle in Two Corridors')
arena.plot_analytical_trajectory(analytical_trajectory, figure)
arena.plot_velocity_profiles(analytical_trajectory, bicycle)

plt.show(block = True)