Python dành riêng cho robot#

Giới thiệu#

Robot anh hùng Flop bao gồm một động cơ hút khí (IntakeMotor), một động cơ tay (ArmMotor) và một cảm biến thị giác IQ AI.

Tất cả các lệnh Python VR VEXcode tiêu chuẩn đều có sẵn để sử dụng trong Sân chơi IQ 26-27 Level Up.

Dưới đây là danh sách tất cả các lệnh Python dành riêng cho Robot:

Chuyển động – Di chuyển và theo dõi các động cơ của robot.

  • Hành động

    • quay – Quay động cơ theo một hướng mãi mãi.

    • spin_for – Quay động cơ một số độ hoặc vòng quay cụ thể.

    • quay đến vị trí – Quay động cơ đến một vị trí cụ thể.

    • dừng – Dừng động cơ quay.

  • Cài đặt

    • set_velocity – Đặt tốc độ quay của động cơ.

    • set_timeout – Đặt thời gian tối đa mà động cơ sẽ cố gắng hoàn thành một chuyển động.

    • set_position – Thay đổi vị trí hiện tại của động cơ thành một giá trị mới.

  • Giá trị

    • is_done – Trả về trạng thái cho biết động cơ đã ngừng chuyển động hay chưa.

    • is_spinning – Trả về trạng thái động cơ có đang quay hay không.

    • vị trí – Trả về vị trí hiện tại của động cơ.

    • vận tốc – Trả về tốc độ quay của động cơ.

Công nghệ Thị giác AI – Thu thập và phân tích đối tượng bằng Cảm biến Thị giác IQ AI.

  • Người đạt giải

    • take_snapshot – Lọc khung hình hiện tại của cảm biến theo một chữ ký cụ thể và trả về một bộ các đối tượng được phát hiện.

  • Của cải

    • .width – Trả về chiều rộng của đối tượng được phát hiện tính bằng pixel.

    • .height – Trả về chiều cao của đối tượng được phát hiện tính bằng pixel.

    • .centerX – Trả về tọa độ x của tâm đối tượng được phát hiện.

    • .centerY – Trả về tọa độ y của tâm đối tượng được phát hiện.

    • .originX – Trả về tọa độ x của góc trên bên trái của hộp giới hạn của đối tượng được phát hiện.

    • .originY – Trả về tọa độ y của góc trên bên trái của hộp giới hạn của đối tượng được phát hiện.

    • .id – Trả về ID của Phân loại AI được phát hiện.

Các ví dụ trên trang này sử dụng vị trí bắt đầu mặc định của Playground.

Cử động#

Flop có hai động cơ chuyên dụng cho robot: intake_motor và arm_motorCả hai đều sử dụng các lệnh điều khiển động cơ tiêu chuẩn.

Các hằng số hướng:

  • intake_motor: FORWARD = hút (thu gom); REVERSE = xả (đẩy ra)

  • arm_motor: FORWARD = lên (nâng); REVERSE = xuống (hạ)

quay#

Lệnh spin sẽ quay động cơ theo hướng đã cho mãi mãi. Động cơ sẽ tiếp tục quay cho đến khi nhận được lệnh khác.

Usage:
motor.spin(direction)

Thông số

Sự miêu tả

direction

Hướng quay: FORWARD hoặc REVERSE.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

quay cho#

spin_for quay một động cơ trong một khoảng cách cụ thể. Dự án sẽ đợi cho đến khi động cơ quay xong trước khi lệnh tiếp theo được thực thi, trừ khi tham số wait=False được truyền vào.

Usage:
motor.spin_for(direction, amount, unit)

Thông số

Sự miêu tả

direction

Hướng quay: FORWARD hoặc REVERSE.

amount

Khoảng cách quay. Giá trị này có thể là số nguyên hoặc số thập phân.

unit

Đơn vị đo: DEGREES hoặc TURNS.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

quay đến vị trí#

spin_to_position quay động cơ đến một vị trí cụ thể. Dự án sẽ đợi cho đến khi động cơ quay xong trước khi lệnh tiếp theo được thực thi, trừ khi tham số wait=False được truyền vào.

Usage:
motor.spin_to_position(position, unit)

Thông số

Sự miêu tả

position

Vị trí cần quay đến. Đây có thể là số nguyên hoặc số thập phân.

unit

Đơn vị đo: DEGREES hoặc TURNS.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_to_position(1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

dừng lại#

stop dừng động cơ quay.

Usage:
motor.stop()

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)
    wait(1, SECONDS)
    intake_motor.stop()

# VR threads — Do not delete
vr_thread(main)

đặt vận tốc#

set_velocity thiết lập tốc độ quay của động cơ dưới dạng phần trăm từ 0 đến 100.

Usage:
motor.set_velocity(velocity, unit)

Thông số

Sự miêu tả

