箱庭ドローンシミュレータで東尋坊上空を飛べた！

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/87aac626-bc90-c8d3-6096-052c95b2b29c.png)

参照 : [toppers](https://github.com/toppers) / [hakoniwa-unity-drone-model](https://github.com/toppers/hakoniwa-unity-drone-model)

復習＆練習も含めて、次のような順番でお試し。
1. NotePC(Intel Core i7 12th)にUbuntu22.04.05LTSをクリーンインストール.
   - Python 3.10 (ubuntu 22.04.LTSに含まれるそのまま)
2. UnityでbuildしたアプリとPX4を連携させて、ドローン飛行.
3. Unity Editorを使用してニューヨークを飛行.
4. Unity Editorを使用して3Dデータを東尋坊に入れ替えて飛行.

Mini PC : Intel Core i7 16G (GPUはCPU内蔵のみ)
Game Controller : Dual Shock

参考: [「箱庭ドローンシミュレータ PX4利用編」がCore i5 8th/8G、PS5コントローラで動いた！](https://qiita.com/yamati-kz/items/854af0ab31a7200c01d3)

[箱庭ドローンシミュレータでニューヨーク上空を飛べた！ (PS5 Controller DualSense使用)](https://qiita.com/yamati-kz/items/43bf7269dcdf4a898cd9)

<HR>
以下、おぼえがき。

```
$ sudo apt install gcc g++ make cmake -y
$ sudo apt install git jq libgtest-dev net-tools python3-dev python3-pip  -y
```
```
# 1.3. 箱庭ドローンシミュレータのセットアップ
$ mkdir ~/work
$ cd ~/work
$ git clone --recursive https://github.com/toppers/hakoniwa-px4sim.git
$ git clone --recursive https://github.com/toppers/hakoniwa-unity-drone-model.git
```
```
# 1.3.1. 箱庭コア機能用のコマンドインストール
$ cd ~/work/hakoniwa-px4sim
$ cd hakoniwa/third-party/hakoniwa-core-cpp-client
$ bash build.bash
$ bash install.bash
```
```
1.3.2. 箱庭コア機能のビルド
$ cd ~/work/hakoniwa-px4sim/hakoniwa
$ bash build.bash
```
```
# 1.3.2.1. 箱庭コア機能のビルド確認 : cmake-build/src/hako-px4sim
$ ls cmake-build/src/hako-px4sim
```
```
# 1.3.3. 箱庭コア機能のインストール
$ cd ~/work/hakoniwa-px4sim/hakoniwa
$ bash install.bash
```
```
# インストール結果の確認を。すべてがOKであること。
$ bash third-party/hakoniwa-core-cpp-client/hako-setup-check.bash
```
```
# 1.3.4. 環境変数の設定 マニュアルはVIだけどコマンドラインで
$ echo 'export LD_LIBRARY_PATH=/usr/local/lib/hakoniwa:$LD_LIBRARY_PATH' >> ~/.bashrc
$ echo 'export PATH=/usr/local/bin/hakoniwa:$PATH' >> ~/.bashrc
$ echo 'export PYTHONPATH=/usr/local/lib/hakoniwa/py:${PYTHONPATH}' >> ~/.bashrc
$ source ~/.bashrc
```
```
# 1.3.5. 箱庭ドローン Unityアプリのセットアップ
# コマンドラインでやってみた。
$ cd ~/Downloads
$ wget https://github.com/toppers/hakoniwa-unity-drone-model/releases/download/v2.8.0/DroneAppLinux.zip
$ cd ~/work/hakoniwa-unity-drone-model
$ unzip ~/Downloads/DroneAppLinux.zip
```
```
# 1.3.6. フライトコントローラ PX4のビルド
$ cd ~/work/hakoniwa-px4sim/px4
$ cp hakoniwa-apps/10016_none_iris PX4-Autopilot/ROMFS/px4fmu_common/init.d-posix/airframes/10016_none_iris 
$ cp hakoniwa-apps/rcS PX4-Autopilot/ROMFS/px4fmu_common/init.d-posix/rcS
```
```
# 1.3.6.2 PX4のビルド
$ cd PX4-Autopilot
$ bash Tools/setup/ubuntu.sh --no-nuttx --no-sim-tools
$ make px4_sitl_default
```
```
1.3.7. QGC(QGroundContorl)ソフトウェアのインストール
# 事前準備
$ sudo usermod -a -G dialout $USER
$ sudo apt-get remove modemmanager -y
$ sudo apt install gstreamer1.0-plugins-bad gstreamer1.0-libav gstreamer1.0-gl -y
$ sudo apt install libfuse2 -y
$ sudo apt install libxcb-xinerama0 libxkbcommon-x11-0 libxcb-cursor-dev -y
```
```
# QGroundControlのダウンロード.
$ cd ~/Downloads
$ wget https://d176tv9ibo4jno.cloudfront.net/latest/QGroundControl.AppImage
$ cd ~/work
$ mkdir qgc
$ cd qgc
$ mv ~/Downloads/QGroundControl.AppImage .
# 起動できたら問題なし
$ chmod +x ./QGroundControl.AppImage
$ ./QGroundControl.AppImage
# ctrl-c で終了
```
|     |
|:---:|
| ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/f2ad1410-8242-84ce-547a-17e0b28ec463.png) |
| Fig. QGroundControl インストール直後の最初の起動画面 |

- Terminal-1 〜 Terminal-4を実行してUnityアプリでの飛行

```
# 1.4. 箱庭ドローンシミュレータの動作確認
1.4.1. PX4の起動
# terminal-1
$ cd ~/work/hakoniwa-px4sim/px4/PX4-Autopilot
$ bash ../sim/simstart.bash
```
```
# 1.4.2. 箱庭コア機能の起動
# terminal-2
$ cd ~/work/hakoniwa-px4sim/hakoniwa
$ bash run.bash
```
```
# 1.4.3. Unityアプリの起動
# terminal-3
$ cd ~/work/hakoniwa-unity-drone-model
$ bash ./plugin/activate_app.bash DroneAppLinux
```
```
#1.5.2. 
# terminal-4
$ cd ~/work/qgc
$ ./QGroundControl.AppImage
```
- QGCの設定. インストール直後にすぐ実施するといいかも.

```
# 1.5. QGCとPX4の接続と動作確認
# 1.5.1. IPアドレスの確認
$ ifconfig
```
|     |
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/637a2e00-4f17-2403-cdd0-be9f85e8b085.png)|
|Fig. IPアドレスの確認|
```
1.5.2. QGCの事前設定
$ cd ~/work/qgc
$ ./QGroundControl.AppImage
```
あとは手順書通り。

設定Windowのデフォルト値がSerialなのでUDPへ変更する。
Automatically Connect on Startにチェックを忘れずに.

||
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/95c1d7af-b602-15ca-1236-691563642424.png)|
|Fig. QGroundControl Application Setting|

