机器人专用 Python#

介绍#

V5RC 21-22 Tipping Point 游乐场采用了专为该游乐场设计的独有方法,包括两种电机选项、AI 视觉传感器、光学传感器和游戏定位系统 (GPS) 传感器。

所有标准的 VEXcode VR 方法都可以在 V5RC 21-22 临界点 游乐场中使用。

以下是所有可用的 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.

  • Getters

    • 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.

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

  • 光学的

    • 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.

  • 旋转

    • set_position – Sets the current position of the Rotation Sensor to a specific value.

    • angle – Returns the current angle of the sensor between 0 and 359.99 degrees.

    • position – Returns the total rotational position in degrees or turns.

  • 全球定位系统

    • x_position – Returns the current x coordinate of a GPS Sensor on the field.

    • y_position – Returns the current y coordinate of a GPS Sensor on the field.

    • heading – Returns the heading that the robot is currently facing based on the GPS Sensor’s readings from 0 to 359 degrees.

本页示例使用 Playground 的默认起始位置。

运动#

行动#

旋转#

spin spins a motor or motor group in the specified direction indefinitely.

Usage:
fork_motor_group.spin(direction)

参数

描述

direction

The direction for the motor group to spin:

  • FORWARD
  • REVERSE
def main():
    # Spin the motor group forward then stop
    fork_motor_group.spin(FORWARD)
    wait(0.5, SECONDS)
    fork_motor_group.stop()

# VR threads — Do not delete
vr_thread(main)

spin_for#

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

Usage:
fork_motor_group.spin_for(direction, distance, units, wait)

参数

描述

direction

The direction for the motor group to spin:

  • FORWARD
  • REVERSE

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():
    # Spin the motor group forward then stop
    fork_motor_group.spin_for(FORWARD, 1, TURNS)

# VR threads — Do not delete
vr_thread(main)

自旋至位置#

spin_to spins a motor or motor group to a given position.

Usage:
fork_motor_group.spin_to_position(angle, units, wait)

参数

描述

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():
    # Spin the motor group forward then stop
    fork_motor_group.spin_to_position(55, DEGREES)

# VR threads — Do not delete
vr_thread(main)

停止#

stop stops a motor or motor group from spinning.

Usage:
fork_motor_group.stop()

参数

描述

此方法没有参数。

def main():
    # Spin the motor group forward then stop
    fork_motor_group.spin(FORWARD)
    wait(0.5, SECONDS)
    fork_motor_group.stop()

# VR threads — Do not delete
vr_thread(main)

变异体#

设置位置#

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

Usage:
fork_motor_group.set_position(position, units)

参数

描述

position

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

units

The unit that represents the angle to rotate to:

  • DEGREES
  • TURNS
def main():
    # Spin the motor group forward then stop
    fork_motor_group.set_position(90, DEGREES)
    fork_motor_group.spin_to_position(0, DEGREES)

# VR threads — Do not delete
vr_thread(main)

设置速度#

set_velocity sets the speed of a motor or motor group.

Usage:
fork_motor_group.set_velocity(velocity, units)

参数

描述

velocity

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

units

The unit that represents the new velocity:

  • PERCENT
def main():
    # Spin the motor group forward then stop
    fork_motor_group.set_velocity(75, PERCENT)
    fork_motor_group.spin_to_position(180, DEGREES)

# VR threads — Do not delete
vr_thread(main)

设置超时#

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

Usage:
fork_motor_group.set_timeout(value, units)

参数

描述

value

电机组在停止前等待的时间。

units

The unit to represent the timeout:

  • SECONDS
  • MSEC — milliseconds
def main():
    # Spin the motor group forward then stop
    fork_motor_group.set_timeout(2, SECONDS)
    fork_motor_group.spin_to_position(270, DEGREES)

# VR threads — Do not delete
vr_thread(main)

获取器#

已完成#

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

  • True – The specified motor is not spinning.

  • False – The specified motor is spinning.

Usage:
fork_motor_group.is_done()

参数

描述

此方法没有参数。

正在旋转#

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

  • True – The specified motor is spinning.

  • False – The specified motor is not spinning.

Usage:
fork_motor_group.is_spinning()

参数

描述

此方法没有参数。

位置#

position returns the total distance the specified motor or motor group has rotated.

Usage:
fork_motor_group.position(units)

参数

描述

units

The units that represent the motor group’s position:

  • DEGREES
  • TURNS

速度#

velocity returns the current rotational speed of the motor or motor group.

Usage:
fork_motor_group.velocity(units)

参数

描述

units

The unit that represent the motor group’s position:

  • PERCENT

传感#

光学的#

靠近对象#

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()

参数

描述

此方法没有参数。

颜色#

color returns the color detected by the Optical Sensor:

返回颜色:

  • NONE – No color detected.
  • RED
  • GREEN
  • BLUE
  • YELLOW
  • ORANGE
  • PURPLE
  • CYAN

Usage:
optical.color()

参数

描述

此方法没有参数。

亮度#

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

Usage:
optical.brightness()

参数

描述

此方法没有参数。

色调#

hue returns the hue detected by the Optical Sensor.

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

一个圆形色轮,显示完整的色调光谱,边缘标有度数,从顶部的 0° 到 360°,以 30° 为增量递增。

Usage:
optical.hue()

参数

描述

此方法没有参数。

检测到目标#

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

Usage:
optical.object_detected(callback, arg)

参数

描述

callback

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

arg

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

丢失对象#

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

Usage:
optical.object_lost(callback, arg)

参数

描述

callback

当检测到的物体丢失时,将调用该函数。

arg

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

旋转#

设置位置#

set_position sets the current position of the Rotation Sensor to a value in degrees.

Usage:
rotation.set_position(position, units)

参数

描述

位置

旋转传感器的设置位置。

单位

Optional. The unit of measurement:

  • DEGREES (default)
  • TURNS

角度#

angle returns the current angle of the sensor as a float.

Usage:
rotation.angle(units)

参数

描述

单位

Optional. The unit of measurement:

  • DEGREES (default) – 0 to 359.99
  • TURNS

位置#

position returns the total rotational position.

Usage:
rotation.position(units)

参数

描述

单位

Optional. The unit of measurement:

  • DEGREES (default) – 0 to 359 as an integer
  • TURNS – As a float

全球定位系统#

x_position#

x_position returns the current x coordinate of a GPS (Game Positioning System™) Sensor on the field.

Usage:
gps.x_position(units)

参数

描述

单位

The unit of the offset value:

  • INCHES
  • MM

y 位置#

y_position returns the current y coordinate of a GPS (Game Positioning System™) Sensor on the field.

Usage:
gps.y_position(units)

参数

描述

单位

The unit of the offset value:

  • INCHES
  • MM

标题#

heading returns the heading that the robot is currently facing based on the GPS (Game Positioning System™) Sensor’s readings from 0 to 359 degrees.

Usage:
gps.heading()

参数

描述

此方法没有参数。