机器人专用 Python#
介绍#
VIQRC 21-22 投篮练习场 具有该练习场独有的功能,包括三种电机选项和颜色传感器。
所有标准的 VEXcode VR 方法都可以在 VIQRC 21-22 Pitching In 游乐场中使用。
以下是所有可用的 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.
感知——利用机器人的各种传感器。
颜色
color- Returns the color detected by the Color Sensor.brightness- Returns the brightness percentage detected by the sensor.hue- Returns the hue value of the detected color.is_near_object- Returns whether a detected object is near the Color Sensor.
本页示例使用 Playground 的默认起始位置。
运动#
行动#
旋转#
spin spins a motor indefinitely.
用法:
此方法可以使用以下三种可用电机对象之一:
发动机 |
命令 |
|---|---|
|
|
|
|
|
|
参数 |
描述 |
|---|---|
|
The direction for the motor to spin depends on the selected motor.
|
def main():
# Spin the Intake Motor to collect a Ball
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.
用法:
此方法可以使用以下三种可用电机对象之一:
发动机 |
命令 |
|---|---|
|
|
|
|
|
|
参数 |
描述 |
|---|---|
|
电机的旋转方向取决于所选电机。 |
|
电机旋转的距离,以整数表示。 |
|
The unit that represents the distance to rotate:
|
|
Optional.
|
def main():
# Collect a Ball with the Intake Motor
intake_motor.spin_for(FORWARD, 1, TURNS)
# VR threads — Do not delete
vr_thread(main)
自旋至位置#
spin_to_position spins a motor to a given position.
用法:
此方法可以使用以下三种可用电机对象之一:
发动机 |
命令 |
|---|---|
|
|
|
|
|
|
参数 |
描述 |
|---|---|
|
电机旋转的具体角度或圈数。 |
|
The unit that represents the angle to rotate to:
|
|
Optional.
|
def main():
# Move the Catapult Motor to a launch position
catapult_motor.spin_to_position(2600, DEGREES)
# VR threads — Do not delete
vr_thread(main)
停止电机#
stop stops a motor from spinning.
用法:
此方法可以使用以下三种可用电机对象之一:
发动机 |
命令 |
|---|---|
|
|
|
|
|
|
参数 |
描述 |
|---|---|
此方法没有参数。 |
def main():
# Spin the Intake Motor, then stop it
intake_motor.spin(FORWARD)
wait(1, SECONDS)
intake_motor.stop()
# VR threads — Do not delete
vr_thread(main)
变异体#
设置位置#
set_position sets a motor’s encoder position to the given position value.
用法:
此方法可以使用以下三种可用电机对象之一:
发动机 |
命令 |
|---|---|
|
|
|
|
|
|
参数 |
描述 |
|---|---|
|
要设置的电机编码器的具体整数值。 |
|
The unit that represents the angle to rotate to:
|
def main():
# Set the Catapult Motor position, then return it to 0 degrees
catapult_motor.set_position(2600, DEGREES)
catapult_motor.spin_to_position(0, DEGREES)
# VR threads — Do not delete
vr_thread(main)
设置速度#
set_velocity sets the speed of a motor.
用法:
此方法可以使用以下三种可用电机对象之一:
发动机 |
命令 |
|---|---|
|
|
|
|
|
|
参数 |
描述 |
|---|---|
|
电机旋转的速度,范围从 -100 到 100。 |
|
The unit that represents the new velocity:
|
def main():
# Set the Catapult Motor velocity before moving it
catapult_motor.set_velocity(75, PERCENT)
catapult_motor.spin_to_position(2600, 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.
用法:
此方法可以使用以下三种可用电机对象之一:
发动机 |
命令 |
|---|---|
|
|
|
|
|
|
参数 |
描述 |
|---|---|
|
电机停止前等待的时间。 |
|
The unit to represent the timeout:
|
def main():
# Limit how long the Catapult Motor waits to reach its target
catapult_motor.set_timeout(2, SECONDS)
catapult_motor.spin_to_position(5000, DEGREES)
# 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.
用法:
此方法可以使用以下三种可用电机对象之一:
发动机 |
命令 |
|---|---|
|
|
|
|
|
|
参数 |
描述 |
|---|---|
此方法没有参数。 |
正在旋转#
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.
用法:
此方法可以使用以下三种可用电机对象之一:
发动机 |
命令 |
|---|---|
|
|
|
|
|
|
参数 |
描述 |
|---|---|
此方法没有参数。 |
位置#
position returns the total distance the specified motor has rotated.
用法:
此方法可以使用以下三种可用电机对象之一:
发动机 |
命令 |
|---|---|
|
|
|
|
|
|
参数 |
描述 |
|---|---|
|
The units that represent the motor’s position:
|
def main():
# Print the current Catapult Motor position
brain.screen.print(catapult_motor.position(DEGREES))
# VR threads — Do not delete
vr_thread(main)
速度#
velocity returns the current rotational speed of the motor.
用法:
此方法可以使用以下三种可用电机对象之一:
发动机 |
命令 |
|---|---|
|
|
|
|
|
|
参数 |
描述 |
|---|---|
|
The unit that represents the motor’s velocity:
|
def main():
# Print the current Intake Motor velocity
brain.screen.print(intake_motor.velocity(PERCENT))
# VR threads — Do not delete
vr_thread(main)
传感#
颜色#
颜色#
color returns the color detected by the Color Sensor:
返回颜色: |
|---|
|
Usage:
color.color()
参数 |
描述 |
|---|---|
此方法没有参数。 |
def main():
# Print the color detected by the Color Sensor
brain.screen.print(color.color())
# VR threads — Do not delete
vr_thread(main)
亮度#
brightness returns the brightness value detected by the Color Sensor as a percent from 0% to 100%.
Usage:
color.brightness()
参数 |
描述 |
|---|---|
此方法没有参数。 |
def main():
# Print the Color Sensor brightness
brain.screen.print(color.brightness())
# VR threads — Do not delete
vr_thread(main)
色调#
hue returns the hue detected by the Color Sensor.
色调值范围从 0 到 359 度,对应于下图所示色轮上的位置。

Usage:
color.hue()
参数 |
描述 |
|---|---|
此方法没有参数。 |
def main():
# Print the hue detected by the Color Sensor
brain.screen.print(color.hue())
# VR threads — Do not delete
vr_thread(main)
靠近对象#
is_near_object returns a Boolean indicating whether or not the Color Sensor detects an object close to the sensor.
True- The object is close to the Color Sensor.False- The object is not close to the Color Sensor.
Usage:
color.is_near_object()
参数 |
描述 |
|---|---|
此方法没有参数。 |
def main():
# Print whether the Color Sensor detects an object nearby
brain.screen.print(color.is_near_object())
# VR threads — Do not delete
vr_thread(main)