---
# System prepended metadata

title: LSTM Anomaly Detection

---

# LSTM Anomaly Detection
線上HackMD連結:https://hackmd.io/@BgZWbLVqSAao47vGhbNIgA/BJiqAkYlY
## ROS robot arm simulation
### Prerequisites
* Ubuntu 16.04
* ROS kinetic
* Python 2.7
* MoveIt

### Ros environment setup
ROS是用宜慧提供的Yaskawa手臂模擬環境，可用以下兩種方法建置
#### Method 1
可以直接用宜慧提供的Virturebox的環境，將~/Trajectory_anomaly_detection/ROS/ 中的檔案全部移動到ROS環境中專案的src/即可。

#### Method 2
也可使用我提供的ROS環境，目前已放在雲端中的~/Trajectory_anomaly_detection/src資料夾中
1.安裝ROS kinetic
http://wiki.ros.org/kinetic/Installation/Ubuntu
2.創建一新工作環境
```c
$ mkdir aoi_flexbe_ws_sg
$ cd aoi_flexbe_ws_sg
$ mkdir src
$ catkin_make
```
3.資料移動
將原始環境中src裡的檔案除了CMakeFile.list全部移動到新環境的src裡

4.安裝moveit
```c
$ rosdep update
$ sudo apt-get update
$ sudo apt-get dist-upgrade
$ sudo apt-get install ros-kinetic-catkin python-catkin-tools
$ sudo apt install ros-kinetic-moveit
```

5.補齊缺少package
```c
$ chmod 777 -R aoi_flexbe_ws_sg
$ cd aoi_flexbe_ws_sg
$ rosdep install --from-paths src -i -y
$ catkin_make -j1  
```
基本到這應該就可以使用，若有catkin_make時仍有缺少librarie可能需要根據缺少內容去下載。

### Open the simulation environment
```c
$ cd /aoi_flexbe_ws_sg
$ source ./devel/setup.bash
$ roslaunch aoi_moveit_config demo.launch 
```

### Generation trajectory plan
Generate certain number trajectories with various velocity(fast/normal/slow),and output trajectories with plan.yaml file
> Parameter:
> -n : number of trajectories
> -v : velocity {fast,normal,slow}
```python
# open a new terminal
$ cd /aoi_flexbe_ws_sg
$ source ./devel/setup.bash
$ cd /src
$ python generate_trajectory_plan.py -cl -n 50 -v normal
```

### Generate Tampered plan
Generate tampered trajectories based on plan.yaml,and output tampered trajectories with spoof.yaml file
```python
# open a new terminal
$ cd /aoi_flexbe_ws_sg
$ source ./devel/setup.bash
$ cd /src
$ python spoofing_plan.py
```
### Excute normal plan:
1.Excute normal trajectories
```python
# open a new terminal
$ cd /aoi_flexbe_ws_sg
$ source ./devel/setup.bash
$ cd /src
$ python excute_plan.py -cl -f plan
```

2.Excute tampered trajectories
```python
# open a new terminal
$ cd /aoi_flexbe_ws_sg
$ source ./devel/setup.bash
$ cd /src
$ python excute_plan.py -cl -f spoof
```

### Collect robot info
First,start collecting robot infomation
```python
# open a new terminal
$ cd /aoi_flexbe_ws_sg
$ source ./devel/setup.bash
$ cd /src
$ python get_robot_info.py -cl
```
Second,excute the trajectory plan,and get_robot_info.py will ouput robot info with output.csv
```python
# open a new terminal
$ cd /aoi_flexbe_ws_sg
$ source ./devel/setup.bash
$ cd /src
$ python excute_plan.py -cl -f plan
```

## Anomaly Detection
### Prerequisites
* Python 3.6
* Pytorch
### Data
#### Data features
In csv file
> Column 1~6:Position of 6 joint angle
> Column 6~9:End effector(x,y,z position)
> Column 10:Index of trajectory

