# Ardupilot

## 韌體編譯

### 0. 前置條件

作業系統為Ubuntu或Windows WSL

### 1. 構建 Ardupilot 開發環境

Ardupilot是一個開源的飛控韌體，主要用於無人機和遙控飛行器。以下是構建 Ardupilot開發環境的步驟：

### 2. 複製Ardupilot Github儲存庫

1. 開啟終端。
2. 執行以下命令以複製 Ardupilot 儲存庫：

   ```bash
   git clone --recurse-submodules https://github.com/ArduPilot/ardupilot.git
   ```
3. 進入儲存庫目錄：

   ```bash
   cd ardupilot
   ```
4. 執行Ardupilot開發環境配置腳本，等待安裝完成：

   <pre class="language-bash"><code class="lang-bash"><strong>Tools/environment_install/install-prereqs-ubuntu.sh -y
   </strong></code></pre>

   <figure><img src="https://160136817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEhjsOwJXR5yBqExlT0G0%2Fuploads%2F5nhZbacqYHCO96xTO6yu%2Fimage.png?alt=media&#x26;token=651aad56-89c3-4409-9b6e-85b7854d554d" alt=""><figcaption></figcaption></figure>
5. 重新讀取路徑：

   ```
   . ~/.profile
   ```
6. 登出後再登入以儲存設定

### 3. 編譯 Ardupilot 韌體

1. 先將工作目錄切換到Ardupilot儲存庫中

   <pre><code><strong>cd ardupilot
   </strong></code></pre>

2. 執行以下命令進行韌體編譯：

   <pre class="language-bash"><code class="lang-bash"><strong>./waf configure --board &#x3C;Board Name, Ex: MATEKF405AIO>
   </strong></code></pre>

   *將* Board Name*替換為飛控板的具體名稱。*

   <figure><img src="https://160136817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEhjsOwJXR5yBqExlT0G0%2Fuploads%2F05ysXn20acCbRb84V1sD%2Fimage.png?alt=media&#x26;token=57a09771-a2bb-4e90-ad55-e1e19042ddff" alt=""><figcaption></figcaption></figure>

   <pre><code><strong>./waf copter # 如果要編譯的是arducopter
   </strong></code></pre>

3. 編譯成功後會出現以下訊息

   <figure><img src="https://160136817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEhjsOwJXR5yBqExlT0G0%2Fuploads%2FlqVlooe94dM49uV1KQu1%2Fimage.png?alt=media&#x26;token=cb882d50-694c-4655-953c-60dea1ee836d" alt=""><figcaption></figcaption></figure>

4. 編譯的韌體位置在build/\<Board Name>/bin底下，包含僅含Ardupilot韌體檔案(ardupilot.bin, ardupilot.apj)與包含bootloader的Ardupilot韌體檔案(ardupilot\_with\_bl.hex)

   <figure><img src="https://160136817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEhjsOwJXR5yBqExlT0G0%2Fuploads%2FHH6Zttq9oM9Ta4gmzj9A%2Fimage.png?alt=media&#x26;token=f8738956-8a62-41a0-88a9-2a4201c1edf1" alt=""><figcaption></figcaption></figure>

## 韌體燒錄

燒錄Ardupilot韌體目前提供兩種方法

{% tabs %}
{% tab title="Mission Planner" %}
**此方法應用於已安裝PX4或Ardupilot的韌體**

1. 飛控板使用USB連接到電腦。
2. 開啟Mission Planner，開啟初始配置，**注意**不需要按下連線

   <figure><img src="https://160136817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEhjsOwJXR5yBqExlT0G0%2Fuploads%2FP9coPSAq9CbJT8rWxbed%2Fimage.png?alt=media&#x26;token=6f69be27-6743-4d0e-bdd6-844ed2eb612c" alt=""><figcaption></figcaption></figure>
