# Gazebo Simulation
> [name=江廷威 (twc+contact@aleeas.com)]
## Intro
Gazebo 是一個 simulation 軟體,可以用來模擬現實中的物理特性如重力、摩擦力和碰撞等,並且可以跟 ros/ros2 整合。我們可以在 simulator 中測試和驗證演算法的效果和AUV在不同任務上的表現。由於場地的限制(缺乏道具及大型泳池)、機構及硬體組需要時間產出等因素,造成軟體組無法有效地同步開發以及測試。使用 simulator 能夠加快我們測試以及驗證的流程,並且幫助我們及早將軟體組的各個部份,如視覺、控制、感測等進行整合。
{%youtube jII8SlZvBcM%}
## Getting started
1. (recommended) Setup [docker container](https://github.com/NCTU-AUV/orca_docker)
2. Launch Gazebo
```shell
gazebo --verbose
```
## Build your models
建議看這組[教學影片](https://www.youtube.com/playlist?list=PLNw2RD-1J5YYvFGiMafRD_axHrBUGvuIg),會比較了解gazebo的基本操作方式
### Model Editor
In a Gazebo world, everything you see is a model. A model is composed of links and joints. Links are the parts of the model, and joints are used to connect these parts together.

#### Link
A link can be simple shape or custom mesh imported by user.
##### Simple Shapes
Some default shapes provided in gazebo.

##### Custom Shapes
Import 3D mesh (.dae, .obj, .stl). We can use [Blender](https://www.blender.org/) to designed the 3D mesh.

##### Link Inspector
We can modify the attributes of a link using link inspector, including the position, orientation, size, visual, and collision. Note that visual and collision are two completely different fields, we should apply what we have changed for the visual attributes to the collision attributes.

#### Joint
A joint is used to connect two links. There are many types of joints(fixed, revolute, ...)

##### Joint Inspector
Joint Inspector is used to modify the attributes of a joint, including the type of a joint, parent and child links the joint is connected to, and the relative position of the two links.

### Building Editor
[Building editor](https://classic.gazebosim.org/tutorials?cat=build_world&tut=building_editor) is useful for creating wall, floor, and stairs. However, unlike model editor, the model built by the building editor can not be modified after created.

## Simulation
Clone the repo and follow `README.md` to build the package
```shell
git clone https://github.com/NCTU-AUV/sauvc_sim
```
Lauch SAUVC simulation environment
```shell
ros2 launch sauvc_sim sauvc_launch.py
```
Use keyboard to control the AUV
```shell
ros2 run sauvc_sim teleop.py
```
### Gazebo with ROS2
ros2 topic list
```
/sauvc_sim/bottom_camera/camera_info
/sauvc_sim/bottom_camera/image_raw
/sauvc_sim/front_camera/camera_info
/sauvc_sim/front_camera/depth/camera_info
/sauvc_sim/front_camera/depth/image_raw
/sauvc_sim/front_camera/image_raw
/sauvc_sim/front_camera/points
/sauvc_sim/link_states
/sauvc_sim/model_states
```
ros2 service list
```
...
/sauvc_sim/gazebo_ros_state/describe_parameters
/sauvc_sim/gazebo_ros_state/get_parameter_types
/sauvc_sim/gazebo_ros_state/get_parameters
/sauvc_sim/gazebo_ros_state/list_parameters
/sauvc_sim/gazebo_ros_state/set_parameters
/sauvc_sim/gazebo_ros_state/set_parameters_atomically
/sauvc_sim/get_entity_state
/sauvc_sim/plugin_name/describe_parameters
/sauvc_sim/plugin_name/get_parameter_types
/sauvc_sim/plugin_name/get_parameters
/sauvc_sim/plugin_name/list_parameters
/sauvc_sim/plugin_name/set_parameters
/sauvc_sim/plugin_name/set_parameters_atomically
/sauvc_sim/set_camera_info
/sauvc_sim/set_entity_state
/sauvc_sim/stereo_camera_plugin/describe_parameters
/sauvc_sim/stereo_camera_plugin/get_parameter_types
/sauvc_sim/stereo_camera_plugin/get_parameters
/sauvc_sim/stereo_camera_plugin/list_parameters
/sauvc_sim/stereo_camera_plugin/set_parameters
/sauvc_sim/stereo_camera_plugin/set_parameters_atomically
...
```
## Useful materials
- https://github.com/ros-simulation/gazebo_ros_pkgs/wiki
- https://classic.gazebosim.org/tutorials?cat=connect_ros