[Install the Unity Hub on Linux](https://docs.unity3d.com/hub/manual/InstallHub.html#install-hub-linux) を参考にUnity Hub 及び Unity Editorを入れていく.

```
# unity install
$ sudo apt update
$ wget -qO - https://hub.unity3d.com/linux/keys/public | gpg --dearmor | sudo tee /usr/share/keyrings/Unity_Technologies_ApS.gpg > /dev/null
$ sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/Unity_Technologies_ApS.gpg] https://hub.unity3d.com/linux/repos/deb stable main" > /etc/apt/sources.list.d/unityhub.list'
$ sudo apt update
$ sudo apt install unityhub
$ reboot
```
|<left>addをクリック!|
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/0a59558b-0aa4-3026-3e34-1992d7f08fb4.png)|
|fig. Projects Add|

- ~/work/hakoniwa-unity-drone-model\plugin\plugin-srcs を Add Projectする。

|~/work/hakoniwa-unity-drone-model\plugin\plugin-srcs|
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/5cfeb5e7-6eb0-cfed-4cfd-ac38dd56ef26.png)|
|Fig. Add project|

| |
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/fe5c0221-91a3-3069-5a62-3e78efc4b482.png)|
| fig. Unity Hub|

- 上記の三角マークをクリック後

|  　 |
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/a79acea4-c6ce-01c1-52d1-20648753f8f2.png)|
| fig. install 2022 3.55f1 |

|     |
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/3833bd2c-106d-5773-fe82-1326e6db12c4.png)|
| fig install |

|     |
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/c1e4d2f8-a8f6-6cbe-bf71-159c4ca449af.png)|
| fig. open "plugin-srcs"|

|     |
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/fb890b3a-6ffb-5f56-3b06-3b2c3aecb58c.png)|
| fig. ontinue |

