Archive

Archive for 02/11/2025

ฝึกการเขียนโปรแกรม ด้วย Robo MIND ภาค #4 คำสั่งตรวจจับสิ่งรอบตัวหุ่นยนต์ / Sensing the environment

สำหรับหัวข้อนี้ก่อน ตัวอย่างต่อไปผมขอเพิ่มเติใ คำสั่งในหมวด ของการตรวจจับสิ่งแวดลอมรอบๆ ตัวหุ่นยนหรือ Sensing the environment ว่ามีกี่คำสั่งอะไรบ้างเพื่อให้เราสามารถทำ Algorithm หุ่นของเราฉลาดขึ้น

1. frontIsClear

คำสั่ง: คืนค่า true ถ้าด้านหน้าว่าง, false ถ้ามีสิ่งกีดขวาง
English: Returns true if the path directly in front of the robot is clear, false otherwise.

ตัวอย่าง / Example

if frontIsClear {
    forward(1)
}

หุ่นยนต์จะเดินหน้า 1 ก้าวถ้าทางว่าง

2. frontIsObstacle

คำสั่ง: ตรวจว่าด้านหน้ามีสิ่งกีดขวางหรือไม่
English: Returns true if there is an obstacle directly in front of the robot.

ตัวอย่าง / Example

if frontIsObstacle {
    left
)
ถ้ามีสิ่งกีดขวางตรงหน้า → หันซ้าย

3. frontIsBeacon

คำสั่ง: ตรวจว่ามี Beacon (สัญญาณเป้าหมาย) อยู่ด้านหน้าหรือไม่
English: Returns true if a beacon is directly in front of the robot.

ตัวอย่าง / Example

if frontIsBeacon{
    paintWhite    # Mark as reached
}
ถ้าเจอสัญญาณ beacon → ระบายสีพื้นเพื่อแสดงว่า “ถึงเป้าหมายแล้ว”

4. Sensing on other sides

นอกจากด้านหน้าแล้ว ยังสามารถตรวจซ้ายและขวาได้เหมือนกัน
Similar commands exist for left and right directions:

leftIsClear
rightIsClear
leftIsObstacle
rightIsObstacle
leftIsBeacon
rightIsBeacon

ตัวอย่าง / Example

if rightIsClear {
    right
}else{
    left
}

ถ้าทางขวาโล่ง → เลี้ยวขวา, ถ้าไม่โล่ง → หันซ้ายแทน

5. Ground Color Sensing

ตรวจสีของพื้นด้านหน้า ซ้าย หรือขวา
Checks for the color of the ground directly ahead or beside the robot.

คำสั่ง / Commands:

frontIsWhite
frontIsBlack
leftIsWhite
leftIsBlack
rightIsWhite
rightIsBlack

ตัวอย่าง / Example

if frontIsBlack() then
    backward(1)
else
    forward(1)
end

ถ้าพื้นด้านหน้าเป็นสีดำ → ถอยหลัง 1 ก้าว
ถ้าไม่ใช่สีดำ → เดินหน้า

สรุปภาพรวม

กลุ่มตัวอย่างคำสั่งหน้าที่ค่าที่คืนกลับ (Return)
ตรวจสิ่งกีดขวางfrontIsClear, rightIsObstacleตรวจว่าทางว่างหรือมีสิ่งขวางtrue / false
ตรวจ BeaconfrontIsBeaconตรวจว่ามีสัญญาณเป้าหมายอยู่ตรงหน้าtrue / false
ตรวจสีพื้นfrontIsWhite, leftIsBlackตรวจสีของพื้นในทิศต่าง ๆtrue / false

ยาวนิสหนี่ง ครับอยากให้เห็น ภาพรวมของคำสั่งที่ใช้ในการตรวจสอบ สิ่งแวดล้อมของหุ่นซึ่งก็มีเท่านี้เลือกใช้ตามสถานการได้ เสริมความฉลาดของหุ่นเรา …

s.teerapong@gmail.com