İlk programın
İlk programın robotu ileri sürer ve bir duvarın önünde durdurur. Kısadır, ama her programın ihtiyaç duyduğu her şeyi içerir.
from controller import Robot
robot = Robot()timeStep = int(robot.getBasicTimeStep())
# Get the devices of the starter robot by nameleftWheel = robot.getDevice("left wheel motor")rightWheel = robot.getDevice("right wheel motor")frontDist = robot.getDevice("frontDist")
# Sensors must be enabled before usefrontDist.enable(timeStep)
# Let the wheels turn without limit, and start driving forwardleftWheel.setPosition(float("inf"))rightWheel.setPosition(float("inf"))leftWheel.setVelocity(3.0)rightWheel.setVelocity(3.0)
# The main loop: each step, the simulation advances by timeStep millisecondswhile robot.step(timeStep) != -1: distance = frontDist.getValue() print("Distance to the wall in front:", round(distance, 3), "m")
if distance < 0.05: # Closer than 5 cm: stop leftWheel.setVelocity(0) rightWheel.setVelocity(0) print("Wall reached!") break#include <iostream>#include <cmath>#include <webots/Robot.hpp>#include <webots/Motor.hpp>#include <webots/DistanceSensor.hpp>
using namespace webots;using namespace std;
int main() { Robot *robot = new Robot(); int timeStep = (int)robot->getBasicTimeStep();
// Get the devices of the starter robot by name Motor *leftWheel = robot->getMotor("left wheel motor"); Motor *rightWheel = robot->getMotor("right wheel motor"); DistanceSensor *frontDist = robot->getDistanceSensor("frontDist");
// Sensors must be enabled before use frontDist->enable(timeStep);
// Let the wheels turn without limit, and start driving forward leftWheel->setPosition(INFINITY); rightWheel->setPosition(INFINITY); leftWheel->setVelocity(3.0); rightWheel->setVelocity(3.0);
// The main loop: each step, the simulation advances by timeStep milliseconds while (robot->step(timeStep) != -1) { double distance = frontDist->getValue(); cout << "Distance to the wall in front: " << distance << " m" << endl;
if (distance < 0.05) { // Closer than 5 cm: stop leftWheel->setVelocity(0); rightWheel->setVelocity(0); cout << "Wall reached!" << endl; break; } }
delete robot; return 0;}Nasıl çalışır
Bölüm başlığı “Nasıl çalışır”- Robotu oluştur.
Robot(), programını simülatördeki robota bağlar. - Zaman adımını al. Simülasyon birkaç milisaniyelik adımlarla ilerler.
getBasicTimeStep()dünyanın adımını verir. - Cihazları al. Her motor ve sensör adıyla bulunur. Bunlar, robotunu yaparken onlara verdiğin adlardır.
- Sensörleri etkinleştir. Bir sensör etkinleştirilene kadar hiçbir değer vermez. Zaman adımı, sensörün ne sıklıkla güncelleneceğini belirler.
- Döngü.
robot.step(timeStep)simülasyonu bir adım ilerletir ve simülasyon bittiğinde -1 döndürür. Robotunun yaptığı her şey bu döngünün içinde olur.
Çalıştır
Bölüm başlığı “Çalıştır”- Programı
hello.pyolarak kaydet. game/worlds/mapping_example_1.wbtdünyasını aç ve sağdaki LOAD düğmesiyle başlangıç robotunu yükle.hello.pydosyasını soldaki LOAD düğmesiyle yükle ve başlat düğmesine bas.
Robot ileri gider ve durana kadar Webots konsolunda önündeki duvara olan uzaklık görüntülenir. Bunun yerine bir hata görürsen Sorun giderme sayfasına bak.
- Hızı
3.0’dan6.0’a değiştir. Duvarın yakınında ne oluyor? - Robotun 5 cm yerine 10 cm’de durmasını sağla.
robot.getTime()ile zamanı da yazdır.
Sırada Hareket sayfasında ilerlemeyi ve dönmeyi öğrenmek var.
