diff --git a/src/Smart_car/Speed_warning.py b/src/Smart_car/Speed_warning.py new file mode 100644 index 0000000000..bb69dd8071 --- /dev/null +++ b/src/Smart_car/Speed_warning.py @@ -0,0 +1,102 @@ +import time +import serial # 需安装:pip install pyserial +import random # 仿真速度数据(真实场景替换为传感器读取) + +# ------------------- 配置参数 ------------------- +# 限速阈值(km/h) +SPEED_LIMIT = 20 +# 预警等级配置 +WARNING_LEVELS = { + "low": (SPEED_LIMIT, SPEED_LIMIT + 5), # 轻度超速:20-25km/h + "medium": (SPEED_LIMIT + 5, SPEED_LIMIT + 10), # 中度超速:25-30km/h + "high": (SPEED_LIMIT + 10, float('inf')) # 重度超速:>30km/h +} +# 串口配置(对接车载显示屏/报警器,真实场景启用) +SERIAL_PORT = "COM3" # Windows: COM3 / Linux: /dev/ttyUSB0 +BAUD_RATE = 9600 +ser = None + + +# ------------------- 初始化函数 ------------------- +def init_serial(): + """初始化串口(用于向硬件发送预警指令)""" + global ser + try: + ser = serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=1) + print(f"Serial port {SERIAL_PORT} initialized successfully") + except Exception as e: + print(f"Serial port init failed: {e}") + ser = None + + +# ------------------- 速度读取函数 ------------------- +def read_vehicle_speed(): + """ + 读取车辆速度(真实场景替换为传感器/总线数据) + 返回:当前速度(km/h) + """ + # 仿真:随机生成10-35km/h的速度(真实场景删除此段) + simulated_speed = random.uniform(10, 35) + return round(simulated_speed, 1) + + # 真实场景示例:从串口读取速度传感器数据 + # if ser and ser.in_waiting > 0: + # speed_data = ser.readline().decode('utf-8').strip() + # return float(speed_data) if speed_data else 0.0 + # return 0.0 + + +# ------------------- 超速预警核心函数 ------------------- +def speed_warning(current_speed): + """ + 超速预警判断与输出 + :param current_speed: 当前速度(km/h) + :return: 预警等级(None/low/medium/high)、预警信息(英文) + """ + if current_speed < SPEED_LIMIT: + return None, f"Current speed: {current_speed} km/h - Normal" + + # 判断预警等级 + warning_level = None + warning_msg = "" + if WARNING_LEVELS["low"][0] <= current_speed < WARNING_LEVELS["low"][1]: + warning_level = "low" + warning_msg = f"Speed Warning: {current_speed} km/h (Over limit by {current_speed - SPEED_LIMIT} km/h) - Slow down!" + elif WARNING_LEVELS["medium"][0] <= current_speed < WARNING_LEVELS["medium"][1]: + warning_level = "medium" + warning_msg = f"Over Speed Warning: {current_speed} km/h - Reduce speed immediately!" + elif current_speed >= WARNING_LEVELS["high"][0]: + warning_level = "high" + warning_msg = f"CRITICAL Over Speed Warning: {current_speed} km/h - STOP VEHICLE!" + + # 输出预警(控制台 + 串口/硬件) + print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] {warning_msg}") + if ser: + ser.write(f"{warning_level}:{warning_msg}\n".encode('utf-8')) + + return warning_level, warning_msg + + +# ------------------- 主循环 ------------------- +def main(): + # 初始化串口(可选) + # init_serial() + + print(f"Autonomous Vehicle Speed Warning System Started | Speed Limit: {SPEED_LIMIT} km/h") + try: + while True: + # 读取当前速度 + current_speed = read_vehicle_speed() + # 触发超速预警 + speed_warning(current_speed) + # 1秒刷新一次(可根据传感器频率调整) + time.sleep(1) + except KeyboardInterrupt: + print("\nSystem stopped by user") + finally: + if ser: + ser.close() + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/src/Smart_car/autonomous_vehicle_battery_level_display.py b/src/Smart_car/autonomous_vehicle_battery_level_display.py new file mode 100644 index 0000000000..40b6b6f8f5 --- /dev/null +++ b/src/Smart_car/autonomous_vehicle_battery_level_display.py @@ -0,0 +1,95 @@ +import time +import random # 仅用于模拟硬件数据,实际场景删除 + + +class UnmannedVehicleBattery: + """无人车电池电量管理类""" + + def __init__(self): + # 电池参数配置(根据实际电池规格调整) + self.max_voltage = 12.6 # 满电电压(12V锂电池为例) + self.min_voltage = 10.0 # 欠压保护电压 + self.current_voltage = 0.0 # 当前电压 + self.battery_percent = 0.0 # 剩余电量百分比 + + def read_battery_voltage(self): + """ + 读取电池电压(模拟硬件采集) + 实际场景:替换为ADC读取/串口接收BMS数据/I2C通信等 + """ + # 模拟电压波动(范围:10.0~12.6V) + self.current_voltage = round(random.uniform(10.0, 12.6), 2) + # 实际硬件示例(以树莓派ADC为例): + # import adafruit_ads1x15.ads1115 as ADS + # from adafruit_ads1x15.analog_in import AnalogIn + # i2c = board.I2C() + # ads = ADS.ADS1115(i2c) + # chan = AnalogIn(ads, ADS.P0) + # self.current_voltage = chan.voltage * voltage_divider_ratio # 电压分压比 + + def calculate_battery_percent(self): + """计算剩余电量百分比""" + if self.current_voltage >= self.max_voltage: + self.battery_percent = 100.0 + elif self.current_voltage <= self.min_voltage: + self.battery_percent = 0.0 + else: + # 线性计算(实际可根据电池放电曲线优化) + self.battery_percent = round( + (self.current_voltage - self.min_voltage) / + (self.max_voltage - self.min_voltage) * 100, + 1 + ) + + def get_battery_status(self): + """判断电量状态""" + if self.battery_percent >= 95: + return "满电", "🟢" + elif 20 <= self.battery_percent < 95: + return "正常", "🟢" + elif 5 <= self.battery_percent < 20: + return "低电量", "🟡" + else: + return "紧急(请充电)", "🔴" + + def display_battery_info(self): + """可视化显示电量信息""" + # 清空控制台(可选) + # os.system('cls' if os.name == 'nt' else 'clear') + + # 电量条可视化 + bar_length = 20 + filled_length = int(bar_length * self.battery_percent // 100) + battery_bar = "█" * filled_length + "-" * (bar_length - filled_length) + + # 获取状态 + status, color = self.get_battery_status() + + # 打印信息 + print(f"\n=== 无人车电池状态 ===") + print(f"当前电压: {self.current_voltage}V") + print(f"剩余电量: |{battery_bar}| {self.battery_percent}%") + print(f"状态: {color} {status}") + + # 低电量告警 + if self.battery_percent < 5: + print("⚠️ 电量过低,立即停止作业并充电") + + +def main(): + """主循环""" + battery = UnmannedVehicleBattery() + print("无人车电量监控系统启动...") + + try: + while True: + battery.read_battery_voltage() # 读取电压 + battery.calculate_battery_percent() # 计算电量 + battery.display_battery_info() # 显示信息 + time.sleep(1) # 1秒刷新一次 + except KeyboardInterrupt: + print("\n监控系统已退出") + + +if __name__ == "__main__": + main() \ No newline at end of file