本指南将帮助您在 WSL2-Ubuntu 22.04 环境中搭建完整的无人机自主定位和避障仿真系统,包括:
bash展开代码# 更新系统
sudo apt update && sudo apt upgrade -y
# 安装基础工具
sudo apt install -y curl wget git vim build-essential
sudo apt install -y lsb-release software-properties-common
sudo apt install -y python3-pip python3-rosdep python3-rosinstall python3-rosinstall-generator python3-wstool
bash展开代码# 设置 DISPLAY 环境变量(MobaXterm 自带 X11 服务器)
echo 'export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '\''{print $2}'\''):0.0' >> ~/.bashrc
source ~/.bashrc
# 验证
echo $DISPLAY
# 应该显示类似:172.x.x.x:0.0
bash展开代码# 添加 ROS 软件源
sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
# 添加密钥(如果失败,尝试备用密钥服务器)
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654 || \
sudo apt-key adv --keyserver 'hkp://pgp.mit.edu:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
# 更新软件包列表
sudo apt update
bash展开代码# 尝试直接安装(Ubuntu 22.04 可能部分支持)
sudo apt install -y ros-noetic-desktop-full || {
echo "尝试安装基础版本..."
sudo apt install -y ros-noetic-desktop || {
echo "apt 安装失败,需要使用源码编译方式(见下方)"
}
}
# 如果 apt 安装成功,继续以下步骤
# 初始化 rosdep
sudo rosdep init
rosdep update
# 配置环境变量
echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc
source ~/.bashrc
bash展开代码# 创建工作目录
mkdir -p ~/ros_noetic_ws/src
cd ~/ros_noetic_ws/src
# 下载 ROS Noetic 源码
rosinstall_generator desktop_full --rosdistro noetic --deps --tar > noetic-desktop-full.rosinstall
wstool init -j8 . noetic-desktop-full.rosinstall
# 安装依赖
rosdep install --from-paths . --ignore-src -r -y
# 编译(需要1-2小时)
cd ~/ros_noetic_ws
./src/catkin/bin/catkin_make_isolated --install -DCMAKE_BUILD_TYPE=Release
# 配置环境变量
echo "source ~/ros_noetic_ws/install_isolated/setup.bash" >> ~/.bashrc
source ~/.bashrc
bash展开代码# 测试 ROS 环境
roscore
# 如果看到 ROS master 启动信息,说明安装成功
# 按 Ctrl+C 退出
bash展开代码# 安装 Gazebo Classic(Gazebo 11)
sudo apt install -y gazebo11 libgazebo11-dev
# 安装 Gazebo ROS 接口
sudo apt install -y ros-noetic-gazebo-ros-pkgs ros-noetic-gazebo-ros-control
# 验证安装
gazebo --version
bash展开代码# 创建模型目录
mkdir -p ~/gazebo_models
cd ~/gazebo_models
# 下载模型(使用 git clone,更稳定)
git clone https://github.com/osrf/gazebo_models.git
mv gazebo_models/* .
rmdir gazebo_models
# 配置环境变量
echo 'export GAZEBO_MODEL_PATH=$HOME/gazebo_models:${GAZEBO_MODEL_PATH}' >> ~/.bashrc
source ~/.bashrc
bash展开代码# Rviz 通常随 ROS desktop-full 一起安装
# 如果没有,手动安装
sudo apt install -y ros-noetic-rviz
# 测试
rviz
# 如果能看到界面,说明安装成功
bash展开代码# 安装 Eigen3(线性代数库)
sudo apt install -y libeigen3-dev
# 安装 Ceres Solver(优化库)
sudo apt install -y libceres-dev
# 安装 OpenCV(计算机视觉库)
sudo apt install -y libopencv-dev python3-opencv
# 检查 OpenCV 版本
pkg-config --modversion opencv4
# 安装其他依赖
sudo apt install -y libgoogle-glog-dev libgflags-dev libatlas-base-dev libsuitesparse-dev
# 安装 ROS 相关依赖
sudo apt install -y ros-noetic-cv-bridge ros-noetic-image-transport ros-noetic-tf ros-noetic-tf2
bash展开代码# 创建 catkin 工作空间
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
# 克隆 VINS-Fusion
git clone https://github.com/HKUST-Aerial-Robotics/VINS-Fusion.git
# 检查代码
cd VINS-Fusion
ls -la
bash展开代码cd ~/catkin_ws/src/VINS-Fusion
# 创建修复脚本
cat > fix_opencv4.sh << 'EOF'
#!/bin/bash
echo "正在修复 OpenCV 4.x 兼容性问题..."
# 修复 CV_LOAD_IMAGE_GRAYSCALE
find . -name "*.cpp" -o -name "*.cc" | xargs sed -i.bak 's/CV_LOAD_IMAGE_GRAYSCALE/cv::IMREAD_GRAYSCALE/g'
# 修复其他常见的 OpenCV 常量
find . -name "*.cpp" -o -name "*.cc" | xargs sed -i.bak \
-e 's/CV_GRAY2RGB/cv::COLOR_GRAY2RGB/g' \
-e 's/CV_GRAY2BGR/cv::COLOR_GRAY2BGR/g' \
-e 's/CV_BGR2GRAY/cv::COLOR_BGR2GRAY/g' \
-e 's/CV_RGB2GRAY/cv::COLOR_RGB2GRAY/g'
echo "修复完成!"
EOF
chmod +x fix_opencv4.sh
./fix_opencv4.sh
bash展开代码# 返回工作空间根目录
cd ~/catkin_ws
# 安装依赖
rosdep install --from-paths src --ignore-src -r -y
# 编译
catkin_make
# 如果编译成功,更新环境
source ~/catkin_ws/devel/setup.bash
bash展开代码# 安装依赖库
sudo apt install -y libpcl-dev ros-noetic-pcl-ros ros-noetic-pcl-conversions
sudo apt install -y ros-noetic-octomap ros-noetic-octomap-ros ros-noetic-octomap-msgs
sudo apt install -y ros-noetic-planning-msgs ros-noetic-trajectory-msgs
sudo apt install -y ros-noetic-geometry-msgs ros-noetic-nav-msgs
bash展开代码cd ~/catkin_ws/src
# 克隆 EGO-Planner(主仓库)
git clone https://github.com/ZJU-FAST-Lab/ego-planner.git
# 克隆依赖包
git clone https://github.com/ZJU-FAST-Lab/ego-planner-swarm.git
git clone https://github.com/ZJU-FAST-Lab/bspline_opt.git
git clone https://github.com/ZJU-FAST-Lab/plan_manage.git
# 检查文件
ls -la
bash展开代码# 返回工作空间根目录
cd ~/catkin_ws
# 安装依赖
rosdep install --from-paths src --ignore-src -r -y
# 编译
catkin_make
# 如果编译成功,更新环境
source ~/catkin_ws/devel/setup.bash
bash展开代码# 安装依赖库
sudo apt install -y ros-noetic-mavros ros-noetic-mavros-extras
sudo apt install -y ros-noetic-geographic-msgs ros-noetic-geodesy
sudo apt install -y ros-noetic-control-msgs ros-noetic-control-toolbox
bash展开代码cd ~/catkin_ws/src
# 克隆 PX4Ctrl(注意:可能需要根据实际仓库地址调整)
git clone https://github.com/PX4/PX4-Autopilot.git --recursive
# 或者使用 PX4Ctrl 的 ROS 包装器(如果存在)
# git clone https://github.com/your-repo/PX4Ctrl.git
# 如果上述仓库不存在,可以尝试:
git clone https://github.com/amov-lab/Prometheus.git
# Prometheus 项目包含了 PX4Ctrl 相关的控制模块
bash展开代码# 返回工作空间根目录
cd ~/catkin_ws
# 安装依赖
rosdep install --from-paths src --ignore-src -r -y
# 编译
catkin_make
# 如果编译成功,更新环境
source ~/catkin_ws/devel/setup.bash
注意:PX4Ctrl 的具体实现可能因项目而异。如果找不到官方 PX4Ctrl ROS 包,您可能需要:
bash展开代码# 创建工作目录
mkdir -p ~/catkin_ws/src/my_simulation/worlds
mkdir -p ~/catkin_ws/src/my_simulation/launch
mkdir -p ~/catkin_ws/src/my_simulation/config
# 创建仿真世界文件(包含障碍物的室内环境)
cat > ~/catkin_ws/src/my_simulation/worlds/drone_obstacle_world.world << 'EOF'
<?xml version="1.0" ?>
<sdf version="1.6">
<world name="default">
<!-- 地面 -->
<include>
<uri>model://ground_plane</uri>
</include>
<!-- 光照 -->
<include>
<uri>model://sun</uri>
</include>
<!-- 障碍物(箱子) -->
<model name="box1">
<pose>2 2 0.5 0 0 0</pose>
<static>true</static>
<link name="link">
<collision name="collision">
<geometry>
<box>
<size>1 1 1</size>
</box>
</geometry>
</collision>
<visual name="visual">
<geometry>
<box>
<size>1 1 1</size>
</box>
</geometry>
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Wood</name>
</script>
</material>
</visual>
</link>
</model>
<model name="box2">
<pose>-2 2 0.5 0 0 0</pose>
<static>true</static>
<link name="link">
<collision name="collision">
<geometry>
<box>
<size>1 1 1</size>
</box>
</geometry>
</collision>
<visual name="visual">
<geometry>
<box>
<size>1 1 1</size>
</box>
</geometry>
</visual>
</link>
</model>
<!-- 四旋翼无人机 -->
<include>
<uri>model://quadrotor</uri>
<name>quadrotor</name>
<pose>0 0 1 0 0 0</pose>
</include>
</world>
</sdf>
EOF
bash展开代码# 创建主启动文件(Gazebo + 无人机)
cat > ~/catkin_ws/src/my_simulation/launch/drone_sim.launch << 'EOF'
<launch>
<!-- 启动 Gazebo -->
<include file="$(find gazebo_ros)/launch/empty_world.launch">
<arg name="world_name" value="$(find my_simulation)/worlds/drone_obstacle_world.world"/>
<arg name="paused" value="false"/>
<arg name="use_sim_time" value="true"/>
<arg name="gui" value="true"/>
<arg name="headless" value="false"/>
<arg name="debug" value="false"/>
</include>
<!-- 启动无人机模型 -->
<include file="$(find hector_quadrotor_gazebo)/launch/spawn_quadrotor.launch">
<arg name="name" value="quadrotor"/>
<arg name="model" value="$(find hector_quadrotor_description)/urdf/quadrotor_hokuyo_utm30lx.gazebo.xacro"/>
</include>
<!-- 启动 Rviz -->
<node name="rviz" pkg="rviz" type="rviz" output="screen"/>
</launch>
EOF
# 创建 VINS-Fusion 启动文件
cat > ~/catkin_ws/src/VINS-Fusion/launch/gazebo_vins.launch << 'EOF'
<launch>
<!-- VINS-Fusion参数 -->
<arg name="config_path" default="$(find vins_estimator)/../config/euroc/euroc_config.yaml"/>
<!-- 启动VINS-Fusion -->
<node name="vins_estimator" pkg="vins_estimator" type="vins_estimator" output="screen">
<param name="config_file" type="string" value="$(arg config_path)"/>
<remap from="/camera/image_raw" to="/quadrotor/camera/image_raw"/>
<remap from="/imu0" to="/quadrotor/imu"/>
</node>
<!-- 启动Rviz -->
<node name="rviz" pkg="rviz" type="rviz" output="screen"
args="-d $(find vins_estimator)/../config/vins_rviz_config.rviz"/>
</launch>
EOF
# 创建 EGO-Planner 启动文件
cat > ~/catkin_ws/src/ego-planner/launch/gazebo_ego.launch << 'EOF'
<launch>
<!-- EGO-Planner参数 -->
<arg name="map_size_x" default="10.0"/>
<arg name="map_size_y" default="10.0"/>
<arg name="map_size_z" default="3.0"/>
<!-- 启动EGO-Planner -->
<node name="ego_planner_node" pkg="ego_planner" type="ego_planner_node" output="screen">
<param name="map/x_size" value="$(arg map_size_x)"/>
<param name="map/y_size" value="$(arg map_size_y)"/>
<param name="map/z_size" value="$(arg map_size_z)"/>
<remap from="/odom_world" to="/vins_estimator/odometry"/>
</node>
</launch>
EOF
bash展开代码cd ~/catkin_ws/src
# 克隆 hector_quadrotor 相关包
git clone https://github.com/tu-darmstadt-ros-pkg/hector_quadrotor.git
git clone https://github.com/tu-darmstadt-ros-pkg/hector_gazebo_plugins.git
git clone https://github.com/tu-darmstadt-ros-pkg/hector_models.git
# 编译
cd ~/catkin_ws
catkin_make
source ~/catkin_ws/devel/setup.bash
bash展开代码# 终端1:启动 Gazebo 和无人机
source ~/catkin_ws/devel/setup.bash
roslaunch my_simulation drone_sim.launch
bash展开代码# 终端2:检查话题
source ~/catkin_ws/devel/setup.bash
rostopic list
# 应该能看到:
# /quadrotor/camera/image_raw
# /quadrotor/imu
# /quadrotor/odometry
# 等话题
# 检查图像话题
rostopic hz /quadrotor/camera/image_raw
# 检查 IMU 话题
rostopic hz /quadrotor/imu
bash展开代码# 终端3:启动 VINS-Fusion
source ~/catkin_ws/devel/setup.bash
roslaunch vins_estimator gazebo_vins.launch
# 在 Rviz 中应该能看到:
# - 点云地图
# - 估计的轨迹
# - 相机图像和特征点
bash展开代码# 终端4:启动 EGO-Planner
source ~/catkin_ws/devel/setup.bash
roslaunch ego_planner gazebo_ego.launch
# EGO-Planner 会:
# - 订阅 VINS-Fusion 的定位信息
# - 根据障碍物信息规划路径
# - 发布规划好的轨迹
bash展开代码# 终端5:启动 PX4Ctrl(或相应的控制节点)
source ~/catkin_ws/devel/setup.bash
# 根据实际安装的包,启动控制节点
# 例如:
roslaunch px4ctrl px4ctrl.launch
# 或者使用 mavros:
roslaunch mavros px4.launch
bash展开代码# 终端6:发送目标点给 EGO-Planner
source ~/catkin_ws/devel/setup.bash
# 使用 rostopic 发布目标点
rostopic pub /waypoint_generator/waypoint geometry_msgs/PoseStamped \
"header:
seq: 0
stamp:
secs: 0
nsecs: 0
frame_id: 'world'
pose:
position:
x: 5.0
y: 5.0
z: 2.0
orientation:
x: 0.0
y: 0.0
z: 0.0
w: 1.0" -1
bash展开代码# 查看所有运行的节点
rosnode list
# 查看节点图
rqt_graph
# 查看话题关系
rostopic list
rostopic echo /vins_estimator/odometry
rostopic echo /ego_planner/trajectory
bash展开代码# 检查 DISPLAY 环境变量
echo $DISPLAY
# 如果为空,重新设置
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0.0
bash展开代码# 检查话题名称是否匹配
rostopic list | grep image
rostopic list | grep camera
# 修改 launch 文件中的 remap 参数
bash展开代码# 检查是否接收到定位信息
rostopic echo /vins_estimator/odometry
# 检查地图参数设置
rosparam list | grep ego
bash展开代码# 清理后重新编译
cd ~/catkin_ws
catkin_make clean
catkin_make
# 检查依赖
rosdep install --from-paths src --ignore-src -r -y
bash展开代码# 启动 ROS 核心
roscore
# 启动 Gazebo
gazebo
# 查看所有话题
rostopic list
# 查看节点
rosnode list
# 编译工作空间
cd ~/catkin_ws && catkin_make
# 更新环境
source ~/catkin_ws/devel/setup.bash
# 查看节点图
rqt_graph
# 查看参数
rosparam list
完成安装后,确认以下命令都能正常执行:
roscore - ROS 核心运行gazebo - Gazebo 界面显示rviz - Rviz 界面显示rostopic list - 话题列表正常roslaunch vins_estimator gazebo_vins.launch - VINS-Fusion 启动roslaunch ego_planner gazebo_ego.launch - EGO-Planner 启动系统架构流程:
/quadrotor/camera/image_raw 和 /quadrotor/imu/vins_estimator/odometry/ego_planner/trajectory预计安装时间:
注意事项:
祝您实验顺利!🎉
本文作者:cc
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!