โปรแกรมแรกของคุณ
โปรแกรมแรกของคุณจะขับหุ่นยนต์ไปข้างหน้า และหยุดเมื่ออยู่หน้าผนัง โปรแกรมนี้สั้น แต่มีทุกสิ่งที่ทุกโปรแกรมต้องมี
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;}การทำงาน
หัวข้อที่มีชื่อว่า “การทำงาน”- สร้างหุ่นยนต์
Robot()เชื่อมต่อโปรแกรมของคุณกับหุ่นยนต์ในซิมูเลเตอร์ - รับไทม์สเต็ป การจำลองเดินหน้าเป็นสเต็ป สเต็ปละไม่กี่มิลลิวินาที
getBasicTimeStep()ให้ค่าสเต็ปของโลก - รับอุปกรณ์ มอเตอร์และเซนเซอร์แต่ละตัวเรียกใช้ได้ด้วยชื่อ ซึ่งเป็นชื่อที่คุณตั้งไว้เมื่อประกอบหุ่นยนต์ของคุณ
- เปิดใช้งานเซนเซอร์ เซนเซอร์จะไม่ให้ค่าใด ๆ จนกว่าจะเปิดใช้งาน ไทม์สเต็ปบอกเซนเซอร์ว่าต้องอัปเดตบ่อยเพียงใด
- ลูป
robot.step(timeStep)ทำให้การจำลองเดินหน้าไปหนึ่งสเต็ป และคืนค่า -1 เมื่อการจำลองสิ้นสุด ทุกสิ่งที่หุ่นยนต์ของคุณทำเกิดขึ้นภายในลูปนี้
รันโปรแกรม
หัวข้อที่มีชื่อว่า “รันโปรแกรม”- บันทึกโปรแกรมเป็น
hello.py - เปิด
game/worlds/mapping_example_1.wbtแล้วโหลดหุ่นยนต์เริ่มต้น ด้วยปุ่ม LOAD ด้านขวา - โหลด
hello.pyด้วยปุ่ม LOAD ด้านซ้าย แล้วกดเริ่ม
หุ่นยนต์จะวิ่งไปข้างหน้า และคอนโซลของ Webots จะแสดงระยะห่างถึงผนังด้านหน้าจนกว่าหุ่นยนต์จะหยุด หากคุณเห็นข้อผิดพลาดแทน ให้ดูที่การแก้ไขปัญหา
ลองทำดู
หัวข้อที่มีชื่อว่า “ลองทำดู”- เปลี่ยนความเร็วจาก
3.0เป็น6.0เกิดอะไรขึ้นเมื่อเข้าใกล้ผนัง? - ทำให้หุ่นยนต์หยุดที่ 10 ซม. แทน 5 ซม.
- พิมพ์เวลาด้วย
robot.getTime()ด้วย
ขั้นต่อไป เรียนรู้การขับเคลื่อนและเลี้ยวในการเคลื่อนที่
