LIDAR
lidar วัดระยะเป็นเมตร รอบตัวหุ่นยนต์ เหมือนมีเซนเซอร์วัดระยะหลายตัวพร้อมกัน บนหุ่นยนต์เริ่มต้นมีชื่อว่า lidar และติดตั้งอยู่ด้านบนของหุ่นยนต์
lidar วัดได้ 512 จุดรอบตัวหุ่นยนต์ ใน 4 ชั้นที่ซ้อนกันในแนวตั้ง ได้ไกลสุด 1 เมตร จุดที่ไม่กระทบสิ่งใดจะอ่านได้ค่า 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;}ต่อไปนี้คือ lidar หนึ่งชั้นที่หุ่นยนต์เริ่มต้นวัดได้ใน world1.wbt วาดจากมุมมองด้านบนโดยมีหุ่นยนต์อยู่ตรงกลาง ผนังรอบตัวหุ่นยนต์ปรากฏเป็นแนวของจุด
กลุ่มจุด (point cloud)
หัวข้อที่มีชื่อว่า “กลุ่มจุด (point cloud)”lidar ยังให้แต่ละจุดเป็นตำแหน่งเทียบกับตัว lidar ได้ด้วย ซึ่งมีประโยชน์ในการวาดแผนที่ของสิ่งที่หุ่นยนต์เห็น การคำนวณนี้ใช้เวลาเพิ่ม จึงควรเปิดใช้เฉพาะเมื่อคุณต้องใช้
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;}