11# -*- coding: utf-8 -*-
2+ """
3+ Main entry point for the CARLA AutoVision Navigator system.
4+ Integrated Perception, Decision, and Control loops.
5+ """
6+
27import carla
38import sys
49import os
510import cv2
6- import math
11+ import time
712import random
813
9- # 将根目录加入系统路径
1014sys .path .append (os .path .abspath (os .path .join (os .path .dirname (__file__ ), '..' )))
1115import config
1216from utils .geometry import get_speed , get_steer_angle
1317
1418
1519class CarlaClient :
16- """负责管理与 CARLA 服务器的通信以及车辆实体的生命周期 """
20+ """Manages CARLA server connection and vehicle actor lifecycle. """
1721
1822 def __init__ (self ):
23+ """Initializes the client attributes."""
1924 self .client = None
2025 self .world = None
2126 self .blueprint_library = None
2227 self .ego_vehicle = None
2328
2429 def connect (self ):
30+ """Establishes connection to the CARLA simulator server."""
2531 try :
26- print (f"正在尝试连接至 CARLA Server: { config .CARLA_HOST } :{ config .CARLA_PORT } ... " )
32+ print (f"Connecting to CARLA Server: { config .CARLA_HOST } :{ config .CARLA_PORT } " )
2733 self .client = carla .Client (config .CARLA_HOST , config .CARLA_PORT )
2834 self .client .set_timeout (config .TIMEOUT )
2935 self .world = self .client .get_world ()
3036 self .blueprint_library = self .world .get_blueprint_library ()
31- print ("连接状态: 成功! " )
37+ print ("Connected successfully. " )
3238 except Exception as e :
33- print (f"连接状态: 失败! { e } " )
39+ print (f"Failed to connect: { e } " )
3440 sys .exit (1 )
3541
3642 def spawn_ego_vehicle (self ):
43+ """Spawns the ego vehicle at the configured spawn point."""
3744 bp = self .blueprint_library .find (config .VEHICLE_FILTER )
3845 bp .set_attribute ('role_name' , 'ego' )
3946 spawn_points = self .world .get_map ().get_spawn_points ()
4047 spawn_point = spawn_points [config .SPAWN_POINT_INDEX ] if config .SPAWN_POINT_INDEX < len (
4148 spawn_points ) else random .choice (spawn_points )
4249 self .ego_vehicle = self .world .spawn_actor (bp , spawn_point )
43- print (f"车辆生成成功: ID[{ self .ego_vehicle .id } ]" )
4450 return self .ego_vehicle
4551
4652 def cleanup (self ):
53+ """Destroys actors and cleans up the simulation environment."""
4754 if self .ego_vehicle and self .ego_vehicle .is_alive :
4855 self .ego_vehicle .destroy ()
49- print ("已销毁主车,资源清理完毕。" )
56+ print ("Environment cleaned up." )
57+
5058
5159if __name__ == "__main__" :
5260 from sensor_manager import SensorManager
5361 from object_detector import YOLOv3Detector
5462 from pid_controller import PIDController
55- from decision_maker import DecisionMaker # 引入决策器
63+ from decision_maker import DecisionMaker
5664
5765 connector = CarlaClient ()
5866 sensors = None
@@ -64,56 +72,53 @@ def cleanup(self):
6472 sensors = SensorManager (connector .world , vehicle )
6573 sensors .attach_camera ()
6674 detector = YOLOv3Detector ()
67- decision = DecisionMaker () # 初始化决策器
75+ decision = DecisionMaker ()
6876
6977 speed_pid = PIDController (config .K_P_SPEED , config .K_I_SPEED , config .K_D_SPEED )
7078 steer_pid = PIDController (config .K_P_STEER , config .K_I_STEER , config .K_D_STEER )
7179
72- print ("全系统启动:感知+决策+控制。按下 'q' 键退出..." )
80+ last_time = time .time ()
81+ print ("System fully integrated. Monitoring performance..." )
82+
7383 while True :
74- frame = sensors .get_current_frame ()
75- current_speed = get_speed (vehicle )
84+ # Performance Monitoring (FPS)
85+ start_time = time .time ()
86+ fps = 1.0 / (start_time - last_time + 1e-6 )
87+ last_time = start_time
7688
77- # 1. 感知与检测
78- detections = []
79- if frame is not None :
80- detections = detector .detect (frame )
81- frame = detector .draw_labels (frame , detections )
89+ # 1. Perception
90+ frame = sensors .get_current_frame ()
91+ detections = detector .detect (frame ) if frame is not None else []
8292
83- # 2. 决策层分析
84- # 根据检测结果判断是否需要紧急刹车
93+ # 2. Decision
8594 is_emergency = decision .process_detections (detections , config .CAMERA_HEIGHT )
8695
87- # 3. 控制信号计算
96+ # 3. Control
97+ current_speed = get_speed (vehicle )
8898 waypoint = carla_map .get_waypoint (vehicle .get_location ()).next (8.0 )[0 ]
8999 angle_error = get_steer_angle (vehicle , waypoint )
90100
91- # 如果是紧急情况,目标速度设为 0
92101 target_v = 0.0 if is_emergency else config .TARGET_SPEED
93102 speed_signal = speed_pid .run_step (target_v , current_speed )
94103 steer_signal = steer_pid .run_step (0 , - angle_error )
95104
96- # 4. 应用控制
97105 control = vehicle .get_control ()
98106 if is_emergency :
99- control .throttle = 0.0
100- control .brake = 1.0 # 全力刹车
101- elif speed_signal >= 0 :
102- control .throttle = speed_signal
103- control .brake = 0.0
107+ control .throttle , control .brake = 0.0 , 1.0
104108 else :
105- control .throttle = 0.0
106- control .brake = abs (speed_signal )
109+ control .throttle = max ( 0 , speed_signal )
110+ control .brake = abs (min ( 0 , speed_signal ) )
107111
108112 control .steer = steer_signal
109113 vehicle .apply_control (control )
110114
111- # 5. UI 显示
115+ # 4. Visualization & UI
112116 if frame is not None :
117+ frame = detector .draw_labels (frame , detections )
118+ cv2 .putText (frame , f"FPS: { int (fps )} " , (10 , 90 ), 1 , 1.5 , (255 , 0 , 0 ), 2 )
113119 if is_emergency :
114- cv2 .putText (frame , "EMERGENCY BRAKE!" , (250 , 300 ),
115- cv2 .FONT_HERSHEY_SIMPLEX , 1.5 , (0 , 0 , 255 ), 4 )
116- cv2 .imshow ("CARLA Full System Integration" , frame )
120+ cv2 .putText (frame , "AEB ACTIVE" , (250 , 300 ), 1 , 3 , (0 , 0 , 255 ), 4 )
121+ cv2 .imshow ("CARLA AutoVision v1.0-RC" , frame )
117122
118123 if cv2 .waitKey (1 ) & 0xFF == ord ('q' ):
119124 break
0 commit comments