# PX4 Installation and setup
PX4 Firmware -
https://docs.px4.io/main/en/getting_started/px4_basic_concepts.html
## MAVROS
#### **NOTE**: Follow all the steps for successful installation. First the binary installation and then source installation needs to be done.
You can also follow [this](https://www.youtube.com/watch?v=jBTikChu02E&list=PLYy2pGCdhu7xEaNN8krzAKxv74L1mD4OV&index=5) video.
### BINARY INSTALLATION
>
sudo apt-get install ros-noetic-mavros ros-noetic-mavros-extras ros-noetic-mavros-msgs
>
wget https://raw.githubusercontent.com/mavlink/mavros/master/mavros/scripts/install_geographiclib_datasets.sh
`sudo bash ./install_geographiclib_datasets.sh`
### SOURCE INSTALLATION
`rosinstall_generator --rosdistro kinetic mavlink | tee /tmp/mavros.rosinstall`
Create a new catkin workspace **outside your original one**.
`mkdir catkin_ws_2`
`cd ~/catkin_ws_2`
`catkin init`
`wstool init src`
`sudo apt-get install python-catkin-tools python-rosinstall-generator -y`
`rosinstall_generator --rosdistro noetic mavlink | tee /tmp/mavros.rosinstall`
`rosinstall_generator --upstream mavros | tee -a /tmp/mavros.rosinstall`
`wstool merge -t src /tmp/mavros.rosinstall`
`wstool update -t src -j4`
> After running the above command, if any error occurs which says update failed to some package/folder, then remove the respective folder from workspace, go to [ROS repos](http://repositories.ros.org/status_page/ros_kinetic_default.html?q=mavlink) and clone the latest repos of those packages.
`rosdep install --from-paths src --ignore-src -y`
> At this moment if an error related to something version, is encountered then refer [this](https://discuss.px4.io/t/package-version-version-does-not-follow-version-conventions/30520)
`sudo ./src/mavros/mavros/scripts/install_geographiclib_datasets.sh`
`catkin build`
### PX4 Environment Setup
Reference [Video](https://www.youtube.com/watch?v=OtValQdAdrU&list=PLYy2pGCdhu7xEaNN8krzAKxv74L1mD4OV&index=4)
Steps -
Clone the repo outside your catkin workspace
`git clone https://github.com/PX4/Firmware.git --recursive`
`cd Firmware/`
`bash ./Tools/setup/ubuntu.sh`
`sudo reboot now`
`make px4_sitl gazebo-classic`
### MAVSDK Installation
>
wget https://github.com/mavlink/MAVSDK/releases/download/v0.37.0/mavsdk_0.37.0_ubuntu20.04_amd64.deb
`sudo dpkg -i mavsdk_0.37.0_ubuntu20.04_amd64.deb`
Now Install **QGroundControl** software from [here](https://docs.qgroundcontrol.com/master/en/qgc-user-guide/getting_started/download_and_install.html)
`sudo apt-get update`
`sudo apt-get install build-essential cmake git`
Clone this repo in the catkin_ws containing mavros
`cd catkin_ws_2/src`
`git clone https://github.com/mavlink/MAVSDK.git`
`cd MAVSDK`
`git submodule update --init --recursive`
`cmake -DCMAKE_BUILD_TYPE=Debug -Bbuild/default -H.`
`cmake --build build/default -j8`
`cmake -Bbuild/default -DCMAKE_BUILD_TYPE=Release -H.`
`cmake --build build/default -j8`
`sudo cmake --build build/default --target install`
#### Run an example mission in Gazebo
In a new terminal
`cd Firmware`
`make px4_sitl gazebo-classic`
In another terminal, run QGroundControl
`cd <path to the folder which contains the QGroundControl.AppImage>`
`./QGroundControl.AppImage`
Now in a third terminal
`cd catkin_ws_2/src/MAVSDK/examples/fly_mission`
`cmake -Bbuild -H.`
`cmake --build build -j4`
`build/fly_mission udp://:14540`
### MAVSDK-Python Setup
`sudo apt update`
`pip3 install mavsdk`
Clone this repo in the directory where you have installed MAVSDK initially
`git clone https://github.com/mavlink/MAVSDK-Python.git`
#### Run an example mission
In a new terminal
`cd Firmware`
`make px4_sitl gazebo-classic`
In another terminal, run QGroundControl
`./QGroundControl.AppImage`
In third terminal
`cd catkin_ws_2/src/MAVSDK-Python/examples/`
`python mission.py`
### Launching Gazebo classic with ROS
Edit the .bashrc file for configuring the px4 environment
`gedit .bashrc`
Add these lines to the end of the script
`px4_dir=~/Firmware`
>
source $px4_dir/Tools/simulation/gazebo-classic/setup_gazebo.bash $px4_dir $px4_dir/build/px4_sitl_default
`export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:$px4_dir`
>
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:$px4_dir/Tools/simulation/gazebo-classic/sitl_gazebo-classic
>
export GAZEBO_PLUGIN_PATH=$GAZEBO_PLUGIN_PATH:/usr/lib/x86_64-linux-gnu/gazebo-11/plugins
> If it gives error related to some gazebo or ros plugin, then
`sudo apt-get install -y libgazebo11-dev`
Clone [this](https://github.com/ros-simulation/gazebo_ros_pkgs/tree/noetic-devel) repo in your catkin_ws, do catkin_make and then replace the gazebo plugin path in the last line with the path of the gazebo_plugins folder of this repo.
>
GAZEBO_PLUGIN_PATH=$GAZEBO_PLUGIN_PATH:~/catkin_ws/build/gazebo_ros_pkgs/gazebo_plugins
Open a new terminal -
`roslaunch px4 mavros_posix_sitl.launch`