机器人专用 Python#

介绍#

VIQRC 22-23 Slapshot 游乐场具有该游乐场独有的方法,包括两种电机选择、进气缓冲器和光学传感器。

所有标准的 VEXcode VR 方法都可以在 VIQRC 22-23 Slapshot 游乐场中使用。

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

运动控制 - 控制机器人电机的移动和跟踪。

  • 操作

    • spin - Spins the selected motor indefinitely.

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

    • spin_to_position - Spins a motor to a set position.

    • stop - Stops a specific motor from spinning.

  • 变异因子

    • set_position - Sets the encoder value of a motor.

    • set_velocity - Sets the speed of a motor as a percentage.

    • set_timeout - Limits how long a motor command 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 percent.

事件 - 当传感器状态改变时运行代码。

  • 保险杠

    • pressed - Registers a callback function for when the Intake Bumper is pressed.

    • released - Registers a callback function for when the Intake Bumper is released.

  • 光学的

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

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

  • 保险杠

    • pressing - Returns whether the Intake Bumper is currently pressed.

  • 光学的

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

    • color - Returns the color detected by the Optical Sensor.

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

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

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

运动#

行动#

旋转#

spin spins a motor indefinitely.

用法:
此方法可以使用两种可用的电机对象之一,如下所示:

发动机

命令

intake_motor

intake_motor.spin(direction) - The intake motor

arm_motor

arm_motor.spin(direction) - The arm motor

参数

描述

direction

The direction for the motor to spin:

  • FORWARD - Spins the intake in the intake direction or raises the arm.
  • REVERSE - Spins the intake in the outtake direction or lowers the arm.

def main():
    # Spin the Intake Motor to collect a Disc
    intake_motor.spin(FORWARD)
    wait(1, SECONDS)
    intake_motor.stop()

# VR threads — Do not delete
vr_thread(main)

spin_for#

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

用法:
此方法可以使用两种可用的电机对象之一,如下所示:

发动机

命令

intake_motor

intake_motor.spin_for(direction, distance, units, wait=True) - The intake motor

arm_motor

arm_motor.spin_for(direction, distance, units, wait=True) - The arm motor

参数

描述

direction

The direction for the motor to spin:

  • FORWARD - Spins the intake in the intake direction or raises the arm.
  • REVERSE - Spins the intake in the outtake direction or lowers the arm.

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.

def main():
    # Collect the preloaded Disc
    intake_motor.spin_for(FORWARD, 180, DEGREES)

# VR threads — Do not delete
vr_thread(main)

自旋至位置#

spin_to_position spins a motor to a given position.

用法:
此方法可以使用两种可用的电机对象之一,如下所示:

发动机

命令

intake_motor

intake_motor.spin_to_position(angle, units, wait=True) - The intake motor

arm_motor

arm_motor.spin_to_position(angle, units, wait=True) - The arm 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.

def main():
    # Raise the Arm to reach over the barrier
    arm_motor.spin_to_position(600, DEGREES)

# VR threads — Do not delete
vr_thread(main)

停止电机#

stop stops a motor from spinning.

用法:
此方法可以使用两种可用的电机对象之一,如下所示:

发动机

命令

intake_motor

intake_motor.stop() - The intake motor

arm_motor

arm_motor.stop() - The arm motor

参数

描述

此方法没有参数。

def main():
    # Raise the Arm briefly, then stop it
    arm_motor.spin(FORWARD)
    wait(1, SECONDS)
    arm_motor.stop()

# VR threads — Do not delete
vr_thread(main)

变异体#

设置位置#

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

用法:
此方法可以使用两种可用的电机对象之一,如下所示:

发动机

命令

intake_motor

intake_motor.set_position(position, units) - The intake motor

arm_motor

arm_motor.set_position(position, units) - The arm motor

参数

描述

position

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

units

The unit that represents the angle to rotate to:

  • DEGREES
  • TURNS

def main():
    # Set the Intake Motor position, then return to 0 degrees
    intake_motor.set_position(180, DEGREES)
    intake_motor.spin_to_position(0, DEGREES)

# VR threads — Do not delete
vr_thread(main)

设置速度#

set_velocity sets the speed of a motor.

用法:
此方法可以使用两种可用的电机对象之一,如下所示:

发动机

命令

intake_motor

intake_motor.set_velocity(velocity, units) - The intake motor

arm_motor

arm_motor.set_velocity(velocity, units) - The arm motor

参数

描述

velocity

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

units

The unit that represents the new velocity:

  • PERCENT

def main():
    # Set the Intake Motor speed before collecting a Disc
    intake_motor.set_velocity(75, PERCENT)
    intake_motor.spin_for(FORWARD, 180, DEGREES)

# VR threads — Do not delete
vr_thread(main)

设置超时#

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.

用法:
此方法可以使用两种可用的电机对象之一,如下所示:

发动机

命令

intake_motor

intake_motor.set_timeout(value, units) - The intake motor

arm_motor

arm_motor.set_timeout(value, units) - The arm motor

参数

描述

value

电机停止前等待的时间。

units

The unit to represent the timeout:

  • SECONDS
  • MSEC - milliseconds

