Skip to content

Commit 7505643

Browse files
committed
Removed numpy_fast and updated dependent files to eliminate dependency
1 parent 0877994 commit 7505643

File tree

19 files changed

+59
-88
lines changed

19 files changed

+59
-88
lines changed

common/numpy_fast.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

common/pid.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import numpy as np
22
from numbers import Number
33

4-
from openpilot.common.numpy_fast import clip, interp
5-
6-
74
class PIDController:
85
def __init__(self, k_p, k_i, k_f=0., k_d=0., pos_limit=1e308, neg_limit=-1e308, rate=100):
96
self._k_p = k_p
@@ -28,15 +25,15 @@ def __init__(self, k_p, k_i, k_f=0., k_d=0., pos_limit=1e308, neg_limit=-1e308,
2825

2926
@property
3027
def k_p(self):
31-
return interp(self.speed, self._k_p[0], self._k_p[1])
28+
return np.interp(self.speed, self._k_p[0], self._k_p[1])
3229

3330
@property
3431
def k_i(self):
35-
return interp(self.speed, self._k_i[0], self._k_i[1])
32+
return np.interp(self.speed, self._k_i[0], self._k_i[1])
3633

3734
@property
3835
def k_d(self):
39-
return interp(self.speed, self._k_d[0], self._k_d[1])
36+
return np.interp(self.speed, self._k_d[0], self._k_d[1])
4037

4138
@property
4239
def error_integral(self):
@@ -64,10 +61,10 @@ def update(self, error, error_rate=0.0, speed=0.0, override=False, feedforward=0
6461

6562
# Clip i to prevent exceeding control limits
6663
control_no_i = self.p + self.d + self.f
67-
control_no_i = clip(control_no_i, self.neg_limit, self.pos_limit)
68-
self.i = clip(self.i, self.neg_limit - control_no_i, self.pos_limit - control_no_i)
64+
control_no_i = np.clip(control_no_i, self.neg_limit, self.pos_limit)
65+
self.i = np.clip(self.i, self.neg_limit - control_no_i, self.pos_limit - control_no_i)
6966

7067
control = self.p + self.i + self.d + self.f
7168

72-
self.control = clip(control, self.neg_limit, self.pos_limit)
69+
self.control = np.clip(control, self.neg_limit, self.pos_limit)
7370
return self.control

common/tests/test_numpy_fast.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import numpy as np
22

3-
from openpilot.common.numpy_fast import interp
43

54

65
class TestInterp:
@@ -11,11 +10,11 @@ def test_correctness_controls(self):
1110
39.999999, 40, 41]
1211

1312
expected = np.interp(v_ego_arr, _A_CRUISE_MIN_BP, _A_CRUISE_MIN_V)
14-
actual = interp(v_ego_arr, _A_CRUISE_MIN_BP, _A_CRUISE_MIN_V)
13+
actual = np.interp(v_ego_arr, _A_CRUISE_MIN_BP, _A_CRUISE_MIN_V)
1514

1615
np.testing.assert_equal(actual, expected)
1716

1817
for v_ego in v_ego_arr:
1918
expected = np.interp(v_ego, _A_CRUISE_MIN_BP, _A_CRUISE_MIN_V)
20-
actual = interp(v_ego, _A_CRUISE_MIN_BP, _A_CRUISE_MIN_V)
19+
actual = np.interp(v_ego, _A_CRUISE_MIN_BP, _A_CRUISE_MIN_V)
2120
np.testing.assert_equal(actual, expected)

scripts/post-commit

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
#!/usr/bin/env bash
2-
set -e
3-
if [[ -f .git/hooks/post-commit.d/post-commit ]]; then
4-
.git/hooks/post-commit.d/post-commit
5-
fi
6-
tools/op.sh lint --fast
7-
echo ""
1+
#!/bin/sh
2+
command -v git-lfs >/dev/null 2>&1 || { echo >&2 "\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'post-commit' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\n"; exit 2; }
3+
git lfs post-commit "$@"

selfdrive/car/cruise.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from cereal import car
44
from openpilot.common.conversions import Conversions as CV
5-
from openpilot.common.numpy_fast import clip
5+
import numpy as np
66

77

88
# WARNING: this value was determined based on the model's training distribution,
@@ -106,7 +106,7 @@ def _update_v_cruise_non_pcm(self, CS, enabled, is_metric):
106106
if CS.gasPressed and button_type in (ButtonType.decelCruise, ButtonType.setCruise):
107107
self.v_cruise_kph = max(self.v_cruise_kph, CS.vEgo * CV.MS_TO_KPH)
108108

109-
self.v_cruise_kph = clip(round(self.v_cruise_kph, 1), V_CRUISE_MIN, V_CRUISE_MAX)
109+
self.v_cruise_kph = np.clip(round(self.v_cruise_kph, 1), V_CRUISE_MIN, V_CRUISE_MAX)
110110

111111
def update_button_timers(self, CS, enabled):
112112
# increment timer for buttons still pressed
@@ -130,6 +130,6 @@ def initialize_v_cruise(self, CS, experimental_mode: bool) -> None:
130130
if any(b.type in (ButtonType.accelCruise, ButtonType.resumeCruise) for b in CS.buttonEvents) and self.v_cruise_initialized:
131131
self.v_cruise_kph = self.v_cruise_kph_last
132132
else:
133-
self.v_cruise_kph = int(round(clip(CS.vEgo * CV.MS_TO_KPH, initial, V_CRUISE_MAX)))
133+
self.v_cruise_kph = int(round(np.clip(CS.vEgo * CV.MS_TO_KPH, initial, V_CRUISE_MAX)))
134134

135135
self.v_cruise_cluster_kph = self.v_cruise_kph

selfdrive/controls/lib/drive_helpers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from cereal import log
2-
from openpilot.common.numpy_fast import clip
2+
import numpy np
33
from openpilot.common.realtime import DT_CTRL
44

55
MIN_SPEED = 1.0
@@ -13,10 +13,10 @@
1313
MAX_VEL_ERR = 5.0
1414

1515
def clip_curvature(v_ego, prev_curvature, new_curvature):
16-
new_curvature = clip(new_curvature, -MAX_CURVATURE, MAX_CURVATURE)
16+
new_curvature = np.clip(new_curvature, -MAX_CURVATURE, MAX_CURVATURE)
1717
v_ego = max(MIN_SPEED, v_ego)
1818
max_curvature_rate = MAX_LATERAL_JERK / (v_ego**2) # inexact calculation, check https://github.com/commaai/openpilot/pull/24755
19-
safe_desired_curvature = clip(new_curvature,
19+
safe_desired_curvature = np.clip(new_curvature,
2020
prev_curvature - max_curvature_rate * DT_CTRL,
2121
prev_curvature + max_curvature_rate * DT_CTRL)
2222

@@ -26,6 +26,6 @@ def clip_curvature(v_ego, prev_curvature, new_curvature):
2626
def get_speed_error(modelV2: log.ModelDataV2, v_ego: float) -> float:
2727
# ToDo: Try relative error, and absolute speed
2828
if len(modelV2.temporalPose.trans):
29-
vel_err = clip(modelV2.temporalPose.trans[0] - v_ego, -MAX_VEL_ERR, MAX_VEL_ERR)
29+
vel_err = np.clip(modelV2.temporalPose.trans[0] - v_ego, -MAX_VEL_ERR, MAX_VEL_ERR)
3030
return float(vel_err)
3131
return 0.0

selfdrive/controls/lib/latcontrol.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from abc import abstractmethod, ABC
22

3-
from openpilot.common.numpy_fast import clip
3+
import numpy np
44
from openpilot.common.realtime import DT_CTRL
55

66
MIN_LATERAL_CONTROL_SPEED = 0.3 # m/s
@@ -28,5 +28,5 @@ def _check_saturation(self, saturated, CS, steer_limited):
2828
self.sat_count += self.sat_count_rate
2929
else:
3030
self.sat_count -= self.sat_count_rate
31-
self.sat_count = clip(self.sat_count, 0.0, self.sat_limit)
31+
self.sat_count = np.clip(self.sat_count, 0.0, self.sat_limit)
3232
return self.sat_count > (self.sat_limit - 1e-3)

selfdrive/controls/lib/latcontrol_torque.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from cereal import log
44
from opendbc.car.interfaces import LatControlInputs
5-
from openpilot.common.numpy_fast import interp
5+
import numpy as np
66
from openpilot.selfdrive.controls.lib.latcontrol import LatControl
77
from openpilot.common.pid import PIDController
88
from openpilot.selfdrive.controls.lib.vehicle_model import ACCELERATION_DUE_TO_GRAVITY
@@ -51,7 +51,7 @@ def update(self, active, CS, VM, params, steer_limited, desired_curvature, calib
5151
else:
5252
assert calibrated_pose is not None
5353
actual_curvature_pose = calibrated_pose.angular_velocity.yaw / CS.vEgo
54-
actual_curvature = interp(CS.vEgo, [2.0, 5.0], [actual_curvature_vm, actual_curvature_pose])
54+
actual_curvature = np.interp(CS.vEgo, [2.0, 5.0], [actual_curvature_vm, actual_curvature_pose])
5555
curvature_deadzone = 0.0
5656
desired_lateral_accel = desired_curvature * CS.vEgo ** 2
5757

@@ -60,7 +60,7 @@ def update(self, active, CS, VM, params, steer_limited, desired_curvature, calib
6060
actual_lateral_accel = actual_curvature * CS.vEgo ** 2
6161
lateral_accel_deadzone = curvature_deadzone * CS.vEgo ** 2
6262

63-
low_speed_factor = interp(CS.vEgo, LOW_SPEED_X, LOW_SPEED_Y)**2
63+
low_speed_factor = np.interp(CS.vEgo, LOW_SPEED_X, LOW_SPEED_Y)**2
6464
setpoint = desired_lateral_accel + low_speed_factor * desired_curvature
6565
measurement = actual_lateral_accel + low_speed_factor * actual_curvature
6666
gravity_adjusted_lateral_accel = desired_lateral_accel - roll_compensation

selfdrive/controls/lib/longcontrol.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from cereal import car
2-
from openpilot.common.numpy_fast import clip
2+
import numpy as np
33
from openpilot.common.realtime import DT_CTRL
44
from openpilot.selfdrive.controls.lib.drive_helpers import CONTROL_N
55
from openpilot.common.pid import PIDController
@@ -84,5 +84,5 @@ def update(self, active, CS, a_target, should_stop, accel_limits):
8484
output_accel = self.pid.update(error, speed=CS.vEgo,
8585
feedforward=a_target)
8686

87-
self.last_output_accel = clip(output_accel, accel_limits[0], accel_limits[1])
87+
self.last_output_accel = np.clip(output_accel, accel_limits[0], accel_limits[1])
8888
return self.last_output_accel

selfdrive/controls/lib/longitudinal_mpc_lib/long_mpc.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import numpy as np
55
from cereal import log
66
from opendbc.car.interfaces import ACCEL_MIN
7-
from openpilot.common.numpy_fast import clip
87
from openpilot.common.realtime import DT_MDL
98
from openpilot.common.swaglog import cloudlog
109
# WARNING: imports outside of constants will not trigger a rebuild
@@ -320,9 +319,9 @@ def process_lead(self, lead):
320319
# MPC will not converge if immediate crash is expected
321320
# Clip lead distance to what is still possible to brake for
322321
min_x_lead = ((v_ego + v_lead)/2) * (v_ego - v_lead) / (-ACCEL_MIN * 2)
323-
x_lead = clip(x_lead, min_x_lead, 1e8)
324-
v_lead = clip(v_lead, 0.0, 1e8)
325-
a_lead = clip(a_lead, -10., 5.)
322+
x_lead = np.clip(x_lead, min_x_lead, 1e8)
323+
v_lead = np.clip(v_lead, 0.0, 1e8)
324+
a_lead = np.clip(a_lead, -10., 5.)
326325
lead_xv = self.extrapolate_lead(x_lead, v_lead, a_lead, a_lead_tau)
327326
return lead_xv
328327

0 commit comments

Comments
 (0)