velocity

Tốc độ của động cơ được biểu diễn bằng phần trăm từ 0 đến 100.

unit

Đơn vị đo: PERCENT.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.set_velocity(100, PERCENT)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

đặt thời gian chờ#

set_timeout thiết lập số giây mà động cơ sẽ cố gắng hoàn thành một chuyển động trước khi dừng lại.

Usage:
motor.set_timeout(timeout, unit)

Thông số

Sự miêu tả

timeout

Số giây mà động cơ sẽ cố gắng hoàn thành một chuyển động.

unit

Đơn vị đo: SECONDS.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.set_timeout(2, SECONDS)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

đặt vị trí#

Lệnh set_position thay đổi vị trí hiện tại của động cơ thành một giá trị mới, đặt lại bộ mã hóa của nó.

Usage:
motor.set_position(position, unit)

Thông số

Sự miêu tả

position

Giá trị vị trí mới.

unit

Đơn vị đo: DEGREES hoặc TURNS.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.set_position(0, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

đã xong#

is_done trả về giá trị Boolean cho biết động cơ đã hoàn thành chuyển động hay chưa.

  • True – Động cơ đã ngừng chuyển động.

  • False – Động cơ vẫn đang hoạt động.

Usage:
motor.is_done()

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS, wait=False)
    while not arm_motor.is_done():
        wait(5, MSEC)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

đang quay#

is_spinning trả về giá trị Boolean cho biết động cơ hiện có đang quay hay không.

  • True – Động cơ đang quay.

  • False – Động cơ không quay.

Usage:
motor.is_spinning()

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)
    while intake_motor.is_spinning():
        wait(5, MSEC)
    intake_motor.stop()

# VR threads — Do not delete
vr_thread(main)

chức vụ#

Vị trí position trả về vị trí hiện tại của động cơ.

Usage:
motor.position(unit)

Thông số

Sự miêu tả

unit

Đơn vị đo: DEGREES hoặc TURNS.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    while arm_motor.position(DEGREES) < 360:
        arm_motor.spin(FORWARD)
        wait(2, MSEC)
    arm_motor.stop()
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)

# VR threads — Do not delete
vr_thread(main)

vận tốc#

Giá trị velocity trả về tốc độ quay hiện tại của động cơ dưới dạng phần trăm, từ -100 đến 100.

Usage:
motor.velocity(unit)

Thông số

Sự miêu tả

unit

Đơn vị đo: PERCENT.

def main():
    # Score a Bean Bag in a Blue Goal
    drivetrain.turn_for(LEFT, 90, DEGREES)
    drivetrain.drive_for(FORWARD, 35, INCHES)
    drivetrain.turn_for(RIGHT, 90, DEGREES)
    arm_motor.spin_for(FORWARD, 1, TURNS)
    drivetrain.drive_for(FORWARD, 40, INCHES)
    intake_motor.spin(REVERSE)
    wait(0.2, SECONDS)
    brain.screen.print(intake_motor.velocity(PERCENT))

# VR threads — Do not delete
vr_thread(main)

Tầm nhìn AI#

chụp ảnh#

take_snapshot lọc dữ liệu từ khung hình của Cảm biến Thị giác IQ AI thành một chữ ký duy nhất — một mô tả đã lưu về thứ mà cảm biến có thể nhận ra, chẳng hạn như một yếu tố trò chơi trên sân — và trả về một bộ dữ liệu.

The tuple stores objects ordered from largest to smallest by width, starting at index 0. Each object’s properties can be accessed using its index. An empty tuple is returned if no matching objects are detected.

Usage:
ai_vision.take_snapshot(signature)

Thông số

Sự miêu tả

signature

Lọc tập dữ liệu chỉ bao gồm dữ liệu có chữ ký đã cho. Các chữ ký có sẵn là:

  • AiVision.ALL_AIOBJS - Phát hiện tất cả các túi đậu.
  • AiVision.ALL_TAGS - Phát hiện AprilTags, được tìm thấy ở các cạnh của khung thành.

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Turn to face a red Bean Bag
    drivetrain.set_turn_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        for ai_object in ai_objects:
            if ai_object.id == GameElements.RED_BEANBAG:
                if ai_object.centerX < 140:
                    drivetrain.turn(LEFT)
                elif ai_object.centerX > 180:
                    drivetrain.turn(RIGHT)
                else:
                    drivetrain.stop()
                    return
                break

# VR threads — Do not delete
vr_thread(main)

Của cải#

Có bảy thuộc tính được bao gồm trong mỗi đối tượng được lưu trữ trong một tuple sau khi take_snapshot được sử dụng.

