Program pertamamu
Program pertamamu membuat robot melaju maju, lalu menghentikannya di depan dinding. Programnya pendek, tetapi berisi semua yang dibutuhkan setiap program.
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;}Cara kerjanya
Section titled “Cara kerjanya”- Buat robot.
Robot()menghubungkan programmu dengan robot di simulator. - Ambil time step. Simulasi berjalan maju dalam langkah-langkah (step) berdurasi beberapa milidetik.
getBasicTimeStep()memberikan time step world tersebut. - Ambil perangkat. Setiap motor dan sensor dicari berdasarkan namanya. Ini adalah nama yang kamu berikan saat merakit robotmu.
- Aktifkan sensor. Sensor tidak memberikan nilai sampai diaktifkan. Time step menentukan seberapa sering sensor diperbarui.
- Loop.
robot.step(timeStep)memajukan simulasi satu langkah, dan mengembalikan -1 saat simulasi berakhir. Semua yang dilakukan robotmu terjadi di dalam loop ini.
Jalankan
Section titled “Jalankan”- Simpan program sebagai
hello.py. - Buka
game/worlds/mapping_example_1.wbt, lalu muat robot pemula dengan tombol LOAD sebelah kanan. - Muat
hello.pydengan tombol LOAD sebelah kiri, lalu tekan start.
Robot melaju maju, dan konsol Webots menampilkan jarak ke dinding di depannya sampai robot berhenti. Jika kamu malah melihat error, periksa Pemecahan masalah.
Coba sendiri
Section titled “Coba sendiri”- Ubah kecepatan dari
3.0menjadi6.0. Apa yang terjadi di dekat dinding? - Buat robot berhenti di 10 cm, bukan 5 cm.
- Cetak juga waktunya dengan
robot.getTime().
Selanjutnya, pelajari cara melaju dan berbelok di Gerakan.
