机器人专用 Python#

介绍#

VIQRC 25-26 Mix & Match 游乐场采用专为此游乐场设计的独特构造,包括两种电机选项、IQ AI 视觉传感器、光学传感器和触摸 LED。

所有标准 VEXcode VR 方法均可用于 VIQRC 25-26 Mix & Match 游乐场。

以下是所有可用的 Playground 特定方法的列表:

运动——移动并跟踪机器人的马达。

  • 行动

    • spin – Spins the selected motor or motor group indefinitely.

    • spin_for – Spins a motor or group for a specific distance in degrees or turns.

    • spin_to_position – Spins a motor or motor group to a set position.

    • stop – Stops a specific motor or motor group from spinning.

  • 修改器

    • set_position – Sets the encoder value of a motor or motor group.

    • set_velocity – Sets the speed of a motor or motor group as a percentage.

    • set_timeout – Limits how long a motor block waits before giving up if movement is blocked.

  • 吸气剂

    • is_done – Returns a Boolean indicating whether the motor is no longer spinning.

    • is_spinning – Returns a Boolean indicating whether the motor is currently spinning.

    • position – Returns the motor’s current rotational position in degrees or turns.

    • velocity – Returns the motor’s current velocity in % or rpm.

AI视觉——使用IQ AI视觉传感器捕捉和分析物体。

  • 吸气剂

    • take_snapshot – Returns a tuple of detected objects based on a given signature.

  • 特性

    • width – Width of the detected object in pixels.

    • height – Height of the detected object in pixels.

    • centerX – X position of the object’s center in pixels.

    • centerY – Y position of the object’s center in pixels.

    • originX – X position of the object’s top-left corner in pixels.

    • originY – Y position of the object’s top-left corner in pixels.

    • id – Classification or tag ID of the object.

感知——利用机器人的各种传感器。

  • 触摸LED

    • set_color – Sets the TouchLED to a selected color.

  • 光学的

    • is_near_object – Returns whether a detected object is near the Optical Sensor.

    • color – Returns the color detected from the Optical Sensor.

    • brightness – Returns the brightness percentage detected by the sensor.

    • hue – Returns the hue value of the detected color.

    • object_detected – Registers a callback function for when the Optical Sensor detects an object.

    • object_lost – Registers a callback function for when the Optical Sensor loses an object.

运动#

Actions#

旋转#

spin spins a motor indefinitely.

用法:
两个可用电机对象之一可与此方法一起使用,如下所示:

发动机

命令

claw_motor

claw_motor.spin(direction) — The Claw Motor

lift_motor

lift_motor.spin(direction) — The Lift Motor

参数

描述

direction

The direction for the motor to spin:

  • FORWARD – Opens claw or lowers the lift.
  • REVERSE – Closes claw or raises the lift.
def main():
  # Place a Pin atop another Pin
  lift_motor.spin_for(REVERSE, 600, DEGREES)
  drivetrain.drive_for(FORWARD, 140, MM)
  claw_motor.spin(FORWARD)

spin_for#

spin_for spins a motor for a given amount of degrees or turns.

用法:
两个可用电机对象之一可与此方法一起使用,如下所示:

发动机

命令

claw_motor

claw_motor.spin_for(direction, distance, units, wait=True) — The Claw Motor

lift_motor

lift_motor.spin_for(direction, distance, units, wait=True) — The Lift Motor

参数

描述

direction

The direction for the motor to spin:

  • FORWARD – Opens claw or lowers the lift.
  • REVERSE – Closes claw or raises the lift.

distance

电机旋转的距离,为整数。

units

The unit that represents the distance to rotate:

  • DEGREES
  • TURNS

wait

Optional.

  • wait=True (default) - The robot waits until spin_for is complete before executing the next line of code.
  • wait=False - The robot starts the action and moves on to the next line of code right away, without waiting for spin_for to finish.

def main():
  # Place a Pin atop another Pin
  lift_motor.spin_for(REVERSE, 600, DEGREES)
  drivetrain.drive_for(FORWARD, 140, MM)
  claw_motor.spin(FORWARD)

旋转到位置#

spin_to spins a motor to a given position.

用法:
两个可用电机对象之一可与此方法一起使用,如下所示:

发动机

命令

claw_motor

claw_motor.spin_to_position(angle, units, wait=True) — The Claw Motor

lift_motor

