#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 机器人日志示例 - 完整模拟机器人运行流程的日志输出 """ import time import random import math import json from utils.log_helper import ( LogHelper, section, info, debug, warning, error, success, timing, table, progress, syntax, json_log, panel ) # 创建机器人控制器特定的日志器 robot_logger = LogHelper("机器人控制器") def simulate_robot_startup(): """模拟机器人启动过程""" section("系统启动", "启动") # 显示启动面板 panel( "MI机器人控制系统 v1.0\n" "开发者: MI研发团队\n" "初始化中...", "系统信息", "bold blue" ) # 模拟初始化各个子系统 components = [ "视觉系统", "导航模块", "电机控制器", "传感器阵列", "通信模块", "决策系统", "电源管理", "安全模块" ] start_time = time.time() for component in progress(components, "初始化组件", unit="组件"): delay = random.uniform(0.1, 0.3) time.sleep(delay) if component == "传感器阵列" and random.random() < 0.3: warning(f"{component}初始化出现延迟", "警告") else: success(f"{component}初始化完成", "成功") # 显示初始化耗时 elapsed = time.time() - start_time timing("系统初始化", elapsed) def check_robot_status(): """检查机器人状态""" section("系统状态检查", "检测") # 模拟传感器读数 battery = random.uniform(80, 100) temperature = random.uniform(25, 35) cpu_load = random.uniform(10, 30) memory_usage = random.uniform(20, 50) # 显示状态表格 columns = ["参数", "数值", "状态", "备注"] rows = [ ["电池电量", f"{battery:.1f}%", "正常" if battery > 20 else "警告", ""], ["系统温度", f"{temperature:.1f}°C", "正常" if temperature < 40 else "警告", ""], ["CPU负载", f"{cpu_load:.1f}%", "正常" if cpu_load < 80 else "警告", ""], ["内存使用", f"{memory_usage:.1f}%", "正常" if memory_usage < 80 else "警告", ""], ] table("系统状态", columns, rows, "当前机器人状态指标") # 模拟系统检查结果 if battery < 90: warning(f"电池电量低于90%: {battery:.1f}%", "警告") if temperature > 30: warning(f"系统温度偏高: {temperature:.1f}°C", "警告") else: info(f"系统温度正常: {temperature:.1f}°C", "信息") def simulate_movement(): """模拟机器人移动""" section("开始移动", "移动") # 模拟路径规划 info("开始规划路径", "计算") time.sleep(0.5) waypoints = [ {"x": 0.0, "y": 0.0, "heading": 0.0}, {"x": 1.0, "y": 0.0, "heading": 0.0}, {"x": 1.0, "y": 1.0, "heading": 90.0}, {"x": 0.0, "y": 1.0, "heading": 180.0}, {"x": 0.0, "y": 0.0, "heading": 270.0} ] # 显示路径JSON json_log(waypoints, "规划路径") # 模拟运动控制代码 code = """ def move_to_waypoint(robot, waypoint): \"\"\"移动到指定航点\"\"\" current_pos = robot.get_position() dx = waypoint['x'] - current_pos['x'] dy = waypoint['y'] - current_pos['y'] # 计算距离和角度 distance = math.sqrt(dx*dx + dy*dy) target_angle = math.degrees(math.atan2(dy, dx)) # 先旋转到目标方向 robot.rotate_to(target_angle) # 然后移动指定距离 robot.move_forward(distance) # 最后调整到目标朝向 robot.rotate_to(waypoint['heading']) return True """ syntax(code, "python", "航点移动函数") # 模拟执行移动 start_time = time.time() for i, waypoint in enumerate(progress(waypoints[1:], "执行移动", unit="航点")): info(f"移动到航点 {i+1}: ({waypoint['x']}, {waypoint['y']})", "移动") # 模拟一些执行延迟 time.sleep(random.uniform(0.3, 0.7)) # 偶尔模拟一些障碍物检测 if random.random() < 0.3: warning(f"检测到障碍物,调整路径", "检测") time.sleep(0.2) success(f"到达航点 {i+1}", "位置") elapsed = time.time() - start_time timing("路径执行", elapsed) def simulate_error_handling(): """模拟错误处理""" section("错误处理测试", "调试") try: info("执行复杂计算", "计算") time.sleep(0.5) # 模拟一个错误 if random.random() < 0.8: # 80%概率出错 raise ValueError("传感器数据异常") success("计算完成", "完成") except Exception as e: error(f"计算过程中出现错误: {str(e)}", "错误") # 显示错误处理代码 error_code = """ try: sensor_data = robot.get_sensor_data() if not validate_sensor_data(sensor_data): raise ValueError("传感器数据异常") # 处理传感器数据 process_sensor_data(sensor_data) except ValueError as e: # 记录错误 log_error(f"传感器错误: {str(e)}") # 尝试重新校准传感器 if robot.recalibrate_sensors(): log_info("传感器重新校准成功") else: log_error("传感器重新校准失败,切换到备用传感器") robot.switch_to_backup_sensors() except Exception as e: log_critical(f"未处理的错误: {str(e)}") robot.emergency_stop() """ syntax(error_code, "python", "错误处理代码") # 模拟错误恢复 info("尝试恢复操作", "旋转") time.sleep(0.7) success("系统已恢复", "成功") def main(): """主函数""" panel( "机器人日志演示程序\n" "此程序展示各种日志功能在机器人控制中的应用", "演示信息", "bold green" ) simulate_robot_startup() check_robot_status() simulate_movement() simulate_error_handling() section("演示完成", "完成") success("机器人日志系统演示完成", "完成") if __name__ == "__main__": main()