# 6. Mapping Courses ## 6.1 URDF Model Introduction ### 6.1.1 URDF Model Introduction The Unified Robot Description Format (URDF) is an XML file format widely used in ROS (`Robot Operating System`) to comprehensively describe all components of a robot. Robots are typically composed of multiple `links` and `joints`. A `link` is defined as a rigid object with certain physical properties, while a `joint` connects two `links` and constrains their relative motion. By connecting `links` with `joints` and imposing motion restrictions, a kinematic model is formed. The `URDF` file specifies the relationships between `joints` and `links`, their inertial properties, geometric characteristics, and collision models. ### 6.1.2 Comparison between Xacro and URDF Models The `URDF` model serves as a description file for simple robot models, offering a clear and easily understandable structure. However, when it comes to describing complex robot structures, using `URDF` alone can result in lengthy and unclear descriptions. To address this limitation, the `xacro` model extends the capabilities of `URDF` while maintaining its core features. The `xacro` format provides a more advanced approach to describe robot structures. It greatly improves code reusability and helps avoid excessive description length. For instance, when describing the two legs of a humanoid robot, the `URDF` model would require separate descriptions for each leg. On the other hand, the `xacro` model allows for describing a single leg and reusing that description for the other leg, resulting in a more concise and efficient representation.
### 6.1.3 Basic Syntax of URDF Model * **XML Basic Syntax** The `URDF` model is written using `XML` standard. (1) Elements: An `element` can be defined as desired using the following formula: ```xml
(1) `
The following tags will be used to write `joint` motion.
(1) `
* **gazebo Tag**
This tag is used in conjunction with the `Gazebo` simulator. Within this tag, you can define simulation parameters and import `Gazebo plugins`, as well as specify `Gazebo's` physical `properties`, and more.
* **Write Simple URDF Model**
(1) Name the model of the robot
To start writing the `URDF` model, we need to set the `name` of the robot following this format: `"
(2) Set links
① To write the first `link` and use indentation to indicate that it is part of the currently set model. Set the `name` of the `link` using the following format: `""`. Finally, conclude with `""` to indicate the successful completion of the `link` definition.
② Write the `link` description and use indentation to indicate that it is part of the currently set `link`, and conclude with `"
③ The `"
④ The `"
⑤ The `"
(3) Set joint
① To write the first `joint`, use indentation to indicate that the `joint` belongs to the current model being set. Then, specify the `name` and `type` of the `joint` as follows: `"
② Write the description section for the connection between the `link` and the `joint`. Use indentation to indicate that it is part of the currently defined `joint`. The `parent parameter` and `child parameter` should be set using the following format: `"
③ `"
④ `"
⑤ `"
⑥ `"
The complete codes are as below.
## 6.2 ROS Robot URDF Model Instruction
### 6.2.1 Getting Ready
To grasp the `URDF` model, check out [6.1.3 Basic Syntax of URDF Model](#anchor_6_1_3) for the key syntax. This part quickly breaks down the robot model code and its components.
### 6.2.2 Check Code of Robot Model
(1) Start the robot, and access the robot system desktop using `VNC`.
(2) Click-on
to create a command-line terminal.
(3) Run the command and hit Enter to disable the app auto-start service.
```bash
~/.stop_ros.sh
```
(4) Execute the command and hit Enter key to navigate to the folder containing startup programs.
```bash
colcon_cd mentorpi_description
```
(5) Execute the command to navigate to the robot simulation model folder.
```bash
cd urdf
```
(6) Execute the command to access the robot simulation model folder.
```bash
vim mentorpi.xacro
```
(7) Locate the code below:
Determine the current form of the robot by reading the machine variable to load the corresponding `URDF` model.
Multiple `URDF` models are called to compose a complete robot.
| **File Name** | **Device** |
|:---:|:---:|
| `inertial_matrix` | Inertia matrix |
| `tank` | Tank chassis `urdf` model |
After experiencing the `Lidar` game, you can activate the app service either by using a command or restarting the robot. If the app service is not activated, related app functions will be disabled. In the case of a robot restart, the app service will start automatically.
Click
and enter the command. Press enter to start the app, and wait for the buzzer to beep.
:::{Note}
Please enter the command in the system path, not in the `Docker` container.
:::
```bash
sudo systemctl restart start_node.service
```
### 6.2.3 Brief Analysis of Robot's Main Body Model
Open a new command terminal. Enter the command to access the robot model file, which contains the description of each part of the robot model.
```bash
vim tank.xacro
```
```xml
Some of the light waves will reflect back to the receiver of the `lidar`, and then, according to the principle of `laser ranging`, the distance from the `lidar` to the target point can be obtained.
Regarding `point clouds`: In simple terms, the surrounding environment information obtained by `lidar` is called a `point cloud`. It reflects a portion of what the **'eyes'** of the robot can see in the environment where it is located. The object information collected presents a series of scattered, accurate angle, and distance information.
(2) `Matching`: Matching the `point cloud data` of the current local environment with the established map to find the corresponding position.
(3) `Map Fusion`: Integrating new round data from the `lidar` into the original map, ultimately completing the map update.
### 6.3.3 Notes
(1) Begin the `mapping process` by positioning the robot in front of a straight wall or within an enclosed box. This enhances the `Lidar's` capacity to capture a higher density of scanning points.
(2) Initiate a 360-degree scan of the environment using the `Lidar` to ensure a comprehensive survey of the surroundings. This step is crucial to guarantee the accuracy and completeness of the resulting map.
(3) For larger areas, it's recommended to complete a full `mapping loop` before focusing on scanning smaller environmental details. This approach enhances the overall efficiency and precision of the `mapping process`.
### 6.3.4 Judge Mapping Result
Finally, assess the robot's `navigation process` against the following criteria once the `mapping` is complete:
(1) Ensure that the edges of obstacles within the map are distinctly defined.
(2) Check for any disparities between the map and the actual environment, such as the presence of `closed loops` or `inconsistencies`.
(3) Verify the absence of `gray areas` within the robot's motion area, indicating areas that haven't been adequately scanned.
(4) Confirm that the map doesn't incorporate obstacles that won't exist during subsequent `localization`.
(5) Validate the `map's coverage` of the entire extent of the robot's motion area.
## 6.4 slam_toolbox Mapping Algorithm
### 6.4.1 Algorithm Definition
`Slam Toolbox` software package combines information from `laser rangefinders` in the form of `LaserScan messages` and performs `TF transformation` from `odom`-> `base link` to create a `two-dimensional map` of space. This software package allows for fully serialized reloadable data and `pose graphs` of `SLAM maps`, used for `continuous mapping`, `localization`, `merging`, or other operations. It allows `Slam Toolbox` to operate in synchronous (i.e., processing all valid sensor measurements regardless of delay) and asynchronous (i.e., processing valid sensor measurements whenever possible) modes.
`ROS` replaces functionalities like `gmapping`, `cartographer`, `karto`, and `hector`, providing comprehensive `SLAM functionality` built upon the powerful `scan matcher` at the core of `Karto`, widely used and accelerated for this package. It also introduces a new `optimization plugin` based on `Google Ceres`. Additionally, it introduces a new `localization method` called `'elastic pose-graph localization,'` which takes measured `sliding windows` and adds them to the graph for `optimization` and `refinement`. This allows for tracking changes in `local features` of the environment instead of considering them as `biases`, and removes these redundant `nodes` when leaving an area without affecting the `long-term map`.
`Slam Toolbox` is a suite of tools for `2D Slam`, including:
(1) `Mapping`, saving map `pgm files`.
(2) `Map refinement`, `remapping`, or continuing `mapping` on saved maps.
(3) `Long-term mapping`: loading saved maps to continue `mapping` while removing irrelevant information from new `laser point clouds`.
(4) `Optimizing positioning mode` on existing maps. `Localization mode` can also be run without `mapping` using the `'laser odometry' mode`.
(5) `Synchronous`, `asynchronous mapping`.
(6) `Dynamic map merging`.
(7) `Plugin-based optimization solver`, with a new `optimization plugin` based on `Google Ceres`.
(8) `Interactive RVIZ plugin`.
(9) `RVIZ graphical manipulation tools` for manipulating `nodes` and `connections` during `mapping`.
(10) `Map serialization` and `lossless data storage`.
**KARTO:**
`Karto_SLAM` is a `graph-based SLAM algorithm` that uses highly optimized and non-iterative `Cholesky decomposition` to solve sparse systems. It represents the map using a `graph structure`, where each `node` corresponds to a robot `pose` along its trajectory along with associated `sensor data`. Every time a new `node` is added, the system recalculates and updates the `graph`.
The `ROS version` of `Karto_SLAM` includes components such as `Sparse Pose Adjustment` (`SPA`) for `scan matching` and `loop closure detection`.
The more `landmarks` present, the greater the `memory usage`. However, compared to other methods, `graph-based optimization` performs better in `large-scale environments` because it focuses solely on the `pose graph` (i.e., robot positions). Once the `poses` are optimized, the map is then generated based on those `poses`.
The `algorithm framework` for `Karto_SLAM` is illustrated in the diagram below:
From the above diagram, it can be seen that the process is relatively straightforward. The traditional `soft real-time operation mechanism` of `slam` involves processing each frame of data upon entry and then returning.
Relevant source code and `WIKI` links for `KartoSLAM`:
* **KartoSLAM ROS Wiki:**
(2) Click on
to open the virtual machine.
(3) Enter the `virtual machine interface` and click on `"Open Virtual Machine"`.
(4) Select the image file provided in this path [Virtual Host->Virtual Machine Image Preconfigured with ROS Environment->HiWonder_ros2_humble (for Mapping and Navigation)](resources_download.md).
For detailed steps on the import operation, please refer to [5. Depth Camera Basics Course: Configuring and Testing with ROS2 on Linux](https://docs.hiwonder.com/projects/Aurora930-Pro/en/latest/docs/2.Configuration_and_Usage_in_ROS.html#depth-camera-configuration-usage-in-ros2).
(5) After the import is completed, follow the prompts to complete the installation process.
* **Copy Robot Files to the Virtual Machine**
(1) Export files from the robot
① Start the robot, and access the robot system desktop using `VNC`.
② Click-on
to open the `ROS2 command-line terminal`.
③ Execute the command to disable the app auto-start service.
```bash
~/.stop_ros.sh
```
④ Enter the command to navigate to the `ros2_ws/src/` directory:
```bash
cd ros2_ws/src/
```
⑤ Enter the command to package the three files `'navigation'`, `'slam'`, and `'simulations'` into a compressed file:
```bash
zip -r src.zip navigation slam simulations
```
⑥ Enter the command to move the compressed file to the shared directory:
```bash
mv src.zip /home/ubuntu/shared
```
⑦ Run the command and return to `ros2_ws` directory:
```bash
cd ..
```
⑧ Enter the command to view the `.typerc` file in this directory:
```bash
ls -a
```
⑨ Enter the command to move the `.typerc` file to the shared directory:
```bash
cp .typerc /home/ubuntu/shared
```
⑩ Click-on
to open the file directory, then navigate to the `home/pi/docker/tmp` directory:
⑪ Use the shortcut `'Ctrl + H'` to show the hidden `.typerc` file:
⑫ Refer to the instruction in [6.6 Install WinSCP](#anchor_6_6) to install and open the `WinSCP` tool.
⑬ Enter the `IP address` of `Raspberry Pi` `"192.168.149.1"`, `username` `"pi"`, and `password` `"raspberrypi"`. Then click `"Login"`.
⑭ Click `"Accept"` on the pop-up window that appears.
⑮ The left side shows the directory of your computer, and the right side shows the `root directory` of `Raspberry Pi`.
⑯ Select the folder `"docker->tmp"` and open the `shared folder` with `Docker`.
⑰ In the bottom right corner of `WinSCP`, there is a number indicating the number of hidden files. Double-click on it to display hidden files.
⑱ Select the `"src"` folder and right-click to choose `"Download"`.
⑲ Select the path where you want to save the file and click `"OK"`.
⑳ Follow step ⑱ to save the `.typerc` file.
(2) Import files into the virtual machine
① Click-on
to navigate to the home directory.
② Drag and drop the `.typerc` and `src.zip` files from the computer desktop into the `virtual machine`:
* **Create and Compile the Workspace**
(1) Click-on
to start the virtual machine command-line terminal.
(2) Enter the command to create the `ros2_ws/src` directory:
```bash
mkdir -p ros2_ws/src
```
(3) Extract the file [src.zip](resources_download.md) to the directory `"home/ubuntu"`.
```bash
unzip src.zip
```
(4) Move the `simulations`, `slam`, and `navigation` files to the `ros2_ws/src` directory:
```bash
mv simulations slam navigation /home/ubuntu/ros2_ws/src/
```
(5) Move the `.typerc` file to the `ros2_ws` directory:
```bash
mv .typerc ros2_ws/
```
(6) Navigate to the `ros2_ws` directory:
```bash
cd ros2_ws/
```
(7) Enter the command to check if `.typerc` has been moved to the `ros2_ws` directory:
```bash
ls -a
```
(8) Input the following command to compile the workspace:
```bash
colcon build
```
(9) Change the `.bashrc` file, and input the following command:
```bash
gedit ~/.bashrc
```
(10) Copy the following content to the `.bashrc` file:
```bash
source /home/ubuntu/ros2_ws/.typerc
source /home/ubuntu/ros2_ws/install/setup.bash
```
(11) After finishing writing, use the shortcut `Ctrl + S` or click the `Save` button in the upper right corner to save and exit.
(12) Run the command below to refresh the environment configuration.
```bash
source ~/.bashrc
```
* **Set the Robot to LAN Mode**
:::{Note}
* When `MentorPi` starts in `LAN mode`, it will first try to connect to a pre-configured network. During this process, the `LED2` on the `Raspberry Pi expansion board` will blink rapidly, indicating that it is searching for the target network. If it fails to find the network after three attempts, `MentorPi` will automatically switch to `Direct Connection mode` (`LED2` blinks slowly), allowing you to connect to the built-in hotspot.
* If you've set `MentorPi` to connect to a specific device's hotspot in `LAN mode`, but that device becomes temporarily unavailable, you can use another device instead. Just make sure the new device uses the same hotspot name and password—`MentorPi` will then be able to connect to it automatically.
* In `LAN mode`, `MentorPi` does not create its own hotspot.
:::
(1) Click-on
to open the command line terminal.
(2) Enter the command to open the `WiFi configuration file`.
```bash
vim hiwonder-toolbox/wifi_conf.py
```
(3) Press `"i"` key to enter the `editing mode`. Change the `WiFi configuration file` to `LAN mode`, and modify your own `WiFi name` and `password`.
:::{Note}
When `MentorPi` fails to find the target network in `LAN mode` and automatically switches to `Direct Connection mode`, the configuration in `wifi_conf.py` will not be updated. It will still contain the `LAN mode settings`. If you don't manually update this file, `MentorPi` will continue to boot in `LAN mode` by default the next time it starts.
:::
(4) After finishing writing, press `"Esc"` and enter the command to save and exit.
```bash
:wq
```
(5) Enter the command to restart the network, or reboot the system. It is recommended to reboot the system.
```bash
sudo systemctl restart wifi.service
```
(6) Refer to [1. Getting Ready->1.6 App Control](1.getting_ready.md#app-control) to obtain the robot's `LAN IP address` through the app connection.
(7) It is important to note that the `virtual machine` and the robot are connected to the same `LAN`, and their `IP addresses` should be within the same `subnet`:
① `Virtual Machine`:
② `Robot`:
(8) After rebooting, click on
to open the robot's `ROS2 command line terminal`. You will notice that the `ROS_DOMAIN_ID` is `0`, which is the same as on the `virtual machine`.
① `Robot`:
② `Virtual Machine`:
### 6.4.3 slam Mapping Instructions
* **Robot operations**
(1) Click-on
to open the command-line terminal.
(2) Execute the following command to disable the app auto-start service.
```bash
~/.stop_ros.sh
```
(3) Run the command to initiate mapping:
```bash
ros2 launch slam slam.launch.py
```
* **Virtual Machine instructions**
(1) Click-on
to open the command line terminal of the `virtual machine system`.
(2) Enter the command to open the `RViz` tool and display the `mapping results`.
```bash
ros2 launch slam rviz_slam.launch.py
```
* **Enable keyboard control**
(1) Click-on
to open the command-line terminal.
(2) Enter the command to start the `keyboard control node`, and press `"Enter"`.
```bash
ros2 launch peripherals teleop_key_control.launch.py
```
If you see the prompt as shown in the following image, it means the `keyboard control service` has been successfully started.
(3) Control the robot to move in the current space to build a more complete map. The table below lists the keyboard keys available for controlling robot movement and their corresponding functions:
| **Key** | **Robot Action** |
|:---:|:---:|
| W | Short press to switch to forward state and continuously move forward |
| S | Short press to switch to backward state and continuously move backward |
| A | Long press to interrupt the forward or backward state and turn left |
| D | Long press to interrupt the forward or backward state and turn right |
(4) When controlling the robot's movement with the keyboard to map, it's advisable to reduce the robot's `movement speed` appropriately. The slower the robot's speed, the smaller the `odometry relative error`, leading to better `mapping results`. As the robot moves, the map displayed in `RVIZ` will continuously expand until the entire `environment scene's map construction` is completed.
### 6.4.4 Save Map
(1) Click-on
to open the command-line terminal.
(2) Run the following command to save the map.
```bash
cd ~/ros2_ws/src/slam/maps && ros2 run nav2_map_server map_saver_cli -f "map_01" --ros-args -p map_subscribe_transient_local:=true
```
(3) If you want to exit the game, press `"Ctrl+C"` in the `terminal interface`.
After experiencing the game, you can enable the app service through commands or by restarting the robot. If the app is not enabled, the related app functions will not work. If the robot is restarted, the app will be automatically enabled.
Click
and enter the command. Press enter to start the app, and wait for the buzzer to beep.
:::{Note}
Please enter the command in the system path, not in the `Docker container`.
:::
```bash
sudo systemctl restart start_node.service
```
### 6.4.5 Effect Optimization
If you desire a more precise `mapping outcome`, optimizing the `odometry` can be beneficial. `Mapping` with the robot requires the use of `odometry`, which in turn relies on the `IMU`.
The robot itself comes with `pre-calibrated IMU data` loaded, enabling it to perform `mapping` and `navigation functions` effectively. However, calibrating the `IMU` can still enhance accuracy further. The `calibration method` and steps for the `IMU` can be found in the [3. Motion Control Courses->3.2 IMU, Linear Velocity and Angular Velocity Calibration](3.motion_control_courses.md#imu-linear-velocity-and-angular-velocity-calibration) section.
### 6.4.6 Parameter Explanation
The parameter file can be found in the `ros2_ws\src\slam\config\slam.yaml` directory.
For more detailed information about the parameters, please refer to the official documentation:
The `launch file` is located at:
[/home/ubuntu/ros2_ws/src/slam/launch/slam.launch.py](../_static/source_code/slam.zip)
* **Import Library**
The `launch library` can be explored in detail in the official `ROS` documentation:
`RTAB-VSLAM` software package link:
on the system desktop to open the command-line terminal.
(2) Run the command to disable the app auto-start service:
```bash
~/.stop_ros.sh
```
(3) Execute the command to start mapping:
```bash
ros2 launch slam rtabmap_slam.launch.py
```
* **Virtual Machine Operation**
(1) Click-on
to open the command-line terminal.
(2) Enter the command to open the `RViz` tool and display the `mapping effect`:
```bash
ros2 launch slam rviz_rtabmap.launch.py
```
* **Enable Keyboard Control**
(1) Click-on
to open the command-line terminal.
(2) Enter the command to start the `keyboard control node`, and press `"Enter"`.
```bash
ros2 launch peripherals teleop_key_control.launch.py
```
If you encounter the prompt as shown in the figure below, it means that the `keyboard control service` has been successfully activated.
(3) Control the robot to move in the current space to build a more complete map. The table below shows the keyboard keys available for controlling robot movement and their corresponding functions:
| **Key** | **Robot Action** |
|:---:|:---:|
| `W` | Short press to switch to the forward state and continuously move forward |
| `S` | Short press to switch to the backward state and continuously move backward |
| `A` | Long press to interrupt the forward or backward state and turn left |
| `D` | Long press to interrupt the forward or backward state and turn right |
When controlling the robot's movement for `mapping` using the keyboard, it's advisable to appropriately reduce the robot's `movement speed`. The smaller the robot's `running speed`, the smaller the `relative error` of the `odometry`, resulting in a better `mapping effect`. As the robot moves, the map displayed in `RVIZ` will continuously expand until the entire `environmental scene's map construction` is completed.
* **Map Saving**
After `mapping` is completed, you can use the shortcut `"Ctrl+C"` in each `command-line terminal window` to close the currently running program.
After experiencing the game, you can enable the app service through commands or by restarting the robot. If the app is not enabled, the related app functions will not work. If the robot is restarted, the app will be automatically enabled.
Click
and enter the command. Press enter to start the app, and wait for the buzzer to beep.
:::{Note}
Please enter the command in the system path, not in the `Docker container`.
:::
```bash
sudo systemctl restart start_node.service
```
:::{Note}
For `3D mapping`, there's no need to manually save the map. When you use `"Ctrl+C"` to close the `mapping command`, the map will be automatically saved.
:::
* **Launch File Analysis**
[Source Code](../_static/source_code/slam.zip)
The `launch file` is located at:
[/home/ubuntu/ros2_ws/src/slam/launch/rtabmap_slam.launch.py](../_static/source_code/slam.zip)
(1) Import Library:
You can refer to the `ROS official documentation` for detailed analysis of the `launch library`:
(2) Click `"Next"`.
(3) Click `"Next"`.
(4) Keep the default installation location and click `"Install"`.
(5) After waiting for a moment, a prompt window will appear indicating that the installation is complete. Click `"Finish"`.
(6) After the installation is completed, click
to open it.