#!/bin/bash
# P600 无人机控制系统 - 启动脚本
# 用法: bash start_all.sh

echo "========================================"
echo "  P600 无人机控制系统 - 启动脚本"
echo "========================================"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo ""
echo -e "${YELLOW}========== 第一步：编译闪光检测节点 ==========${NC}"
echo ""
echo "在编译前，请确保已安装 ROS 环境 (如 noetic)"
echo ""
echo -e "${GREEN}# 进入工作空间目录${NC}"
echo "cd ~/ZhiTu/flash_detector_ws"
echo ""
echo -e "${GREEN}# 编译闪光检测节点${NC}"
echo "source /opt/ros/noetic/setup.bash"
echo "catkin_make"
echo ""
echo -e "${GREEN}# 如果使用 catkin_tools，则使用:${NC}"
echo "catkin build"
echo ""
echo -e "${BLUE}编译完成后，devel/ 目录会自动生成${NC}"
echo ""

echo "========================================"
echo -e "${YELLOW}========== 第二步：启动各组件 ==========${NC}"
echo "========================================"
echo ""
echo "请在 4 个终端中分别执行以下命令:"
echo ""

echo -e "${GREEN}# 终端 1 - ROS Master (仿真模式)${NC}"
echo "roslaunch prometheus_gazebo sitl.launch"
echo ""
echo -e "${BLUE}# 或者实机模式，先启动 roscore，再启动 MAVROS:${NC}"
echo "roscore"
echo "# 另一个终端:"
echo "roslaunch mavros_node.launch"
echo ""

echo -e "${GREEN}# 终端 2 - rosbridge_server${NC}"
echo "roslaunch rosbridge_server rosbridge_websocket.launch"
echo ""

echo -e "${GREEN}# 终端 3 - 闪光检测节点 (C++)${NC}"
echo "cd ~/ZhiTu/flash_detector_ws"
echo "source /opt/ros/noetic/setup.bash"
echo "source devel/setup.bash"
echo "roslaunch flash_detector flash_detector.launch camera_topic:=/uav1/camera/color/image_raw"
echo ""
echo -e "${BLUE}# 常用参数说明:${NC}"
echo "#   camera_topic  - 相机图像话题 (根据实际情况修改)"
echo "#   debug_view    - 是否显示调试窗口 (true/false)"
echo ""

echo -e "${GREEN}# 终端 4 - 前端服务 (软件电脑端)${NC}"
echo "cd ~/ZhiTu/软件电脑端/server"
echo "python3 app.py"
echo ""

echo "========================================"
echo -e "${YELLOW}浏览器访问: http://localhost:5000${NC}"
echo "========================================"
echo ""

echo -e "${YELLOW}========== 常见问题排查 ==========${NC}"
echo ""
echo -e "${RED}问题1: 编译失败 - 找不到 OpenCV${NC}"
echo "解决: sudo apt install libopencv-dev python3-opencv"
echo ""
echo -e "${RED}问题2: 话题无数据${NC}"
echo "解决: rostopic list 检查话题是否存在"
echo "      rostopic echo /uav1/camera/color/image_raw 检查图像数据"
echo ""
echo -e "${RED}问题3: 前端无法连接 rosbridge${NC}"
echo "解决: 检查 rosbridge 是否启动，端口是否为 9090"
echo "      浏览器控制台查看 WebSocket 连接状态"
echo ""

echo "========================================"
echo -e "${BLUE}目录结构说明:${NC}"
echo "========================================"
echo ""
echo "闪光检测/"
echo "├── flash_detector_ws/        # ROS 工作空间"
echo "│   ├── src/                  # 源代码 (保留)"
echo "│   │   └── flash_detector/   # 闪光检测节点源码"
echo "│   ├── build/                # 编译缓存 (可删除，重新编译生成)"
echo "│   └── devel/                # 编译输出 (可删除，重新编译生成)"
echo "└── start_all.sh              # 本启动脚本"
echo ""
echo "软件电脑端/                    # 主前端 (已集成闪光检测功能)"
echo "├── index.html                # 主页面"
echo "├── js/"
echo "│   ├── ui.js                 # UI 模块 (含闪光检测)"
echo "│   ├── flash_detector.js     # 闪光检测前端模块"
echo "│   └── flash_avoidance.js    # 避闪光模块"
echo "├── css/"
echo "└── server/"
echo "    └── app.py                # Flask 后端服务"
echo ""

echo "========================================"
echo -e "${GREEN}启动完成!${NC}"
echo "========================================"
