ตัวจับเวลา#

การแนะนำ#

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

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

การกระทำ – ควบคุมตัวจับเวลา

  • reset – รีเซ็ตตัวจับเวลาเป็นศูนย์

Getter – รายงานค่าตัวจับเวลาปัจจุบัน

  • time – ส่งคืนว่าเวลาผ่านไปนานเท่าใด

Callback – เรียกใช้ฟังก์ชันหลังจากหน่วงเวลา

  • event – ลงทะเบียนฟังก์ชันที่จะถูกเรียกหลังจากจำนวนมิลลิวินาทีที่กำหนด

ตัวสร้าง - สร้างตัวจับเวลาเพื่อติดตามเวลา

  • Timer - สร้างวัตถุตัวจับเวลาใหม่ที่สามารถใช้กับวิธีการเหล่านี้ได้

การกระทำ#

reset#

reset จะตั้งค่าตัวจับเวลาเป็นศูนย์ สามารถใช้เพื่อจับเวลาส่วนอื่นๆ ของโค้ดภายในโปรเจกต์เดียวกันได้

Usage:
timer.reset()

พารามิเตอร์

คำอธิบาย

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

# Display the time the drone takes to climb upwards.
drone.take_off(500)
timer.reset()
drone.climb_for(UP, 500)
controller.screen.print("Time elapsed: ")
controller.screen.print(timer.time(SECONDS))
drone.land()

เกตเตอร์#

time#

ค่า time จะแสดงเวลาตั้งแต่รีเซ็ตตัวจับเวลาครั้งล่าสุดในรูปแบบทศนิยม ตัวจับเวลาจะถูกรีเซ็ตโดยอัตโนมัติเมื่อเริ่มต้นโครงการ

Usage:
timer.time(units)

พารามิเตอร์

คำอธิบาย

units

หน่วยที่ใช้แสดงเวลา: MSEC (ค่าเริ่มต้น) – มิลลิวินาที หรือ SECONDS

# Display the time in two units.
while True:
    controller.screen.clear_screen()
    controller.screen.set_cursor(1, 1)
    controller.screen.print("Time: {} ms".format(timer.time(MSEC)))
    controller.screen.next_row()
    controller.screen.print("Time: {:.2f} s".format(timer.time(SECONDS)))
    wait(50, MSEC)

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

event#

event calls a function after a specified amount of time passes.

วิธีใช้:

timer.event(callback, delay, arg)

พารามิเตอร์

คำอธิบาย

callback

ฟังก์ชันที่จะทำงานเมื่อเหตุการณ์ตัวจับเวลาเกิดขึ้น

delay

ระยะเวลาหน่วงก่อนการเรียกใช้ฟังก์ชัน (หน่วยเป็นมิลลิวินาที)

arg

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

# Create a function to play a looping sound.
def on_timer():
    controller.sound.play(LOOPING)

# Play the sound after 4 seconds (4000 mS).
timer.event(on_timer, 4000)

ผู้สร้าง#

Timer#

Timer creates a new timer. A Timer object will immediately begin counting the moment it is created and will work with all timer methods.

Usage:
Timer()

พารามิเตอร์

คำอธิบาย

คอนสตรัคเตอร์นี้ไม่มีพารามิเตอร์

# Display 2 timers with a 2 second difference.
wait(2, SECONDS)

stopwatch = Timer()

while True:
    controller.screen.clear_screen()

    controller.screen.set_cursor(1, 1)
    controller.screen.print("Timer: ")
    controller.screen.print(timer.time(SECONDS))

    controller.screen.set_cursor(4, 1)
    controller.screen.print("Stopwatch: ")
    controller.screen.print(stopwatch.time(SECONDS))

    wait(0.1, SECONDS)