def main():
    # Limit how long the Arm Motor waits to reach its target
    arm_motor.set_timeout(2, SECONDS)
    arm_motor.spin_for(FORWARD, 3, TURNS)

# VR threads — Do not delete
vr_thread(main)

获取器#

已完成#

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.

用法:
此方法可以使用两种可用的电机对象之一,如下所示:

发动机

命令

intake_motor

intake_motor.is_done() - The intake motor

arm_motor

arm_motor.is_done() - The arm motor

参数

描述

此方法没有参数。

正在旋转#

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.

用法:
此方法可以使用两种可用的电机对象之一,如下所示:

发动机

命令

intake_motor

intake_motor.is_spinning() - The intake motor

arm_motor

arm_motor.is_spinning() - The arm motor

参数

描述

此方法没有参数。

位置#

position returns the total distance the specified motor has rotated.

用法:
此方法可以使用两种可用的电机对象之一,如下所示:

发动机

命令

intake_motor

intake_motor.position(units) - The intake motor

arm_motor

arm_motor.position(units) - The arm motor

参数

描述

units

The units that represent the motor’s position:

  • DEGREES
  • TURNS

def main():
    # Print the current Arm Motor position
    brain.screen.print(arm_motor.position(DEGREES))

# VR threads — Do not delete
vr_thread(main)

速度#

velocity returns the current rotational speed of the motor.

用法:
此方法可以使用两种可用的电机对象之一,如下所示:

发动机

命令

intake_motor

intake_motor.velocity(units) - The intake motor

arm_motor

arm_motor.velocity(units) - The arm motor

参数

描述

units

The unit that represents the motor’s velocity:

  • PERCENT

def main():
    # Print the current Intake Motor velocity
    brain.screen.print(intake_motor.velocity(PERCENT))

# VR threads — Do not delete
vr_thread(main)

活动#

保险杠#

按下#

pressed registers a callback function for when the Intake Bumper is pressed.

Usage:
intake_bumper.pressed(callback)

参数

描述

callback

按下进气缓冲器时将调用的函数。

def stop_intake():
    intake_motor.stop()

def main():
    # Stop the Intake Motor when a Disc reaches the Intake Bumper
    intake_bumper.pressed(stop_intake)
    intake_motor.spin(FORWARD)

# VR threads — Do not delete
vr_thread(main)

发布#

released registers a callback function for when the Intake Bumper is released.

Usage:
intake_bumper.released(callback)

参数

描述

callback

当进气缓冲器释放时将调用的函数。

def lower_arm():
    arm_motor.spin_to_position(0, DEGREES)

def main():
    # Lower the Arm when the Intake Bumper is released
    intake_bumper.released(lower_arm)

# VR threads — Do not delete
vr_thread(main)

光学的#

检测到目标#

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

Usage:
front_optical.object_detected(callback, arg)

参数

描述

callback

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

arg

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

def stop_intake():
    intake_motor.stop()

def main():
    # Stop the Intake Motor when the Optical Sensor detects a Disc
    front_optical.object_detected(stop_intake)
    intake_motor.spin(FORWARD)

# VR threads — Do not delete
vr_thread(main)

丢失对象#

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

Usage:
front_optical.object_lost(callback, arg)

参数

描述

callback

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

arg

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

def resume_intake():
    intake_motor.spin(FORWARD)

def main():
    # Restart the Intake Motor when the Optical Sensor no longer detects a Disc
    front_optical.object_lost(resume_intake)

# VR threads — Do not delete
vr_thread(main)

传感#

保险杠#

紧迫#

pressing returns a Boolean indicating whether the Intake Bumper is currently pressed.

  • True - The Intake Bumper is pressed.

  • False - The Intake Bumper is not pressed.

Usage:
intake_bumper.pressing()

参数

描述

此方法没有参数。

def main():
    # Print whether the Intake Bumper is pressed
    brain.screen.print(intake_bumper.pressing())

# VR threads — Do not delete
vr_thread(main)

光学的#

靠近对象#

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:
front_optical.is_near_object()

参数

描述

此方法没有参数。

def main():
    # Print whether the Front Optical Sensor detects a nearby Disc
    brain.screen.print(front_optical.is_near_object())

# VR threads — Do not delete
vr_thread(main)

颜色#

color returns the color detected by the Optical Sensor:

返回颜色:

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

Usage:
front_optical.color()

参数

描述

此方法没有参数。

def main():
    # Print the color detected by the Front Optical Sensor
    brain.screen.print(front_optical.color())

# VR threads — Do not delete
vr_thread(main)

亮度#

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

Usage:
front_optical.brightness()

参数

描述

此方法没有参数。

def main():
    # Print the Front Optical Sensor brightness
    brain.screen.print(front_optical.brightness())

# VR threads — Do not delete
vr_thread(main)

色调#

hue returns the hue detected by the Optical Sensor.

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

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

Usage:
front_optical.hue()

参数

描述

此方法没有参数。

def main():
    # Print the hue detected by the Front Optical Sensor
    brain.screen.print(front_optical.hue())

# VR threads — Do not delete
vr_thread(main)