3. 按下想要燒錄的韌體(例如Copter)，按下Yes

   <figure><img src="https://160136817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEhjsOwJXR5yBqExlT0G0%2Fuploads%2FnVtyvLWj5pxI2z94lY2D%2Fimage.png?alt=media&#x26;token=3cec59d0-760d-434b-9e07-e5e4a26045cf" alt=""><figcaption></figcaption></figure>
4. 燒錄中

   <figure><img src="https://160136817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEhjsOwJXR5yBqExlT0G0%2Fuploads%2FaEaBmQZMDj8R1Fekxpd8%2Fimage.png?alt=media&#x26;token=4bcb1245-1db3-4ba0-b5d1-45b90327fb2f" alt=""><figcaption></figcaption></figure>
5. 燒錄完成

   <figure><img src="https://160136817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEhjsOwJXR5yBqExlT0G0%2Fuploads%2F6k7XrBslHO0R1fijlnAJ%2Fimage.png?alt=media&#x26;token=399b5b35-2cb9-4cba-8bd1-edda275daeeb" alt=""><figcaption></figcaption></figure>
6. 按下右上角連線，回到飛行數據，有姿態以及飛行數據表示燒錄完成

   <figure><img src="https://160136817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEhjsOwJXR5yBqExlT0G0%2Fuploads%2FEXTCwTRnh8BklQPUyoR6%2Fimage.png?alt=media&#x26;token=44c8e687-6a68-4c53-aa23-1d479a7243ba" alt=""><figcaption></figcaption></figure>

{% endtab %}

{% tab title="STM32CubeProgrammer " %}
**此方法可以應用於原先安裝Betaflight或其他非PX4與Ardupilot的韌體**

