Ubuntu18.04下XTDrone无人机仿真平台搭建

一、依赖安装

sudo apt install ninja-build exiftool ninja-build protobuf-compiler libeigen3-dev genromfs xmlstarlet libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev python-pip python3-pip gawk
pip2 install pandas jinja2 pyserial cerberus pyulog==0.7.0 numpy toml pyquaternion empy pyyaml 
pip3 install packaging numpy empy toml pyyaml jinja2 pyargparse

这第二个下载可能比较慢,我这里pip从清华源下载(在代码后面加上 -i https://pypi.tuna.tsinghua.edu.cn/simple),要是失败了多试几次
如果出现如下报错情况,可先更新 setuptools 和 pip

Collecting pandas
  Using cached https://files.pythonhosted.org/packages/64/f1/8fdbd74edfc31625d597717be8c155c6226fc72a7c954c52583ab81a8614/pandas-1.1.2.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-build-qtvsjq8t/pandas/setup.py", line 349
        f"{extension}-source file '{sourcefile}' not found.\n"
                                                             ^
    SyntaxError: invalid syntax
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-qtvsjq8t/pandas/
#若未报错不需要输入这两行命令
pip install --upgrade setuptools
python -m pip install --upgrade pip

二、ROS安装

安装见ROS官方网站:http://wiki.ros.org/Installation/Ubuntu
或者参考教程:https://blog.csdn.net/qq_44830040/article/details/106049992(这个教程是用的国内源安装,虽然可以使用ros,但是可能在和其他的一起使用时出现问题例如darknet_ros,虽然国内源下载会快很多,但还是建议从官网下载)

注意:Ubuntu18.04可以直接全部安装(即指令 sudo apt-get install ros-melodic-desktop 即对于ROS Melodic,选择desktop-full的方式安装,这将同时安装Gazebo9和感知相关库)但是我个人建议18.04的也不进行全部安装,直接安装的gazebo9.0存在缺陷可能在后面出现问题,建议也是选择安装desktop,然后在下面的步骤中安装gazebo9.13或者以上
Ubuntu16.04(ROS Kinetic)选择destop否则安装gazebo7无法进行后续仿真
这里把安装ROS(以melodic为例)也列举出来:
1.配置Ubuntu存储库
在软件与更新中点击 源代码
在这里插入图片描述

2.设置sources.list
以下分别是清华镜像、中科大镜像和官方默认选其中一个即可,但是个人推荐国内镜像,下载会快很多
清华镜像:

sudo sh -c '. /etc/lsb-release && echo "deb http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu/ `lsb_release -cs` main" > /etc/apt/sources.list.d/ros-latest.list'

中科大镜像:

sudo sh -c '. /etc/lsb-release && echo "deb http://mirrors.ustc.edu.cn/ros/ubuntu/ `lsb_release -cs` main" > /etc/apt/sources.list.d/ros-latest.list'

官方默认:

sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'

3.设置key

sudo apt install curl
curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -

要是显示报错的话可以用下面的方法

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F42ED6FBAB17C654

4.安装

sudo apt update
sudo apt install ros-melodic-desktop

正常安装ROS的话可以sudo apt install ros-melodic-desktop-full,我这里是sudo apt install ros-melodic-desktop原因前面已经说了

5.设定环境

echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
source ~/.bashrc

6.构建软件包的依赖关系

sudo apt install python-rosdep python-rosinstall python-rosinstall-generator python-wstool build-essential
sudo apt install python-rosdep
sudo rosdep init

如果出现错误:
ERROR: cannot download default sources list from:
https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/sources.list.d/20-default.list
Website may be down.
执行如下命令:

cd /etc
sudo gedit hosts

末尾添加:151.101.84.133 raw.githubusercontent.com然后保存文件退出即可。

如果出现错误:
ERROR: default sources list file already exists:
/etc/ros/rosdep/sources.list.d/20-default.list
Please delete if you wish to re-initialize

解决办法,执行如下命令:

sudo rm /etc/ros/rosdep/sources.list.d/20-default.list

然后再次执行sudo rosdep init

7.执行rosdep update更新

rosdep update

如果出现如下错误:
ERROR: error loading sources list:
(‘The read operation timed out’,)

大概是网络的问题,反复执行rosdep update命令即可,用手机热点可能会好一点
如果还是不能解决建议参考链接
最近发现这个方法也不是能一定解决,如果可以还是科学上网比较好些
安装以后启动ROS:

roscore

有下列输出说明安装成功:

... logging to /home/robin/.ros/log/a5118af0-5474-11ea-8b86-e454e828c524/r
Checking log directory for disk usage. This may take awhile.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.
started roslaunch server http://robin-G3-3590:34223/
ros_comm version 1.12.14
SUMMARY
========
PARAMETERS
* /rosdistro: kinetic
* /rosversion: 1.12.14
NODES
auto-starting new master
process[master]: started with pid [30244]
ROS_MASTER_URI=http://robin-G3-3590:11311/
setting /run_id to a5118af0-5474-11ea-8b86-e454e828c524
process[rosout-1]: started with pid [30261]
started core service [/rosout]

新建工作空间,推荐使用catkin-tools管理工作空间,之后除去PX4仿真环境启动外,其余ROS相关工程在此工作空间下管理。

mkdir -p ~/catkin_ws/src
mkdir -p ~/catkin_ws/scripts
cd catkin_ws/src 
catkin_init_workspace
cd .. 
catkin_make

三、Gazebo安装

无论是ros Kinetic还是Melodic,自带的Gazebo都存在缺陷,若是安装ros时已经安装了gazebo建议卸载并重新安装。建议gazebo版本在9.13以上
Gazebo安装见官网:
http://gazebosim.org/tutorials?tut=install_ubuntu&cat=install
注意几点:
1.选用Alternative installation: step-by-step的安装方式
2.安装Gazebo9.13或者以上

这里为了方便把gazebo的安装也列出来
0.卸载之前的Gazebo(如果安装的话)
sudo apt-get remove gazebo*
sudo apt-get remove libgazebo*
sudo apt-get remove ros-melodic-gazebo* #kinetic noetic对应修改
1.设置计算机以接受来自package.osrfoundation.org的软件

sudo sh -c 'echo "deb http://packages.osrfoundation.org/gazebo/ubuntu-stable `lsb_release -cs` main" > /etc/apt/sources.list.d/gazebo-stable.list'
cat /etc/apt/sources.list.d/gazebo-stable.list
#如果出现deb http://packages.osrfoundation.org/gazebo/ubuntu-stable xenial main表示没问题

2.设置密钥

wget https://packages.osrfoundation.org/gazebo.key -O - | sudo apt-key add -
sudo apt-get update

3.安装gazebo9.1指令:

sudo apt-get install gazebo9=9.1*

安装之后终端输入gazebo若打开Gazebo说明安装成功

Gazebo

4.Gazebo本身是独立于ROS的,因此还需要安装ROS的Gazebo插件

sudo apt install ros-melodic-gazebo9-*
sudo apt install ros-melodic-gazebo-*

测试

roscore  
rosrun gazebo_ros gazebo 

若能打开Gazebo说明Gazebo和ROS间的插件也安装成功。
输入gazebo --version也可以查看到gazebo的版本

Gazebo有很多开源的模型文件,XTDrone的作者已经上传,大家可以去自己寻找下载
https://www.yuque.com/xtdrone/manual_cn/basic_config
将该附件解压缩后放在~/.gazebo中,此时在 ~/.gazebo/models/路径下可以看到很多模型。如果不做这一步,之后运行Gazebo仿真,可能会缺模型,这时会自动下载,国内的网比较慢,自动下载会下载比较久

四、MAVROS安装

注意,mavros-extras一定别忘记装,否则视觉定位将无法完成(因为ubuntu是18.04 对应的是melodic)

sudo apt install ros-melodic-mavros ros-melodic-mavros-extras 		# for ros-melodic
wget https://gitee.com/robin_shaun/XTDrone/raw/master/sitl_config/mavros/install_geographiclib_datasets.sh
sudo chmod a+x ./install_geographiclib_datasets.sh
sudo ./install_geographiclib_datasets.sh  #这步需要装一段时间,请耐心等待PX4配置

五、PX4配置(1.11版本)

由于国内的github下载速度较慢,XTrone的作者们把固件和submodule同步到了gitee,如果嫌github慢,可以从gitee下载,Xtdrone的官网在这一步也直接提供了编译好的PX4固件,要是实在不行可以直接下载。
github

git clone https://github.com/PX4/PX4-Autopilot.git
mv PX4-Autopilot PX4_Firmware
cd PX4_Firmware
git checkout -b xtdrone/dev v1.11.0-beta1
git submodule update --init --recursive
make px4_sitl_default gazebo

gitee

git clone https://gitee.com/robin_shaun/PX4_Firmware
cd PX4_Firmware
git checkout -b xtdrone/dev v1.11.0-beta1

修改.gitmodules(在PX4_Firmware文件夹中ctrl+h查看隐藏文件可以找到 )
在这里插入图片描述

打开之后修改如下:

[submodule "mavlink/include/mavlink/v2.0"]
	path = mavlink/include/mavlink/v2.0
	url = https://gitee.com/robin_shaun/c_library_v2.git
	branch = master
[submodule "src/drivers/uavcan/libuavcan"]
	path = src/drivers/uavcan/libuavcan
	url = https://gitee.com/robin_shaun/uavcan.git
	branch = px4
[submodule "Tools/jMAVSim"]
	path = Tools/jMAVSim
	url = https://gitee.com/robin_shaun/jMAVSim.git
	branch = master
[submodule "Tools/sitl_gazebo"]
	path = Tools/sitl_gazebo
	url = https://gitee.com/robin_shaun/sitl_gazebo.git
	branch = master
[submodule "src/lib/matrix"]
	path = src/lib/matrix
	url = https://gitee.com/robin_shaun/Matrix.git
	branch = master
[submodule "src/lib/ecl"]
	path = src/lib/ecl
	url = https://gitee.com/robin_shaun/ecl.git
	branch = master
[submodule "boards/atlflight/cmake_hexagon"]
	path = boards/atlflight/cmake_hexagon
	url = https://gitee.com/robin_shaun/cmake_hexagon.git
	branch = px4
[submodule "src/drivers/gps/devices"]
	path = src/drivers/gps/devices
	url = https://gitee.com/robin_shaun/GpsDrivers.git
	branch = master
[submodule "src/modules/micrortps_bridge/micro-CDR"]
	path = src/modules/micrortps_bridge/micro-CDR
	url = https://gitee.com/robin_shaun/micro-CDR.git
	branch = px4
[submodule "platforms/nuttx/NuttX/nuttx"]
	path = platforms/nuttx/NuttX/nuttx
	url = https://gitee.com/robin_shaun/NuttX.git
	branch = px4_firmware_nuttx-9.1.0+
[submodule "platforms/nuttx/NuttX/apps"]
	path = platforms/nuttx/NuttX/apps
	url = https://gitee.com/robin_shaun/NuttX-apps.git
	branch = px4_firmware_nuttx-9.1.0+
[submodule "platforms/qurt/dspal"]
	path = platforms/qurt/dspal
	url = https://gitee.com/robin_shaun/dspal.git
[submodule "Tools/flightgear_bridge"]
	path = Tools/flightgear_bridge
	url = https://gitee.com/robin_shaun/PX4-FlightGear-Bridge.git
	branch = master 
[submodule "Tools/jsbsim_bridge"]
	path = Tools/jsbsim_bridge
	url = https://gitee.com/robin_shaun/px4-jsbsim-bridge.git
[submodule "src/examples/gyro_fft/CMSIS_5"]
	path = src/examples/gyro_fft/CMSIS_5
	url = https://gitee.com/mirrors/CMSIS_5

执行子模块更新指令

git submodule update --init

更改子模块下的submodule:
1、

cd ~/PX4_Firmware/src/drivers/uavcan/libuavcan

修改.gitmodules:(要先到PX4_Firmware/src/drivers/uavcan/libuavcan这个文件夹中找到.gitmodules这个文件,下面也是同样)

[submodule "dsdl"]
	path = dsdl
	url = https://gitee.com/robin_shaun/dsdl
	branch = legacy-v0
[submodule "libuavcan/dsdl_compiler/pyuavcan"]
	path = libuavcan/dsdl_compiler/pyuavcan
	url = https://gitee.com/robin_shaun/pyuavcan
[submodule "libuavcan_drivers/kinetis"]
	path = libuavcan_drivers/kinetis
	url = https://gitee.com/robin_shaun/libuavcan_kinetis.git
git submodule update --init

2、
修改.gitmodules:

cd ~/PX4_Firmware/src/drivers/uavcan/libuavcan/libuavcan/dsdl_compiler/pyuavcan
[submodule "jMAVlib"]
	path = jMAVlib
	url = https://gitee.com/robin_shaun/jMAVlib
	branch = master
git submodule update --init

3、

cd ~/PX4_Firmware/Tools/jMAVSim

修改.gitmodules:

[submodule "jMAVlib"]
	path = jMAVlib
	url = https://gitee.com/robin_shaun/jMAVlib
	branch = master
git submodule update --init

4、

cd ~/PX4_Firmware/Tools/sitl_gazebo

修改.gitmodules:

[submodule "external/OpticalFlow"]
	path = external/OpticalFlow
	url = https://gitee.com/robin_shaun/OpticalFlow
git submodule update --init

5、

cd ~/PX4_Firmware/platforms/qurt/dspal

修改.gitmodules:

[submodule "cmake_hexagon"]
	path = cmake_hexagon
	url = https://gitee.com/robin_shaun/cmake_hexagon
git submodule update --init

6、

cd ~/PX4_Firmware/Tools/sitl_gazebo/external/OpticalFlow

修改.gitmodules:

``````javascript
[submodule "external/klt_feature_tracker"]
	path = external/klt_feature_tracker
	url = https://gitee.com/robin_shaun/klt_feature_tracker
git submodule update --init

安装必要的python依赖:(如果前面已经安装可以跳过这步)

cd ~/PX4_Firmware
bash ./Tools/setup/ubuntu.sh --no-nuttx --no-sim-tools

编译

make px4_sitl_default gazebo

修改 ~/.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

然后运行如下命令,此时会启动Gazebo,如下图所示。

cd ~/PX4_Firmware
roslaunch px4 mavros_posix_sitl.launch

在这里插入图片描述

并运行

rostopic echo /mavros/state

若connected: True,则说明MAVROS与SITL通信成功。如果是false,一般是因为.bashrc里的路径写的不对,请仔细检查。

---
header:
seq: 11
stamp:
secs: 1827
nsecs: 173000000
frame_id: ''
connected: True
armed: False
guided: False
manual_input: True
mode: "MANUAL"
system_status: 3

六、QGC地面站安装(选择)

安装链接:https://docs.qgroundcontrol.com/en/getting_started/download_and_install.html
官网只有最新版本的包,下载后发现和当前版本不匹配,作者在github上上传了历史版本,可下载对应版本:https://github.com/mavlink/qgroundcontrol/releases?page=4,我这里下载的是稳定版本 v3.5.6
启动后,将出现下图 所示画面。注意Ubuntu16.04没法直接使用QGroundcontrol 版本4系列(可以使用版本3系列),Ubuntu16.04需要源码编译版本4系列,请仔细查看安装链接。
在这里插入图片描述

七、XTDrone源码下载

git clone https://gitee.com/robin_shaun/XTDrone.git
cd XTDrone
git submodule update --init --recursive
cp sensing/gimbal/gazebo_gimbal_controller_plugin.cpp ~/PX4_Firmware/Tools/sitl_gazebo/src/
cp sitl_config/init.d-posix/rcS ~/PX4_Firmware/ROMFS/px4fmu_common/init.d-posix/
cp sitl_config/worlds/* ~/PX4_Firmware/Tools/sitl_gazebo/worlds/
cp -r sitl_config/models/* ~/PX4_Firmware/Tools/sitl_gazebo/models/ 
cp -r sitl_config/launch/* ~/PX4_Firmware/launch/
cd ~/.gazebo/models/
rm -r stereo_camera/ 3d_lidar/ 3d_gpu_lidar/ hokuyo_lidar/

注意由于修改了PX4 sitl_gazebo中的gazebo_gimbal_controller_plugin.cpp(源代码不能控制多无人机的云台),要再编译一次。

cd ~/PX4_Firmware
make px4_sitl_default gazebo

因为Gazebo寻找模型的顺序,是先在.gazebo/models/下寻找,然后在其他路径寻找,所以在往PX4 SITL复制models时,要注意.gazebo/models/下有没有同名文件(比如.gazebo/models/下默认有stereo_camera),有的话要么将该同名文件删去,要么替换该同名文件。
注意:cp -r sitl_config/models/* ~/.gazebo/models/ 这一步可能出现如下情况:

这是gazebo的models没有的原因,按照上面的方法把下载的models解压放到.gazebo文件夹里就行,然后再执行指令就可以

八、用键盘控制无人机飞行

在一个终端运行

cd ~/PX4_Firmware
roslaunch px4 indoor1.launch

第一次运行roslaunch px4 indoor1.launch时gazebo可能需要加载一段时间,请耐心等待

Gazebo启动后,在另一个终端运行

cd ~/XTDrone/communication/
python multirotor_communication.py iris 0

与0号iris建立通信后,在另一个终端运行

cd ~/XTDrone/control/keyboard
python multirotor_keyboard_control.py iris 1 vel

成功后如图所示
在这里插入图片描述

到此,仿真平台基础配置完成
可以通过键盘控制1架iris的解锁/上锁,修改飞行模式,飞机速度等。使用t起飞利用的是takeoff飞行模式,相关参数(起飞速度、高度)要在rcS中设置。一般可以使用offboard模式起飞,这时起飞速度要大于0.3m/s才能起飞。(起飞方法:先按t键解锁,再按i键使得upward velocity大于等于0.3,此时可以看到旋翼转起来,最后按b键起飞。这之间的按键的时候间隔应当快一点,否则时间间隔过长的话无人机会自动锁上,又要重复刚刚的步骤)
注意,飞机要先解锁才能起飞!飞到一定高度后可以切换为‘hover’模式悬停,再运行自己的飞行脚本,或利用键盘控制飞机。
推荐起飞流程,按i把向上速度加到0.3以上,再按b切offboard模式,最后按t解锁。
使用键盘进行操作:
解锁并起飞: t —> i —> b —> s ,飞机将悬停
w/x :向前的速度
a/d:向左的速度
i:向上的速度
j/l:应该是 yaw角

参考:
[1]: https://blog.csdn.net/qq_45067735/article/details/107303796
[2]:https://www.bilibili.com/video/BV1Ku41197V5/?spm_id_from=333.788.recommend_more_video.0&vd_source=6a4316fd1cc70af28a81d366f3236429
[3]:https://www.yuque.com/xtdrone/manual_cn

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值