Ubuntu18.04配置搭建基于Gazebo的虚拟仿真平台(Px4):无人机(UAV)、无人车等模拟实验平台

开篇警示!


  • 安装Ubuntu需要的环境可为Ubuntu16.04与Ubuntu18.04(此环境检验可用,其他未知),推荐Ubuntu18.04
  • 要保证磁盘空间足够大或是后续磁盘空间可扩容,因为配置完成后,20G的磁盘只会剩余1.3G左右的可用空间,后续编译运行直接挂
  • 本文教程配置如下:
项目内容
环境VMWare16虚拟环境
系统Ubuntu18.04
ROS版本ros-melodic
控制PX4
仿真平台Gazebo

[(img-EJDN8ir4-1610537951700)(https://i.loli.net/2021/01/13/qKEu12DyI5kZix7.png)]

一、安装依赖

  • 使用apt安装安装Python2、pip、Python3、pip3
sudo apt update

# 安装Python2.7
sudo apt install python
sudo apt install python-pip

#Ubuntu18.04默认安装Python3.6.9,如果没有则下载
sudo apt install python3.6
sudo apt install python3-pip

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RtLSWGeY-1610537951705)()]

  • 其他依赖安装
sudo apt install -y ninja-build exiftool python-argparse python-empy python-toml python-numpy python-yaml python-dev python-pip ninja-build protobuf-compiler libeigen3-dev genromfs xmlstarlet libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev

pip2 install pandas jinja2 pyserial cerberus pyulog numpy toml pyquaternion

pip3 install packaging numpy empy toml pyyaml jinja2
  • 有时候apt报错,提示“有几个软件包无法下载,要不运行 apt-get update 或者加上 --fix-missing 的选项再试试?”,这时按照提示的要求进行操作即可。

(扩展)如果出现如下报错情况,可先更新 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

• 特别提醒,在整个环境配置中,不要轻易使用 apt-get autoremove,详见
慎用apt-get autoremove
• 特别提醒,PX4和ROS不能使用Anaconda,如果之前已经安装,在.bashrc里一定要把相关代码注释掉

二、ROS安装

安装步骤ROS官网,需注意:

  1. Ubuntu 16.04对应Kinetic,18.04对应Melodic,XTDrone平台目前支持这两个发行版,本教程以Ubuntu18.04为基准,对应Melodic。

  2. 如果您的网络环境不佳,Setup your sources.list 时中可选择中科大镜像或清华镜像。

下面开始安装ROS-Melodic教程(也可自行查看官网教程):

配置国内镜像源(Ubuntu的Desktop版本可用设置选项配置 或 命名行配置):

手动配置:在软件列表中找到 software & updates,点击后Download from选择other更改:

在这里插入图片描述

建议自己点击Select Best Server自行定位到最佳下载速度的源,我定位到的是China下的清华源:

在这里插入图片描述

保存退出。

执行:sudo apt undate,此步骤实际上可不用,在配置后关闭时,系统已经更新了,但为了避免个别朋友没有注意关闭保存,可以执行一次确保更新。

安装ROS

  1. 配置ROS软件源以接受来自package.ros.org的软件包:
sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
  1. 配置密钥
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654

执行结果如下即可:

sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
Executing: /tmp/apt-key-gpghome.0oSwXKzemT/gpg.1.sh --keyserver hkp://keyserver.ubuntu.com:80 --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
gpg: key F42ED6FBAB17C654: public key "Open Robotics <info@osrfoundation.org>" imported
gpg: Total number processed: 1
gpg:               imported: 1

  1. 再次更新系统软件源
sudo apt update
  1. Desktop版本桌面安装:ROS、rqt、rviz和机器人通用库
sudo apt install ros-melodic-desktop

可以选择安装Melodic不同的版本,不同版本之间,包含的软件包不同,其他版本官方的指令如下,可自行选择:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CR5q71bH-1610537951714)()]

  1. 初始化rosdep(问题常出)
sudo rosdep init

sudo rosdep fix-permissions

rosdep update

