学习记录,前置环境(docker和gpu支持)搭建详见笔者的另一篇文章:Mobile Aloha 基于ros-Docker-Ubuntu23.10环境搭建
本文主要介绍XTDrone仿真平台在docker下的环境搭建。
1 ros-noetic安装
由于XTDrone仿真平台依赖ros-melodic开发环境,但我们使用版本更新的ros-noetic进行安装,先安装它(此处引用的是fishros大佬打包好的docker镜像,亲测好用)
sudo docker run -dit --gpus all --name=ros-noetic -v /tmp/.X11-unix:/tmp/.X11-unix --device=/dev/dri/renderD128 -v /dev/dri:/dev/dri --device=/dev/snd -e DISPLAY=unix$DISPLAY -e NVIDIA_DRIVER_CAPABILITIES=compute,utility -e NVIDIA_VISIBLE_DEVICES=all -w /root fishros2/ros:noetic-desktop-full
进入容器查看安装情况
sudo docker exec -it ros-noetic /bin/bash
roscore
屏幕打印以下信息说明安装成功
2 安装Anaconda开发环境
下载Anaconda3-2022.05版本
wget https://repo.anaconda.com/archive/Anaconda3-2022.05-Linux-x86_64.sh
安装Anaconda
chmod +x Anaconda3-2022.05-Linux-x86_64.sh
bash Anaconda3-2022.05-Linux-x86_64.sh
上述步骤中如碰到需要选择的部分全部选yes即可
3 创建catkin工作空间
安装依赖
apt install aptitude
apt install ninja-build exiftool protobuf-compiler libeigen3-dev genromfs xmlstarlet libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev python3-pip gawk python3-catkin-tools vim
修改pip源
mkdir ~/.pip
cd ~/.pip
touch pip.conf
vim ~/.pip/pip.conf
# 在pip.conf中输入以下内容
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
安装python相关依赖
pip3 install --upgrade setuptools
python3 -m pip install --upgrade pip
pip3 install numpy pandas jinja2 pyserial cerberus pyulog==0.7.0 toml pyquaternion empy pyyaml
pip3 install packaging pyyaml pyargparse kconfiglib jsonschema future empy catkin_pkg rospkg
创建workspace
cd ~
mkdir -p catkin_ws/src
mkdir -p catkin_ws/scripts
cd ~/catkin_ws/src
catkin_init_workspace
cd ~/catkin_ws/
catkin init
catkin build
4 安装gazebo
ros-noetic镜像安装的是gazebo11,可以直接使用,不必改成官方教程的gazebo9
安装gazebo相关依赖
sudo apt-get install ros-noetic-moveit-msgs ros-noetic-object-recognition-msgs ros-noetic-octomap-msgs ros-noetic-camera-info-manager ros-noetic-control-toolbox ros-noetic-polled-camera ros-noetic-controller-manager ros-noetic-transmission-interface ros-noetic-joint-limits-interface git
下载gazebo对XTDrone的支持代码并编译
cd ~/catkin_ws/src
git clone -b noetic-devel https://github.com/ros-simulation/gazebo_ros_pkgs.git
cd ~/catkin_ws
catkin build
5 安装mavros
输入以下指令进行安装
apt install ros-noetic-mavros ros-noetic-mavros-extras
wget https://gitee.com/robin_shaun/XTDrone/raw/master/sitl_config/mavros/install_geographiclib_datasets.sh
chmod a+x ./install_geographiclib_datasets.sh
./install_geographiclib_datasets.sh
6 安装XTDrone
配置PX4_Firmware
cd ~
git clone https://github.com/PX4/PX4-Autopilot.git
mv PX4-Autopilot PX4_Firmware
cd PX4_Firmware
git checkout -b xtdrone/dev v1.13.2
git submodule update --init --recursive
bash Tools/setup/ubuntu.sh --no-sim-tools
make px4_sitl_default gazebo
vim ~/.bashrc
# 在文件末尾添加以下几行
source ~/catkin_ws/devel/setup.bash
source ~/PX4_Firmware/Tools/setup_gazebo.bash ~/PX4_Firmware/ ~/PX4_Firmware/build/px4_sitl_default
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/PX4_Firmware
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/PX4_Firmware/Tools/sitl_gazebo
使配置生效
source ~/.bashrc
运行如下指令检查安装是否成功
cd ~/PX4_Firmware
roslaunch px4 mavros_posix_sitl.launch
若gazebo成功运行,则说明安装成功
可能遇到如下问题
Authorization required, but no authorization protocol specified
qt.qpa.xcb: could not connect to display unix:1
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, xcb.
Aborted (core dumped)
应在宿主机输入以下指令
sudo su
xhost +
出现提示“access control disabled, clients can connect from any host”则说明指令运行成功,此时可以重新尝试进入gazebo
下载XTDrone源码并编译
cd ~
git clone https://gitee.com/robin_shaun/XTDrone.git
cd XTDrone
git checkout 1_13_2
git submodule update --init --recursive
cp -r sitl_config/gazebo_ros_pkgs ../catkin_ws/src/
cd ~/catkin_ws
catkin build
输入以下指令复制文件
cd ~/XTDrone
cp sitl_config/init.d-posix/* ~/PX4_Firmware/ROMFS/px4fmu_common/init.d-posix/
cp -r sitl_config/launch/* ~/PX4_Firmware/launch/
cp sitl_config/worlds/* ~/PX4_Firmware/Tools/sitl_gazebo/worlds/
cp sitl_config/gazebo_plugin/gimbal_controller/gazebo_gimbal_controller_plugin.cpp ~/PX4_Firmware/Tools/sitl_gazebo/src
cp sitl_config/gazebo_plugin/gimbal_controller/gazebo_gimbal_controller_plugin.hh ~/PX4_Firmware/Tools/sitl_gazebo/include
cp sitl_config/gazebo_plugin/wind_plugin/gazebo_ros_wind_plugin_xtdrone.cpp ~/PX4_Firmware/Tools/sitl_gazebo/src
cp sitl_config/gazebo_plugin/wind_plugin/gazebo_ros_wind_plugin_xtdrone.h ~/PX4_Firmware/Tools/sitl_gazebo/include
cp sitl_config/CMakeLists.txt ~/PX4_Firmware/Tools/sitl_gazebo
cp -r sitl_config/models/* ~/PX4_Firmware/Tools/sitl_gazebo/models/
# 若~/.gazebo/models/不存在则不需要进行下列步骤,若存在则输入以下指令删除重名模型
cd ~/.gazebo/models/
rm -r stereo_camera/ 3d_lidar/ 3d_gpu_lidar/ hokuyo_lidar/
重新编译PX4
cd ~/PX4_Firmware
rm -r build/
make px4_sitl_default gazebo
可能遇到错误如下
FAILED: /root/PX4_Firmware/Tools/sitl_gazebo/models/typhoon_h480/typhoon_h480.sdf
cd /root/PX4_Firmware/build/px4_sitl_default/build_gazebo && /usr/bin/python3 /root/PX4_Firmware/Tools/sitl_gazebo/scripts/jinja_gen.py /root/PX4_Firmware/Tools/sitl_gazebo/models/typhoon_h480/typhoon_h480.sdf.jinja /root/PX4_Firmware/Tools/sitl_gazebo --generate_ros_models true
Traceback (most recent call last):
File "/root/PX4_Firmware/Tools/sitl_gazebo/scripts/jinja_gen.py", line 96, in <module>
raise Exception("ERROR: generation would overwrite changes to `{}`. ".format(filename_out) + \
Exception: ERROR: generation would overwrite changes to `/root/PX4_Firmware/Tools/sitl_gazebo/models/typhoon_h480/typhoon_h480.sdf`. Changes should only be made to the template file `/root/PX4_Firmware/Tools/sitl_gazebo/models/typhoon_h480/typhoon_h480.sdf.jinja`. Remove `typhoon_h480.sdf` (after extracting your changes) to disable this overwrite protection.
解决办法
mv /root/PX4_Firmware/Tools/sitl_gazebo/models/typhoon_h480/typhoon_h480.sdf /root/PX4_Firmware/Tools/sitl_gazebo/models/typhoon_h480/typhoon_h480.sdf.bak
重新编译即可
cd ~/PX4_Firmware
rm -r build/
make px4_sitl_default gazebo
到此,XTDrone仿真平台安装完毕,使用方法见下篇。