lift_motor.spin_to_position(angle, units, wait=True) — The Lift Motor

参数

描述

angle

电机旋转的特定角度或圈数。

units

The unit that represents the angle to rotate to:

  • DEGREES
  • TURNS

wait

Optional.

  • wait=True (default) - The robot waits until spin_to_position is complete before executing the next line of code.
  • wait=False - The robot starts the action and moves on to the next line of code right away, without waiting for spin_to_position to finish.

def main():
  # Place a Pin atop another Pin
  lift_motor.spin_to_position(-2, TURNS)
  drivetrain.drive_for(FORWARD, 140, MM)
  claw_motor.spin(FORWARD)

停止#

stop stops a motor from spinning.

用法:
两个可用电机对象之一可与此方法一起使用,如下所示:

发动机

命令

claw_motor

claw_motor.stop() — The Claw Motor

lift_motor

lift_motor.stop() — The Lift Motor

参数

描述

该方法没有参数。

def main():
  # Place a Pin atop another Pin
  lift_motor.spin(REVERSE)
  wait(2, SECONDS)
  lift_motor.stop()
  drivetrain.drive_for(FORWARD, 140, MM)
  claw_motor.spin(FORWARD)

Mutators#

设置位置#

set_position sets a motor’s encoder position to the given position value.

用法:
两个可用电机对象之一可与此方法一起使用,如下所示:

发动机

命令

claw_motor

claw_motor.set_position(position, units) — The Claw Motor

lift_motor

lift_motor.set_position(position, units) — The Lift Motor

参数

描述

position

要设置的电机编码器的具体整数。

units

The unit that represents the angle to rotate to:

  • DEGREES
  • TURNS
def main():
  # Place a Pin atop another Pin
  lift_motor.set_position(100, DEGREES)
  lift_motor.spin_to_position(-500, DEGREES)
  drivetrain.drive_for(FORWARD, 140, MM)
  claw_motor.spin(FORWARD)

设置速度#

set_velocity sets the speed of a motor.

用法:
两个可用电机对象之一可与此方法一起使用,如下所示:

发动机

命令

claw_motor

claw_motor.set_velocity(velocity, units) — The Claw Motor

lift_motor

lift_motor.set_velocity(velocity, units) — The Lift Motor

参数

描述

velocity

IQ 电机旋转的速度,范围从 0 到 100。

units

The unit that represents the new velocity:

  • PERCENT
def main():
  # Place a Pin atop another Pin
  lift_motor.set_velocity(100, PERCENT)
  lift_motor.spin_for(REVERSE, 600, DEGREES)
  drivetrain.drive_for(FORWARD, 140, MM)
  claw_motor.spin(FORWARD)

设置超时#

set_timeout sets a time limit for a motor’s movement commands. This prevents motion commands that do not reach their intended position from preventing subsequent commands from running.

用法:
两个可用电机对象之一可与此方法一起使用,如下所示:

发动机

命令

claw_motor

claw_motor.set_timeout(value, units) — The Claw Motor

lift_motor

lift_motor.set_timeout(value, units) — The Lift Motor

参数

描述

value

电机停止前等待的时间。

units

The unit to represent the timeout:

  • SECONDS
  • MSEC – milliseconds
def main():
  # Place a Pin atop another Pin
  lift_motor.set_timeout(2, SECONDS)
  lift_motor.spin_for(REVERSE, 5, TURNS)
  drivetrain.drive_for(FORWARD, 140, MM)
  claw_motor.spin(FORWARD)

Getters#

完成#

is_done returns a Boolean indicating whether the specified motor is not spinning.

  • True – The specified motor is not spinning.

  • False – The specified motor is spinning.

用法:
两个可用电机对象之一可与此方法一起使用,如下所示:

发动机

命令

claw_motor

claw_motor.is_done() — The Claw Motor

lift_motor

lift_motor.is_done() — The Lift Motor

参数

描述

该方法没有参数。

def main():
  # Place a Pin atop another Pin
  lift_motor.spin_for(REVERSE, 600, DEGREES, wait=False)
  wait(0.1, SECONDS)
  while not lift_motor.is_done():
      touchled.set_color(RED)
      wait(0.5, SECONDS)
      touchled.set_color(NONE)
      wait(0.5, SECONDS)
  drivetrain.drive_for(FORWARD, 140, MM)
  claw_motor.spin(FORWARD)