5.1 如报错找不到 rosdep 命令:

ganahe@ganahe-virtual-machine:~$ sudo rosdep init
sudo: rosdep: command not found

则执行再继续上面的操作:

sudo apt-get install python-rosdep

5.2 如报错网络连接失败,是网络问题导致,而不是软件源的问题,解决办法:

#备份hosts文件,以防瞎改
sudo cd /etc/hosts /etc/hosts_backup
#打开hosts文件
sudo gedit /etc/hosts
#在文件末尾添加
151.101.84.133  raw.githubusercontent.com

保存后退出再执行以上操作。

处理完成后,出现如下则正常:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-45fgXkUP-1610537951715)(https://i.loli.net/2021/01/13/xgHTDMacyRF4uIl.png)]

  1. 配置用户环境
echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
source ~/.bashrc

这两条指令相当于Windows系统中设置环境变量path,实现在terminal中执行ROS系统指令,这里强调是系统指令,而不是自己创建的包或者节点,后者是需要使能相应的工作空间,具体可以参考:ROS工作空间。

查看环境变量是否设置正确,命令如下:

env | grep ROS
  1. 构建包的依赖关系
    到目前为止,已经安装了运行核心ROS软件包所需的软件。 为了创建和管理自己的ROS工作区,有各种工具和要求分别分发。 例如,rosinstall是一个常用的命令行工具,使您可以通过一个命令轻松下载ROS软件包的许多源代码树。
    要安装此工具和其他依赖项以构建ROS软件包,请运行:
sudo apt install python-rosdep python-rosinstall python-rosinstall-generator python-wstool build-essential
  1. 验证安装
    执行rosversion -d出现:
ganahe@ganahe-virtual-machine:~$ rosversion -d
melodic

安装完成后,启动roscore:

roscore

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Tdfdrzjj-1610537951716)(https://i.loli.net/2021/01/13/5riNZy3agQfbAT2.png)]

成功安装ROS melodic。

  1. 工作空间管理

如果之前没有catkin_ws,则需要新建工作空间,之后除去PX4仿真环境启动外,其余ROS相关工程在此工作空间下管理。

mkdir -p ~/catkin_ws/src
mkdir -p ~/catkin_ws/scripts

# 移动到工作空间,如果使用catkin-tools话,则为cd catkin_ws && catkin init 
cd catkin_ws/src && catkin_init_workspace 

cd .. && catkin_make # 使用catkin-tools话,则为catkin build 

catkin-tools与catkin_make的区别参见Migrating from catkin_make,自行选择合适的管理工具。

执行效果如下:

ganahe@ganahe-virtual-machine:~$ mkdir -p ~/catkin_ws/src
ganahe@ganahe-virtual-machine:~$ mkdir -p ~/catkin_ws/scripts
ganahe@ganahe-virtual-machine:~$ cd catkin_ws/src && catkin_init_workspace 
Creating symlink "/home/ganahe/catkin_ws/src/CMakeLists.txt" pointing to "/opt/ros/melodic/share/catkin/cmake/toplevel.cmake"
ganahe@ganahe-virtual-machine:~/catkin_ws/src$ ls
CMakeLists.txt
ganahe@ganahe-virtual-machine:~/catkin_ws/src$ cd .. && catkin_make
Base path: /home/ganahe/catkin_ws
Source space: /home/ganahe/catkin_ws/src
Build space: /home/ganahe/catkin_ws/build
Devel space: /home/ganahe/catkin_ws/devel
Install space: /home/ganahe/catkin_ws/install
####
#### Running command: "cmake /home/ganahe/catkin_ws/src -DCATKIN_DEVEL_PREFIX=/home/ganahe/catkin_ws/devel -DCMAKE_INSTALL_PREFIX=/home/ganahe/catkin_ws/install -G Unix Makefiles" in "/home/ganahe/catkin_ws/build"
####
l
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Using CATKIN_DEVEL_PREFIX: /home/ganahe/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /opt/ros/melodic
-- This workspace overlays: /opt/ros/melodic
-- Found PythonInterp: /usr/bin/python2 (found suitable version "2.7.17", minimum required is "2") 
-- Using PYTHON_EXECUTABLE: /usr/bin/python2
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/ganahe/catkin_ws/build/test_results
-- Found gtest sources under '/usr/src/googletest': gtests will be built
-- Found gmock sources under '/usr/src/googletest': gmock will be built
-- Found PythonInterp: /usr/bin/python2 (found version "2.7.17") 
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.7.29
-- BUILD_SHARED_LIBS is on
-- BUILD_SHARED_LIBS is on
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ganahe/catkin_ws/build
####
#### Running command: "make -j4 -l4" in "/home/ganahe/catkin_ws/build"
####
ganahe@ganahe-virtual-machine:~/catkin_ws$ l
build/  devel/  scripts/  src/

三、安装Gazebo

(一)注意事项

• 注意:Melodic自带的gazebo9版本过低,建议升级

  1. 选用Alternative installation: step-by-step的安装方式,安装最新的gazebo9,不要安装gazebo11

  2. 如果安装有依赖问题,可以使用sudo aptitude install gazebo9,选择合理的依赖解决办法(别把ROS删了)

(二)开始安装

有两种安装方式,一种从官方获取脚本一行代码实现安装,但是此方式虽然简单,但安装时缺斤少两,不好处理;

选择”循序渐进“方式:

  1. 配置源以接受来自packes.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

ganahe@ganahe-virtual-machine:~/catkin_ws$ cat /etc/apt/sources.list.d/gazebo-stable.list
deb http://packages.osrfoundation.org/gazebo/ubuntu-stable bionic main
  1. 配置密钥
wget https://packages.osrfoundation.org/gazebo.key -O - | sudo apt-key add -
  1. 安装Gazebo9

首先更新debian数据库:sudo apt-get update

下载版本9:sudo apt-get install gazebo9

安装完成后执行gazebo运行如下:

如果报错:

ganahe@ganahe-virtual-machine:~$ gazebo
VMware: vmw_ioctl_command error Invalid argument.

执行export SVGA_VGPU10=0再运行。

  1. 安装ROS的Gazebo插件

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

sudo apt install ros-melodic-gazebo-*

如果是kinetic版本,执行sudo apt install ros-kinetic-gazebo9-*

  1. 获取gazebo模型

Gazebo有很多开源的模型文件,整个模型文件大致有1G,官方下载的速度极慢,可从合作平台微信公众号:星辰换日,获取下载提取码:

链接:https://pan.baidu.com/s/16LWBEBkxDLjRdgKfDm-mXA

回复**Gazebo仿真模型**即可。

将该压缩包解压缩后放在~/.gazebo中(一般在home目录),此时在~/.gazebo/models/路径下可以看到很多模型。如果不做这一步,之后运行Gazebo仿真,可能会缺模型,这时会自动下载,国内的网比较慢,自动下载会下载比较久。

四、MAVROS安装

注意,mavros-extras一定别忘记装,否则视觉定位将无法完成


   # ros-melodic版本,ros-kinetic的需要看后面标注。
sudo apt install ros-melodic-mavros ros-melodic-mavros-extras    
wget https://raw.githubusercontent.com/mavlink/mavros/master/mavros/scripts/install_geographiclib_datasets.sh
sudo chmod a+x ./install_geographiclib_datasets.sh
sudo ./install_geographiclib_datasets.sh #这步需要装一段时间,请耐心等待PX4配置

ros-kinetic第一句执行版本:sudo apt install ros-kinetic-mavros ros-kinetic-mavros-extras

五、PX4配置等

阅读完整原文:传送

参考文献

[1]XTDrone无人机仿真平台基础配置
[2]ROS Melodic安装官方教程
[3]Gazebo安装官方教程
[4]Px4仿真文档

https://dgzc.ganahe.top/ganahe/2021/uavgazebomoni.html

  • 5
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 在Ubuntu 18.04下搭建PX4环境的步骤如下: 1. 安装依赖项 在终端中输入以下命令: sudo apt-get update sudo apt-get install git zip qtcreator cmake build-essential genromfs ninja-build libopencv-dev libeigen3-dev -y 2. 克隆PX4代码 在终端中输入以下命令: cd ~ git clone https://github.com/PX4/Firmware.git --recursive 3. 安装PX4工具链 在终端中输入以下命令: cd ~/Firmware make px4_sitl_default gazebo 4. 配置环境变量 在终端中输入以下命令: echo "source ~/Firmware/Tools/setup_gazebo.bash ~/Firmware ~/Firmware/build/px4_sitl_default" >> ~/.bashrc source ~/.bashrc 5. 启动PX4仿真 在终端中输入以下命令: cd ~/Firmware make px4_sitl_default gazebo 6. 测试 在终端中输入以下命令: cd ~/Firmware make px4_sitl_default gazebo 以上就是在Ubuntu 18.04下搭建PX4环境的步骤。 ### 回答2: 在ubuntu18.04下搭建PX4环境,需要进行以下步骤: 1.更新安装包: sudo apt-get update sudo apt-get upgrade 2.安装GNUPG: sudo apt install gnupg 3.下载PX4 GPG key: sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 045F9926759BAC3C 4.添加PX4软件源: sudo add-apt-repository "deb https://px4.io/repos/apt/ubuntu `lsb_release -cs` main" 5.更新软件源: sudo apt-get update 6.安装PX4: sudo apt install px4 7.安装PX4模拟器: sudo apt install sitl 8.安装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 install curl curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add - sudo apt-get update sudo apt-get install ros-melodic-desktop-full 9.依照机型安装相应依赖包: sudo apt-get install protobuf-compiler libeigen3-dev libxml2-utils libopencv-dev libblas-dev liblapack-dev libboost-all-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev gstreamer1.0-plugins-good python-matplotlib python-argparse python-lxml python-yaml python-six python-dev python-numpy python-pip python-setuptools cmake build-essential sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc) universe" sudo apt-get update 10.构建PX4: 注意,以下操作要在PX4源代码目录下执行 cd PX4-Autopilot make px4_sitl_default gazebo 11.启动PX4模拟器: ./Tools/gazebo_sitl.sh -i 0 -m iris gazebo 以上就是在ubuntu18.04下搭建PX4环境的完整步骤,如果按照以上步骤进行操作,就能够成功地搭建并启动模拟器,进行相关的开发调试。 ### 回答3: 在Ubuntu 18.04下搭建PX4环境,需要以下步骤: 1. 安装依赖项 在Ubuntu中,需要安装以下软件包: - git - cmake - python-jinja2 - python-pip - python-dev - build-essential - pkg-config - libgtk-3-dev - libcanberra-gtk3-module - libavcodec-dev - libavformat-dev - libswscale-dev - libreadline-dev - libudev-dev 可以通过以下命令进行安装: sudo apt-get install git cmake python-jinja2 python-pip python-dev build-essential pkg-config libgtk-3-dev libcanberra-gtk3-module libavcodec-dev libavformat-dev libswscale-dev libreadline-dev libudev-dev 2. 下载PX4源代码 安装完成依赖项后,需要从Github上下载PX4源代码,可以通过以下命令进行: git clone https://github.com/PX4/Firmware.git --recursive 3. 安装PX4的依赖项 PX4的依赖项可以通过以下命令进行安装: cd Firmware make px4_sitl_default gazebo 如果出现错误,请安装缺少的依赖项。常见的依赖项包括python-future、python-empy、python-toml等。 4. 编译PX4 编译PX4可以通过以下命令进行: cd Firmware make px4_fmu-v3_default 5. 启动PX4 启动PX4可以通过以下命令进行: cd Firmware make px4_sitl_default 在启动过程中可能会出现一些错误,常见的错误包括端口被占用、缺少权限等。需要根据实际情况进行处理。 以上就是Ubuntu 18.04下搭建PX4环境的步骤,希望能对大家有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值