从零开始的Ubuntu 16.04下PX4编译环境的搭建

      近来入手了一块pixhawk,想进行一些基于已有代码的二次加工,于是到官网https://dev.px4.io/上看教程。官网上的教程是分为中文、英文以及韩文的版本。很多人肯定第一反应就是看中文的版本。但是这样做弊端真的很大,原因如下:

  1.官网上的中文汉化较差,很多地方的翻译不但意思不准确,甚至还在教程里面的关键步骤上出现了遗漏

  2.中文版本似乎已经长时间没有更新。里面有的固件库已经过时,在这样的固件库下编译,报错绝对是家常便饭。

笔者在这样的教程指导下走了很多弯路。所以写下这篇教程,给像我一样第一次接触PX4,Ubuntu,甚至编程的人一些帮助。

一、操作系统的选择——windows还是Ubuntu

跟大多数人一样,我之前从未接触过linux,ubuntu更是一无所知。但是鉴于官网上对Ubuntu的强烈推荐与windows的强烈不支持。我还是建议大家装一个Ubuntu。(版本最好是16.04)笔者曾经装了Ubuntu 18.04,后来发现因为操作系统版本过新导致了一系列问题(主要是编译环境的程序有的不被兼容)。所以强烈推荐大家装一个Ubuntu 16.04.

二、怎么装?

两种方式1.虚拟机(推荐)2.双系统。

笔者采用的是虚拟机的方式,原因很简单,作为小白,第一次使用ubuntu这样的系统的时候难免会有操作失误的时候。这时候当你不知道如何还原的时候,只需要把虚拟机卸载了然后重装就好了。

教程如下:https://blog.csdn.net/gongxifacai_believe/article/details/52444938

有一个地方需要注意。默认的内存是1G,但是我在编译的时候遇到了内存溢出的问题,所以比较推荐设置成2G(后面再改完全来得及)。

装好了虚拟机之后,建议安装一个vmtools,安装之后可以从windows界面下复制命令(ctrl+c),到ubuntu下面的终端(ctrl+alt+T)中粘贴(ctrl+shift+v)使用。

教程如下https://blog.csdn.net/tjcwt2011/article/details/72638977

三、正题——px4编译环境的搭建

在开始正题之前,我还是照例把要做的前戏说完。(很重要!)

px4编译环境所需要的文件大多数是从国外节点下载的,速度异常缓慢,经常还有找不到仓库的现象。所以需要更换国内镜像节点以加快下载速度。

教程如下

https://blog.csdn.net/Mordiary/article/details/80533627

然后终于可以开始搭建所需的编译环境了。

1. 开启某些权限: 
sudo usermod -a -G dialout $USER 
注销之后再登入,这样你就成为新的用户了,这个操作只需要进行一次,之后你再需要重新下载固件就不需要这一步了

2. 运行脚本文件:

官网最新提供了一个脚本文件,新建一个脚本文件ubuntu_sim_nuttx.sh,这个脚本文件不仅有PX4环境所需要的各种库,并且也安装了jMAVSim的各种依赖。复制以下内容到脚本文件中:

#!/bin/bash

## Bash script for setting up a PX4 development environment for Pixhawk/NuttX targets on Ubuntu LTS (16.04).
## It can be used for installing simulators and the NuttX toolchain.
##
## Installs:
## - Common dependencies and tools for all targets (including: Ninja build system, Qt Creator, pyulog)
## - FastRTPS and FastCDR
## - jMAVSim simulator
## - Gazebo8 simulator
## - NuttX toolchain (i.e. gcc compiler)
## - PX4/Firmware source (to ~/src/Firmware/)

# Ubuntu Config
sudo apt-get remove modemmanager -y


# Ninja build system
ninja_dir=$HOME/ninja
echo "Installing Ninja to: $ninja_dir."
if [ -d "$ninja_dir" ]
then
    echo " Ninja already installed."
else
    pushd .
    mkdir -p $ninja_dir
    cd $ninja_dir
    wget https://github.com/martine/ninja/releases/download/v1.6.0/ninja-linux.zip
    unzip ninja-linux.zip
    rm ninja-linux.zip
    exportline="export PATH=$ninja_dir:\$PATH"
    if grep -Fxq "$exportline" ~/.profile; then echo " Ninja already in path" ; else echo $exportline >> ~/.profile; fi
    . ~/.profile
    popd
fi


# Common Dependencies
echo "Installing common dependencies"
sudo add-apt-repository ppa:george-edison55/cmake-3.x -y
sudo apt-get update
sudo apt-get install python-argparse git-core wget zip python-empy qtcreator cmake build-essential genromfs -y
# required python packages
sudo apt-get install python-dev -y
sudo apt-get install python-pip
sudo -H pip install pandas jinja2
pip install pyserial
# optional python tools
pip install pyulog


