กันชน#

การแนะนำ#

กันชนเป็นสวิตช์เชิงกลที่ทำให้วงจรสมบูรณ์เมื่อถูกกด ทำให้หุ่นยนต์สามารถตรวจจับการกดและการปล่อยได้

ด้านล่างนี้คือรายการวิธีการทั้งหมดที่มีให้เลือกใช้:

ผู้ควบคุม – ตรวจสอบว่าเซ็นเซอร์กันชนถูกกดอยู่หรือไม่

  • pressed – ส่งคืนว่าเซ็นเซอร์กันชนถูกกดหรือไม่

ฟังก์ชันเรียกกลับ – เรียกใช้โค้ดเมื่อกดหรือปล่อยกันชน

  • on_pressed – ลงทะเบียนฟังก์ชันที่จะถูกเรียกเมื่อเซ็นเซอร์กันชนถูกกด

  • on_released – ลงทะเบียนฟังก์ชันที่จะถูกเรียกเมื่อเซ็นเซอร์กันชนถูกปล่อย

ผู้รับ#

pressed#

pressed returns a Boolean indicating whether the Bumper Switch is currently being pressed. This can be used to check if the robot bumps into other objects.

  • True – สวิตช์กันชนกำลังถูกกดอยู่

  • False – ไม่ได้กดสวิตช์กันชน

Usage:
bumper.pressed()

พารามิเตอร์

คำอธิบาย

เมธอดนี้ไม่มีพารามิเตอร์

def main():
    # Stop when the Left Bumper is pressed
    drivetrain.drive(FORWARD)
    while True:
        if left_bumper.pressed():
            drivetrain.stop()
        wait(5, MSEC)

# VR threads — Do not delete
vr_thread(main)

การเรียกกลับ#

on_pressed#

on_pressed ลงทะเบียนฟังก์ชันที่จะถูกเรียกเมื่อเซ็นเซอร์กันชนถูกกด

Usage:
bumper.on_pressed(callback)

พารามิเตอร์

คำอธิบาย

callback

A function that is previously defined to execute when the Bumper Sensor is being pressed.

def bumper_pressed():
    # Reverse when the bumper is pressed
    drivetrain.drive_for(REVERSE, 100, MM)
    drivetrain.stop()

def main():
    # Drive forward until the left bumper is pressed
    drivetrain.drive(FORWARD)
    left_bumper.on_pressed(bumper_pressed)

# VR threads — Do not delete
vr_thread(main)

on_released#

on_released ลงทะเบียนฟังก์ชันที่จะถูกเรียกเมื่อเซ็นเซอร์กันชนถูกปล่อย

Usage:
bumper.on_released(callback)

พารามิเตอร์

คำอธิบาย

callback

A function that is previously defined to execute when the Bumper Sensor is being pressed.

def bumper_released():
    # Display a message
    brain.print("Bumper released!")

def main():
    # Hit and back away from an object
    left_bumper.on_released(bumper_released)
    drivetrain.drive_for(FORWARD, 600, MM)
    drivetrain.drive_for(REVERSE, 100, MM)

# VR threads — Do not delete
vr_thread(main)