正在旋转#

is_spinning returns a Boolean indicating whether the specified motor is spinning.

  • True – The specified motor is spinning.

  • False – The specified motor is not spinning.

用法:
两个可用电机对象之一可与此方法一起使用,如下所示:

发动机

命令

claw_motor

claw_motor.is_spinning() — The Claw Motor

lift_motor

lift_motor.is_spinning() — The Lift Motor

参数

描述

该方法没有参数。

def main():
  # Place a Pin atop another Pin
  lift_motor.spin_for(REVERSE, 600, DEGREES, wait=False)
  wait(0.1, SECONDS)
  while lift_motor.is_spinning():
      touchled.set_color(RED)
      wait(0.5, SECONDS)
      touchled.set_color(NONE)
      wait(0.5, SECONDS)
  drivetrain.drive_for(FORWARD, 140, MM)
  claw_motor.spin(FORWARD)

位置#

position returns the total distance the specified motor has rotated.

用法:
两个可用电机对象之一可与此方法一起使用,如下所示:

发动机

命令

claw_motor

claw_motor.position(units) — The Claw Motor

lift_motor

lift_motor.position(units) — The Lift Motor

参数

描述

units

The units that represent the motor’s position:

  • DEGREES
  • TURNS
def main():
  # Place a Pin atop another Pin
  lift_motor.spin(REVERSE)
  while not -600 > claw_motor.position(DEGREES):
      wait(2, MSEC)
  lift_motor.stop()
  drivetrain.drive_for(FORWARD, 140, MM)
  claw_motor.spin(FORWARD)

速度#

velocity returns the current rotational speed of the motor.

用法:
两个可用电机对象之一可与此方法一起使用,如下所示:

发动机

命令

claw_motor

claw_motor.velocity(units) — The Claw Motor

lift_motor

lift_motor.velocity(units) — The Lift Motor

参数

描述

units

The unit that represent the motor’s position:

  • PERCENT
def main():
  # Place a Pin atop another Pin
  lift_motor.set_velocity(100, PERCENT)
  lift_motor.spin_for(REVERSE, 600, DEGREES, wait=False)
  wait(0.5, SECONDS)
  brain.screen.print(lift_motor.velocity(PERCENT))
  drivetrain.drive_for(FORWARD, 140, MM)
  claw_motor.spin(FORWARD)

人工智能视觉#

Getters#

拍摄快照#

take_snapshot filters the data from the IQ AI Vision Sensor frame to return a tuple.

该元组按宽度从大到小排序存储对象,索引从 0 开始。每个对象的属性 (#properties) 可以通过其索引访问。如果没有检测到匹配的对象,则返回空元组。

Usage:
ai_vision.take_snapshot(signature)

参数

描述

signature

Which signature to get data of. The only available signature is:

  • AiVision.ALL_AIOBJS - Detects Beams and Blue, Red, and Orange Pins.

def main():
    # Place the Red Pin on the top left Blue Pin
    lift_motor.spin_for(REVERSE, 600, DEGREES)
    drivetrain.turn_for(LEFT, 38, DEGREES)
    
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
    while not ai_objects[0].width > 48:
        # Move forward until the Robot is close to the Pin
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        drivetrain.drive(FORWARD)
        
    drivetrain.drive_for(FORWARD, 160, MM)
    claw_motor.spin(FORWARD)

vr_thread(main)

Properties#

There are seven properties that are included with each object stored in a tuple after take_snapshot is used.

Some property values are based off of the detected object’s position in the IQ AI Vision Sensor’s view at the time that take_snapshot was used. The IQ AI Vision Sensor has a resolution of 320 by 240 pixels.

。宽度#

.width returns the width of the detected object in pixels, which is an integer between 1 and 320.

def main():
    # Place the Red Pin on the top left Blue Pin
    lift_motor.spin_for(REVERSE, 600, DEGREES)
    drivetrain.turn_for(LEFT, 38, DEGREES)
    
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
    while not ai_objects[0].width > 48:
        # Move forward until the Robot is close to the Pin
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        drivetrain.drive(FORWARD)
        
    drivetrain.drive_for(FORWARD, 160, MM)
    claw_motor.spin(FORWARD)

vr_thread(main)

。高度#

.height returns the height of the detected object in pixels, which is an integer between 1 and 240.

def main():
    # Place the Red Pin on the top left Blue Pin
    lift_motor.spin_for(REVERSE, 600, DEGREES)
    drivetrain.turn_for(LEFT, 38, DEGREES)
    
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
    while not ai_objects[0].height > 68:
        # Move forward until the Robot is close to the Pin
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        drivetrain.drive(FORWARD)
        
    drivetrain.drive_for(FORWARD, 160, MM)
    claw_motor.spin(FORWARD)

vr_thread(main)

.centerX#

.centerX returns the x-coordinate of the detected object’s center in pixels, which is an integer between 0 and 320.

def main():
    # Place the Red Pin on the bottom left Blue Pin
    drivetrain.drive_for(FORWARD, 50, MM)
    lift_motor.spin_for(REVERSE, 450, DEGREES)
    drivetrain.set_turn_velocity(10, PERCENT)
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)

    while not ai_objects[0].centerX < 190:
        # Turn until the robot is centered on the Blue Pin
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        drivetrain.turn(RIGHT)
    drivetrain.drive_for(FORWARD, 350, MM)
    claw_motor.spin(FORWARD)

