Skip to content
English

LIDAR

The lidar measures distances in metres all around the robot, like many distance sensors at once. On the starter robot it is named lidar, on top of the robot.

It measures 512 points around the robot, in 4 layers stacked on top of each other, up to 1 m away. A point that hits nothing reads inf.

getLayerRangeImage(0) gives the 512 distances of one layer. The first one is in front of the robot, and they go clockwise: a quarter of the way is on the right, half is behind, and three quarters is on the left.

The points of a lidar layer around the robot: 0 in front, 128 on the right, 256 behind and 384 on the left

from controller import Robot
robot = Robot()
timeStep = int(robot.getBasicTimeStep())
lidar = robot.getDevice("lidar")
lidar.enable(timeStep)
n = lidar.getHorizontalResolution() # Number of points in each layer
print("Points per layer:", n, "Layers:", lidar.getNumberOfLayers())
while robot.step(timeStep) != -1:
distances = lidar.getLayerRangeImage(0) # One layer, in meters, clockwise from the front
front = distances[0]
right = distances[n // 4]
back = distances[n // 2]
left = distances[3 * n // 4]
print("front:", round(front, 3), "right:", round(right, 3), "back:", round(back, 3), "left:", round(left, 3))

Here is one layer measured by the starter robot in world1.wbt, drawn from above with the robot in the middle. The walls around it appear as lines of points.

One lidar layer drawn from above: walls around the robot as lines of points

The lidar can also give each point as a position relative to the lidar, which is useful to draw a map of what the robot sees. Computing it takes extra time, so enable it only if you use it.

from controller import Robot
robot = Robot()
timeStep = int(robot.getBasicTimeStep())
lidar = robot.getDevice("lidar")
lidar.enable(timeStep)
lidar.enablePointCloud() # Computing the point cloud takes extra time
while robot.step(timeStep) != -1:
points = lidar.getPointCloud()
point = points[0] # Positions relative to the lidar, in meters
print("x:", round(point.x, 3), "y:", round(point.y, 3), "z:", round(point.z, 3), "layer:", point.layer)