|
16 | 16 | # from pyextra.acados_template import AcadosOcpSolver as AcadosOcpSolverFast
|
17 | 17 | from selfdrive.controls.lib.longitudinal_mpc_lib.c_generated_code.acados_ocp_solver_pyx import AcadosOcpSolverFast # pylint: disable=no-name-in-module, import-error
|
18 | 18 |
|
19 |
| -from casadi import SX, vertcat |
| 19 | +from casadi import SX, vertcat, if_else |
20 | 20 |
|
21 | 21 | LONG_MPC_DIR = os.path.dirname(os.path.abspath(__file__))
|
22 | 22 | EXPORT_DIR = os.path.join(LONG_MPC_DIR, "c_generated_code")
|
|
55 | 55 | COMFORT_BRAKE = 2.5
|
56 | 56 | STOP_DISTANCE = 6.0
|
57 | 57 |
|
58 |
| -def get_stopped_equivalence_factor(v_lead): |
59 |
| - return (v_lead**2) / (2 * COMFORT_BRAKE) |
| 58 | +def get_stopped_equivalence_factor(v_lead, v_ego): |
| 59 | + # KRK add linear offset based on speed differential in attempt to |
| 60 | + # rectify sluggish start issue |
| 61 | + v_diff_offset = if_else(v_lead - v_ego > 0, (v_lead - v_ego) * 1, 0) # Linear offset |
| 62 | + v_diff_offset = if_else(v_diff_offset > STOP_DISTANCE, STOP_DISTANCE, v_diff_offset) # Limit max offset |
| 63 | + v_diff_offset = v_diff_offset * ((10 - v_ego)/10) # Offset tapers off ending at v_ego of 10m/s |
| 64 | + distance = (v_lead**2) / (2 * COMFORT_BRAKE) + v_diff_offset |
| 65 | + return distance |
60 | 66 |
|
61 | 67 | def get_safe_obstacle_distance(v_ego, t_follow=T_FOLLOW):
|
62 | 68 | return (v_ego**2) / (2 * COMFORT_BRAKE) + t_follow * v_ego + STOP_DISTANCE
|
63 | 69 |
|
64 | 70 | def desired_follow_distance(v_ego, v_lead, t_follow=T_FOLLOW):
|
65 |
| - return get_safe_obstacle_distance(v_ego, t_follow) - get_stopped_equivalence_factor(v_lead) |
| 71 | + return get_safe_obstacle_distance(v_ego, t_follow) - get_stopped_equivalence_factor(v_lead, v_ego) |
66 | 72 |
|
67 | 73 |
|
68 | 74 | def gen_long_model():
|
@@ -234,6 +240,24 @@ def set_weights(self):
|
234 | 240 | else:
|
235 | 241 | self.set_weights_for_lead_policy()
|
236 | 242 |
|
| 243 | + def get_cost_multipliers(self): |
| 244 | + v_ego = self.x0[1] |
| 245 | + v_ego_bps = [0, 10] |
| 246 | + TFs = [1.0, 1.25, T_FOLLOW] |
| 247 | + # KRKeegan adjustments to costs for different TFs |
| 248 | + # these were calculated using the test_longitudial.py deceleration tests |
| 249 | + x_ego_obstacle_cost_multiplier_tf = interp(self.desired_TF, TFs, [2., 1.3, 1.]) |
| 250 | + j_ego_cost_multiplier_tf = interp(self.desired_TF, TFs, [.1, .8, 1.]) |
| 251 | + d_zone_cost_multiplier_tf = interp(self.desired_TF, TFs, [1.8, 1.3, 1.]) |
| 252 | + # KRKeegan adjustments to improve sluggish acceleration these also |
| 253 | + # alter deceleration in the same range |
| 254 | + x_ego_obstacle_cost_multiplier_v_ego = interp(v_ego, v_ego_bps, [2., 1.]) # double |
| 255 | + j_ego_cost_multiplier_v_ego = interp(v_ego, v_ego_bps, [.5, 1.]) # halve |
| 256 | + # Select the appropriate min/max of the options |
| 257 | + x_ego_obstacle_cost_multiplier = max(x_ego_obstacle_cost_multiplier_tf, x_ego_obstacle_cost_multiplier_v_ego) |
| 258 | + j_ego_cost_multiplier = min(j_ego_cost_multiplier_tf, j_ego_cost_multiplier_v_ego) |
| 259 | + return (x_ego_obstacle_cost_multiplier, j_ego_cost_multiplier, d_zone_cost_multiplier_tf) |
| 260 | + |
237 | 261 | def set_weights_for_lead_policy(self):
|
238 | 262 | # WARNING: deceleration tests with these costs:
|
239 | 263 | # 1.0 TR fails at 3 m/s/s test
|
@@ -343,8 +367,8 @@ def update(self, carstate, radarstate, v_cruise, prev_accel_constraint=False):
|
343 | 367 | # To estimate a safe distance from a moving lead, we calculate how much stopping
|
344 | 368 | # distance that lead needs as a minimum. We can add that to the current distance
|
345 | 369 | # and then treat that as a stopped car/obstacle at this new distance.
|
346 |
| - lead_0_obstacle = lead_xv_0[:,0] + get_stopped_equivalence_factor(lead_xv_0[:,1]) |
347 |
| - lead_1_obstacle = lead_xv_1[:,0] + get_stopped_equivalence_factor(lead_xv_1[:,1]) |
| 370 | + lead_0_obstacle = lead_xv_0[:,0] + get_stopped_equivalence_factor(lead_xv_0[:,1], v_ego) |
| 371 | + lead_1_obstacle = lead_xv_1[:,0] + get_stopped_equivalence_factor(lead_xv_1[:,1], v_ego) |
348 | 372 |
|
349 | 373 | # Fake an obstacle for cruise, this ensures smooth acceleration to set speed
|
350 | 374 | # when the leads are no factor.
|
|
0 commit comments