# PX4 sitl & Gazebo modeling guide
###### tags: `飛控`
## Install evironment
Follow the [instruction](https://docs.px4.io/master/en/dev_setup/dev_env_linux_ubuntu.html) to install px4 with gazebo .
or you can simply do:
1. Download the px4 source code:
```bash=
git clone https://github.com/PX4/PX4-Autopilot.git --recursive
```
2.Run the script:
```bash=
bash ./PX4-Autopilot/Tools/setup/ubuntu.sh
```
### testing
After the installation, there should be a 'PX4-Autopilot' folder for px4 development.
You can run terminal to test the installation.
Go to the folder:
```bash=
cd PX4-Autopilot
```
source setup:
```bash=
source Tools/setup_gazebo.bash $(pwd) $(pwd)/build/px4_sitl_default
```
run simulation with [cmake](https://zh.wikipedia.org/wiki/CMake):
```bash=
make px4_sitl_default gazebo_iris
```

## Add new model for gazebo
### step1
Models for gazebo are all in the ```PX4-Autopilot/Tools/sitl_gazebo/models``` folders. To add a new model, follow the [gazebo tutoriol](http://gazebosim.org/tutorials?cat=build_robot).
or simply modify the existed model folder.

* Model config:
```xml=
<?xml version="1.0" ?>
<model>
<name>Noah</name> //model name
<version>1.0</version>
<sdf version="1.6">Noah.sdf</sdf> //reference sdf (name should be the same as model)
<author>
<name></name>
<email></email>
</author>
<description></description>
</model>
```
To easily modify models, try the gazebo model editor(remember to save the file):

### step2
Add a world file for new model in PX4-Autopilot/Tools/sitl_gazebo/worlds folders and include new model.

```xml=
<include>
<uri>model://Noah</uri>
</include>
```
## Create new airframe for px4 sitl
1. Create [custom mixer file](https://dev.px4.io/v1.10_noredirect/en/airframes/adding_a_new_frame.html#mixer-file) under ROMFS/px4fmu_common/mixers-sitl folder.

2. add mixer file in ROMFS/px4fmu_common/mixers-sitl/CMakeList.txt.
```cmake=
px4_add_romfs_files(
delta_wing_sitl.main.mix
plane_sitl.main.mix
over_sitl.main.mix
standard_vtol_sitl.main.mix
tiltrotor_sitl.main.mix
Noah_sitl.main.mix
)
```
4. Create an [airframe file](https://dev.px4.io/v1.10_noredirect/en/airframes/adding_a_new_frame.html#config-file) under ROMFS/px4fmu_common/init.d-posix (Yet again, this can be based off the iris or solo airframe files), give it a number (for example 1043) and name it 1043_my_vehicle.

5. set the mixer file in airframe file.
```cmake=
set MIXER_FILE etc/mixers-sitl/Noah_sitl.main.mix
set MIXER custom
```
6. Add the airframe name (my_vehicle) to the file platforms/posix/cmake/sitl_target.cmake in the command set(models …

## Run and test
Go to the folder:
```bash=
cd PX4-Autopilot
```
source setup:
```bash=
source Tools/setup_gazebo.bash $(pwd) $(pwd)/build/px4_sitl_default
```
run simulation with [cmake](https://zh.wikipedia.org/wiki/CMake):
```bash=
make px4_sitl_default gazebo_Noah
```