vr_thread(main)

.centerY#

.centerY returns the y-coordinate of the detected object’s center in pixels, which is an integer between 0 and 240.

def main():
    # Place the Red Pin on the top left Blue Pin
    lift_motor.spin_for(REVERSE, 600, DEGREES)
    drivetrain.turn_for(LEFT, 38, DEGREES)
    
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
    while not ai_objects[0].centerY > 210:
        # Move forward until the Robot is close to the Pin
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        drivetrain.drive(FORWARD)
        
    drivetrain.drive_for(FORWARD, 190, MM)
    claw_motor.spin(FORWARD)

vr_thread(main)

.originX#

.originX returns the x-coordinate of the top-left corner of the detected object’s bounding box in pixels, which is an integer between 0 and 320.

def main():
    # Place the Red Pin on the bottom left Blue Pin
    drivetrain.drive_for(FORWARD, 50, MM)
    lift_motor.spin_for(REVERSE, 450, DEGREES)
    drivetrain.set_turn_velocity(10, PERCENT)
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
    
    while not ai_objects[0].originX < 160:
        # Turn until the robot is centered on the Blue Pin
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        drivetrain.turn(RIGHT)
    drivetrain.drive_for(FORWARD, 350, MM)
    claw_motor.spin(FORWARD)

vr_thread(main)

.originY#

.originY returns the y-coordinate of the top-left corner of the detected object’s bounding box in pixels, which is an integer between 0 and 240.

def main():
    # Place the Red Pin on the top left Blue Pin
    lift_motor.spin_for(REVERSE, 600, DEGREES)
    drivetrain.turn_for(LEFT, 38, DEGREES)
    
    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
    while not ai_objects[0].centerY > 210:
        # Move forward until the Robot is close to the Pin
        ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
        drivetrain.drive(FORWARD)
        
    drivetrain.drive_for(FORWARD, 270, MM)
    claw_motor.spin(FORWARD)

vr_thread(main)

。ID#

.id returns the ID of the detected AI Classification as an integer.

人工智能分类

ID

签名

光束

0

GameElements.BEAM

蓝针

1

GameElements.BLUE_PIN

红针

2

GameElements.RED_PIN

橙色别针

3

GameElements.ORANGE_PIN

def main():
    # Find the object index of the nearest Blue Pin
    lift_motor.spin_for(REVERSE, 600, DEGREES)
    object_count = 1

    ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)

    # Iterate through all detected objects
    if len(ai_objects) > 0:
        for repeat_count in range(len(ai_objects)):
            ai_objects_index = object_count - 1
            if ai_objects[repeat_count].id == GameElements.BLUE_PIN:
                brain.screen.print(str("Closest Blue Pin is ") + str(object_count))
            else:
                object_count = object_count + 1

vr_thread(main)

传感#

Touch LED#

设置颜色#

set_color sets the color of the Touch LED.

Usage:
touchled.set_color(color)

参数

描述

color

The color to set the Touch LED to:

  • NONE – Turns off the Touch LED
  • RED
  • GREEN
  • BLUE
  • YELLOW
  • ORANGE
  • PURPLE
  • WHITE
  • RED_VIOLET
  • VIOLET
  • BLUE_VIOLET
  • BLUE_GREEN
  • YELLOW_GREEN
  • YELLOW_ORANGE
  • RED_ORANGE

