激光雷达
激光雷达(LIDAR)测量机器人四周的距离,单位为米,就像同时使用许多个距离传感器一样。在入门机器人上,它的名字是 lidar,位于机器人顶部。
它会测量机器人周围的 512 个点,共有 4 层,一层叠在另一层上面,最远可达 1 m。没有碰到任何物体的点读数为 inf。
getLayerRangeImage(0) 给出某一层的 512 个距离。第一个点在机器人正前方,之后按顺时针方向排列:四分之一处在右侧,一半处在后方,四分之三处在左侧。
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 layerprint("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))#include <iostream>#include <webots/Robot.hpp>#include <webots/Lidar.hpp>
using namespace webots;using namespace std;
int main() { Robot *robot = new Robot(); int timeStep = (int)robot->getBasicTimeStep();
Lidar *lidar = robot->getLidar("lidar"); lidar->enable(timeStep); int n = lidar->getHorizontalResolution(); // Number of points in each layer cout << "Points per layer: " << n << " Layers: " << lidar->getNumberOfLayers() << endl;
while (robot->step(timeStep) != -1) { const float *distances = lidar->getLayerRangeImage(0); // One layer, in meters, clockwise from the front cout << "front: " << distances[0] << " right: " << distances[n / 4] << " back: " << distances[n / 2] << " left: " << distances[3 * n / 4] << endl; }
delete robot; return 0;}下图是入门机器人在 world1.wbt 中测得的一层数据,从上方绘制,机器人位于中心。周围的墙壁显示为一排排的点。
激光雷达还可以把每个点表示为相对于激光雷达的位置,这对于绘制机器人所见环境的地图很有用。计算点云需要额外的时间,所以只有在用到时才启用它。
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)#include <iostream>#include <webots/Robot.hpp>#include <webots/Lidar.hpp>
using namespace webots;using namespace std;
int main() { Robot *robot = new Robot(); int timeStep = (int)robot->getBasicTimeStep();
Lidar *lidar = robot->getLidar("lidar"); lidar->enable(timeStep); lidar->enablePointCloud(); // Computing the point cloud takes extra time
while (robot->step(timeStep) != -1) { const LidarPoint *points = lidar->getPointCloud(); const LidarPoint &point = points[0]; // Positions relative to the lidar, in meters cout << "x: " << point.x << " y: " << point.y << " z: " << point.z << " layer: " << point.layer_id << endl; }
delete robot; return 0;}