|   |
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/204620cc-bfb4-e85c-735c-e735cbb211d1.png) |
| fig. igrore|

|     |
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/866df0d4-822c-45cd-b6da-fa2668ddb924.png)|
|fig. 起動画面|

- consoleのエラーの解消する。

|     |
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/685df7bc-4918-c28b-525d-5adca2c8578f.png)
| fig. open Package manager |

|Add pacakge from git URL...|
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/46c64786-ce4e-58dc-75e2-0ec2967b437d.png)|
|Fig. Add pacakge from git URL...|

|     |
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/22728a03-7478-0453-85a6-829df861d8ca.png)|
|fig. input "com.unity.nuget.newtonsoft-json", add |


https://qiita.com/yamati-kz/items/d5b1e4e443bfc4f19418
```
$ cd ~/Downloads
$ wget https://github.com/toppers/hakoniwa-unity-drone-model/releases/download/v2.8.0/FlightOverTojinboSummer.zip
$ cd work
$ unzip ~/Downloads/FlightOverTojinboSummer.zip -d ./FlightOverTojinboSummer
```

[Unity asset store](https://assetstore.unity.com/) から 
[Real New York City Vol. 2](https://assetstore.unity.com/packages/3d/environments/urban/real-new-york-city-vol-2-222827)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/f6d57c6f-6cf6-9df6-ed06-eb92b54ed743.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/de6a4f9c-bd07-8bea-cf11-16d120873996.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/d7649ab5-a0b9-42b5-e28b-182b5e24209e.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/42b31a9e-3e2e-b273-4e5d-35fecdb6eb1d.png)


以下でいいのかな？
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/b0ec4dd8-3003-ba4c-a2cc-97f73ad26b3a.png)

- import
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/93b263a3-e9e1-360a-f961-0d235a4d6280.png)


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/c8a9f671-ce5b-2054-ca13-bf3b097f0873.png)

