本文主要介绍的是Ubuntu 18.04 LTS环境下,编译PX4的Firmware源码的过程和发现的问题。正文中不会介绍Ubuntu 18.04环境的安装过程,有需要的同学我后续再出相关教程。
官方教程:https://dev.px4.io/ 跳转链接:link
英文版,中文版都行,哪个看的懂就可以参考哪个;
必备内容:
PX4的源代码可以去github网站去自行下载,也可以在终端中用指令下载。个人建议:网络环境好的的话直接指令下。代码下载指令如下:
git clone https://github.com/PX4/Firmware.git --recursive
PS:这里先不要直接下,如果你是刚安装好的Ubuntu系统,里面可能没有git相关指令。直接用上面的指令会报错。
一切开始之间需要注意一下权限问题,目前权限问题是我在18.04环境遇见的最为头疼的问题。还没有完全研究明白权限的影响程度,后续博文可能会单独开一篇讨论。我参考了全面版本权限处理问题的方法:
利用用户组“dialout”具体操作如下图:在终端中输入指令后,登出在登入即可。
移除modemmanager,这个主要印象串口问题,将会影响后续固件上传。
PX4-Firmware源码的版本可以自行选择,国内好多教程、二次开发基线版本均为v1.8.2(stable)。代码版本不同可能在编译的过程中出现新的问题,问题不可控,请自行取舍利弊。
下载网站:https://github.com/PX4/Firmware 跳转链接: link
PS:一种下载方式,点击图片中
把压缩包解压,这里解压出来的是master的代码;
后续代码问题看下文代码讲解。
sh文件下载好后直接执行sh文件即可:
bash ./对应文件夹/ubuntu.sh
sh文件的好处是只要网络畅通的情况下,可以一次性把需要的编译环境全部安装完毕。如果中间断网或者手动停止后,可再次运营sh脚本完成后续安装。
上述两个sh文件可在网站直接下载,点击ubuntu.sh 和ubuntu_sim_ros_melodic.sh即可出现预览界面。提示:因为某些众所周知的原因,文件ubuntu_sim_ros_melodic.sh可能打不开,请用梯子重新打开。
此处将两个文件内容全部全部贴出,这是目前开发者网站中给的两个内容,后续可能更改。
ubuntu.sh
#! /usr/bin/env bash
## Bash script to setup PX4 development environment on Ubuntu LTS (18.04, 16.04).
## Can also be used in docker.
##
## Installs:
## - Common dependencies and tools for nuttx, jMAVSim, Gazebo
## - NuttX toolchain (omit with arg: --no-nuttx)
## - jMAVSim and Gazebo9 simulator (omit with arg: --no-sim-tools)
##
## Not Installs:
## - FastRTPS and FastCDR
set -e
INSTALL_NUTTX="true"
INSTALL_SIM="true"
# Parse arguments
for arg in "$@"
do
if [[ $arg == "--no-nuttx" ]]; then
INSTALL_NUTTX="false"
fi
if [[ $arg == "--no-sim-tools" ]]; then
INSTALL_SIM="false"
fi
done
# detect if running in docker
if [ -f /.dockerenv ]; then
echo "Running within docker, installing initial dependencies";
apt-get --quiet -y update && DEBIAN_FRONTEND=noninteractive apt-get --quiet -y install \
ca-certificates \
gnupg \
lsb-core \
sudo \
wget \
;
fi
# script directory
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# check requirements.txt exists (script not run in source tree)
REQUIREMENTS_FILE="requirements.txt"
if [[ ! -f "${DIR}/${REQUIREMENTS_FILE}" ]]; then
echo "FAILED: ${REQUIREMENTS_FILE} needed in same directory as ubuntu.sh (${DIR})."
return 1
fi
# check ubuntu version
# instructions for 16.04, 18.04, 20.04
# otherwise warn and point to docker?
UBUNTU_RELEASE="`lsb_release -rs`"
if [[ "${UBUNTU_RELEASE}" == "14.04" ]]; then
echo "Ubuntu 14.04 unsupported, see docker px4io/px4-dev-base"
exit 1
elif [[ "${UBUNTU_RELEASE}" == "16.04" ]]; then
echo "Ubuntu 16.04"
elif [[ "${UBUNTU_RELEASE}" == "18.04" ]]; then
echo "Ubuntu 18.04"
elif [[ "${UBUNTU_RELEASE}" == "20.04" ]]; then
echo "Ubuntu 20.04"
fi
echo
echo "Installing PX4 general dependencies"
sudo apt-get update -y --quiet
sudo DEBIAN_FRONTEND=noninteractive apt-get -y --quiet --no-install-recommends install \
astyle \
build-essential \
ccache \
clang \
clang-tidy \
cmake \
cppcheck \
doxygen \
file \
g++ \
gcc \
gdb \
git \
lcov \
make \
ninja-build \
python3 \
python3-dev \
python3-pip \
python3-setuptools \
python3-wheel \
rsync \
shellcheck \
unzip \
xsltproc \
zip \
;
if [[ "${UBUNTU_RELEASE}" == "16.04" ]]; then
echo "Installing Ubuntu 16.04 PX4-compatible ccache version"
wget -O /tmp/ccache_3.4.1-1_amd64.deb http://launchpadlibrarian.net/356662933/ccache_3.4.1-1_amd64.deb
sudo dpkg -i /tmp/ccache_3.4.1-1_amd64.deb
fi
# Python3 dependencies
echo
echo "Installing PX4 Python3 dependencies"
pip3 install --user -r ${DIR}/requirements.txt
# NuttX toolchain (arm-none-eabi-gcc)
if [[ $INSTALL_NUTTX == "true" ]]; then
echo
echo "Installing NuttX dependencies"
sudo DEBIAN_FRONTEND=noninteractive apt-get -y --quiet --no-install-recommends install \
autoconf \
automake \
bison \
bzip2 \
flex \
gdb-multiarch \
gperf \
libncurses-dev \
libtool \
pkg-config \
vim-common \
;
if [ -n "$USER" ]; then
# add user to dialout group (serial port access)
sudo usermod -a -G dialout $USER
fi
# arm-none-eabi-gcc
NUTTX_GCC_VERSION="7-2017-q4-major"
if [ $(which arm-none-eabi-gcc) ]; then
GCC_VER_STR=$(arm-none-eabi-gcc --version)
GCC_FOUND_VER=$(echo $GCC_VER_STR | grep -c "${NUTTX_GCC_VERSION}")
fi
if [[ "$GCC_FOUND_VER" == "1" ]]; then
echo "arm-none-eabi-gcc-${NUTTX_GCC_VERSION} found, skipping installation"
else
echo "Installing arm-none-eabi-gcc-${NUTTX_GCC_VERSION}";
wget -O /tmp/gcc-arm-none-eabi-${NUTTX_GCC_VERSION}-linux.tar.bz2 https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu-rm/7-2017q4/gcc-arm-none-eabi-${NUTTX_GCC_VERSION}-linux.tar.bz2 && \
sudo tar -jxf /tmp/gcc-arm-none-eabi-${NUTTX_GCC_VERSION}-linux.tar.bz2 -C /opt/;
# add arm-none-eabi-gcc to user's PATH
exportline="export PATH=/opt/gcc-arm-none-eabi-${NUTTX_GCC_VERSION}/bin:\$PATH"
if grep -Fxq "$exportline" $HOME/.profile;
then
echo "${NUTTX_GCC_VERSION} path already set.";
else
echo $exportline >> $HOME/.profile;
fi
fi
fi
# Simulation tools
if [[ $INSTALL_SIM == "true" ]]; then
echo
echo "Installing PX4 simulation dependencies"
# General simulation dependencies
sudo DEBIAN_FRONTEND=noninteractive apt-get -y --quiet --no-install-recommends install \
bc \
;
# Java 8 (jmavsim or fastrtps)
sudo DEBIAN_FRONTEND=noninteractive apt-get -y --quiet --no-install-recommends install \
ant \
openjdk-8-jre \
openjdk-8-jdk \
;
# Set Java 8 as default
sudo update-alternatives --set java $(update-alternatives --list java | grep "java-8")
# Gazebo
sudo sh -c 'echo "deb http://packages.osrfoundation.org/gazebo/ubuntu-stable `lsb_release -cs` main" > /etc/apt/sources.list.d/gazebo-stable.list'
wget http://packages.osrfoundation.org/gazebo.key -O - | sudo apt-key add -
sudo DEBIAN_FRONTEND=noninteractive apt-get -y --quiet --no-install-recommends install \
gazebo9 \
gstreamer1.0-plugins-bad \
gstreamer1.0-plugins-base \
gstreamer1.0-plugins-good \
gstreamer1.0-plugins-ugly \
gstreamer1.0-libav \
libeigen3-dev \
libgazebo9-dev \
libgstreamer-plugins-base1.0-dev \
libimage-exiftool-perl \
libopencv-dev \
libxml2-utils \
pkg-config \
protobuf-compiler \
;
fi
if [[ $INSTALL_NUTTX == "true" ]]; then
echo
echo "Reboot or logout, login computer before attempting to build NuttX targets"
fi
ubuntu_sim_ros_melodic.sh
#!/bin/bash
## Bash script for setting up ROS Melodic (with Gazebo 9) development environment for PX4 on Ubuntu LTS (18.04).
## It installs the common dependencies for all targets (including Qt Creator)
##
## Installs:
## - Common dependencies libraries and tools as defined in `ubuntu_sim_common_deps.sh`
## - ROS Melodic (including Gazebo9)
## - MAVROS
if [[ $(lsb_release -sc) == *"xenial"* ]]; then
echo "OS version detected as $(lsb_release -sc) (16.04)."
echo "ROS Melodic requires at least Ubuntu 18.04."
echo "Exiting ...."
return 1;
fi
echo "Downloading dependent script 'ubuntu_sim_common_deps.sh'"
# Source the ubuntu_sim_common_deps.sh script directly from github
common_deps=$(wget https://raw.githubusercontent.com/PX4/Devguide/master/build_scripts/ubuntu_sim_common_deps.sh -O -)
wget_return_code=$?
# If there was an error downloading the dependent script, we must warn the user and exit at this point.
if [[ $wget_return_code -ne 0 ]]; then echo "Error downloading 'ubuntu_sim_common_deps.sh'. Sorry but I cannot proceed further :("; exit 1; fi
# Otherwise source the downloaded script.
. <(echo "${common_deps}")
# ROS Melodic
## Gazebo simulator dependencies
sudo apt-get install protobuf-compiler libeigen3-dev libopencv-dev -y
## ROS Gazebo: http://wiki.ros.org/melodic/Installation/Ubuntu
## Setup keys
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
## For keyserver connection problems substitute hkp://pgp.mit.edu:80 or hkp://keyserver.ubuntu.com:80 above.
sudo apt-get update
## Get ROS/Gazebo
sudo apt install ros-melodic-desktop-full -y
## Initialize rosdep
sudo rosdep init
rosdep update
## Setup environment variables
rossource="source /opt/ros/melodic/setup.bash"
if grep -Fxq "$rossource" ~/.bashrc; then echo ROS setup.bash already in .bashrc;
else echo "$rossource" >> ~/.bashrc; fi
eval $rossource
## Install rosinstall and other dependencies
sudo apt install python-rosinstall python-rosinstall-generator python-wstool build-essential -y
# MAVROS: https://dev.px4.io/en/ros/mavros_installation.html
## Install dependencies
sudo apt-get install python-catkin-tools python-rosinstall-generator -y
## Create catkin workspace
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws
catkin init
wstool init src
## Install MAVLink
###we use the Kinetic reference for all ROS distros as it's not distro-specific and up to date
rosinstall_generator --rosdistro kinetic mavlink | tee /tmp/mavros.rosinstall
## Build MAVROS
### Get source (upstream - released)
rosinstall_generator --upstream mavros | tee -a /tmp/mavros.rosinstall
### Setup workspace & install deps
wstool merge -t src /tmp/mavros.rosinstall
wstool update -t src
if ! rosdep install --from-paths src --ignore-src -y; then
# (Use echo to trim leading/trailing whitespaces from the unsupported OS name
unsupported_os=$(echo $(rosdep db 2>&1| grep Unsupported | awk -F: '{print $2}'))
rosdep install --from-paths src --ignore-src --rosdistro melodic -y --os ubuntu:bionic
fi
if [[ ! -z $unsupported_os ]]; then
>&2 echo -e "\033[31mYour OS ($unsupported_os) is unsupported. Assumed an Ubuntu 18.04 installation,"
>&2 echo -e "and continued with the installation, but if things are not working as"
>&2 echo -e "expected you have been warned."
fi
#Install geographiclib
sudo apt install geographiclib-tools -y
echo "Downloading dependent script 'install_geographiclib_datasets.sh'"
# Source the install_geographiclib_datasets.sh script directly from github
install_geo=$(wget https://raw.githubusercontent.com/mavlink/mavros/master/mavros/scripts/install_geographiclib_datasets.sh -O -)
wget_return_code=$?
# If there was an error downloading the dependent script, we must warn the user and exit at this point.
if [[ $wget_return_code -ne 0 ]]; then echo "Error downloading 'install_geographiclib_datasets.sh'. Sorry but I cannot proceed further :("; exit 1; fi
# Otherwise source the downloaded script.
sudo bash -c "$install_geo"
## Build!
catkin build
## Re-source environment to reflect new packages/build environment
catkin_ws_source="source ~/catkin_ws/devel/setup.bash"
if grep -Fxq "$catkin_ws_source" ~/.bashrc; then echo ROS catkin_ws setup.bash already in .bashrc;
else echo "$catkin_ws_source" >> ~/.bashrc; fi
eval $catkin_ws_source
前面基本环境搭建很重要,直接影响整个编译能否成功。下面开始进行代码编译。首先,打开终端Terminal。
此步仅限于:没有执行1.2节中所提内容或者通过github单独下载代码的情况。如果已经通过git指令下载好了完整的代码,请跳过此节,直接进入2.2节。
cd Firmware
git submodule update --init --recursive
终端中输入:
mkdir PX4
就会在Home文件夹下生成一个PX4文件夹
然后执行:
cd PX4
再执行:
git clone https://github.com/PX4/Firmware.git --recursive
就会在PX4文件下自动下载对应的Firmware。
首先进入Firmware文件夹:
cd Firmware
我这里是基于master版本代码进行编译。注:可以在master代码版本上先进行分支建立切换对应版本,在执行编译。
图片中执行的是:
make px4_fmu-v2_default
这里make指令的区别是对应生成Pixhawk版本不一样,执行指令与版本对应关系如下图所示:
执行完毕后应该显示如下:
至此编译完成。
出现问题不要怕,宗旨是出现什么问题就google什么问题,为什么用google?理由是好多问题度娘不出来。
1.编译过程中的问题
比如出现下面这个问题:
解决方法很简单:
1)先确定你的python是不是安装了toml这库;
怎么检查?操作如下
在终端中输入:
>python3
>help()
>modules
检查是否有toml
没有就按问题图片中提示的指令安装toml
注意:因为python2已经终结了,Firmware的编译中涉及到的python库均为装入python3的函数库。另外,前面运行的脚本已经让系统安装了python3。如果你的系统没有python3 请确保前面环境搭建中的每一项都已经安装完毕了。
2)如果你发现已经安装了toml 但是仍然出现这个界面,证明你用户权限有问题。我是在make指令前加了sudo ,同样编译成功了。
如果出现下图问题:
说明环境变量中gcc的路径有问题
请检查gcc的环境变量是否添加争取,我个人在此处卡了很久,而且找不到准确的解决方案。
我个人的解决方法是参考v1.8.2版本中gcc环境变量的设置方式进行操作的。目前已解决。
2.后续可能出现的问题
接前面的问题,如果用了sudo指令编译成功,你会发现在对应编译好的bin文件出现一个锁头的标志。查看属性,所属权为root。目前还没进行后续上传板子操作,目测可能会出现权限问题。后续更新再讨论。
1.研究权限的影响
2.更新出现问题清单
3.ubuntu系统搭建教程