LIDAR

Gets distances to objects in the environment in a wider field of view

This page is under construction.

Basics

The Lidar returns the distances to the objects in the environment in meters, similar to the Distance Sensor, but provides 360 degrees of full coverage. The two basic properties of a Lidar in Webots are the number of layers and the horizontal resolution. The number of layers is the amount of lasers, “stacked” on top of each other, to provide vertical information in addition to horizontal. The horizontal resolution is the number of points per layer. These properties can be found using:

   
1
2
horizontalResolution = lidar.getHorizontalResolution();
numberOfLayers = lidar.getNumberofLayers();
1
2
int horizontalResolution = lidar->getHorizontalResolution();
int numberOfLayers = lidar->getNumberofLayers();

There are 4 basic steps to using this sensor:

  1. Start by importing or including the appropriate class for the lidar
  2. Retrieve the sensor by name from the robot.
  3. Enable the sensor by passing in the update rate of the sensor. (Usually the timestep)
  4. Finally, retrieve values from the sensor.

Note: The lidar relies on OpenGL rendering information to retrieve distances. As such, make sure the lidar is outside the body of the robot, so that it is not blocked by the inside of the robot.

   
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
from controller import Robot, Lidar # Step 1: Import Lidar

robot = Robot()

lidar = robot.getDevice("lidar") # Step 2: Retrieve the sensor, named "lidar", from the robot. Note that the sensor name may differ between robots.

timestep = int(robot.getBasicTimeStep())

lidar.enable(timestep) # Step 3: Enable the sensor, using the timestep as the update rate

while robot.step(timestep) != -1:
    
    rangeImage = lidar.getRangeImage(); # Step 4: Retrieve the range image
    
    # Print the first 10 values of the range image.
    # The range image stores the distances from left to right, from first to last layer
    for i in range(10):
        print(str(rangeImage[i]) + " ", end='')
        
    print('')
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

#include <webots/Robot.hpp>
#include <webots/Lidar.hpp> // Step 1: Include Lidar

using namespace webots;
using namespace std;

int main(int argc, char **argv) {

  Robot *robot = new Robot();
  
  Lidar* lidar = robot->getLidar("lidar"); // Step 2: Retrieve the sensor, named "lidar", from the robot. Note that the sensor name may differ between robots.

  int timeStep = (int)robot->getBasicTimeStep();
  
  lidar->enable(timeStep); // Step 3: Enable the sensor, using the timestep as the update rate
  
  while (robot->step(timeStep) != -1) {
    
    const float *rangeImage = lidar->getRangeImage(); // Step 4: Retrieve the range image
    for(int i = 0; i < 10; i++) {
    
      // Print the first 10 values of the range image.
      // The range image stores the distances from left to right, from first to last layer
      cout << rangeImage[i] << " ";
    }
    
    cout << endl << endl;
  }
    
  delete robot;
  return 0;
}

In addition to retrieving the distances, the lidar also has the option of constructing a point cloud from the distances. The point cloud is a collection of x, y, and z ray collision points relative to the position of the lidar. Each point is stored in a LidarPoint object, which also contains information on the layer where the point originated from and what time the point was taken.

Note: Enabling the point cloud is computationally expensive.

   
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from controller import Robot, Lidar

robot = Robot()

lidar = robot.getDevice("lidar")

timestep = int(robot.getBasicTimeStep())

lidar.enable(timestep)
lidar.enablePointCloud(); # Enable the point cloud computation

while robot.step(timestep) != -1:
    
    lidarPoints = lidar.getPointCloud(); # Get the point cloud

    # Print out the x, y, z, and layer information for the first point in the point cloud    
    print("x: " + str(lidarPoints[0].x) + " y: " + str(lidarPoints[0].y) + " z: " + str(lidarPoints[0].z) + " layer: " + str(lidarPoints[0].layer_id))
        
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

#include <webots/Robot.hpp>
#include <webots/Lidar.hpp>

using namespace webots;
using namespace std;

int main(int argc, char **argv) {

  Robot *robot = new Robot();
  
  Lidar* lidar = robot->getLidar("lidar"); 

  int timeStep = (int)robot->getBasicTimeStep();
  
  lidar->enable(timeStep); 
  lidar->enablePointCloud(); // Enable the point cloud computation
  
  while (robot->step(timeStep) != -1) {
    
    const LidarPoint* lidarPoints = lidar->getPointCloud(); // Get the point cloud

    // Print out the x, y, z, and layer information for the first point in the point cloud 
    cout << "x: " << lidarPoints[0].x << " y: " << lidarPoints[0].y << " z: " << lidarPoints[0].z << " layer: " << lidarPoints[0].layer_id << endl;
  }
    
  delete robot;
  return 0;
}