# 17 ROS2-Lidar Course ## 17.1 Lidar Introduction ### 17.1.1 Preface Lidar, a high-precision and high-speed remote sensing technology, is pivotal in numerous domains, including map creation, autonomous driving, environmental perception, and robot navigation. This document provides an overview of Lidar's principles, components, working mechanisms, application areas, advantages, and development trends. In autonomous driving and intelligent transportation, Lidar assumes a critical role by offering real-time perception of obstacles, pedestrians, and vehicles on the road, providing precise distance and location information. In robot navigation and environmental perception, Lidar contributes by furnishing accurate maps and information about the surrounding environment. Furthermore, Lidar finds extensive applications in areas such as 3D modeling, map creation, safety monitoring, and remote sensing mapping. ### 17.1.2 Lidar Composition and Classification Lidar is composed of essential components such as a laser emitter, receiver, photodetector, scanning mechanism, and angle resolver. The laser emitter produces laser beams, while the receiver and photodetector capture reflected light signals. The scanning mechanism is responsible for surveying the surrounding environment, and the angle resolver assists in determining the position of target objects. Lidar can be categorized into various types based on the scanning method: **Rotating Lidar**: Achieves omnidirectional scanning horizontally by rotating the emitter or scanning mechanism. Known for high scanning speed and measurement accuracy, it is widely applied in areas such as autonomous driving, 3D environmental modeling, and map creation. **Solid-State Lidar**: Utilizes solid-state laser emitters, eliminating the need for rotating components. Compact, lightweight, and energy-efficient, solid-state lidar is suitable for applications like mobile devices, drones, and robots. **Mechanical Lidar**: Employs mechanical components like rotating mirrors or prisms for laser beam scanning. Although offering longer measurement distances and higher accuracy, mechanical lidar tends to have slower scanning speeds. It is commonly used in terrain measurement, building scanning, and navigation. **Phase-Modulated Lidar**: Measures the distance between target objects and the lidar by altering the phase of the laser beam. Known for high measurement accuracy and a large measurement range, phase-modulated lidar is extensively used in map creation, surveying, and industrial applications. **Flash Lidar**: Utilizes brief, high-power laser pulses to illuminate the entire scene simultaneously. It captures reflected light signals through an array of receivers, making it suitable for high-speed and high-resolution applications, including rapid scene capture and motion tracking. ### 17.1.3 Switch Lidar Version > [!NOTE] > > **Note: the following instructions are based on G4 Lidar, and is also applicable to A1 Lidar.** The robot provides two Lidar options, and the default Lidar version in the system image is G4 Lidar. Therefore, please confirm the Lidar version upon you receiving the robot kit.

A1 Lidar

G4 Lidar

