
PX4从放弃到精通(二):ubuntu18.04配置px4编译环境及mavros环境
一个人可以走的更快,一群人才能走的更远,交流学习可加微信18362086993
👉👉👉**无人机硬件,提供全程指导**👈👈👈
文章目录
前言
如果要在虚拟机上使用Ubuntu,可以参考
https://mbot1.blog.csdn.net/article/details/121270282
如果已经装好系统或虚拟机,则可以进行下面的环境安装,在安装环境时,建议使用比较纯净的系统,刚装好系统的电脑或虚拟机最好,网速尽量快,减少报错的概率.
视频教程可在B站观看
ROS+PX4开源无人机系列-PX4编译环境安装
已经安装好PX4 ROS MAVROS环境的虚拟机,用vm16 pro打开
链接:https://pan.baidu.com/s/1gGK9H-HYAhKhDFwBsXX_MA?pwd=527e
提取码:527e
–来自百度网盘超级会员V6的分享
一、下载源码
先换源,可以换成清华源或阿里源
然后执行
sudo apt-get update
如果没安装git就先安装git
sudo apt-get install git
如果提示
E: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系。
则更新源
执行
sudo apt-get update
如果报网络下载失败的错,可以把源换成清华源或者阿里源
执行
cd /etc/apt
sudo cp sources.list sources.list.bak
sudo gedit sources.list
更换内容为
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-security main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-security main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic main universe restricted
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ bionic main universe restricted
如果报错
E: GPG error: file:/var/visionworks-repo Release: Signed file isn’t
valid, got ‘NODATA’ (does the network require authentication?) E: GPG
error: file:/var/visionworks-sfm-repo Release: Signed file isn’t
valid, got ‘NODATA’ (does the network require authentication?) E: GPG
error: file:/var/visionworks-tracking-repo Release: Signed file isn’t
valid, got ‘NODATA’ (does the network require authentication?)
执行
sudo rm /etc/apt/sources.list.d/***.list
sudo apt-get update -y
然后再执行sudo apt-get update
下载PX4源码
git clone -b stable https://github.com/PX4/PX4-Autopilot.git --recursive
这里下载的是stable版本的固件,即最新稳定版的固件,如果要下载指定版本的固件,则将上面的stable换成版本号,如下载1.11.3版本的固件,则下载命令为
git clone -b v1.11.3 https://github.com/PX4/PX4-Autopilot.git --recursive
用git clone下载px4 PX4-Autopilot时,如果一些子模块因为网络原因下载失败。可以在网络正常后,在PX4-Autopilot路径下执行以下命令继续下载剩余的子模块。
git submodule update --init --recursive
二、安装PX4和gazebo9环境
源码下载完毕后
cd PX4-Autopilot
sudo bash ./Tools/setup/ubuntu.sh
如果装的是PX4 1.12.3的编译环境,会无法编译1.13.0的固件,需要用1.13.0的脚本进行配置,如果装的是1.13.0的版本的编译环境,则可以编译1.12.3的固件
在执行这个脚本的过程中会自动安装px4的编译环境和gazebo仿真环境,环境安装过程中如果报下载失败的错错误,一般是网速问题,可以解决网速问题后重新执行脚本。直到安装成功。
如果报以下错误
http://packages.ros.org/ros/ubuntu xenial InRelease: 由于没有公钥,无法验证下列签名:
执行下面命令再重新执行脚本,最后的码,根据不同的提示的进行修改
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F42ED6FBAB17C654
如果报以下错误
Installing PX4 simulation dependencies E: 无法获得锁
/var/lib/dpkg/lock-frontend - open (11: 资源暂时不可用) E: 无法获取 dpkg 前端锁
(/var/lib/dpkg/lock-frontend),是否有其他进程正占用它? E: 无法获得锁
/var/lib/dpkg/lock-frontend - open (11: 资源暂时不可用) E: 无法获取 dpkg 前端锁
(/var/lib/dpkg/lock-frontend),是否有其他进程正占用它?
执行以下三条命令再重新执行脚本
sudo rm /var/lib/dpkg/lock-frontend
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock
如果执行脚本过程中出现以下报错
错误:7 http://packages.ros.org/ros/ubuntu bionic InRelease
则执行下面的命令再重新运行脚本
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F42ED6FBAB17C654
如果执行脚本过程中出现以下报错
则执行下面的命令再重新运行脚本
sudo apt-get install libjpeg-dev zlib1g-dev
直到出现下面提示重启电脑且无报错说明脚本执行完毕
三、编译PX4固件
安装完环境后编译生成gazebo仿真所需的sdf文件
make px4_sitl_default gazebo
如果要编译下载到飞控的代码,需重启电脑编译,以v5固件为例
make px4_fmu-v5_default
如果在编译代码时报错
ModuleNotFoundError: No module named ‘menuconfig’ CMake Error at
cmake/kconfig.cmake:6 (message): kconfiglib is not installed or not
in PATHplease install using “pip3 install kconfiglib”
Call Stack (most recent call first): CMakeLists.txt:174 (include)
执行下面命令即可解决
sudo -H python3 -m pip install kconfiglib
如果在编译代码时报错
Failed to import genmsg: No module named ‘genmsg’
You may need to install it using:
pip3 install --user pyros-genmsg
执行下面命令即可解决
sudo -H python3 -m pip install pyros-genmsg
如果在编译代码时报错
Failed to import toml: No module named ‘toml’
You may need to install it using:
pip3 install --user toml
执行
sudo -H python3 -m pip install toml
如果在编译代码时报错
Failed to import numpy: No module named ‘numpy’
You may need to install it using:
pip3 install --user numpy
执行
sudo -H python3 -m pip install numpy
如果在编译代码时报错
Failed to import jinja2: No module named ‘jinja2’
You may need to install it using:
pip3 install --user jinja2
执行
sudo -H python3 -m pip install jinja2
如果在编译代码时报错
Failed to import packaging: No module named ‘em’
则执行下面语句安装软件包
sudo -H python3 -m pip install em
如果在编译代码时报错
Failed to import jsonschema: No module named ‘jsonschema’
You may need to install it using:
pip3 install --user jsonschema
执行
sudo -H python3 -m pip install jsonschema
如果报
Failed to import packaging: No module named ‘packaging’
You may need to install it using:
pip3 install --user packaging
执行
sudo -H python3 -m pip install packaging
出现以下提示表示安装成功
如果报错的话可以尝试执行下面的命令安装
sudo pip install --ignore-installed packaging
同理如果缺少toml/jinja2/numpy等其他包,安装方法同上,只是把最后的包名换一下.
如果编译时报以下错
AttributeError: module ‘em’ has no attribute ‘Interpreter’
则执行下面两条命令并重新编译
sudo -H python3 -m pip uninstall em
sudo -H python3 -m pip install --ignore-installed empy
如果编译时报以下错
ModuleNotFoundError: No module named ‘future’
执行
sudo -H python3 -m pip install --ignore-installed future
再重新编译
常见的缺少的包的安装命令如下:
sudo -H python3 -m pip install kconfiglib
sudo -H python3 -m pip install jinja2
sudo -H python3 -m pip install jsonschema
sudo -H python3 -m pip install empy
sudo -H python3 -m pip install pyros-genmsg
sudo -H python3 -m pip install packaging
sudo -H python3 -m pip install toml
sudo -H python3 -m pip install numpy
sudo -H python3 -m pip install future
如果上面的命令安装后还是报错,可以用下面的命令安装:
pip3 install kconfiglib
pip3 install --user empy
pip3 install --user jsonschema
pip3 install --user jinja2
pip3 install --user pyros-genmsg
pip3 install --user packaging
pip3 install --user toml
pip3 install --user numpy
如果编译时报错:
CMake Error at CMakeLists.txt:235 (project): The CMAKE_C_COMPILER:
arm-none-eabi-gcc
is not a full path and was not found in the PATH.
执行下面命令解决:
sudo apt-get install gcc-arm-none-eabi
注意编译的时候Firmware路径不能不能有英文的括号,否则编译会报错或者一直循环config
笔者在编译成功后,利用make clean
清除编译后再次编译会报如下错
解决办法是用make distclean
重新清除一下或者将build文件夹删除即可。
如果在启动gazebo时报错
gzserver: symbol lookup error:
/usr/lib/x86_64-linux-gnu/libgazebo.so.9: undefined symbol:
_ZN3sdf28recursiveSameTypeUniqueNamesESt10shared_ptrINS_7ElementEE
则执行下面的语句再重新编译即可
sudo apt upgrade
如果使用gazebo仿真时出现无法分配内存错误,执行
free -m
sudo mkdir /opt/images/
sudo rm -rf /opt/images/swap
sudo dd if=/dev/zero of=/opt/images/swap bs=1024 count=2048000
sudo mkswap /opt/images/swap
sudo swapon /opt/images/swap
如果编译时报错:
– PX4 config: px4_fmu-v5_default
– PX4 platform: nuttx make[2]: *** 没有规则可制作目标“dirlinks”。 停止。 make[1]: *** [tools/Makefile.unix:332:dirlinks] 错误 2
– cmake build type: MinSizeRel
– The CXX compiler identification is GNU 9.2.1
– The C compiler identification is GNU 9.2.1
– The ASM compiler identification is GNU
– Found assembler: /usr/bin/arm-none-eabi-gcc
原因是子模块没下载完全,执行下面命令
make distclean
再执行下面命令直到子模块下载完全即可
git submodule update --init --recursive
如果编译时报错:“ULong”does not name a type
解决办法:
修改/usr/include/newlib/math.h
首先添加权限
sudo chmod 777 /usr/include/newlib/math.h
然后在文件中添加一行:
#define __ULong long
保存修改后,再次编译即可
如果编译make px4_sitl_default gazebo
时出现下面的报错:
CMake Error at CMakeLists.txt:32 (find_package): By not providing
“Findgazebo.cmake” in CMAKE_MODULE_PATH this project has asked CMake
to find a package configuration file provided by “gazebo”, but CMake
did not find one.
Could not find a package configuration file provided by “gazebo” with any of the following names:gazeboConfig.cmake gazebo-config.cmake Add the installation prefix of "gazebo" to CMAKE_PREFIX_PATH or set "gazebo_DIR" to a directory containing one of the above files.
If “gazebo” provides a separate development package or SDK, be sure
it has been installed.
执行
sudo apt-get install libgazebo9-dev
再重新编译
如果编译时提示找不到opencv
执行
sudo apt install libopencv-dev python3-opencv
如果报错找不到eigen3:
执行:
sudo apt-get install libeigen3-dev
四、安装ros和mavros环境
4.1安装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
ruguo shangmian zhe yi bu baocuo ,changshi huan yuan
sudo apt update
如果报错
W: An error occurred during the signature verification. The repository
is not updated and the previous index files will be used. GPG error:
http://packages.ros.org/ros/ubuntu bionic InRelease: The following
signatures were invalid: EXPKEYSIG F42ED6FBAB17C654 Open Robotics
info@osrfoundation.org W: Failed to fetch
http://packages.ros.org/ros/ubuntu/dists/bionic/InRelease The
following signatures were invalid: EXPKEYSIG F42ED6FBAB17C654 Open
Robotics info@osrfoundation.org W: Some index files failed to
download. They have been ignored, or old ones used instead.
执行下面两条命令:
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'
curl -sSL 'http://keyserver.ubuntu.com/pks/lookup?op=get&search=0xC1CF6E31E6BADE8868B172B4F42ED6FBAB17C654' | sudo apt-key add -
然后继续执行:
sudo apt install ros-melodic-desktop-full
echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
source ~/.bashrc
sudo apt install python-rosdep
sudo rosdep init
rosdep update
如果rosdep init出现以下错误
ERROR: cannot download default sources list from:
https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/sources.list.d/20-default.list
一般是网络问题,无法访问那个链接,解决方法:
sudo gedit /etc/hosts
在文件末尾添加
151.101.84.133 raw.githubusercontent.com
保存后退出重新sudo rosdep init
rosdep update执行成功后的界面如下
继续执行下面的命令
sudo apt install python-catkin-tools
4.2 二进制安装mavros
如果不修改mavros,推荐使用二进制安装
sudo apt-get install ros-melodic-mavros ros-melodic-mavros-extras
如果是ubuntu20.04则执行下面的命令安装:
sudo apt-get install ros-noetic-mavros ros-noetic-mavros-extras
安装完之后执行
wget https://raw.githubusercontent.com/mavlink/mavros/master/mavros/scripts/install_geographiclib_datasets.sh
chmod +x install_geographiclib_datasets.sh
sudo ./install_geographiclib_datasets.sh
4.3源码安装maros
源码安装参考
https://docs.px4.io/master/en/ros/mavros_installation.html
首先检查一下home目录下是否有.catkin_tools文件夹,如果有,就删除,否面后面编译时会报类似下面的错:
[build] Error: Unable to find source space
/home/amov/src
然后新建工作空间,名称可以自己任取
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws
catkin init
wstool init src
安装ROS Python工具
sudo apt-get install python-catkin-tools python-rosinstall-generator -y
初始化源码空间
wstool init ~/catkin_ws/src
注意,后面的操作都在catkin_ws
目录下执行
执行下面语句安装mavlink
rosinstall_generator --rosdistro melodic mavlink | tee /tmp/mavros.rosinstall
成功后如下:
执行下面两条语句的其中一条安装mavros
稳定版mavros
rosinstall_generator --upstream mavros | tee -a /tmp/mavros.rosinstall
最新版mavros
rosinstall_generator --upstream-development mavros | tee -a /tmp/mavros.rosinstall
成功后如下:
执行
wstool merge -t src /tmp/mavros.rosinstall
执行过程中输入y,成功后如下:
执行下面语句下载源码
wstool update -t src -j4
成功后如下:
执行
rosdep install --from-paths src --ignore-src -y
成功后如下:
执行
sudo ./src/mavros/mavros/scripts/install_geographiclib_datasets.sh
成功后如下:(笔者执行这一步时经常卡住,执行了好几次才成功)
执行
catkin build
成功后如下:
最后执行
source devel/setup.bash
roscd mavros
如果安装mavros安装成功的话,显示如下:
4.4其他
安装完后在~/.bashrc中检查是否已添加下面的命令
source /opt/ros/melodic/setup.bash
安装好mavros后,需要在.bashrc中添加一下px4源码的路径,否则在使用px4的launch文件时,会出现类似以下的报错:
Resource not found: The following package was not found in : mavlink_sitl_gazebo ROS path
[0]=/opt/ros/melodic/share/ros ROS path [1]=/home/mobot/catkin_ws/src
ROS path [2]=/home/mobot/vision_ws/src ROS path
[3]=/opt/ros/melodic/share ROS path
[4]=/home/mobot/ORB_SLAM2_workspace/ORB_SLAM2/Examples/ROS The
traceback for the exception was written to the log file
gedit .bashrc
以Firmware在home目录下为例,添加如下:
source ~/Firmware/Tools/setup_gazebo.bash ~/Firmware/ ~/Firmware/build/px4_sitl_default
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/Firmware
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/Firmware/Tools/sitl_gazebo
如果是PX4-Autopilot则添加如下
source ~/PX4-Autopilot/Tools/setup_gazebo.bash ~/PX4-Autopilot/ ~/PX4-Autopilot/build/px4_sitl_default
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/PX4-Autopilot
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/PX4-Autopilot/Tools/sitl_gazebo
如果PX4固件是1.14版本,则添加如下:
source ~/PX4-Autopilot/Tools/simulation/gazebo-classic/setup_gazebo.bash ~/PX4-Autopilot/ ~/PX4-Autopilot/build/px4_sitl_default
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/PX4-Autopilot
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/PX4-Autopilot/Tools/simulation/gazebo-classic/sitl_gazebo-classic
注意export 开头的环境变量必须放在最下面,source开头的如果放在最下的话,会导致无法使用roslaunch px4 XXX.launch,以及直接启动launch文件是提示找不到路径
如下图:
注意这里的路径是每次ros启动的时候会从这里启动无人机的模型,因此只有修改这个路径下的固件,在通过roslaunch启动无人机的时候,对应的修改才会对仿真的无人机有效
保存后执行
source .bashrc
在执行完 source .bashrc
后,相关的配置在新开的终端里才会生效
测试mavros
roslaunch px4 mavros_posix_sitl.launch
如果正常启动gazebo,仿真中的无人机能连接QGC地面站,并且终端显示版本号,则说明px4、ros、mavros都正常安装了。
如果启动roslaunch px4 mavros_posix_sitl.launch
报错
Resource not found: gazebo_ros
解决办法:
sudo apt install ros-melodic-gazebo-ros-pkgs
如果启动仿真后GAZEBO中没有无人机模型或者无人机的模型可以出来,但是无人机的桨叶一直在转,且无法连接地面站和ros.原因是模型没有编译,找到所用模型所在的Firmware文件夹下编译模型
make px4_sitl_default gazebo
如果执行上述命令(或者make px4_sitl_default gazebo_rover等命令)时出现类似下面报错:
CMake Error at CMakeLists.txt:103 (add_subdirectory): The source
directory/home/mbot/1.13.0/PX4-Autopilot/Tools/sitl_gazebo/external/OpticalFlow
does not contain a CMakeLists.txt file.
原因是子模块没下全,在PX4-Autopilot目录下重新执行下面命令下载子模块:
git submodule update --init --recursive
然后再启动脚本,注意要编译.bashrc脚本里用的模型,编译别的Firmware路径下的模型是没用的
如果在执行roslaunch px4 mavros_posix_sitl.launch
时出现下面的报错:
/opt/ros/melodic/lib/libmavros.so: undefined symbol:
_ZN7mavconn16MAVConnInterface19open_url_no_connectENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEhh
- SYS_AUTOCONFIG: curr: 1 -> new: 0 IMU_INTEG_RATE: curr: 200 -> new: 250 INFO [dataman] Unknown restart, data manager file
‘./dataman’ size is 11798680 bytes PX4 SIM HOST: localhost INFO
[simulator] Waiting for simulator to accept connection on TCP port
4560
================================================================================REQUIRED process [mavros-6] has died! process has died [pid 11920, exit code
127, cmd /opt/ros/melodic/lib/mavros/mavros_node __name:=mavros
__log:=/home/amov/.ros/log/7da43b80-d44b-11ec-ac84-00e93a00f7ed/mavros-6.log].
log file:
/home/amov/.ros/log/7da43b80-d44b-11ec-ac84-00e93a00f7ed/mavros-6*.log
Initiating shutdown!
================================================================================
执行下面的命令解决
卸载gazebo9.0
sudo apt-get remove gazebo9*
安装gazebo9.19
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
wget https://packages.osrfoundation.org/gazebo.key -O - | sudo apt-key add -
sudo apt-get update
sudo apt-get install gazebo9=9.1*
sudo apt upgrade libignition-math2
安装相关依赖
sudo apt-get install ros-melodic-gazebo-ros
sudo apt-get install ros-melodic-gazebo-ros-control
sudo apt-get install ros-melodic-gazebo-ros-pkgs
如果已经安装了gazebo(可以使用gazebo -version查看是否已安装),但是在执行make px4_sitl_default gazebo
编译时出现报错:
Could not find a package configuration file provided by “gazebo”
with any of the following names:gazeboConfig.cmake gazebo-config.cmake
执行下面的命令解决:
sudo apt-get install ros-$ROS_DISTRO-gazebo-ros-control -y
卸载mavros可以用下面的命令
sudo apt-get remove ros-melodic-mavros ros-melodic-mavros-extras
如果启动roslaunch px4 mavros_posix_sitl.launch
后可以弹出gazebo界面,但是提示找不到iris模型,并且gazebo中也没有无人机,则检查.bashrc中设置的固件目录下的固件是否编译,我这里设置的路径如下:
但是我在
~/PX4-Autopilot
下编译make px4_sitl_default gazebo
却没有在~/PX4-Autopilot/Tools/sitl_gazebo/models/iris
目录下生成iris.sdf,并且提示使用的是~/0/PX4-Autopilot/Tools/sitl_gazebo/models/iris/iris.sdf
,原因是我的代码PX4-Autopilot代码是从~/0/下拷过来的。
Using:
/home/aaa/0/PX4-Autopilot/Tools/sitl_gazebo/models/iris/iris.sdf
解决办法是在`~/PX4-Autopilot下先
make distclean
再
make px4_sitl_default gazebo
出现下面提示
Using: /home/aaa/PX4-Autopilot//Tools/sitl_gazebo/models/iris/iris.sdf
并且~/PX4-Autopilot/Tools/sitl_gazebo/models/iris下生成了iris.sdf,说明正常
再继续执行roslaunch px4 mavros_posix_sitl.launch
就正常了
五、安装QGC地面站
如果需要安装QGC地面站,可以在下面的网址下载
https://docs.qgroundcontrol.com/master/en/getting_started/download_and_install.html
下载后在地面站目录下执行
chmod +x QGroundControl.AppImage
再双击打开地面站即可,如果打开地面站时出现下面的提示
执行下面命令,再打开即可
sudo usermod -a -G dialout $USER
sudo apt-get remove modemmanager
更多推荐
所有评论(0)