# Install from air planner
```bash
sudo apt-get update
sudo apt-get install git cmake g++

mkdir air_planner_ws && cd air_planner_ws
export AIR_PLANNER_WS_DIR=$(pwd)
echo "export AIR_PLANNER_WS_DIR"=$AIR_PLANNER_WS_DIR >> ~/.bashrc

# Install NVidia drivers, CUDA 11.2 and cuDNN
# This can be quite painful, but this tutorial helped me a lot https://yakhyo.medium.com/cuda-11-2-installation-on-ubuntu-20-04-e83f7561ccc1
# Be careful with version compatibility of CUDA and nvidia driver!

# Install Open3D v0.9.0
# only need to be installed once on one machine
git clone --recursive https://github.com/intel-isl/Open3D
cd Open3D
git checkout v0.9.0
git submodule update --init --recursive
./util/scripts/install-deps-ubuntu.sh
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/bin/cmake ..
make -j$(nproc)
sudo make install

# Install catkin
sudo apt-get install ros-noetic-catkin python3-catkin-tools

###
### NOTICE: if ros has been installed, please jump this part
###
# Install ROS Noetic
# Don't forget to configure your Ubuntu repositories to allow "restricted," "universe," and "multiverse."
sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
sudo apt install curl # if you haven't already installed curl
wget https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc 
sudo apt-key add ros.asc && rm ros.asc
sudo apt update
sudo apt install ros-noetic-desktop-full
source /opt/ros/noetic/setup.bash
echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc
source ~/.bashrc
sudo apt install python3-rosdep python3-rosinstall python3-rosinstall-generator python3-wstool build-essential
sudo apt install python3-rosdep
sudo rosdep init
rosdep update


# Install air planner
# At that moment, you must create an ssh key and bind it to your github. Otherwise, clone and submodule update commands will fail. Check https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent for details

sudo apt-get install g++-7 gcc-7 # airplanner requires old compilers for some reason
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 100
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 100

cd $AIR_PLANNER_WS_DIR
export ROS_VERSION=noetic
export CATKIN_WS=./catkin_aa
mkdir -p $CATKIN_WS/src
cd $CATKIN_WS
catkin init
catkin config --extend /opt/ros/$ROS_VERSION
catkin config --merge-devel
catkin config --cmake-args -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS=-fdiagnostics-color -DPYTHON_EXECUTABLE:FILEPATH=/usr/bin/python3 -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda
cd src

###
### NOTICE: before this step, generate your ssh key and add it to your github account (command: ssh-keygen -t rsa)
###
git clone git@github.com:thu-uav/air_planner.git
#never forget to change branch
cd air_planner
git switch dev
cd ..

sudo apt install python3-vcstool
vcs-import < air_planner/dependencies.yaml
cd air_planner
git submodule update --init --recursive

# Install extra dependencies (might need more depending on your OS)
sudo apt-get install -y libqglviewer-dev-qt5

# Install external libraries for rpg_flightmare
sudo apt install -y libzmqpp-dev libeigen3-dev libglfw3-dev libglm-dev

# Install dependencies for rpg_flightmare renderer
sudo apt install -y libvulkan1 vulkan-utils gdb

# Install other dependencies not listed in original installation guide
sudo apt-get install libsdl-dev libsdl-image1.2-dev ros-noetic-octomap ros-noetic-octomap-msgs ros-noetic-octomap-ros

# Add environment variables (Careful! Modify path according to your local setup)
# if you have already installed agile_autonomy or airplanner, just modify the path
echo 'export RPGQ_PARAM_DIR='$AIR_PLANNER_WS_DIR'/catkin_aa/src/rpg_flightmare' >> ~/.bashrc


###
### NOTICE: Before building the project, check several files in this project as described below (these files should have been modified when installing from air planner)
###
# File modifications:
# 1) catkin_aa/src/air_planner/dep/rpg_mpl_ros/open3d_conversions/CMakeLists.txt
#       -> replace line 13 with find_package(Open3D HINTS /usr/local/bin/cmake/)
# 2) catkin_aa/src/air_planner/dep/rpg_mpl_ros/planning_ros_utils/src/planning_rviz_plugins/map_display.cpp
#       -> Comment out line 39
# 3) catkin_aa/src/air_planner/dep/agile_autonomy/data_generation/traj_sampler/CMakeLists.txt
#       -> replace line 12 with find_package(Open3D HINTS /usr/local/bin/cmake/)

# (Open another terminal window and?) build the project. This may take several minutes
cd $AIR_PLANNER_WS_DIR
sudo catkin build
```
Download the simulator unity from [this link](https://zenodo.org/record/5517791/files/standalone.tar?download=1) and put it into ``src/rpg_flightmare/flight_render`` folder

```bash
# Python env
conda create --name air_planner python=3.7
conda activate air_planner

# install nvidia driver and cuda

pip install rospkg==1.2.3 pyquaternion open3d opencv-python
pip install pyqt5==5.10.1 PySide2
conda install pytorch torchvision torchaudio pytorch-cuda=11.6 -c pytorch-nightly -c nvidia
pip install wandb
pip install defusedxml
pip install rospy
# config wandb
# remember to install some packages for "onpolocy"
pip install gym
pip install absl-py



cd air_planner && pip install -e .
cd air_planner/onpolicy && pip install -e .

echo 'alias start_air="cd '$AIR_PLANNER_WS_DIR'/catkin_aa && source devel/setup.bash && cd src/air_planner/air_planner/scripts"' >> ~/.bashrc
echo 'alias build_air="cd '$AIR_PLANNER_WS_DIR'/catkin_aa && source devel/setup.bash && sudo catkin build"' >> ~/.bashrc
```


# Test the installation

Before test, check the path in file ``air_planner/config/yaml_files/test_traj.yaml``

```bash
# Assume that you have added our alias to .bashrc and sourced it

# In Terminal 1 or using Tmux
start_air
./run_virtual_display.sh 1 # change the display ID to what you need

# In Terminal 2 or using Tmux
start_air
./run_sim.sh 1 # use the same display ID

# In Terminal 3 or using Tmux
start_air
./run_env_test.sh
```



# TroubleShooting
## 1. `Project 'mpl_test_node' tried to find library 'Open3D'`
```
Errors     << mpl_test_node:cmake /home/ysa/air_planner_ws/catkin_aa/logs/mpl_test_node/build.cmake.000.log                                                                                            
CMake Error at /home/ysa/air_planner_ws/catkin_aa/devel/share/open3d_conversions/cmake/open3d_conversionsConfig.cmake:173 (message):
  Project 'mpl_test_node' tried to find library 'Open3D'.  The library is
  neither a target nor built/installed properly.  Did you compile project
  'open3d_conversions'? Did you find_package() it before the subdirectory
  containing its code is included?
Call Stack (most recent call first):
  CMakeLists.txt:27 (find_package)
```
### Solution

`/path/to/catkin_aa/devel/share/open3d_conversions/cmake/open3d_conversionsConfig.cmake` line 157，added Open3D path：foreach(**;/usr/local/bin/cmake/lib)

## 2. `No kernel image is available for execution on the device 209`

### Solution

i) Open `/path/to/catkin_aa/src/agile_autonomy/data_generation/agile_autonomy/CMakeLists.txt`, change line 14 to 
```
set(CUDA_NVCC_FLAGS
        ${CUDA_NVCC_FLAGS};
        -O3
        -arch=sm_80
        -gencode=arch=compute_80,code=sm_80
        -gencode=arch=compute_86,code=sm_86
        -gencode=arch=compute_86,code=compute_86
        #-gencode=arch=compute_52,code=sm_52
        #-gencode=arch=compute_61,code=sm_61
        #-gencode=arch=compute_62,code=sm_62
        #-gencode=arch=compute_75,code=sm_75
        )
```
ii) redo 
```
catkin config --cmake-args -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS=-fdiagnostics-color -DPYTHON_EXECUTABLE:FILEPATH=/usr/bin/python3 -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda
sudo catkin build
```