1. 先下載並安裝STM32CubeProgrammer，[下載來源](https://www.st.com/en/development-tools/stm32cubeprog.html)

2. 飛控板開啟DFU模式，USB 連接到電腦。

3. 開啟STM32CubeProgrammer，選擇USB並按下Connect，連線到飛控板

   <figure><img src="https://160136817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEhjsOwJXR5yBqExlT0G0%2Fuploads%2F3dP5X2OEpZdPVafizR8l%2Fimage.png?alt=media&#x26;token=08a189f6-f9f1-4683-a5c7-24124e0cf310" alt=""><figcaption></figcaption></figure>

4. 按下Open file，開啟包含bootloader的檔案

   <figure><img src="https://160136817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEhjsOwJXR5yBqExlT0G0%2Fuploads%2FJlLMAFP5BMppS1Q2VnVG%2Fimage.png?alt=media&#x26;token=19b7335f-80d0-413a-b8d0-687c5bd6a326" alt=""><figcaption></figcaption></figure>

   <figure><img src="https://160136817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEhjsOwJXR5yBqExlT0G0%2Fuploads%2FXie8M5goVet43t6vIkl2%2Fimage.png?alt=media&#x26;token=e9c9dd8b-41cf-4649-b68c-18fe8a5639bb" alt=""><figcaption></figcaption></figure>

5. 下載/燒錄韌體到飛控板上

   <figure><img src="https://160136817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEhjsOwJXR5yBqExlT0G0%2Fuploads%2FOjny4CjDVEFcYCLvtChp%2Fimage.png?alt=media&#x26;token=b1dddf30-a0f5-452e-874e-75ef2179e076" alt=""><figcaption></figcaption></figure>

6. 燒錄中

   <figure><img src="https://160136817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEhjsOwJXR5yBqExlT0G0%2Fuploads%2FstENUXh69t1nO809TVlg%2Fimage.png?alt=media&#x26;token=d2b3110c-8047-435d-be1a-96e4dc40c1b9" alt=""><figcaption></figcaption></figure>

7. 完成下載/燒錄

8. 燒錄完成

   <figure><img src="https://160136817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEhjsOwJXR5yBqExlT0G0%2Fuploads%2FLItbYpFEgzrbclvmexWa%2Fimage.png?alt=media&#x26;token=8d9794bb-ae66-4736-8c8b-0b37d247a0b2" alt=""><figcaption></figcaption></figure>

9. 開啟Mission Planner，連線到飛控

   <figure><img src="https://160136817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEhjsOwJXR5yBqExlT0G0%2Fuploads%2F9DiDZj5htxjIHWUVAiHE%2Fimage.png?alt=media&#x26;token=4fd60f80-ee80-4fce-a607-e15ec8178dce" alt=""><figcaption></figcaption></figure>

{% endtab %}

{% tab title="QGroundControl" %}

{% endtab %}
{% endtabs %}

***

## 如何編譯自定義的飛控板

1. 確認已經成功配置Ardupilot開發環境
2. 移動到ardupilot/library/AP\_HAL\_ChibiOS/hwdef中，複製**已經存在**的飛控板定義，重新命名新飛控板名稱的資料夾，此處以Morakot為例。

   <figure><img src="https://160136817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEhjsOwJXR5yBqExlT0G0%2Fuploads%2Fy1elkAWC3zypAZroFuJF%2Fimage.png?alt=media&#x26;token=b45763f3-06fb-4541-85db-f50a31e7d672" alt=""><figcaption></figcaption></figure>
3. 修改hwdef.dat(不含Bootloader的版本)與hwdef-bl.dat(含Bootloader的版本)

   <figure><img src="https://160136817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEhjsOwJXR5yBqExlT0G0%2Fuploads%2FUVUQ8gVClckBuFqrBR83%2Fimage.png?alt=media&#x26;token=618c662c-c656-40b9-8ca3-197e10acc2d1" alt=""><figcaption></figcaption></figure>
4. 修改其中的內容，根據實際板子所使用的腳位定義與接線，撰寫自己的飛控板配置

   <figure><img src="https://160136817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEhjsOwJXR5yBqExlT0G0%2Fuploads%2FF9mkWwEsxVuoHKc8rsiN%2Fimage.png?alt=media&#x26;token=05d53e15-f396-4f70-a973-e8a8fa07d296" alt=""><figcaption></figcaption></figure>
5. 修改完畢後，開啟終端機，切換到ardupilot儲存庫的資料夾
6. 首先，編譯新飛控板的Bootloader，**此步驟不可跳過**。：

   `./Tools/scripts/build_bootloaders.py <Board Name>`

   <figure><img src="https://160136817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEhjsOwJXR5yBqExlT0G0%2Fuploads%2F0zrlkmVgYjGP278Oc42X%2Fimage.png?alt=media&#x26;token=25df3916-79c3-4840-8be3-61f3cf77e9c2" alt=""><figcaption></figcaption></figure>
7. 接著配置Ardupilot韌體編譯設定，此時還不會編譯，**此步驟不可跳過。**

   `./waf configure --board <Board Name>`

   <figure><img src="https://160136817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEhjsOwJXR5yBqExlT0G0%2Fuploads%2FNpXXpKty2pQiWfJCoMeO%2Fimage.png?alt=media&#x26;token=347614e3-149c-4ea8-bfc1-e8ebf927d7c4" alt=""><figcaption></figcaption></figure>
8. 接著編譯Ardupilot韌體，**此步驟不可跳過。**

   `./waf copter`

   <figure><img src="https://160136817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEhjsOwJXR5yBqExlT0G0%2Fuploads%2F865iUy3foE4fNQBEfKFB%2Fimage.png?alt=media&#x26;token=855b3fa5-1928-4925-827f-17289f3969c7" alt=""><figcaption></figcaption></figure>
9. 編譯完成的Ardupilot韌體將會位於ardupilot/build/\<Board Name>/bin裡面

   <figure><img src="https://160136817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEhjsOwJXR5yBqExlT0G0%2Fuploads%2FDOg0yck3YGHnhHUsJ2fZ%2Fimage.png?alt=media&#x26;token=b73a32fb-dbb8-456a-af2f-afae67db8724" alt=""><figcaption></figcaption></figure>
