라이다
라이다는 여러 개의 거리 센서를 한꺼번에 쓰는 것처럼 로봇 주위 전체의 거리를 미터 단위로 측정해요. 스타터 로봇에서는 로봇 위에 달려 있고, 이름은 lidar예요.
로봇 주위의 점 512개를, 위아래로 쌓인 레이어 4개에 걸쳐 최대 1 m까지 측정해요. 아무것에도 닿지 않은 점은 inf가 돼요.
거리 읽기
섹션 제목: “거리 읽기”getLayerRangeImage(0)은 한 레이어의 512개 거리를 줘요. 첫 번째 값은 로봇의 정면이고, 시계 방향으로 이어져요. 4분의 1 지점은 오른쪽, 절반은 뒤쪽, 4분의 3 지점은 왼쪽이에요.
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;}