and velocity of 6 joint angle will generate when training
#### Data categories
1.Normal data
* train data(train_with_ee.csv)
* test data(test_with_ee.csv)
2.Anomaly data
* Fast trajectory(v_1_ee.csv)
* Slow trajectory(v_0.01_ee.csv)
* Tampered trajectory(spoof.csv)


### Performance
Parameters
* Hidden size: 32
* Batch size: 32
* Learning rate: 0.01
* Epochs: 20


Evaluate metrics
* FPR:(False Positive Rate) at 95% TPR (True Positive Rate)
* AUROC:Area Under the Receiver Operating Characteristic curve
* AUPR:Area under the Precision-Recall curve

Anomaly score:
* Max SE: Max square error of one trajectory
* MSE: Mean square error of one trajectory

Result:
1.Max SE
![](https://i.imgur.com/38wKhuj.png)


| Anomaly data        | FPR(%) | AUROC(%) | AUPR(%) |
|---------------------|--------|----------|---------|
| Fast trajectory     | 9.8    | 97.7     | 92.8    |
| Slow trajectory     | 4.2    | 99.1     | 99.1    |
| Tampered trajectory | 2      | 97.9     | 92.2    |

2.MSE
![](https://i.imgur.com/eWWNyCI.png)


| Anomaly data        | FPR(%) | AUROC(%) | AUPR(%) |
|---------------------|--------|----------|---------|
| Fast trajectory     | 3.4    | 97.9     | 94.5    |
| Slow trajectory     | 2.5    | 99.3     | 99.5    |
| Tampered trajectory | 2      | 97.9     | 92.2    |


## Coordination trasform

### Coordination
Ros 模擬中有三個坐標系(x axis:紅, y axis:綠, z axis:藍)
1.world:
為excel中收到end effector值的座標
![](https://i.imgur.com/8rmRwyk.png)

2.base_link:
以手臂模型基座無原點
![](https://i.imgur.com/2oBHX98.png)

3.workcell_turntable
為操作平台為座標原點，軌跡規劃中的目標點以此為座標
![](https://i.imgur.com/U0PqOBH.png)

### Trasform
兩兩坐標系之間可透過rotation matrix$(R)$與Traslation matrix$(T)$轉換
$$
\left[
 \begin{matrix}
   x  \\
   y  \\
   z 
  \end{matrix}
  \right]=
 \left[
 \begin{matrix}
  R
  \end{matrix}
  \right]
 \left[
 \begin{matrix}
   x_0  \\
   y_0  \\
   z_0 
  \end{matrix}
  \right]
  +
  \left[
 \begin{matrix}
  T
  \end{matrix}
  \right]
$$

1.從wolrd到base_link
$$
R=
 \left[
 \begin{matrix}
   0 & -1 & 0 \\
   1 & 0 & 0 \\
   0 & 0 & 1
  \end{matrix}
  \right]
$$

$$
T=
\left[
 \begin{matrix}
   0.210  \\
   0.000  \\
   -0.870 
  \end{matrix}
  \right]
$$

2.從wolrd到workcell_turntable
$$
R=
 \left[
 \begin{matrix}
   -1 & 0 & 0 \\
   0 & -1 & 0 \\
   0 & 0 & 1
  \end{matrix}
  \right]
$$

$$
T=
\left[
 \begin{matrix}
   0.006  \\
   -0.320  \\
   -1.045 
  \end{matrix}
  \right]
$$

### tf_echo
Reference:http://wiki.ros.org/tf
可透過tf_echo了解兩坐標系之間關係
```python
$ tf_echo <source_frame> <target_frame>
```
Example
```bash
$  rosrun tf tf_echo /base_link /world
At time 0.00
- Translation: [0.210, 0.000, -0.870]
- Rotation: in Quaternion [0.000, 0.000, 0.707, 0.707]
            in RPY (radian) [0.000, -0.000, 1.571]
            in RPY (degree) [0.000, -0.000, 90.000]
```