Skip to content

Commit 655b3e1

Browse files
committed
fix back trajectory
1 parent 765e802 commit 655b3e1

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

GEMstack/onboard/planning/racing_planning.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def waypoint_generate(vehicle_state, cones, cone_idx):
6262
target_heading = car_heading
6363

6464
# ===== Parameters =====
65-
u_turn_radius = 10.0 # Radius for U-turn
65+
u_turn_radius = 11.5 # Radius for U-turn
6666
offset = 2.0 # Offset for left/right pass
6767
lookahead_distance = 10.0 # Distance ahead for fixed point
6868
# ======================
@@ -213,6 +213,19 @@ def trajectory_generation(init_state, final_state, N=30, T=0.1, Lr=1.5,
213213
- x, y, psi, c, v, eps (np.ndarray): Arrays of optimized state and control values.
214214
- final_error (dict): Final state errors in x, y, psi, and c.
215215
"""
216+
final_heading = (final_state['psi'] + np.pi) % (2 * np.pi) - np.pi
217+
init_heading = (init_state['psi'] + np.pi) % (2 * np.pi) - np.pi
218+
219+
if abs(final_heading - init_heading) > np.pi:
220+
if final_heading < init_heading:
221+
final_heading += 2 * np.pi
222+
else:
223+
final_heading -= 2 * np.pi
224+
225+
init_state['psi'] = init_heading
226+
final_state['psi'] = final_heading
227+
print("init and final headings: ", init_heading, final_heading)
228+
216229
def cost(p):
217230
x_, y_, psi_, c_, v_, eps_ = np.split(p, [N - 1, 2 * (N - 1), 3 * (N - 1), 4 * (N - 1), 5 * (N - 1)])
218231
c_seq = np.concatenate(([init_state['c']], c_))
@@ -692,6 +705,8 @@ def got_new_cone(current, prev):
692705
'c': 0.0,
693706
'v': vehicle_state['velocity']
694707
}
708+
vehicle_state['position'] = np.array([init_point[0], init_point[1]])
709+
vehicle_state['heading'] = heading
695710

696711
print("all cones: ", self.cones)
697712
current_cone_idx, updated_cones = self.get_current_cone_idx(self.cones, init_state)
@@ -710,7 +725,7 @@ def got_new_cone(current, prev):
710725
if not new_cone_detected and self.no_cone_ahead:
711726
return self.trajectory
712727

713-
self.visited_cone_ids.add(self.cones[current_cone_idx]['id'])
728+
self.visited_cone_ids.add(self.cones[current_cone_idx]['id'])
714729
scenario, flex_wps, fixed_wp, target_heading = waypoint_generate(vehicle_state, self.cones, current_cone_idx)
715730

716731
if flex_wps and fixed_wp is not None:
@@ -720,8 +735,9 @@ def got_new_cone(current, prev):
720735

721736
# Stitch from current vehicle position to new plan start
722737
if self.trajectory is not None:
738+
print("init and final state: ", init_state, final_state)
723739
# 1. Plan new trajectory from init_state onward
724-
x_new, y_new, _, _, v_new, _, _ = trajectory_generation(init_state, final_state, waypoints=flex_wps)
740+
x_new, y_new, psi_new, _, v_new, _, _ = trajectory_generation(init_state, final_state, waypoints=flex_wps)
725741

726742
# 2. Cut old trajectory up to init_state (e.g., index `stitch_idx`)
727743
old_points = self.trajectory.points[:stitch_idx]
@@ -734,6 +750,29 @@ def got_new_cone(current, prev):
734750
y_full = np.concatenate([old_y, y_new])
735751
v_full = np.concatenate([old_v, v_new])
736752

753+
if current_cone_idx == 6:
754+
# Plot overall trajectory
755+
plt.figure()
756+
plt.plot(x_full, y_full, label='Overall Trajectory')
757+
758+
# Plot cones
759+
for i, cone in enumerate(self.cones):
760+
plt.scatter(cone['x'], cone['y'], color='orange', s=10, label='Cone' if i == 0 else "")
761+
plt.text(cone['x'], cone['y'] + 0.5, f'C{i+1}', ha='center', fontsize=9, color='darkorange')
762+
763+
# Plot fixed waypoint
764+
if fixed_wp is not None:
765+
plt.plot(fixed_wp[0], fixed_wp[1], 'ro', label='Fixed Waypoint')
766+
plt.text(fixed_wp[0], fixed_wp[1] + 0.5, 'Fixed', fontsize=9, color='red')
767+
768+
plt.title('4-Cone Full Course Trajectory')
769+
plt.xlabel('X')
770+
plt.ylabel('Y')
771+
plt.legend()
772+
plt.axis('equal')
773+
plt.grid(True)
774+
plt.show()
775+
737776
# 4. Create trajectory
738777
self.trajectory = to_gemstack_trajectory(x_full, y_full, v_full)
739778
else:

0 commit comments

Comments
 (0)