Mesafe sensörü
Mesafe sensörü, önündeki ilk nesneye olan uzaklığı metre cinsinden, en fazla 0,8 m’ye kadar ölçer. Başlangıç robotunda üç tane vardır: sola, öne ve sağa bakan leftDist, frontDist ve rightDist.

Sensörü okuma
Bölüm başlığı “Sensörü okuma”from controller import Robot
robot = Robot()timeStep = int(robot.getBasicTimeStep())
frontDist = robot.getDevice("frontDist") # Get the sensor by namefrontDist.enable(timeStep) # Enable it
while robot.step(timeStep) != -1: print("Front:", round(frontDist.getValue(), 3), "m")#include <iostream>#include <webots/Robot.hpp>#include <webots/DistanceSensor.hpp>
using namespace webots;using namespace std;
int main() { Robot *robot = new Robot(); int timeStep = (int)robot->getBasicTimeStep();
DistanceSensor *frontDist = robot->getDistanceSensor("frontDist"); // Get the sensor by name frontDist->enable(timeStep); // Enable it
while (robot->step(timeStep) != -1) cout << "Front: " << frontDist->getValue() << " m" << endl;
delete robot; return 0;}Duvarları bulma
Bölüm başlığı “Duvarları bulma”Bir kare 12 cm genişliğindedir. Bu yüzden robot bir karenin ortasındayken yanındaki bir duvar yaklaşık 5 cm uzaktadır. Bu program üç sensörü kontrol eder ve hangilerinin duvar gördüğünü yazdırır:
from controller import Robot
robot = Robot()timeStep = int(robot.getBasicTimeStep())
# Keep the sensors in a dictionary, to use them by namesensors = {}for name in ["leftDist", "frontDist", "rightDist"]: sensors[name] = robot.getDevice(name) sensors[name].enable(timeStep)
WALL = 0.07 # Closer than 7 cm counts as a wall next to the robot
while robot.step(timeStep) != -1: walls = [name for name, sensor in sensors.items() if sensor.getValue() < WALL] print("Walls:", walls)#include <iostream>#include <string>#include <vector>#include <webots/Robot.hpp>#include <webots/DistanceSensor.hpp>
using namespace webots;using namespace std;
int main() { Robot *robot = new Robot(); int timeStep = (int)robot->getBasicTimeStep();
vector<string> names = {"leftDist", "frontDist", "rightDist"}; vector<DistanceSensor *> sensors; for (const string &name : names) { sensors.push_back(robot->getDistanceSensor(name)); sensors.back()->enable(timeStep); }
const double WALL = 0.07; // Closer than 7 cm counts as a wall next to the robot
while (robot->step(timeStep) != -1) { cout << "Walls:"; for (size_t i = 0; i < sensors.size(); i++) if (sensors[i]->getValue() < WALL) cout << " " << names[i]; cout << endl; }
delete robot; return 0;}Sensörler, robotun merkezinden değil, robot üzerinde bulundukları yerden ölçer. Kendi eşik değerlerini belirlemek için bir duvarın yanında değerleri yazdır.