Tất cả các giá trị thuộc tính ngoại trừ .id mô tả vị trí và kích thước của đối tượng được phát hiện trong tầm nhìn của Cảm biến Thị giác IQ AI tại thời điểm lệnh take_snapshot được sử dụng. Các giá trị này được đo bằng pixel, dựa trên độ phân giải 320 x 240 pixel của cảm biến.

.chiều rộng#

.width trả về chiều rộng của đối tượng được phát hiện tính bằng pixel, là một số nguyên nằm giữa 1 và 320.

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Approach the Bean Bag, then pick it up
    drivetrain.set_drive_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        for ai_object in ai_objects:
            if ai_object.id == GameElements.RED_BEANBAG:
                if ai_object.width > 200:
                    drivetrain.stop()
                    intake_motor.spin(FORWARD)
                    return
                else:
                    drivetrain.drive(FORWARD)
                break

# VR threads — Do not delete
vr_thread(main)

.chiều cao#

.height trả về chiều cao của đối tượng được phát hiện tính bằng pixel, là một số nguyên nằm giữa 1 và 240.

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Approach the Bean Bag, then pick it up
    drivetrain.set_drive_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        for ai_object in ai_objects:
            if ai_object.id == GameElements.RED_BEANBAG:
                if ai_object.height > 110:
                    drivetrain.stop()
                    intake_motor.spin(FORWARD)
                    return
                else:
                    drivetrain.drive(FORWARD)
                break

# VR threads — Do not delete
vr_thread(main)

.centerX#

.centerX trả về tọa độ x của tâm đối tượng được phát hiện tính bằng pixel, là một số nguyên nằm giữa 0 và 320.

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Steer to keep the Bean Bag centered
    drivetrain.set_turn_velocity(30, PERCENT)
    drivetrain.set_drive_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        for ai_object in ai_objects:
            if ai_object.id == GameElements.RED_BEANBAG:
                if ai_object.centerX < 140:
                    drivetrain.turn_for(LEFT, 5, DEGREES)
                else:
                    drivetrain.drive_for(FORWARD, 150, MM)
                    intake_motor.spin(FORWARD)
                    return
                break

# VR threads — Do not delete
vr_thread(main)

.centerY#

.centerY trả về tọa độ y của tâm đối tượng được phát hiện tính bằng pixel, là một số nguyên nằm giữa 0 và 240.

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Print the y-position of the Bean Bag
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
    for ai_object in ai_objects:
        if ai_object.id == GameElements.RED_BEANBAG:
            brain.screen.print(ai_object.centerY)
            break

# VR threads — Do not delete
vr_thread(main)

.originX#

.originX trả về tọa độ x của góc trên bên trái của khung bao quanh đối tượng được phát hiện tính bằng pixel, là một số nguyên nằm giữa 0 và 320.

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Steer to keep the left edge centered
    drivetrain.set_turn_velocity(30, PERCENT)
    drivetrain.set_drive_velocity(30, PERCENT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        for ai_object in ai_objects:
            if ai_object.id == GameElements.RED_BEANBAG:
                if ai_object.originX < 100:
                    drivetrain.turn_for(LEFT, 5, DEGREES)
                else:
                    drivetrain.drive_for(FORWARD, 150, MM)
                    intake_motor.spin(FORWARD)
                    return
                break

# VR threads — Do not delete
vr_thread(main)

.originY#

.originY trả về tọa độ y của góc trên bên trái của khung bao quanh đối tượng được phát hiện tính bằng pixel, là một số nguyên nằm giữa 0 và 240.

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Print the y-position of the Bean Bag's top edge
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
    for ai_object in ai_objects:
        if ai_object.id == GameElements.RED_BEANBAG:
            brain.screen.print(ai_object.originY)
            break

# VR threads — Do not delete
vr_thread(main)

.nhận dạng#

.id trả về ID của Phân loại AI được phát hiện dưới dạng số nguyên.

Phân loại AI

nhận dạng

BlueBeanBag

0

RedBeanBag

1

YellowBeanBag

2

def main():
    # Drop the preloaded Bean Bag
    intake_motor.spin_for(REVERSE, 180, DEGREES)
    drivetrain.turn_for(LEFT, 90, DEGREES)

    # Pick up the Bean Bag if it's red
    drivetrain.set_drive_velocity(30, PERCENT)
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
    for ai_object in ai_objects:
        if ai_object.id == GameElements.RED_BEANBAG:
            intake_motor.spin(FORWARD)
            drivetrain.drive_for(FORWARD, 150, MM)
            break

# VR threads — Do not delete
vr_thread(main)


def main():
    # Stop when you see the Red Goal
    drivetrain.set_turn_velocity(30, PERCENT)
    drivetrain.turn(RIGHT)
    while True:
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_TAGS)
        for ai_object in ai_objects:
            if ai_object.id == 1:
                drivetrain.stop()
                brain.screen.print("Found the Red Goal!")
                return

# VR threads — Do not delete
vr_thread(main)