消えないときここをさいど見直す。
[toppers / hakoniwa-unity-drone-model](https://github.com/toppers/hakoniwa-unity-drone-model) の中の
[Unityエディタを利用する場合](https://github.com/toppers/hakoniwa-unity-drone-model/blob/main/README-ja.md#unity%E3%82%A8%E3%83%87%E3%82%A3%E3%82%BF%E3%82%92%E5%88%A9%E7%94%A8%E3%81%99%E3%82%8B%E5%A0%B4%E5%90%88)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/9bd6f52d-65ac-53ad-adbb-4b6516b17207.png)


https://github.com/toppers/hakoniwa-unity-simasset-plugin
```
# これまでのインストール手順でclone済み
# git clone --recursive https://github.com/toppers/hakoniwa-unity-drone-model.git
unity editorのエラーを消す
$ cd ~/work/hakoniwa-unity-drone-model/
$ bash install.bash
```

参照 : [PX4と連携可能なUnityで作られたドローンシミュレータ](https://github.com/toppers/hakoniwa-unity-drone-model/blob/main/README-ja.md)

今回は
```
$ cd ~/work
$ git clone --recursive https://github.com/toppers/hakoniwa-unity-drone-model.git
$ cd hakoniwa-unity-drone-model
$ bash install.bash 
```

Unity Editor起動後にconsolを開くとエラーが出てるので、解消する。再度見直す。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/353e9a50-8361-286e-dc6a-cb5c5ed5ab2f.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/78413974-ba74-4a3f-5f6d-a4158db0eb05.png)

#Pakage Manegerを開き、com.unity.nuget.newtonsoft-jsonをAddする。
Unity Editor操作:Window -> Package Manager　-> + -> Add pacakge from git URL...
![Screenshot from 2024-08-24 06-07-45.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/3fb46c9e-003a-9c68-eda7-f48156c7a5ac.png)

com.unity.nuget.newtonsoft-jsonをAddする。
![Screenshot from 2024-08-24 06-10-31.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/b1dbae6d-3874-3a74-95f7-a0eb1f64b331.png)

NO_USE_GRPC を Apply
Edit -> Project Settings -> Player -> Other Settings -> Script Compilation
-> + NO_USE_GRPC(inoutする) -> Apply
!![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/37c70eb7-a583-f15d-cf92-17cc5ff79b0d.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/16443dec-b67a-00bb-5420-df7b4c558a0a.png)

- 一旦閉じる

エラーが消えないので追加で必要・重要
```
# これまでのインストール手順でclone済み
# git clone --recursive https://github.com/toppers/hakoniwa-unity-drone-model.git
unity editorのエラーを消す
$ cd ~/work/hakoniwa-unity-drone-model/
$ bash install.bash
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/01ce1bb4-ef40-a383-51f7-839a30601c8a.png)

|更に再起動するとwarningもなくなる|
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/15abe99a-e0a3-4cb0-e0e3-59bce6f3cc25.png)|
|Fig.|

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/36290f8c-ee86-6b36-5037-68d5b6923f00.png)

|Drag and Drop|
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/0c86820b-d405-bf3c-ac76-898f19f23b79.png)|
|Fig. 3Dデータをヒエラルキーへコピー|

|x = 600, y = -40, z = -370|
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/5a1892cc-2bec-808a-f180-0ffb6e30ecde.png)|
|Fig. |

|Window -> Hakoniwa -> Generate|
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/fa97f38e-f8cc-7eb4-3297-cddf3e89bfd4.png)|
|Fig. Generate|

|     |
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/853929fd-e5b0-410e-36da-e1a4c506872e.png)|
| Fig. Double-click on ApiDemo | 

|     |
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/071baac0-ed62-a13f-936d-fb118da1cc4d.png)|
| Fig. |

- Window -> Hakoniwa -> Generate

|   |
|:---:|
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/501c2808-263a-1375-2026-e7964c6efce0.png)
|fig.Generte|

|   |
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/3b209849-1818-91c3-f9e6-05558a950875.png)|
|fig. Click play button ( after Ganarate )

- 実際に飛ばしてみる

```
# Terminal-1
# 1.4.1. PX4の起動
$ cd ~/work/hakoniwa-px4sim/px4/PX4-Autopilot
$ bash ../sim/simstart.bash
```
```
# Terminal-2
# 1.4.2. 箱庭コア機能の起動
$ cd ~/work/hakoniwa-px4sim/hakoniwa
$ bash run.bash
```
```
# 1.4.3. Unityアプリの起動 -> Unity Editorを起動
# STARTボタンが押せない時 (Unity Editor内のPlay:右三角をクリックし緑色に変わった)
```
```
# Terminal-3
# 1.5.3. QGCとPX4の動作確認
$ cd ~/work/qgc
$ ./QGroundControl.AppImage
```

||
|:----:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/85de37a7-fc45-a759-9e6f-7e69dde87c50.png)
|fig takeoff|

||
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/35f62b16-dffe-779f-cd5e-42d2448e9043.png)|
|Fig. Takeoff from ground hold position. slide or hold spaceber| 

|  |
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/c016494c-6131-ece4-2b0b-460f6006fb8f.png)|
| Fig. |


参照 : [ニューヨーク3Dモデルの統合方法](https://qiita.com/kanetugu2018/items/11f9c90aa2711eba5f7f#%E3%83%8B%E3%83%A5%E3%83%BC%E3%83%A8%E3%83%BC%E3%82%AF3d%E3%83%A2%E3%83%87%E3%83%AB%E3%81%AE%E7%B5%B1%E5%90%88%E6%96%B9%E6%B3%95)

- 東尋坊の3Dデータを取り込み、NewYorkのデータと差し替える。

まず、東尋坊3Dデータを落とす。
```
$ cd ~/Downloads
$ wget https://github.com/toppers/hakoniwa-unity-drone-model/releases/download/v2.8.0/FlightOverTojinboSummer.zip
$ cd ~/work
$ unzip ~/Downloads/FlightOverTojinboSummer.zip -d FlightOverTojinboSummer
```

||
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/6e867256-9cfe-8c75-4804-a7f00a1843cb.png)|
|Fig. Assets配下に東尋坊3Dデータの格納場所の確保|

||
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/050e3ff9-defb-9440-c52f-c65e92c85459.png)|
|Fig. 東尋坊3Dデータをunity editor(Project)へインポート|

||
|:--:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/a3b31520-59db-b734-0b08-2192d381e824.png)|
|Fig . select files|

| hierarchy の下へ|
|:---:|
|![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3089345/a2793c37-e7d2-e0e1-e097-5e84fc4edfa6.png)|
|Fig Drag & Drop|

- NewYorkと同じ手順で飛ばせばOK!!