@@ -25,7 +25,7 @@ def solve_for_v_peak(v0: float, acceleration: float, deceleration: float, total_
2525
2626 return math .sqrt (v_peak_sq )
2727
28- def compute_dynamic_dt (acceleration , speed , k = 0.02 , a_min = 0.5 ):
28+ def compute_dynamic_dt (acceleration , speed , k = 0.005 , a_min = 0.5 ):
2929 position_step = k * max (speed , 1.0 ) # Ensures position step is speed-dependent
3030 return np .sqrt (2 * position_step / max (acceleration , a_min ))
3131
@@ -38,6 +38,14 @@ def longitudinal_plan(path, acceleration: float, deceleration: float, max_speed:
3838 path_norm = path .arc_length_parameterize (speed = 1.0 )
3939 total_length = path .length ()
4040
41+ # -------------------
42+ # If the path is too short, just return the path for preventing sudden halt of simulation
43+ if total_length < 0.05 :
44+ points = [p for p in path_norm .points ]
45+ times = [t for t in path_norm .times ]
46+ return Trajectory (path .frame , points , times )
47+ # -------------------
48+
4149 # 2. Compute distances for d_accel,d_decel
4250 if max_speed > current_speed :
4351 d_accel = (max_speed ** 2 - current_speed ** 2 ) / (2 * acceleration )
@@ -69,8 +77,6 @@ def longitudinal_plan(path, acceleration: float, deceleration: float, max_speed:
6977 s_vals = []
7078 num_time_steps = 0
7179 while t < t_final :
72- dt = compute_dynamic_dt (acceleration if t < t_accel else deceleration ,current_speed )
73- t = t + dt
7480 times .append (t )
7581 if profile_type == "trapezoidal" :
7682 if t < t_accel :
@@ -95,6 +101,10 @@ def longitudinal_plan(path, acceleration: float, deceleration: float, max_speed:
95101 s_vals .append (min (s , total_length ))
96102 if s >= total_length :
97103 break
104+
105+ dt = compute_dynamic_dt (acceleration if t < t_accel else deceleration ,current_speed )
106+ t = t + dt
107+
98108 num_time_steps += 1
99109
100110 # Compute trajectory points
@@ -104,7 +114,16 @@ def longitudinal_plan(path, acceleration: float, deceleration: float, max_speed:
104114 # return Trajectory(path_norm.frame, points, times)
105115
106116
107-
117+ # # Plot: update a single window
118+ # import matplotlib.pyplot as plt
119+ # plt.figure("Distance vs Time")
120+ # plt.clf() # Clear the current figure
121+ # plt.plot(times, s_vals)
122+ # plt.xlabel("Time (s)")
123+ # plt.ylabel("Distance (m)")
124+ # plt.title("Distance vs Time")
125+ # plt.draw()
126+ # plt.pause(0.001)
108127
109128
110129
0 commit comments