ตัวจับเวลา#

การแนะนำ#

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

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

วิธีการ – ควบคุมตัวจับเวลา

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

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

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

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

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

วิธีการ#

reset#

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

วิธีใช้:

timer.reset()

พารามิเตอร์

คำอธิบาย

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

# Display how long a turn takes
robot.move_for(100, 0)
timer.reset()
robot.turn_for(RIGHT, 90)
robot.screen.print("Time elapsed:")
robot.screen.next_row()
robot.screen.print("%d seconds" % timer.time(SECONDS))

time#

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

วิธีใช้:

timer.time(units)

พารามิเตอร์

คำอธิบาย

units

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

# Print the time in seconds
# and in milliseconds on the next row.
while timer.time(SECONDS) <= 5:
    robot.screen.clear_screen()
    robot.screen.set_cursor(1, 1)
    robot.screen.print("Time: %d" % timer.time(MSEC))
    robot.screen.next_row()
    robot.screen.print("Time: %.2f" % 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 sparkle sound
def timer_callback():
    robot.sound.play(SPARKLE)

# Call the function after 2000 milliseconds (2 seconds)
timer.event(timer_callback, 2000)

ผู้สร้าง#

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:
    robot.screen.clear_screen()

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

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

    wait(0.1, SECONDS)