1. If the Lidar you are using serves as G4 Lidar, you don’t need to change the Lidar configuration in the system image. If you are using A1 Lidar, please follow the below steps to switch the Lidar version. 2. Start the robot, and connect it to NoMachine according to the instructions provided in ‘**[1. Quick Start Guide->1.6 Establish Development Environment]()**’. 3. After successful connection, double-click . 4. Select “**A1**” Click-on Lidar TAB, and select “A1”, then click-on “Save” and “Apply” in sequence. 5. After you reselect the Lidar version, the below prompt will appear. 6. After the system completes a restart, click-on “**Exit**” icon. 7. Finally, click-on to confirm the Lidar version. ## 17.2 Lidar Working and Ranging Principle ### 17.2.1 Lidar Ranging Two common methods are employed by Lidar to determine the distance to a target: triangulation and Time of Flight (TOF). In the case of TOF, as depicted in the diagram, the Lidar initially projects light onto the object. The object reflects the light directly back to the Lidar, which calculates the time it takes for the light to return. The distance between the object and the Lidar is then obtained by multiplying this time by the speed of light. In triangulation, as illustrated in the diagram, the Lidar undergoes adjustments during manufacturing to ensure that the light does not directly strike the object. Instead, it is projected at a specific angle, a pre-set value that remains constant during operation. The distance from the object to the Lidar can be calculated by incorporating this angle into trigonometric functions. ### 17.2.2 Lidar Working Result Its working result is as follows. Lidar will emit light and shine it on the object surface. When receiving the light reflected by the object, Lidar will mark the contour of the object at the position where the light is reflected. ## 17.3 Lidar Obstacle Avoidance The robot senses the distance between itself and an object directly in front of it. Depending on the configured distance, the robot will either turn left or right to avoid obstacles. If no obstacles are detected, it will proceed to move forward. This game can be activated in two ways: either through a mobile app or by issuing commands after establishing a remote connection to the system. For instructions on app connection, please consult the tutorial located in '**[1 Quick Start Guide->1.5 APP Control]()**' 1. Start the robot, and access the robot system desktop using NoMachine according to the tutorial saved in ‘**[1 Quick Start Guide->1.6 Establish Development Environment]()**.’. 2. Click-on to open the ROS1 command line terminal. 3. Execute the command and press Enter to deactivate the automatic start service for the app. This service includes the handle and depth camera services. ```py sudo systemctl stop start_app_node.service ``` 4. Click-on to start the ROS2 command-line terminal. 5. Run the command to enable the local services for app-related game and chassis control services. ```py ros2 launch app lidar_node.launch.py ``` 6. Open a new ROS2 terminal, and execute the command, then press Enter to start the Lidar game. ```py ros2 service call /lidar_controller/enter std_srvs/srv/Trigger {} ``` 7. Enter the command and hit Enter to launch Lidar obstacle avoidance game. ```py ros2 service call /lidar_controller/set_running interfaces/srv/SetInt64 "{data: 1}" ``` 8. If you need to stop the current function, simply enter the command in the current command line terminal. ```py ros2 service call /lidar_controller/set_running interfaces/srv/SetInt64 "{data: 0}" ``` 9) To exit the game mode, simply press "Ctrl+C" in the terminal interface at step 4) or 5). ## 17.4 Lidar Following The robot monitors the distance between itself and an object directly in front. If the distance exceeds 35cm, it will track the object ahead. If the distance is less than 35cm, it will reverse. The robot automatically halts when the distance directly in front is detected as 35cm. This game can be activated in two ways: either through a mobile app or by issuing commands after establishing a remote connection to the system. For instructions on app connection, please consult the tutorial located in '**[1 Quick Start Guide/ 1.5 App Control]()**'. 1. Start the robot, and access the robot system desktop using NoMachine according to the tutorial saved in ‘**[1 Quick Start Guide/1.6 Establish Development Environment]()**.’. 2. Click-on to open the ROS1 command line terminal. 3. Execute the command and press Enter to deactivate the automatic start service for the app. This service includes the handle and depth camera services. ```py sudo systemctl stop start_app_node.service ``` 4. Click-on to start the ROS2 command-line terminal. 5. Run the command to enable the local services for app-related game and chassis control services. ```py ros2 launch app lidar_node.launch.py ``` 6. Open a new ROS2 terminal, and execute the command, then press Enter to start the Lidar game. ```py ros2 service call /lidar_controller/enter std_srvs/srv/Trigger {} ``` 7. Enter the command and hit Enter to launch Lidar following game. ```py ros2 service call /lidar_controller/set_running interfaces/srv/SetInt64 "{data: 2}" ``` 8. If you need to stop the current function, simply enter the command in the current command line terminal. ```py ros2 service call /lidar_controller/set_running interfaces/srv/SetInt64 "{data: 0}" ``` 9) To exit the game mode, simply press "**Ctrl+C**" in the terminal interface at step 4) or 5). ## 17.5 Lidar Guarding The robot assesses the distance between itself and an object directly in front. If an object is detected within the configured distance, it will initiate a turn to ensure that the vehicle consistently faces the object. This game can be activated through two methods: either via a mobile app or by issuing commands after establishing a remote connection to the system. For instructions on app connection, please consult the tutorial located in '**[1 Quick Start Guide/ 1.5 App Control]()**'. 1. Start the robot, and access the robot system desktop using NoMachine according to the tutorial saved in ‘**[1 Quick Start Guide/1.6 Establish Development Environment]()**.’. 2. Click-on to open the ROS1 command line terminal. 3. Execute the command and press Enter to deactivate the automatic start service for the app. This service includes the handle and depth camera services. ```py sudo systemctl stop start_app_node.service ``` 4. Click-on to start the ROS2 command-line terminal. 5. Run the command to enable the local services for app-related game and chassis control services. ```py ros2 launch app lidar_node.launch.py ``` 6. Open a new ROS2 terminal, and execute the command, then press Enter to start the Lidar game. ```py ros2 service call /lidar_controller/enter std_srvs/srv/Trigger {} ``` 7. Enter the command and hit Enter to launch Lidar guarding game. ```py ros2 service call /lidar_controller/set_running interfaces/srv/SetInt64 "{data: 3}" ``` 8. If you need to stop the current function, simply enter the command in the current command line terminal. ```py ros2 service call /lidar_controller/set_running interfaces/srv/SetInt64 "{data: 0}" ``` 9) To exit the game mode, simply press "**Ctrl+C**" in the terminal interface at step (4) or (5).