def main():
  # Place a Pin atop another Pin
  touchled.set_color(RED)
  lift_motor.spin_for(REVERSE, 600, DEGREES)
  drivetrain.drive_for(FORWARD, 140, MM)
  claw_motor.spin(FORWARD)
  touchled.set_color(NONE)

Optical#

靠近物体#

is_near_object returns a Boolean indicating whether or not the Optical Sensor detects an object close to the sensor.

  • True – The object is close to the Optical Sensor.

  • False – The object is not close to the Optical Sensor.

Usage:
optical.is_near_object()

参数

描述

该方法没有参数。

def main():
  # Place a Pin atop another Pin
  lift_motor.spin_for(REVERSE, 600, DEGREES)
  drivetrain.drive_for(FORWARD, 140, MM)
  claw_motor.spin(FORWARD)
  while not not optical.is_near_object():
      wait(2, MSEC)
  lift_motor.spin_to_position(0, DEGREES)

颜色#

color returns the color detected by the Optical Sensor:

返回的颜色:

  • NONE – No color detected.
  • RED
  • GREEN
  • BLUE
  • YELLOW
  • ORANGE
  • PURPLE
  • WHITE
  • RED_VIOLET
  • VIOLET
  • BLUE_VIOLET
  • BLUE_GREEN
  • YELLOW_GREEN
  • YELLOW_ORANGE
  • RED_ORANGE

Usage:
optical.color()

参数

描述

该方法没有参数。

def main():
  # Place a Pin atop another Pin
  lift_motor.spin_for(REVERSE, 600, DEGREES)
  drivetrain.drive_for(FORWARD, 140, MM)
  claw_motor.spin(FORWARD)
  while not optical.color() == RED:
      wait(5, MSEC)
  lift_motor.spin_to_position(0, DEGREES)

亮度#

brightness returns the brightness value detected by the Optical Sensor as a percent from 0% to 100%.

Usage:
optical.brightness()

参数

描述

该方法没有参数。

def main():
  # Place a Pin atop another Pin
  lift_motor.spin_for(REVERSE, 600, DEGREES)
  drivetrain.drive_for(FORWARD, 140, MM)
  claw_motor.spin(FORWARD)
  while not 30 > optical.brightness():
      wait(5, MSEC)
  lift_motor.spin_to_position(0, DEGREES)

色调#

hue returns the hue detected by the Optical Sensor.

色调值范围从 0 到 359 度,对应于下面显示的色轮上的位置。

圆形色轮,显示全光谱色调,周边标有度数值,从顶部的 0° 到 360°,以 30 度为增量增加。

Usage:
optical.hue()

参数

描述

该方法没有参数。

def main():
  # Place a Pin atop another Pin
  lift_motor.spin_for(REVERSE, 600, DEGREES)
  drivetrain.drive_for(FORWARD, 140, MM)
  claw_motor.spin(FORWARD)
  while not optical.hue() > 0:
      wait(5, MSEC)
  lift_motor.spin_to_position(0, DEGREES)

检测到物体#

object_detected registers a callback function for when the Optical Sensor detects an object.

Usage:
optical.object_detected(callback, arg)

参数

描述

callback

当检测到物体时调用的函数。

arg

可选。用于将参数传递给回调函数的元组。

def pin_in_open_claw():
  # Change color when claw isn't holding anything
  touchled.set_color(RED)

def main():
  # Place a Pin atop another Pin
  optical.object_detected(pin_in_open_claw)
  touchled.set_color(GREEN)
  lift_motor.spin_for(REVERSE, 600, DEGREES)
  drivetrain.drive_for(FORWARD, 140, MM)
  claw_motor.spin(FORWARD)

object_lost#

object_lost registers a callback function for when the Optical Sensor loses an object.

Usage:
optical.object_lost(callback, arg)

参数

描述

callback

当对象丢失时调用的函数。

arg

可选。用于将参数传递给回调函数的元组。

def lower_lift():
  # Lower lift when Pin not in claw
  lift_motor.spin_to_position(0, DEGREES)

def main():
  # Place a Pin atop another Pin
  optical.object_lost(lower_lift)
  lift_motor.spin_for(REVERSE, 600, DEGREES)
  drivetrain.drive_for(FORWARD, 140, MM)
  claw_motor.spin(FORWARD)