PX4 User Guide Original Version
===
[TOC]
:::info
**Official Website: [PX4 User Guide](https://docs.px4.io/main/en/)**
:::
PX4 Setting
---
### Download bootloader on laptop
* Follow the instruction: [PX4-Bootloader](https://github.com/PX4/PX4-Bootloader)
>Reference: https://hackmd.io/VYuIrmeMRSy2pcKGMvT15Q?view
### Download st-link on laptop
1. Download st-link
```shell=
git clone http://github.com/texane/stlink.git
cd stlink
mkdir build
cd build
cmake ..
make -j4
sudo make install
```
2. Copy 49-stlinkv2.rules
```shell=
cd stlink/config/udev/rules.d
sudo cp 49-stlinkv2.rules /etc/udev/rules.d/
```
>Reference: https://github.com/shengwen-tw/stm32f4-examples
### Bootloader program(only px4fmuv2_bl)
1. Connect st-link and check

2. 燒錄程式
```shell=
cd PX4-Bootloader/build/px4fmuv2_bl
st-flash write px4fmuv2_bl.bin 0x8000000
```
>Reference: https://hackmd.io/VYuIrmeMRSy2pcKGMvT15Q?view
### Download Mavros
:::info
**You can download mavros on upboard or laptop**
:::
1. Download Mavors
* Follow the instruction: [Install Mavros](https://hackmd.io/iOtCxeZtSSqi2rCyHt2dyg#Install-Mavros)
2. Test
* Connect px4 & upboard

* Command
```shell=
roslaunch mavros px4.launch
```
* Result

* Topic


3. IMU data
* Command
```shell=
rostopic echo /mavros/imu/data
```
* Result

* Frame

Implement
---
### Basic Assembly
* Follow the instruction: [無人機組裝](https://hackmd.io/E-y2BgvjTOKLia2niLHndQ)
* Additional: You can mount upboard on multicopter
* 
### Basic Configuration
#### RC sysyem
* Connection of RC system has already done by lab member
#### Xbee Moudle
* Follow the instruction: [天線設定](https://hackmd.io/TNvNXB9sQoilNvrLHINYDw#%E5%A4%A9%E7%B7%9A%E8%A8%AD%E5%AE%9A)
#### OptiTrack
* Follow the instruction: [OptiTrack](https://hackmd.io/TNvNXB9sQoilNvrLHINYDw#optiTrack)
### QGroundControl
#### Download QGroundControl(QGC)
* Follow the instruction: [QGC](https://docs.qgroundcontrol.com/master/en/getting_started/download_and_install.html#ubuntu)
#### Connection through usb
* Connection
* 
* Add a link
* Serial port & baud rate need to change according to your device.

* Interface
* Connection success, you will see the user interface.

#### Connection through WiFi
* Upboard need to use network interface card to connect wifi.
* Follow the instruction: [QGC通過mavros連接到飛控](https://blog.csdn.net/qq_38649880/article/details/88342904)
#### Configuration
* Firmware
* Follow the instruction: [Install Stable PX4](https://docs.px4.io/main/en/config/firmware.html#install-stable-px4)
* Airframe
* Choose the best fit airframe according to your multicopter. For instance, [Quadrotor-X](https://docs.px4.io/main/en/airframes/airframe_reference.html#quadrotor-x) F450 is suitable for our drone.
* Follow the instruction: [Airframe](https://docs.px4.io/main/en/config/airframe.html )

* Power moudal:
* Follow the instruction: [Battery and Power Module Setup](https://docs.px4.io/main/en/config/battery.html)

* Safety:
* Follow the instruction: [Low Battery](https://docs.px4.io/main/en/config/safety.html#low-battery-failsafe)

* Follow the instruction: [RC Loss](https://docs.px4.io/main/en/config/safety.html#rc-loss-failsafe)

* Actuators:
* Choose appropriate ==**Airframe**== by instruction above.
* Follow the instruction: [Motor Geometry](https://docs.px4.io/main/en/config/actuators.html#motor-geometry)
* Rotation and order of motor according to Quadrotor-X.


* Follow the instruction: [Actuator Outputs](https://docs.px4.io/main/en/config/actuators.html#actuator-outputs)
* It is better to choose AUX output.

* Follow the instruction: [Actuator Testing](https://docs.px4.io/main/en/config/actuators.html#actuator-testing)
* Make sure rotation of each motor is accurate before flying.

* If you have problem, check [Actuator Configuration and Testing](https://docs.px4.io/main/en/config/actuators.html#actuator-configuration-and-testing)
#### Calibration

* Compass
* Follow the instruction: [Compass Calibration](https://docs.px4.io/main/en/config/compass.html#compass-calibration)
* Gyroscope
* Follow the instruction: [Gyroscope Calibration](https://docs.px4.io/main/en/config/gyroscope.html#gyroscope-calibration)
* Accelerometer
* Follow the instruction: [Accelerometer](https://docs.px4.io/main/en/config/accelerometer.html#accelerometer)
==Above three is done, click level to update.==
* ESC
* Follow the instruction: [ESC Calibration](https://docs.px4.io/main/en/advanced_config/esc_calibration.html#esc-calibration)

#### Tuning
* Follow the instruction: [Auto-tuning](https://docs.px4.io/main/en/config/autotune.html#auto-tuning)

#### Serial Setting
* Concept: [Serial Port Configuration](https://docs.px4.io/main/en/peripherals/serial_configuration.html#serial-port-configuration)
* Instruction: [MAVLink Peripherals (GCS/OSD/Companion)](https://docs.px4.io/main/en/peripherals/mavlink_peripherals.html#mavlink-peripherals-gcs-osd-companion)
#### Parameters
:::info
**All setting can modify here**
:::

### Flying
#### Flight Mode
* Concept: [Flight Modes](https://docs.px4.io/main/en/flight_modes/)
* Configuration
* Notice, Arm switch & Emergency kill switch must setup.
* Arm switch: motor will not rotate until switch on.
* Emergency kill switch: Force motor to stop.

#### Manual Mode
{%youtube Kml9Gl0oOMA %}
#### Offboard Mode
:::danger
Test of this mode is fail, probably due to nonlinearity of multicopter, PID tune is not suffieient to control our multicopter.
:::
* Procedure
* Usage: On-board processor and wifi link to ROS
* Ground Station: Laptop(ROS + mavros)
* Protocol: MAVlink
* Commuciation medium: Wifi
* Vehicle: Upboard & PX4
Upboard with ROS & mavros, connect to PX4 through usb.

* GPS / Pose estimate
* [Using Vision or Motion Capture Systems for Position Estimation](https://docs.px4.io/main/en/ros/external_position_estimation.html#specific-system-setups)
* [基於PX4、樹莓派和Mocap(Optitrack)的室內飛行環境搭建](https://blog.csdn.net/qq_42879437/article/details/117514082?fbclid=IwAR0ST33CtXGXvtWUF2k2PrrCwiGSPqj0ZcK3VUGFmPOsyznJo8lpeypD7Yw#t14)
* Image

* Control Objective:
* Position: x、y、z
* Velocity: vx、vy、vz
* Attitude: quaternion、roll/yaw/pitch rate、thrust
* Parameter:

* Example Code:
[Official Example Code](https://github.com/PX4/PX4-Devguide/blob/master/en/ros/mavros_offboard.md)
[mavros_offboard_control](https://github.com/raaslab/mavros_offboard_control)
[offboard_control](https://github.com/TIERS/offboard-control)
###### tags: `PX4`