# Install FastRTPS 1.5.0 and FastCDR-1.0.7
fastrtps_dir=$HOME/eProsima_FastRTPS-1.5.0-Linux
echo "Installing FastRTPS to: $fastrtps_dir"
if [ -d "$fastrtps_dir" ]
then
    echo " FastRTPS already installed."
else
    pushd .
    cd ~
    wget http://www.eprosima.com/index.php/component/ars/repository/eprosima-fast-rtps/eprosima-fast-rtps-1-5-0/eprosima_fastrtps-1-5-0-linux-tar-gz
    mv eprosima_fastrtps-1-5-0-linux-tar-gz eprosima_fastrtps-1-5-0-linux.tar.gz
    tar -xzf eprosima_fastrtps-1-5-0-linux.tar.gz eProsima_FastRTPS-1.5.0-Linux/
    tar -xzf eprosima_fastrtps-1-5-0-linux.tar.gz requiredcomponents
    tar -xzf requiredcomponents/eProsima_FastCDR-1.0.7-Linux.tar.gz
    cd eProsima_FastCDR-1.0.7-Linux; ./configure --libdir=/usr/lib; make; sudo make install
    cd ..
    cd eProsima_FastRTPS-1.5.0-Linux; ./configure --libdir=/usr/lib; make; sudo make install
    popd
fi


# jMAVSim simulator
sudo apt-get install ant openjdk-8-jdk openjdk-8-jre -y

# Gazebo simulator
sudo apt-get install protobuf-compiler libeigen3-dev libopencv-dev -y
sudo sh -c 'echo "deb http://packages.osrfoundation.org/gazebo/ubuntu-stable `lsb_release -cs` main" > /etc/apt/sources.list.d/gazebo-stable.list'
## Setup keys
wget http://packages.osrfoundation.org/gazebo.key -O - | sudo apt-key add -
## Update the debian database:
sudo apt-get update
## Install Gazebo8
sudo apt-get install gazebo8 -y
## For developers (who work on top of Gazebo) one extra package
sudo apt-get install libgazebo8-dev


# NuttX
sudo apt-get install python-serial openocd \
    flex bison libncurses5-dev autoconf texinfo \
    libftdi-dev libtool zlib1g-dev -y

# Clean up old GCC
sudo apt-get remove gcc-arm-none-eabi gdb-arm-none-eabi binutils-arm-none-eabi gcc-arm-embedded
sudo add-apt-repository --remove ppa:team-gcc-arm-embedded/ppa



# Install GCC 5.4
gcc_dir=$HOME/gcc-arm-none-eabi-5_4-2016q2
echo "Installing GCC to: $gcc_dir"
if [ -d "$gcc_dir" ]
then
    echo " GCC already installed."
else
    pushd .
    cd ~    
    wget https://developer.arm.com/-/media/Files/downloads/gnu-rm/5_4-2016q2/gccarmnoneeabi542016q220160622linuxtar.bz2
    tar -jxf gccarmnoneeabi542016q220160622linuxtar.bz2
    exportline="export PATH=$HOME/gcc-arm-none-eabi-5_4-2016q2/bin:\$PATH"
    if grep -Fxq "$exportline" ~/.profile; then echo " GCC path already set." ; else echo $exportline >> ~/.profile; fi
    . ~/.profile
    popd

    # Install 32 bit support libraries (ignore if fails)
    sudo dpkg --add-architecture i386
    sudo apt-get update
    sudo apt-get install libc6:i386 libgcc1:i386 libstdc++5:i386 libstdc++6:i386
    sudo apt-get install gcc-5.4-base:i386
fi



# Clone PX4/Firmware
clone_dir=~/src
echo "Cloning PX4 to: $clone_dir."
if [ -d "$clone_dir" ]
then
    echo " Firmware already cloned."
else
    mkdir -p $clone_dir
    cd $clone_dir
    git clone https://github.com/PX4/Firmware.git
    cd Firmware
fi
cd $clone_dir/Firmware


#Reboot the computer (required before building)
echo RESTART YOUR COMPUTER to complete installation of PX4 development toolchain

随后运行一下命令:source ubuntu_sim_nuttx.sh,这样就安装了各种依赖。为保险起见,打开Firmware文件夹,submodule一下:

cd ~src/Firmware/
git submodule init
git submodule update --init --recursive

然后可以编译固件

cd Firmware
make px4fmu-v2_default

没出现error即为编译成功。

猜你喜欢

转载自blog.csdn.net/Mordiary/article/details/80536859