Skip to content

Commit

Permalink
Future: longitudinal planner: disable change cost when stopped. not e…
Browse files Browse the repository at this point in the history
…ngaged or gas pressed (commaai#23639)

* disable change cost completely on standstill and gas press

* cleanup

* set accel to zero

* clean up logic around standstill

* update ref
  • Loading branch information
pd0wm authored and mostafaayesh committed May 22, 2022
1 parent 8f824bd commit a472115
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
17 changes: 7 additions & 10 deletions selfdrive/controls/lib/longitudinal_mpc_lib/long_mpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,14 @@ def reset(self):
self.x0 = np.zeros(X_DIM)
self.set_weights()

def set_weights(self):
def set_weights(self, prev_accel_constraint=True):
if self.e2e:
self.set_weights_for_xva_policy()
self.params[:,0] = -10.
self.params[:,1] = 10.
self.params[:,2] = 1e5
else:
self.set_weights_for_lead_policy()
self.set_weights_for_lead_policy(prev_accel_constraint)

def get_cost_multipliers(self):
v_ego = self.x0[1]
Expand All @@ -260,14 +260,15 @@ def get_cost_multipliers(self):
a_change = min(a_change_tf, a_change_v_ego)
return (a_change, j_ego, d_zone_tf)

def set_weights_for_lead_policy(self):
def set_weights_for_lead_policy(self, prev_accel_constraint=True):
cost_mulitpliers = self.get_cost_multipliers()
a_change_cost = A_CHANGE_COST if prev_accel_constraint else 0
W = np.asfortranarray(np.diag([X_EGO_OBSTACLE_COST, X_EGO_COST, V_EGO_COST,
A_EGO_COST, A_CHANGE_COST * cost_mulitpliers[0],
A_EGO_COST, a_change_cost * cost_mulitpliers[0],
J_EGO_COST * cost_mulitpliers[1]]))
for i in range(N):
# KRKeegan, decreased timescale to .5s since Toyota lag is set to .3s
W[4,4] = A_CHANGE_COST * cost_mulitpliers[0] * np.interp(T_IDXS[i], [0.0, 0.5, 2.0], [1.0, 1.0, 0.0])
W[4,4] = a_change_cost * cost_mulitpliers[0] * np.interp(T_IDXS[i], [0.0, 0.5, 2.0], [1.0, 1.0, 0.0])
self.solver.cost_set(i, 'W', W)
# Setting the slice without the copy make the array not contiguous,
# causing issues with the C interface.
Expand Down Expand Up @@ -355,7 +356,6 @@ def update(self, carstate, radarstate, v_cruise, prev_accel_constraint=False):
self.update_TF(carstate)
self.set_weights()
v_ego = self.x0[1]
a_ego = self.x0[2]
self.status = radarstate.leadOne.status or radarstate.leadTwo.status

lead_xv_0 = self.process_lead(radarstate.leadOne)
Expand Down Expand Up @@ -383,10 +383,7 @@ def update(self, carstate, radarstate, v_cruise, prev_accel_constraint=False):
x_obstacles = np.column_stack([lead_0_obstacle, lead_1_obstacle, cruise_obstacle])
self.source = SOURCES[np.argmin(x_obstacles[0])]
self.params[:,2] = np.min(x_obstacles, axis=1)
if prev_accel_constraint:
self.params[:,3] = np.copy(self.prev_a)
else:
self.params[:,3] = a_ego
self.params[:,3] = np.copy(self.prev_a)
self.params[:,4] = self.desired_TF

self.run()
Expand Down
20 changes: 13 additions & 7 deletions selfdrive/controls/lib/longitudinal_planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def __init__(self, CP, init_v=0.0, init_a=0.0):

def update(self, sm):
v_ego = sm['carState'].vEgo
a_ego = sm['carState'].aEgo

v_cruise_kph = sm['controlsState'].vCruise
v_cruise_kph = min(v_cruise_kph, V_CRUISE_MAX)
Expand All @@ -67,12 +66,16 @@ def update(self, sm):
long_control_state = sm['controlsState'].longControlState
force_slow_decel = sm['controlsState'].forceDecel

prev_accel_constraint = True
if long_control_state == LongCtrlState.off or sm['carState'].gasPressed:
# Reset current state when not engaged, or user is controlling the speed
reset_state = long_control_state == LongCtrlState.off
reset_state = reset_state or sm['carState'].gasPressed

# No change cost when user is controlling the speed, or when standstill
prev_accel_constraint = not (reset_state or sm['carState'].standstill)

if reset_state:
self.v_desired_filter.x = v_ego
self.a_desired = a_ego
# Smoothly changing between accel trajectory is only relevant when OP is driving
prev_accel_constraint = False
self.a_desired = 0.0

# Prevent divergence, smooth in current v_ego
self.v_desired_filter.x = max(0.0, self.v_desired_filter.update(v_ego))
Expand All @@ -86,9 +89,12 @@ def update(self, sm):
# clip limits, cannot init MPC outside of bounds
accel_limits_turns[0] = min(accel_limits_turns[0], self.a_desired + 0.05)
accel_limits_turns[1] = max(accel_limits_turns[1], self.a_desired - 0.05)

self.mpc.set_weights(prev_accel_constraint)
self.mpc.set_accel_limits(accel_limits_turns[0], accel_limits_turns[1])
self.mpc.set_cur_state(self.v_desired_filter.x, self.a_desired)
self.mpc.update(sm['carState'], sm['radarState'], v_cruise, prev_accel_constraint=prev_accel_constraint)
self.mpc.update(sm['carState'], sm['radarState'], v_cruise)

self.v_desired_trajectory = np.interp(T_IDXS[:CONTROL_N], T_IDXS_MPC, self.mpc.v_solution)
self.a_desired_trajectory = np.interp(T_IDXS[:CONTROL_N], T_IDXS_MPC, self.mpc.a_solution)
self.j_desired_trajectory = np.interp(T_IDXS[:CONTROL_N], T_IDXS_MPC[:-1], self.mpc.j_solution)
Expand Down

0 comments on commit a472115

Please sign in to comment.