diff --git a/src/Driving_Car/Autonomous_vehicle_airbag_trigger_system.py b/src/Driving_Car/Autonomous_vehicle_airbag_trigger_system.py new file mode 100644 index 0000000000..00449b2099 --- /dev/null +++ b/src/Driving_Car/Autonomous_vehicle_airbag_trigger_system.py @@ -0,0 +1,64 @@ +import tkinter as tk +import random +import time + +# -------------------------- 配置参数 -------------------------- +# 压力阈值(单位:牛) +LOW_PRESSURE_THRESHOLD = 40000 # 4万牛 +MAX_PRESSURE = 2000000 # 200万牛 +UPDATE_INTERVAL = 300 # 界面更新间隔(毫秒),约30FPS + +# -------------------------- 主窗口初始化 -------------------------- +root = tk.Tk() +root.title("Autonomous Vehicle Airbag Trigger System") +root.geometry("800x600") # 窗口大小 + +# 用于显示文字的标签(居中、大字体) +text_label = tk.Label( + root, + font=("SimHei", 36), # 黑体,36号字(支持中文) + bg="black" # 黑色背景 +) +text_label.pack(expand=True, fill="both") # 标签占满整个窗口 + +# 存储当前压力值 +current_pressure = 0.0 + +# -------------------------- 核心函数 -------------------------- +def generate_pressure(): + """生成0~200万牛的模拟压力数据""" + global current_pressure + # 5%概率触发高压力(碰撞),否则低压力 + if random.random() < 0.05: + # 4万~200万牛:随机值 + current_pressure = random.uniform(LOW_PRESSURE_THRESHOLD, MAX_PRESSURE) + else: + # 0~4万牛:随机值 + current_pressure = random.uniform(0, LOW_PRESSURE_THRESHOLD) + return current_pressure + +def update_ui(): + """更新界面文字和颜色""" + # 生成新的压力数据 + pressure = generate_pressure() + # 判断压力区间,设置文字和颜色 + if 0 <= pressure <= LOW_PRESSURE_THRESHOLD: + # 黄色:车辆碰撞 + text_label.config(text="车辆碰撞", fg="#FFFF00") + elif LOW_PRESSURE_THRESHOLD < pressure < MAX_PRESSURE: + # 红色:危险,安全气囊已打开 + text_label.config(text="危险,安全气囊已打开", fg="#FF0000") + else: + # 灰色:无碰撞(超出范围的情况) + text_label.config(text="无碰撞", fg="#808080") + # 打印压力值到控制台(调试用) + print(f"当前压力:{pressure:.2f} N") + # 定时更新(递归调用,实现循环) + root.after(UPDATE_INTERVAL, update_ui) + +# -------------------------- 启动程序 -------------------------- +if __name__ == "__main__": + # 首次调用更新函数 + update_ui() + # 启动主循环 + root.mainloop() \ No newline at end of file diff --git a/src/Driving_Car/Autonomous_vehicle_automatic_windshield_wiper_system.py b/src/Driving_Car/Autonomous_vehicle_automatic_windshield_wiper_system.py new file mode 100644 index 0000000000..134d036d2a --- /dev/null +++ b/src/Driving_Car/Autonomous_vehicle_automatic_windshield_wiper_system.py @@ -0,0 +1,59 @@ +import time +import random +from collections import deque + +def get_raindrop_count(): + """模拟雨滴识别器,返回当前挡风玻璃的雨滴数量(0~150)""" + return random.randint(0, 150) + +def get_stable_rain_count(history, current_count, window_size=5): + """ + 滑动窗口平均法处理雨滴数量,避免瞬时波动导致档位频繁切换(防抖逻辑) + :param history: 存储历史雨滴数量的队列 + :param current_count: 当前检测的雨滴数量 + :param window_size: 滑动窗口大小 + :return: 稳定后的雨滴数量平均值 + """ + history.append(current_count) + if len(history) > window_size: + history.popleft() + return sum(history) / len(history) + +def judge_wiper_gear(raindrop_num): + """ + 根据雨滴数量判断雨刮器档位 + :param raindrop_num: 雨滴数量 + :return: 雨刮器状态、档位参数值 + """ + if raindrop_num == 0: + return "暂停", 0.0 + elif 0 < raindrop_num <= 50: + return "小档", 0.33 + elif 50 < raindrop_num <= 100: + return "中档", 0.66 + else: + return "高档", 1.0 + +def main(): + # 初始化滑动窗口队列,用于存储历史雨滴数量(防抖) + rain_history = deque(maxlen=5) + print("自动雨刮器系统已启动(按Ctrl+C退出)...\n") + try: + while True: + # 1. 获取当前雨滴数量(模拟传感器数据) + current_rain = get_raindrop_count() + # 2. 处理雨滴数量,得到稳定值 + stable_rain = get_stable_rain_count(rain_history, current_rain) + # 3. 判断雨刮器档位 + wiper_status, wiper_value = judge_wiper_gear(round(stable_rain)) + # 4. 输出状态信息 + print(f"当前检测雨滴数量:{current_rain} | 稳定后数量:{round(stable_rain)} | 雨刮器状态:{wiper_status}(参数值:{wiper_value})") + # 5. 控制雨刮器(此处为模拟,实际场景中替换为硬件/仿真接口调用) + # control.wiper = wiper_value # CARLA环境下的控制逻辑 + # vehicle.apply_control(control) + time.sleep(1) # 每秒更新一次状态 + except KeyboardInterrupt: + print("\n\n自动雨刮器系统已停止运行") + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/src/Driving_Car/Autonomous_vehicle_battery_gauge.py b/src/Driving_Car/Autonomous_vehicle_battery_gauge.py new file mode 100644 index 0000000000..aeee430c47 --- /dev/null +++ b/src/Driving_Car/Autonomous_vehicle_battery_gauge.py @@ -0,0 +1,100 @@ +# 注释掉carla导入 +# import carla +import tkinter as tk +from tkinter import ttk +import random +import threading +import time + +class CarlaBatteryMeter: + def __init__(self, root): + self.root = root + self.root.title("Autonomous Vehicle Battery Gauge") + self.root.geometry("400x300") + self.root.resizable(False, False) + + self.battery_capacity = 100 + self.current_battery = 85 + self.lock = threading.Lock() + + # 注释掉Carla初始化 + # self.carla_client = None + # self.carla_vehicle = None + # self.init_carla() + + self.create_battery_ui() + + self.running = True + self.battery_thread = threading.Thread(target=self.update_battery_from_carla, daemon=True) + self.battery_thread.start() + + self.update_battery_display() + + # 注释掉Carla初始化函数 + # def init_carla(self): + # ... + + def create_battery_ui(self): + title_label = ttk.Label(self.root, text="Autonomous Vehicle Battery Gauge", font=("Microsoft YaHei", 16)) + title_label.pack(pady=20) + + self.battery_label = tk.Label( + self.root, + text=str(self.current_battery), + font=("Microsoft YaHei", 48, "bold"), + bg="white" + ) + self.battery_label.pack(pady=30, fill=tk.BOTH, expand=True) + + desc_label = ttk.Label( + self.root, + text="Battery Range: 70~100(Green) | 20~69(Yellow) | 3~19(Red) | 0~2(Low Power)", + font=("Microsoft YaHei", 10) + ) + desc_label.pack(pady=10) + + def update_battery_from_carla(self): + while self.running: + with self.lock: + # 直接使用模拟数据,注释掉Carla相关逻辑 + # if self.carla_vehicle: + # ... + # else: + if random.random() > 0.4: + self.current_battery = max(0, self.current_battery - 1) + else: + self.current_battery = min(self.battery_capacity, self.current_battery + 1) + time.sleep(1) + + def update_battery_display(self): + with self.lock: + current = self.current_battery + + if 70 <= current <= 100: + text = str(current) + color = "#00cc00" + elif 20 <= current < 70: + text = str(current) + color = "#ffcc00" + elif 3 <= current < 20: + text = str(current) + color = "#ff3300" + else: + text = "Low Power" + color = "#ff0000" + + self.battery_label.config(text=text, fg=color) + self.root.after(200, self.update_battery_display) + + def stop(self): + self.running = False + # 注释掉Carla车辆销毁逻辑 + # if self.carla_vehicle: + # self.carla_vehicle.destroy() + self.root.quit() + +if __name__ == "__main__": + root = tk.Tk() + app = CarlaBatteryMeter(root) + root.protocol("WM_DELETE_WINDOW", app.stop) + root.mainloop() \ No newline at end of file diff --git a/src/Driving_Car/Autonomous_vehicle_fuel_gauge.py b/src/Driving_Car/Autonomous_vehicle_fuel_gauge.py new file mode 100644 index 0000000000..453e0ec569 --- /dev/null +++ b/src/Driving_Car/Autonomous_vehicle_fuel_gauge.py @@ -0,0 +1,73 @@ +import random +import time +import sys + + +def get_fuel_color_code(fuel): + """返回ANSI颜色码,终端支持时生效""" + # ANSI颜色码:绿色、黄色、红色、默认 + if fuel >= 70: + return "\033[32m" # 绿色 + elif fuel >= 20: + return "\033[33m" # 黄色 + elif fuel >= 3: + return "\033[31m" # 红色 + else: + return "\033[31m" # 红色 + + +RESET_COLOR = "\033[0m" # 重置颜色 + + +def display_fuel_gauge(fuel): + """在控制台显示油表信息""" + # 清屏(跨平台) + import os + os.system('cls' if os.name == 'nt' else 'clear') + + # 输出油表标题 + print("=" * 40) + print(" Carla Autonomous Vehicle Fuel Gauge") + print("=" * 40) + + # 处理油量显示逻辑 + if fuel >= 3: + fuel_text = f"{fuel}" + color_code = get_fuel_color_code(fuel) + print(f"\n Current Fuel Level: {color_code}{fuel_text}{RESET_COLOR}") + else: + print(f"\n {get_fuel_color_code(fuel)}油量不足{RESET_COLOR}") + + # 绘制简易进度条 + print("\n [", end="") + progress = min(int(fuel), 100) + for i in range(100): + if i < progress: + print(get_fuel_color_code(fuel) + "■" + RESET_COLOR, end="") + else: + print(" ", end="") + print("]") + print("\n (Press Ctrl+C to exit)") + + +def get_carla_fuel_data(): + """模拟从Carla获取油量数据""" + global current_fuel + # 模拟油量缓动变化 + current_fuel += random.randint(-2, 2) + current_fuel = max(0, min(100, current_fuel)) + return current_fuel + + +# 初始化油量 +current_fuel = 80 + +if __name__ == "__main__": + try: + while True: + fuel_level = get_carla_fuel_data() + display_fuel_gauge(fuel_level) + time.sleep(0.5) # 控制刷新频率 + except KeyboardInterrupt: + print("\n\n Exiting Fuel Gauge...") + sys.exit(0) \ No newline at end of file diff --git a/src/Driving_Car/Autonomous_vehicle_temperature_control_system.py b/src/Driving_Car/Autonomous_vehicle_temperature_control_system.py new file mode 100644 index 0000000000..897af6343a --- /dev/null +++ b/src/Driving_Car/Autonomous_vehicle_temperature_control_system.py @@ -0,0 +1,47 @@ +import random +import time +import sys + +# 模拟温度传感器:生成-50~50℃的随机温度 +def get_temperature(): + """生成-50~50℃的随机温度,也可手动设置固定值测试""" + return random.uniform(-50.0, 50.0) + # 测试固定温度:return 26.0 # 温度适宜 + # return -10.0 # 温度过低 + # return 35.0 # 温度过高 + +# 温度判断与显示(控制台彩色输出+弹窗可选) +def display_temperature_info(temp): + """根据温度显示对应信息,支持控制台彩色输出""" + # 控制台彩色字体(Windows/Linux通用的ANSI转义码) + class Color: + BLUE = '\033[94m' # 蓝色 + GREEN = '\033[92m' # 绿色 + RED = '\033[91m' # 红色 + RESET = '\033[0m' # 重置颜色 + + if -50.0 <= temp <= 25.0: + # 温度过低:蓝色文本 + text = "温度过低,是否需要打开空调?" + print(f"{Color.BLUE}{text}{Color.RESET}") + elif temp == 26.0: + # 温度适宜:绿色文本 + text = "温度适宜" + print(f"{Color.GREEN}{text}{Color.RESET}") + elif 27.0 <= temp <= 50.0: + # 温度过高:红色文本 + text = "温度过高,是否需要打开空调?" + print(f"{Color.RED}{text}{Color.RESET}") + else: + text = "温度超出测量范围" + print(text) + +# 主循环 +try: + while True: + current_temp = get_temperature() + print(f"\n当前温度: {current_temp:.1f}℃", end=" ") + display_temperature_info(current_temp) + time.sleep(0.5) # 控制帧率 +except KeyboardInterrupt: + print("\n程序手动终止") \ No newline at end of file diff --git a/src/Driving_Car/Autonomous_vehicle_undercarriage_anti_scraping_system.py b/src/Driving_Car/Autonomous_vehicle_undercarriage_anti_scraping_system.py new file mode 100644 index 0000000000..75ddc38825 --- /dev/null +++ b/src/Driving_Car/Autonomous_vehicle_undercarriage_anti_scraping_system.py @@ -0,0 +1,45 @@ +import random +import time + +def get_chassis_ground_distance(): + """自动模拟底盘距离地面的距离(0~15cm 随机波动)""" + return round(random.uniform(0, 15), 1) + +def get_safety_status(distance_cm): + """根据距离返回状态文本和颜色标识""" + if distance_cm > 10.0: + return "安全", "绿色" + elif 5.0 <= distance_cm <= 10.0: + return "注意安全", "黄色" + else: + return "危险", "红色" + +def print_color_text(text, color): + """终端带颜色输出文字(Windows/Linux/Mac通用)""" + color_codes = { + "绿色": "\033[32m", + "黄色": "\033[33m", + "红色": "\033[31m", + "重置": "\033[0m" + } + print(f"{color_codes[color]}{text}{color_codes['重置']}") + +def main(): + """主函数:自动循环测试防刮车底系统""" + print("===== 无人车防刮车底系统(自动测试模式)=====") + print("系统已启动,每秒自动更新距离和状态 | 按 Ctrl+C 退出\n") + + try: + while True: + distance = get_chassis_ground_distance() + status_text, status_color = get_safety_status(distance) + # 输出实时数据 + print(f"当前底盘离地距离:{distance} cm | 状态:", end="") + print_color_text(status_text, status_color) + # 每秒更新一次 + time.sleep(1) + except KeyboardInterrupt: + print("\n\n自动测试结束,系统退出!") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/src/Driving_Car/Autonomous_vehicle_vear_vollision_avoidance_system.py b/src/Driving_Car/Autonomous_vehicle_vear_vollision_avoidance_system.py new file mode 100644 index 0000000000..21be621e0d --- /dev/null +++ b/src/Driving_Car/Autonomous_vehicle_vear_vollision_avoidance_system.py @@ -0,0 +1,71 @@ +import random +import time +import sys + +# -------------------------- 防追尾预警系统核心逻辑 -------------------------- +class RearCollisionWarning: + def __init__(self): + # 初始化后方障碍物距离(模拟200米外无障碍物) + self.rear_distance = float('inf') + + def simulate_distance_detection(self): + """模拟距离识别器:随机生成0-200米的障碍物距离(可改为手动输入)""" + # 随机生成距离,模拟实时检测(也可注释此行为固定值测试) + self.rear_distance = random.uniform(0, 200) + # 测试固定距离示例:取消下面注释,注释上面随机数即可 + # self.rear_distance = 5 # 测试0-10米场景 + # self.rear_distance = 30 # 测试10-49米场景 + # self.rear_distance = 75 # 测试50-99米场景 + # self.rear_distance = 150 # 测试100-200米场景 + + def get_warning_info(self): + """根据距离返回预警文本和颜色标识(控制台用字符/文字表示颜色)""" + distance = self.rear_distance + + if 100 < distance <= 200: + return "后方安全", "绿色" + elif 50 < distance <= 99: + return "后方有障碍物,谨慎驾驶!", "黄色" + elif 10 < distance <= 49: + return "后方注意安全", "红色" + elif 0 <= distance <= 10: + return "小心追尾", "红色" + else: + # 超出200米,默认显示后方安全 + return "后方安全", "绿色" + +# -------------------------- 控制台输出美化(可选) -------------------------- +def print_warning(text, color, distance): + """在控制台打印带颜色标识的预警信息(Windows/Linux通用)""" + # 清空控制台(跨平台方式) + print("\033c" if sys.platform != "win32" else "\n" * 50, end="") + # 打印标题 + print("=" * 50) + print(" 无人车防追尾预警系统") + print("=" * 50) + # 打印距离和预警信息 + print(f"当前后方障碍物距离:{distance:.1f} 米") + print(f"预警信息({color}):{text}") + print("=" * 50) + print("按 Ctrl+C 退出程序") + +# -------------------------- 主循环 -------------------------- +def main(): + warning_system = RearCollisionWarning() + try: + while True: + # 更新障碍物距离 + warning_system.simulate_distance_detection() + # 获取预警信息 + text, color = warning_system.get_warning_info() + # 控制台输出 + print_warning(text, color, warning_system.rear_distance) + # 每秒更新一次(模拟实时检测频率) + time.sleep(1) + except KeyboardInterrupt: + # 捕获Ctrl+C,优雅退出 + print("\n程序已退出") + sys.exit(0) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/src/Driving_Car/README.md b/src/Driving_Car/README.md index 8c27ba2124..aea3eb6553 100644 --- a/src/Driving_Car/README.md +++ b/src/Driving_Car/README.md @@ -1 +1,39 @@ -无人车 \ No newline at end of file + +--- + +# 无人驾驶汽车项目 + +## 项目简介 + +本项目是一个基于Python和PyCharm Community Edition开发的无人驾驶汽车仿真系统。通过计算机视觉、机器学习和传感器数据融合技术,实现车辆在模拟环境中的自主导航和避障功能。 + +## 核心功能 + +**路径规划**: 基于A*算法和RRT算法的智能路径规划 + +**障碍物检测**: 使用YOLO和OpenCV进行实时目标检测与识别 + +**车辆控制**: PID控制器实现精确的转向和速度控制 + +**传感器模拟**: 模拟激光雷达、摄像头和超声波传感器数据 + +**3D可视化**: 基于PyGame的实时场景渲染 + +## 技术栈 +**开发环境**: PyCharm Community Edition 2024+ + +**核心语言**: Python 3.8+ + +**计算机视觉**: OpenCV, PIL, NumPy + +**机器学习**: TensorFlow, scikit-learn + +**3D图形**: PyGame, Pyglet. + +**数据处理**: Pandas, Matplotlib + +## 快速开始 + +1. 克隆项目到PyCharm +2. 安装依赖: `pip install -r requirements.txt` +3. 运行主程序: `python main.py` diff --git a/src/Driving_Car/Unmanned_vehicle_directional_movemen.py b/src/Driving_Car/Unmanned_vehicle_directional_movemen.py new file mode 100644 index 0000000000..de1c836fd2 --- /dev/null +++ b/src/Driving_Car/Unmanned_vehicle_directional_movemen.py @@ -0,0 +1,71 @@ +import tkinter as tk + +class UnmannedCarSimulator: + def __init__(self, root): + # 初始化主窗口 + self.root = root + self.root.title("无人车平地移动模拟") + self.root.resizable(False, False) + + # 无人车参数 + self.car_x = 250 # 初始x坐标 + self.car_y = 250 # 初始y坐标 + self.car_size = 30 # 车的大小(正方形边长) + self.move_step = 10 # 每次移动的步长 + + # 创建画布(模拟平地) + self.canvas = tk.Canvas(root, width=500, height=500, bg="white") + self.canvas.pack() + + # 绘制初始无人车(矩形) + self.car = self.canvas.create_rectangle( + self.car_x - self.car_size/2, self.car_y - self.car_size/2, + self.car_x + self.car_size/2, self.car_y + self.car_size/2, + fill="blue" + ) + + # 绑定方向键事件 + self.root.bind("", self.move_up) + self.root.bind("", self.move_down) + self.root.bind("", self.move_left) + self.root.bind("", self.move_right) + + # 聚焦窗口以接收键盘事件 + self.canvas.focus_set() + + def move_up(self, event): + """向上移动(y坐标减小)""" + new_y = self.car_y - self.move_step + # 边界检测:不超出画布上沿 + if new_y - self.car_size/2 >= 0: + self.car_y = new_y + self.canvas.move(self.car, 0, -self.move_step) + + def move_down(self, event): + """向下移动(y坐标增大)""" + new_y = self.car_y + self.move_step + # 边界检测:不超出画布下沿 + if new_y + self.car_size/2 <= 500: + self.car_y = new_y + self.canvas.move(self.car, 0, self.move_step) + + def move_left(self, event): + """向左移动(x坐标减小)""" + new_x = self.car_x - self.move_step + # 边界检测:不超出画布左沿 + if new_x - self.car_size/2 >= 0: + self.car_x = new_x + self.canvas.move(self.car, -self.move_step, 0) + + def move_right(self, event): + """向右移动(x坐标增大)""" + new_x = self.car_x + self.move_step + # 边界检测:不超出画布右沿 + if new_x + self.car_size/2 <= 500: + self.car_x = new_x + self.canvas.move(self.car, self.move_step, 0) + +if __name__ == "__main__": + root = tk.Tk() + simulator = UnmannedCarSimulator(root) + root.mainloop() \ No newline at end of file diff --git a/src/Driving_Car/Unmanned_vehicle_overload_detection_system.py b/src/Driving_Car/Unmanned_vehicle_overload_detection_system.py new file mode 100644 index 0000000000..a087c0b644 --- /dev/null +++ b/src/Driving_Car/Unmanned_vehicle_overload_detection_system.py @@ -0,0 +1,84 @@ +import tkinter as tk +import random +import threading +import time + +# ------------------- 系统配置常量 ------------------- +# 颜色定义 +COLOR_NORMAL = "#00ff00" # 正常-绿色 +COLOR_OVERLOAD = "#ff0000" # 超重-红色 +COLOR_BACKGROUND = "#000000"# 背景-黑色 +TEXT_SIZE = 40 # 文字大小 + +# 重量阈值配置 +MIN_NORMAL_WEIGHT = 1 # 最小正常重量(kg) +MAX_NORMAL_WEIGHT = 1000 # 最大正常重量(kg) + +# 窗口配置 +WINDOW_WIDTH = 500 +WINDOW_HEIGHT = 500 + +class VehicleOverloadSystem: + def __init__(self, root): + self.root = root + self.root.title("Unmanned Vehicle Overload Detection System") + self.root.geometry(f"{WINDOW_WIDTH}x{WINDOW_HEIGHT}") + self.root.configure(bg=COLOR_BACKGROUND) + + # 创建显示文字的标签 + self.status_label = tk.Label( + root, + text="", + font=("Arial", TEXT_SIZE, "bold"), + bg=COLOR_BACKGROUND + ) + # 居中显示 + self.status_label.place(relx=0.5, rely=0.5, anchor=tk.CENTER) + + # 控制循环的标志 + self.running = True + + # 启动重量检测线程 + self.detection_thread = threading.Thread(target=self.check_weight) + self.detection_thread.daemon = True # 随主窗口关闭而退出 + self.detection_thread.start() + + # 窗口关闭事件绑定 + self.root.protocol("WM_DELETE_WINDOW", self.on_close) + + def get_vehicle_weight(self): + """模拟无人车重量传感器数据(可替换为真实硬件读取逻辑)""" + # 随机生成0~1500kg的重量 + return random.uniform(0, 1500) + + def check_weight(self): + """循环检测重量并更新界面""" + while self.running: + current_weight = self.get_vehicle_weight() + # 判断重量状态 + if MIN_NORMAL_WEIGHT <= current_weight <= MAX_NORMAL_WEIGHT: + status_text = "正常" + text_color = COLOR_NORMAL + else: + status_text = "超重" + text_color = COLOR_OVERLOAD + + # 更新界面(需在主线程中执行) + self.root.after(0, self.update_label, status_text, text_color) + # 控制检测频率(每秒1次,可调整) + time.sleep(1) + + def update_label(self, text, color): + """更新标签的文字和颜色""" + self.status_label.config(text=text, fg=color) + + def on_close(self): + """窗口关闭时的清理操作""" + self.running = False + self.root.destroy() + print("无人车超重检测系统已关闭") + +if __name__ == '__main__': + root = tk.Tk() + app = VehicleOverloadSystem(root) + root.mainloop() \ No newline at end of file diff --git a/src/Driving_Car/Unmanned_vehicle_speed_alarm_system.py b/src/Driving_Car/Unmanned_vehicle_speed_alarm_system.py new file mode 100644 index 0000000000..5dd8c6bf3c --- /dev/null +++ b/src/Driving_Car/Unmanned_vehicle_speed_alarm_system.py @@ -0,0 +1,63 @@ +import random +import time + + +class CarlaSpeedAlarmSystem: + """Carla无人车速度报警系统""" + + def __init__(self): + # 速度区间配置(可扩展) + self.speed_ranges = [ + (0, 60, "正常"), + (61, 120, "超速"), + (121, 150, "超速危险") + ] + + def get_carla_vehicle_speed(self): + """ + 模拟从Carla获取车辆实时速度(km/h) + 实际项目中替换为Carla API的速度读取逻辑: + vehicle.get_velocity() → 转换为km/h + """ + # 模拟速度波动:0~160(包含异常值) + return random.uniform(0, 160) + + def judge_speed_status(self, speed): + """根据速度判断状态""" + # 检查速度是否合法范围外 + if speed < 0 or speed > 150: + return "速度异常" + + # 遍历速度区间判断状态 + for min_speed, max_speed, status in self.speed_ranges: + if min_speed <= speed <= max_speed: + return status + return "未知状态" + + def run(self): + """运行速度报警系统""" + print("=== Carla无人车速度报警系统启动 ===") + print("速度区间说明:") + print("0~60 km/h → 正常") + print("61~120 km/h → 超速") + print("121~150 km/h → 超速危险") + print("-" * 50) + + try: + while True: + # 获取速度 + current_speed = self.get_carla_vehicle_speed() + # 判断状态 + speed_status = self.judge_speed_status(current_speed) + # 输出控制面板信息 + print(f"当前车速:{current_speed:.1f} km/h | 状态:{speed_status}") + # 模拟实时更新(每1秒刷新一次) + time.sleep(1) + except KeyboardInterrupt: + print("\n=== 系统已停止运行 ===") + + +if __name__ == "__main__": + # 实例化并运行系统 + alarm_system = CarlaSpeedAlarmSystem() + alarm_system.run() \ No newline at end of file