###### tags: `ROSKY2` `ROS2` `Jetson Nano` `reComputer-J1010` `lidar detection`
# Ch 5: ROSKY2 - SLAM and Gmapping
In this note, user will use package ["slam_gmapping"](https://github.com/Adlink-ROS/slam_gmapping)(thanks Adlink for providing) to build map for navigation.
## Hardware user have to prepare before doing...
1. [ROSKY2](https://www.icshop.com.tw/product-page.php?28598)
> **Note**
> Navigation2 just can be used on Ubuntu 20.04
3. A host machine with the operating system is Window 10(above) or Ubuntu.
---
## Some things user should know before operating...
Please check the note and documentation below:
- [CH 1. ROSKY2 - remotely log in](https://hackmd.io/@weichih-lin/ROSKY2_log_in_remotely)
- [CH 2. ROSKY2 - Keyboard teleop](https://hackmd.io/@weichih-lin/ROSKY2_keyboard_teleop)
Now let’s go ahead to build map.
---
## Step 1. Open Terminator in ROSKY2 through remote desktop
### Step 1-1. Open terminal on the remote desktop
Because of the keyboard shortcut keys in NoMachine, user can not use the keyboard keys 【ctrl】+【alt】+【T】to open the terminal quickly.
Please right-click the mouse on the remote desktop to open the menu item and then choose【Open Terminal】to open terminal.
Once user see the roe_menu is shown on the terminal, please choose【2】to start the operating environment for ROSKY2.

### Step 1-2. Open terminator on the remote desktop
Terminator is a nice terminal emulator, please check the [Terminator’s documentation](https://terminator-gtk3.readthedocs.io/en/latest/) to know more detail.
User can type the command command to start terminator:
```bash=
$ terminator
```
Once start the terminator successfully, user can see ros_menu is shown on the terminator. Like the operation in [step 1-1](#Step-1-1-Open-terminal-on-the-remote-desktop), user have to choose [2] to start the environment for ROSKY2.

More keyboard shortcut in the terminator user can find on the [Terminator’s documentation - Using the keyboard](https://terminator-gtk3.readthedocs.io/en/latest/gettingstarted.html#using-the-keyboard).
## Step 2. Start the communication with all sensors, motor control board and more on ROSKY2
Now user just running the system on the SBC and it can not send/receive data from sensor and more. Hence, user have to start the communication with these to send/receive data.
> **Note**
> Seneors on ROSKY2 will start running after typing the command below. Please make sure **no more things** result in stopping these sensor work, such as **something is on the LiDAR**, **transfer cable connect between host machine and ROSKY2** and more.
Please type the command below in the terminator to start the communication:
```bash=
$ ros2 launch rosky2_bringup bringup.launch.py
```

## Step 3. Start using gmapping to build map
Awesome! User start the communication between ROSKY2 and sensors. Now user can build map through topics published above.
From [Step 1-2 in this note](#Step-1-2-Open-terminator-on-the-remote-desktop), user can know that use keyboard shortcut 【ctrl】+【shift】+【E】 in terminator to split terminal vertically. Next the same thing is choosing 【2】 to start the environment for ROSKY2 after user opening ros_menu successfully.

Now is ready to build map, please type the command below in terminal to launch the file "slam_gmapping.launch.py"
```bash=
$ ros2 launch slam_gmapping slam_gmapping.launch.py
```

## Step 4. Visualize mapping in Rviz2
After launching file "slam_gmapping.launch.py" successfully, user can use [Rviz2](https://turtlebot.github.io/turtlebot4-user-manual/software/rviz.html) to watch the mapping progress.
### Step 4-1. Running Rviz2
From [Step 1-2 in this note](#Step-1-2-Open-terminator-on-the-remote-desktop), user can know that use keyboard shortcut 【ctrl】+【shift】+【O】 in terminator to split terminal horizontally. Next the same thing is choosing 【2】 to start the environment for ROSKY2 after user opening ros_menu successfully.

Please type command below in the terminal wait for receiving command to run Rviz2:
```bash=
$ rviz2
```

And then user can see the window RViz:

### Step 4-2. Display information in Rviz2
There is noting in the default window. User can add some displays to watch the information they want. But in this step, user have to add these displays below:
- Map: Displays an occupancy grid on the ground plane from a nav_msgs::OccupancyGrid. [Please check here to know more information.](http://www.ros.org/wiki/rviz/DisplayTypes/Map)
- TF: Displays the TF transform hierarchy. [Please check here to know more information.](http://www.ros.org/wiki/rviz/DisplayTypes/TF)
- LaserScan: Displays the data from a sensor_msgs::LaserScan message as points in the world, drawn as points, billboards, or cubes. [Please check here to know more information.](http://www.ros.org/wiki/rviz/DisplayTypes/LaserScan)
Here is the example to add Map. Please click the button 【Add】buttom left, and then user can see the small window 【rviz2】for creating visualization:
<img src="https://i.imgur.com/0L6vpWh.png" width=400 alt="Click button 【Add】buttom left" title="Click button 【Add】buttom left"><br/>
Rviz2 have to subscribe the topic "/map" to visualize map. Hence, please click the label 【By topic】top and then choose 【Map】 in the topic "/map":
<img src="https://i.imgur.com/xGWM5d5.png" width=400 alt="Choose 【Map】" title="Choose 【Map】"><br/>
After checking and clicking the button 【OK】down right, user can see the map on Rviz2:

Nice! But where is the ROSKY2? User can add TF to check it location, and user have to notice:
> **Note**
> If user want to add TF to display, just click the label 【By display type】and choose 【TF】.

How about the laser? User can add LaserScan by the label 【By topic】, but user have to know:
> **Note**
> The [QOS](https://docs.ros.org/en/rolling/Concepts/About-Quality-of-Service-Settings.html#qos-compatibilities) of topic /scan is "Best effort". Hence, please choose "Besto Effort" in the topic /scan and then user can see red points on the map:
>

### Step 4-3. teleop ROSKY2 to build map
Please check the note [CH 2. ROSKY2 - Keyboard teleop](https://hackmd.io/@weichih-lin/ROSKY2_keyboard_teleop) to teleop ROSKY2. After moving ROSKY2, user can build map through gmapping.
## Step 5. Save map through Navigation2
After building map, user can save map through Navigation2. Navigation2 provides package "nav2_map_server" to help user to manipulate map. Please type the command below to save map user build:
```bash=
$ ros2 run nav2_map_server map_saver_cli -f [map_dir]/[map_name] --ros-args -p save_map_timeout:=10000
```
And
- [map_dir]: the directory that user want to save map. For this example will use "~/ROSKY2/ros2_ws/src/slam_gmapping/slam_gmapping/map"
- [map_name]: the name that user want to use. For this example will use "test20220830"
According the examples metion above, the full command is shown below(the window bottom right):

Finally, user can find the map in the directory:

## Step 6. Shutdown all running node.
If user finished saving map, please remember to shutdown all running node through the shortcut key 【ctrl】+【C】 in terminal.
## Next
To be continued...
I plan to do navigation with ROSKY2 through Navigation2.
Please have fun to use ROSKY2 :)