mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-08-22 00:33:44 +08:00
memory leaks
This commit is contained in:
@@ -25,12 +25,55 @@ class CurveSpeedController:
|
||||
self.training_timer = 0
|
||||
|
||||
curvature_data = self.starpilot_planner.params.get("CurvatureData")
|
||||
self.curvature_data = curvature_data if isinstance(curvature_data, dict) else {}
|
||||
self.curvature_data = self._normalize_curvature_data(curvature_data)
|
||||
|
||||
self.required_curvatures = [str(round(road_curvature, ROUNDING_PRECISION)) for road_curvature in np.arange(MIN_CURVATURE, MAX_CURVATURE + STEP, STEP)]
|
||||
|
||||
self.update_lateral_acceleration()
|
||||
|
||||
@staticmethod
|
||||
def _bucket_curvature(road_curvature):
|
||||
clipped_curvature = float(np.clip(road_curvature, MIN_CURVATURE, MAX_CURVATURE))
|
||||
bucket_index = round((clipped_curvature - MIN_CURVATURE) / STEP)
|
||||
bucketed_curvature = MIN_CURVATURE + (bucket_index * STEP)
|
||||
return str(round(bucketed_curvature, ROUNDING_PRECISION))
|
||||
|
||||
@classmethod
|
||||
def _normalize_curvature_data(cls, curvature_data):
|
||||
if not isinstance(curvature_data, dict):
|
||||
return {}
|
||||
|
||||
normalized = {}
|
||||
for key, value in curvature_data.items():
|
||||
if not isinstance(value, dict):
|
||||
continue
|
||||
|
||||
try:
|
||||
raw_curvature = abs(float(key))
|
||||
average = float(value["average"])
|
||||
count = int(value["count"])
|
||||
except (KeyError, TypeError, ValueError):
|
||||
continue
|
||||
|
||||
if count <= 0:
|
||||
continue
|
||||
|
||||
bucket = cls._bucket_curvature(raw_curvature)
|
||||
if bucket in normalized:
|
||||
existing = normalized[bucket]
|
||||
total_count = existing["count"] + count
|
||||
normalized[bucket] = {
|
||||
"average": ((existing["average"] * existing["count"]) + (average * count)) / total_count,
|
||||
"count": total_count,
|
||||
}
|
||||
else:
|
||||
normalized[bucket] = {
|
||||
"average": average,
|
||||
"count": count,
|
||||
}
|
||||
|
||||
return normalized
|
||||
|
||||
def log_data(self, v_ego, sm):
|
||||
self.enable_training = v_ego > CRUISING_SPEED
|
||||
self.enable_training &= not self.starpilot_planner.tracking_lead
|
||||
@@ -41,9 +84,9 @@ class CurveSpeedController:
|
||||
|
||||
if self.training_timer >= PLANNER_TIME and self.starpilot_planner.driving_in_curve and not (sm["carState"].leftBlinker or sm["carState"].rightBlinker):
|
||||
lateral_acceleration = abs(self.starpilot_planner.lateral_acceleration)
|
||||
road_curvature = abs(round(self.starpilot_planner.road_curvature, ROUNDING_PRECISION))
|
||||
road_curvature = self._bucket_curvature(abs(self.starpilot_planner.road_curvature))
|
||||
|
||||
key = str(road_curvature)
|
||||
key = road_curvature
|
||||
if key in self.curvature_data:
|
||||
data = self.curvature_data[key]
|
||||
|
||||
|
||||
@@ -70,11 +70,16 @@ class SpeedLimitController:
|
||||
self.previous_target = self.starpilot_planner.params.get_float("PreviousSpeedLimit")
|
||||
|
||||
self.executor = ThreadPoolExecutor(max_workers=1)
|
||||
self.mapbox_future = None
|
||||
|
||||
self.session = requests.Session()
|
||||
self.session.headers.update({"Accept-Language": "en"})
|
||||
self.session.headers.update({"User-Agent": "starpilot-mapbox-speed-limit-retriever/1.0 (https://github.com/FrogAi/StarPilot)"})
|
||||
|
||||
def shutdown(self):
|
||||
self.executor.shutdown(wait=False, cancel_futures=True)
|
||||
self.session.close()
|
||||
|
||||
@property
|
||||
def experimental_mode(self):
|
||||
return self.target == 0 and bool(getattr(self.starpilot_toggles, "slc_fallback_experimental_mode", False))
|
||||
@@ -113,11 +118,9 @@ class SpeedLimitController:
|
||||
return
|
||||
|
||||
def make_request():
|
||||
successful = False
|
||||
response_data = None
|
||||
try:
|
||||
self.calling_mapbox = True
|
||||
|
||||
successful = False
|
||||
|
||||
if not is_url_pingable(self.mapbox_host):
|
||||
self.segment_distance = 1000
|
||||
return None
|
||||
@@ -160,8 +163,7 @@ class SpeedLimitController:
|
||||
response.raise_for_status()
|
||||
|
||||
successful = True
|
||||
|
||||
return response.json()
|
||||
response_data = response.json()
|
||||
except Exception as exception:
|
||||
print(f"Unexpected error in Mapbox request: {exception}")
|
||||
finally:
|
||||
@@ -170,8 +172,7 @@ class SpeedLimitController:
|
||||
if not successful:
|
||||
self.mapbox_limit = 0
|
||||
self.segment_distance = v_ego
|
||||
|
||||
return None
|
||||
return response_data
|
||||
|
||||
def complete_request(future):
|
||||
try:
|
||||
@@ -212,8 +213,18 @@ class SpeedLimitController:
|
||||
print(f"Mapbox Callback Error: {exception}")
|
||||
self.mapbox_limit = 0
|
||||
self.segment_distance = v_ego
|
||||
finally:
|
||||
self.mapbox_future = None
|
||||
|
||||
future = self.executor.submit(make_request)
|
||||
self.calling_mapbox = True
|
||||
try:
|
||||
future = self.executor.submit(make_request)
|
||||
except RuntimeError:
|
||||
self.calling_mapbox = False
|
||||
self.segment_distance = v_ego
|
||||
return
|
||||
|
||||
self.mapbox_future = future
|
||||
future.add_done_callback(complete_request)
|
||||
|
||||
def handle_limit_change(self, desired_source, desired_target, sm):
|
||||
|
||||
@@ -55,7 +55,7 @@ class StarPilotPlanner:
|
||||
self.tracking_lead_filter = FirstOrderFilter(0, 0.5, DT_MDL)
|
||||
|
||||
def shutdown(self):
|
||||
self.starpilot_vcruise.slc.executor.shutdown(wait=False, cancel_futures=True)
|
||||
self.starpilot_vcruise.slc.shutdown()
|
||||
self.starpilot_weather.executor.shutdown(wait=False, cancel_futures=True)
|
||||
|
||||
def update(self, now, time_validated, sm, starpilot_toggles):
|
||||
|
||||
Reference in New Issue
Block a user