Dronecode - PX4環境構築

ドローンの自律飛行を実現するには、DronecodeとMAVSDKを試してみます。

# 目次
1. [PX4環境構築](https://qiita.com/k-koh/items/77af5422b3857e68ad9a)
2. [uORB Messaging](https://qiita.com/k-koh/items/c57c02df772785816c14)
3. [QGroundControlの使い方（ユーザーガイド）](https://qiita.com/k-koh/items/eedc303cb0b72d1272ee)
4. [MAVLinkの入門](https://qiita.com/k-koh/items/9e9c1694dbb5a2a4117a)
5. [QGroundControlのカスタマイズ開発](https://qiita.com/k-koh/items/36c34f0e3e18b2e055ac)

## 動作環境
- Ubuntu 18.04
- ROS Melodic

## PX4環境構築

ユーザ権限の設定、dialoutグループに追加

```
$ sudo usermod -a -G dialout $USER
```

ユーザ権限反映には、Logout and Loginしてください

### Gazebo, JMAVSim and NuttX (Pixhawk) Targets

1. Download ubuntu.sh and requirements.txt from the PX4
2. Run the ubuntu.sh to install

```
wget https://raw.githubusercontent.com/PX4/Firmware/v1.10.0/Tools/setup/ubuntu.sh
wget https://raw.githubusercontent.com/PX4/Firmware/v1.10.0/Tools/setup/requirements.txt
source ubuntu.sh
```

### ROS/Gazebo

1. Download ubuntu_sim_ros_melodic.sh
2. Run the ubuntu_sim_ros_melodic.sh to install

install ROS/Gazebo ("Melodic") for use with PX4

```
$ wget https://raw.githubusercontent.com/PX4/Devguide/v1.9.0/build_scripts/ubuntu_sim_ros_melodic.sh

$ source ubuntu_sim_ros_melodic.sh
```

### Downloading PX4 Source Code
```
$ git clone https://github.com/PX4/Firmware.git
```

### First Build (Using the jMAVSim Simulator) 

use java 8

```
$ sudo apt install openjdk-8-jdk
$ sudo update-alternatives --config java # choose java-8
```

PX4 Make Build Targets

```
make [VENDOR_][MODEL][_VARIANT] [VIEWER_MODEL_DEBUGGER]
VENDOR: The manufacturer of the board: px4(Pixhawk), intel, parrot, etc. 
MODEL: The board model : sitl, fmu-v2, etc.
VARIANT: Indicates particular configurations: rtps, lpe, default, and may be omitted.

VIEWER: This is the simulator : gazebo, jmavsim
MODEL: The vehicle model : iris (default), rover, etc.
DEBUGGER: Debugger to use: none (default), ide, gdb, lldb, ddd, valgrind, callgrind
```

Use jMAVSim Simulation

```
$ cd Firmware
$ make px4_sitl jmavsim
pxh> commander takeoff
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/386259/2a30aa81-09e4-3c84-717c-11fa0f414e96.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/386259/85c6c05f-efa7-895c-3a02-a79a6aaa0f2e.png)



Use Gazebo Simulation

```
$ make px4_sitl gazebo
pxh> commander takeoff
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/386259/4e1d3454-5a75-eb04-fd53-4b8415f2845a.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/386259/6d48edda-d150-060b-cd7c-6951407c20bf.png)


### Fast RTPS のインストール

eProsima Fast RTPSとは、RTPS (Real Time Publish Subscribe) protocolです。

Fast RTPS を使ってPX4 uORB topicsのデータを各モジュールの間で共有する。

eProsima Fast RTPSを使用には、Java（Java JDK 8推奨）が必要です。


#### Installation from Sources

- foonathan_memory のインストール

```
$ git clone https://github.com/foonathan/memory.git
$ cd memory
$ git submodule update --init --recursive
$ mkdir build && cd build
$ cmake -DCMAKE_CXX_FLAGS=-fPIC ..
$ cmake --build . --target install
```

- Fast-RTPS のインストール

```
$ git clone -b master https://github.com/eProsima/Fast-RTPS
$ mkdir Fast-RTPS/build && cd Fast-RTPS/build
$ sudo apt-get install gradle
$ cmake -DBUILD_JAVA=ON -DCOMPILE_EXAMPLES=ON -DPERFORMANCE_TESTS=ON -DTHIRDPARTY=ON -DCMAKE_PREFIX_PATH=/usr/local/lib/ ..
$ make
$ sudo make install
```

### QGroundControl のインストール

```
sudo usermod -a -G dialout $USER
sudo apt-get remove modemmanager -y
sudo apt-get install gstreamer1.0-plugins-bad gstreamer1.0-libav gstreamer1.0-gl -y
```

1. Download QGroundControl.AppImage.
2. Install (and run) using the terminal commands:

```
chmod +x ./QGroundControl.AppImage
./QGroundControl.AppImage  (or double click)
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/386259/9ba3a31a-1490-9e77-79ba-a754781275f1.png)


Next:　[uORB Messaging](https://qiita.com/k-koh/items/c57c02df772785816c14)