การเคลื่อนที่
หุ่นยนต์มีล้อสองล้อ แต่ละล้อมีมอเตอร์ของตัวเอง: left wheel motor และ right wheel motor บนหุ่นยนต์เริ่มต้น คุณขับหุ่นยนต์โดยตั้งความเร็วของแต่ละล้อ:
| ล้อซ้าย | ล้อขวา | หุ่นยนต์จะ… |
|---|---|---|
| ไปข้างหน้า | ไปข้างหน้า | วิ่งไปข้างหน้า |
| ถอยหลัง | ถอยหลัง | วิ่งถอยหลัง |
| ถอยหลัง | ไปข้างหน้า | หมุนตัวไปทางซ้าย |
| ไปข้างหน้า | ถอยหลัง | หมุนตัวไปทางขวา |
| เร็ว | ช้า | เลี้ยวโค้งไปทางขวา |
ความเร็วมีหน่วยเป็นเรเดียนต่อวินาที สูงสุด 6.28 (ล้อหมุนหนึ่งรอบต่อวินาที) ความเร็วติดลบจะทำให้ล้อหมุนถอยหลัง
การขับเคลื่อน
หัวข้อที่มีชื่อว่า “การขับเคลื่อน”- รับมอเตอร์แต่ละตัวด้วยชื่อของมัน
- ตั้งตำแหน่งเป็นอนันต์ เพื่อให้หมุนได้โดยไม่จำกัด
- ตั้งความเร็ว
มอเตอร์จะคงความเร็วล่าสุดที่คุณตั้งไว้ หุ่นยนต์จึงเคลื่อนที่ต่อไปขณะที่โปรแกรมของคุณทำอย่างอื่น โปรแกรมนี้ขับไปข้างหน้า ถอยหลัง และหมุนตัวไปทางซ้าย อย่างละหนึ่งวินาที:
from controller import Robot
robot = Robot()timeStep = int(robot.getBasicTimeStep())maxSpeed = 6.28 # The maximum speed of the wheels, in radians per second
leftWheel = robot.getDevice("left wheel motor")rightWheel = robot.getDevice("right wheel motor")leftWheel.setPosition(float("inf"))rightWheel.setPosition(float("inf"))
def drive(left, right, seconds): # Set the speed of each wheel, and keep it for some time leftWheel.setVelocity(left) rightWheel.setVelocity(right) start = robot.getTime() while robot.step(timeStep) != -1: if robot.getTime() - start >= seconds: break
drive(maxSpeed, maxSpeed, 1.0) # Forwarddrive(-maxSpeed, -maxSpeed, 1.0) # Backwarddrive(-2.0, 2.0, 1.0) # Spin to the leftdrive(0, 0, 0.1) # Stopprint("Done")#include <iostream>#include <cmath>#include <webots/Robot.hpp>#include <webots/Motor.hpp>
using namespace webots;
Robot *robot;Motor *leftWheel, *rightWheel;int timeStep;const double maxSpeed = 6.28; // The maximum speed of the wheels, in radians per second
// Set the speed of each wheel, and keep it for some timevoid drive(double left, double right, double seconds) { leftWheel->setVelocity(left); rightWheel->setVelocity(right); double start = robot->getTime(); while (robot->step(timeStep) != -1) { if (robot->getTime() - start >= seconds) break; }}
int main() { robot = new Robot(); timeStep = (int)robot->getBasicTimeStep();
leftWheel = robot->getMotor("left wheel motor"); rightWheel = robot->getMotor("right wheel motor"); leftWheel->setPosition(INFINITY); rightWheel->setPosition(INFINITY);
drive(maxSpeed, maxSpeed, 1.0); // Forward drive(-maxSpeed, -maxSpeed, 1.0); // Backward drive(-2.0, 2.0, 1.0); // Spin to the left drive(0, 0, 0.1); // Stop std::cout << "Done" << std::endl;
delete robot; return 0;}การวัดว่าล้อหมุนไปเท่าใด
หัวข้อที่มีชื่อว่า “การวัดว่าล้อหมุนไปเท่าใด”มอเตอร์แต่ละตัวมีเซนเซอร์ตำแหน่ง หรือที่เรียกว่าเอนโค้ดเดอร์ (encoder) ซึ่งนับว่าล้อหมุนไปเท่าใดเป็นเรเดียน เซนเซอร์นี้มีประโยชน์ในการขับไปเป็นระยะทางที่กำหนด การหมุนถอยหลังจะทำให้ค่าที่นับลดลง
from controller import Robot
robot = Robot()timeStep = int(robot.getBasicTimeStep())
leftWheel = robot.getDevice("left wheel motor")rightWheel = robot.getDevice("right wheel motor")leftWheel.setPosition(float("inf"))rightWheel.setPosition(float("inf"))
# Each wheel has a position sensor, which counts how far it has turnedleftEncoder = leftWheel.getPositionSensor()rightEncoder = rightWheel.getPositionSensor()leftEncoder.enable(timeStep)rightEncoder.enable(timeStep)
robot.step(timeStep) # Step once, so that the sensors have a valuestart = leftEncoder.getValue()
leftWheel.setVelocity(5.0)rightWheel.setVelocity(5.0)
while robot.step(timeStep) != -1: turned = leftEncoder.getValue() - start if turned > 10.0: # Stop after 10 radians break
leftWheel.setVelocity(0)rightWheel.setVelocity(0)print("Left wheel:", round(leftEncoder.getValue() - start, 2), "rad")#include <iostream>#include <cmath>#include <webots/Robot.hpp>#include <webots/Motor.hpp>#include <webots/PositionSensor.hpp>
using namespace webots;using namespace std;
int main() { Robot *robot = new Robot(); int timeStep = (int)robot->getBasicTimeStep();
Motor *leftWheel = robot->getMotor("left wheel motor"); Motor *rightWheel = robot->getMotor("right wheel motor"); leftWheel->setPosition(INFINITY); rightWheel->setPosition(INFINITY);
// Each wheel has a position sensor, which counts how far it has turned PositionSensor *leftEncoder = leftWheel->getPositionSensor(); PositionSensor *rightEncoder = rightWheel->getPositionSensor(); leftEncoder->enable(timeStep); rightEncoder->enable(timeStep);
robot->step(timeStep); // Step once, so that the sensors have a value double start = leftEncoder->getValue();
leftWheel->setVelocity(5.0); rightWheel->setVelocity(5.0);
while (robot->step(timeStep) != -1) { double turned = leftEncoder->getValue() - start; if (turned > 10.0) // Stop after 10 radians break; }
leftWheel->setVelocity(0); rightWheel->setVelocity(0); cout << "Left wheel: " << leftEncoder->getValue() - start << " rad" << endl;
delete robot; return 0;}การควบคุมการเคลื่อนที่ด้วยเวลานั้นง่าย แต่ผลลัพธ์จะเปลี่ยนไปตามความเร็ว เอนโค้ดเดอร์วัดการเคลื่อนที่โดยตรง จึงน่าเชื่อถือกว่า สำหรับการเลี้ยวที่แม่นยำ ไจโร ดียิ่งกว่า
ขั้นต่อไป เรียนรู้การอ่านค่าเซนเซอร์
