# AIR Planner
Codes of _Reinforcement Learning for **A**erial **I**ntelligent **R**apid Planning_, dubbed as  **AIR-Planner**

## Observation
Obs is a dict containing 2 elements: `state` and `depth`, both of which store the information of the last `min(seq_len, queue_length)` second. The max queue length is equal to `input_update_freq`. So the first dim of obs is changing at the early stage. The [0,...] element is the newest one.

Each layer \[i,:\] of state is a numpy array of size (21,), consisting of:
- \[0,3\]: positoin x, y, z 
- \[3:12\]: rotation matrix, 9 elements
- \[13:16\]: linear velocity x,y,z in body frame or world frame determined by param: velocity_frame
- \[16:19\]: body_rates
- \[19:21\]: goal direction, a vector point to goal point

Each layer \[i,:,:\] of depth is a numpy array of size (img_height,img_width)

## Action
an action consists of severial trajectories, the number of which depends on param: modes. The number of points in each traj dependes on param: out_seq_len. The action should be a numpy array of shape (mode * dim * out_seq_len), where default dim is three (x, y, z) in real world.

All the parameters mentioned above could be found in `config/yaml_files/test_traj.yaml`.

## Installation
Refer to [install.md](install.md)

### Test your installation
Before test, modiefy 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
```

## Save Envs
```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_save_sim.sh 1 # use the same display ID

# In Terminal 3 or using Tmux
start_air
./run_save.sh 1 100 # The two parameters are the environment serial numbers to start and end the save
```
The envs will be saved in `env_save/data/` folder. Before running the save script, make sure that there's no folders of env_id between the two serial numbers in `env_save/data/`.

## Training

1. First, roslaunch the simulation
```bash
source xx/devel/setup.bash
conda activate air_planner
roslaunch agile_autonomy simulation.launch
```

If you want to run simulation on a headless server, install X-server to run a virtual DISPLAY. Note that this operation will disable the actual physical display.
```bash
# install X server
sudo apt-get install xserver-xorg

# configure virtual display device for X server
# nvidia-xconfig will update xorg.conf file
sudo nvidia-xconfig -a --use-display-device=None --virtual=1280x1024

# run X server and call it display 0 (if occupied, set it to another number)
sudo /usr/bin/X :0 &

# run Unity standalone on display 0
DISPLAY=:0 roslaunch agile_autonomy simulation.launch
```

To recover the physical display, remove or rename `/etc/X11/xorg.conf` and **KILL ALL PROCESSES ABOUT VIRTUAL DISPLAY**

then run
```bash
sudo service gdm stop
sudo service gdm start
```
if the command ``sudo service gdm stop`` takes long time to execute, check process with high CPU usage and kill it.

2. Then open a new terminal, and test your environment
```
source xx/devel/setup.bash
conda activate air_planner
python test_env.py --settings_file config/yaml_files/test_traj.yaml
```

3. Since you have installed the environment successfully, enjoy your training!
```
TBA
```

