# PX4 Development

## Ubuntu Development Environment

On a clean Ubuntu environment (e.g., 18.04 LTS), we need to&#x20;

* Run the script [ubuntu.sh](https://github.com/PX4/PX4-Autopilot/blob/master/Tools/setup/ubuntu.sh) to install PX4 related dependencies (both hardware and simulation)
  * use arg `--no-nuttx` to omit NuttX/Pixhawk tools
  * use arg `--no-sim-tools` to omit jMAVSim and Gazebo9 simulator
* Install ROS Melodic by following the [official ROS Wiki](http://wiki.ros.org/melodic/Installation/Ubuntu).
* Run the script [ubuntu\_sim\_common\_deps.sh](https://github.com/PX4/PX4-Devguide/blob/master/build_scripts/ubuntu_sim_common_deps.sh) to install common dependencies for simulation.
* Install MAVROS by following the [instructions in its official repository](https://github.com/mavlink/mavros/tree/master/mavros#source-installation).
  * we use source installation for development
  * use `sudo` for `install_geographiclib_datasets.sh`
  * `echo "source ~/catkin_ws/devel/setup.bash --extend" >> ~/.bashrc`

#### Notes

* `ubuntu.sh` is needed for both hardware and simulation
* [`ubuntu_sim_ros_melodic.sh`](https://docs.px4.io/master/en/dev_setup/dev_env_linux_ubuntu.html#bash-scripts) is not used/recommended&#x20;
  * To have more flexibility, we install three parts separately: ROS, sim dependencies, and MAVROS

## Gazebo SITL Simulation

In order to run PX4 in Gazebo simulation, we have three more steps.

* build PX4 source code to generate a Iris quadrotor model in Gazebo (i.e. the target)
* set up environment variables
* roslaunch Gazebo simulation and run offboard control program&#x20;

#### Build Iris quadrotor model in Gazebo

```bash
git clone git@github.com:UCR-Robotics/PX4-Autopilot.git --recursive
cd PX4-Autopilot
DONT_RUN=1 make px4_sitl_default gazebo
# iris.sdf will be generated in PX4-Autopilot/Tools/sitl_gazebo/models/iris
```

#### Ser up environment variables&#x20;

```bash
# may add the following to ~/.bashrc 
PX4_SRC_DIR=~/PX4-Autopilot
export GAZEBO_PLUGIN_PATH=$GAZEBO_PLUGIN_PATH:${PX4_SRC_DIR}/build/px4_sitl_default/build_gazebo
export GAZEBO_MODEL_PATH=$GAZEBO_MODEL_PATH:${PX4_SRC_DIR}/Tools/sitl_gazebo/models
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${PX4_SRC_DIR}/build/px4_sitl_default/build_gazebo
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:${PX4_SRC_DIR}
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:${PX4_SRC_DIR}/Tools/sitl_gazebo
```

#### Run Offboard Control

* launch SITL and MAVROS by [`mavros_posix_sitl.launch`](https://github.com/PX4/PX4-Autopilot/blob/master/launch/mavros_posix_sitl.launch)
  * similar to `px4.launch` or `posix_sitl.launch`
* run offboard control C++ program by `rosrun <package_name> offboard_node`&#x20;

## References

* PX4 documentation: [Ubuntu Development Environment](https://docs.px4.io/master/en/dev_setup/dev_env_linux_ubuntu.html)
  * provides two bash scripts: `ubuntu.sh` and `ubuntu_sim_ros_melodic.sh`&#x20;
  * build targets for Gazebo, jMAVSim, or NuttX
  * ROS/Gazebo and Raspberry Pi (armhf or aarch64)
* PX4 documentation: [MAVROS installation](https://docs.px4.io/master/en/ros/mavros_installation.html)
* PX4 documentation: [ROS with Gazebo Simulation](https://docs.px4.io/master/en/simulation/ros_interface.html)
  * set up environment variables
  * ROS launch files
* PX4 documentation: [MAVROS Offboard Control C++ Example](https://docs.px4.io/master/en/ros/mavros_offboard.html)
