机器人专用 Python#
介绍#
Hero Bot Swish 包含一个电机选项、一个电机组选项和光学传感器。
所有标准的 VEXcode VR 方法都可以在 VIQRC 24-25 快速接力 游乐场中使用。
以下是所有可用的机器人专用方法列表:
驱动系统——控制机器人的移动和转向。
操作
drive— Moves the robot forward or reverse forever.drive_for— Moves the robot forward or reverse for a specific distance.turn— Turns the robot left or right forever.turn_for— Turns the robot left or right for a specific number of degrees.turn_to_heading— Turns the robot to face a specific heading from -359 to 359 degrees.turn_to_rotation— Turns the robot to a specific rotation.stop— Stops the robot’s movement.
变异因子
set_heading— Changes the robot’s current heading to a new heading.set_rotation— Changes the robot’s current rotation to a new rotation.set_timeout— Sets how much time the robot will try to finish a movement.set_drive_velocity— Tells the robot how fast to drive.set_turn_velocity— Tells the robot how fast to turn.
Getters
heading— Returns the robot’s current heading from 0 to 359.99 degrees.rotation— Returns the robot’s current rotation.is_done— Returns whether the robot is finished moving, as a Boolean value.is_moving— Returns whether the robot is moving, as a Boolean value.velocity— Returns how fast the robot is driving.
运动控制 - 控制机器人的电机和电机组的移动和跟踪。
操作
spin— Spins a motor or motor group forward or reverse forever.spin_for— Spins a motor or motor group for a specific distance.spin_to_position— Spins a motor or motor group to a specific position.stop— Stops a motor or motor group from spinning.
变异因子
set_position— Changes the motor or motor group’s current position to a new value.set_velocity— Tells a motor or motor group how fast to spin.set_timeout— Sets how much time a motor or motor group will try to finish a movement.
Getters
is_done— Returns whether the motor or motor group is finished moving, as a Boolean value.is_spinning— Returns whether the motor or motor group is spinning, as a Boolean value.position— Returns the motor or motor group’s current position.velocity— Returns how fast the motor or motor group is spinning.
控制台 – 将信息打印到控制台并监控数值。
打印
brain.screen.print— Displays text, numbers, or variable values in the Console.brain.screen.next_row— Moves the Console cursor to the start of the next row.brain.screen.clear_screen— Clears all rows from the Console.
监视器
monitor_variable— Adds one or more predefined variables to the Monitor tab.monitor_sensor— Adds one or more sensor values to the Monitor tab.
感知——利用机器人的各种传感器。
光学的
is_near_object– Returns whether the Optical Sensor detects an object within range.color– Returns the color detected by the Optical Sensor.brightness– Returns the amount of light reflected from the object.hue– Returns the hue detected by the Optical Sensor.object_detected– Registers a function to be called when the Optical Sensor detects an object.object_lost– Registers a function to be called when the Optical Sensor loses an object.
本页示例使用 Playground 的默认起始位置。
传动系统#
动力系统控制着VR机器人的行驶和转向。动力系统可以前进或后退、左转或右转、转向特定航向并跟踪其旋转。
行动#
驾驶#
drive moves the robot forward or reverse forever. The robot will continue to move until it is given another action, like turning or stopping.
Usage:
drivetrain.drive(direction)
参数 |
描述 |
|---|---|
|
The direction the robot moves: |
def main():
# Drive forward then stop
drivetrain.drive(FORWARD)
wait(2, SECONDS)
drivetrain.stop()
# VR threads — Do not delete
vr_thread(main)
驱动#
drive_for moves the robot forward or reverse for a specific distance. The project will wait until the robot is done moving before the next line of code runs.
Usage:
drivetrain.drive_for(direction, distance, units, wait)
参数 |
描述 |
|---|---|
|
The direction the robot moves: |
|
The distance the robot drives. This can be an |
|
The distance unit: |
|
Optional. |
def main():
# Drive forward and backward
drivetrain.drive_for(FORWARD, 200, MM)
drivetrain.drive_for(REVERSE, 200, MM)
# VR threads — Do not delete
vr_thread(main)
转动#
turn turns the robot left or right forever. The robot will continue to turn until it is given another action, like driving or stopping.
Usage:
drivetrain.turn(direction)
参数 |
描述 |
|---|---|
|
The direction the robot turns: |
def main():
# Turn right and left, then stop
drivetrain.turn(RIGHT)
wait(2, SECONDS)
drivetrain.turn(LEFT)
wait(2, SECONDS)
drivetrain.stop()
# VR threads — Do not delete
vr_thread(main)
转弯#
turn_for turns the robot left or right for a specific number of degrees. The turn is relative to the current position of the robot. The project will wait until the robot is done turning before the next line of code runs.
Usage:
drivetrain.turn_for(direction, angle, units, wait)
参数 |
描述 |
|---|---|
|
The direction the robot turns: |
|
The number of degrees the robot turns. This can be an |
|
The rotation unit: |
|
Optional. |
def main():
# Turn the robot right and left
drivetrain.turn_for(RIGHT, 90, DEGREES)
wait(1, SECONDS)
drivetrain.turn_for(LEFT, 90, DEGREES)
# VR threads — Do not delete
vr_thread(main)
转向航向#
A heading is the direction the robot is facing, measured in degrees. turn_to_heading turns the robot to face a specific heading from -359 to 359 degrees. The robot will turn the shortest direction to reach the target heading.
机器人的初始航向角为0度。
程序会等到机器人完成转向后才运行下一行代码。
Usage:
drivetrain.turn_to_heading(angle, units, wait)
参数 |
描述 |
|---|---|
|
The direction the robot should face, in degrees. This can be an |
|
The rotation unit: |
|
Optional. |
def main():
# Turn to face the cardinal directions
drivetrain.turn_to_heading(90, DEGREES)
wait(1, SECONDS)
drivetrain.turn_to_heading(180, DEGREES)
wait(1, SECONDS)
drivetrain.turn_to_heading(270, DEGREES)
wait(1, SECONDS)
drivetrain.turn_to_heading(0, DEGREES)
# VR threads — Do not delete
vr_thread(main)
转向旋转#
turn_to_rotation turns the robot to a specific rotation.
Rotation is how much the robot has turned, measured in degrees. At the beginning of a project, the rotation value is set to 0 degrees. Rotation can also be set using the set_rotation method.
旋转角度是绝对值。这意味着转弯方向取决于机器人当前的旋转角度。向右转弯会增加旋转角度,向左转弯会减少旋转角度。
例如,如果机器人从0度开始,你将其旋转720度,它会向右转两次。如果你再将其旋转360度,它会向左转一次,因为360小于720。
程序会等到机器人完成转向后才运行下一行代码。
Usage:
drivetrain.turn_to_rotation(angle, units, wait)
参数 |
描述 |
|---|---|
|
The rotation value, in degrees, that the robot will turn to. This can be an |
|
The rotation unit: |
|
Optional. |
def main():
# Turn left, then spin in a circle
# clockwise and face right
drivetrain.turn_to_rotation(-90, DEGREES)
wait(1, SECONDS)
drivetrain.turn_to_rotation(450, DEGREES)
# VR threads — Do not delete
vr_thread(main)
停止#
stop stops the robot’s movement.
Usage:
drivetrain.stop()
参数 |
描述 |
|---|---|
此方法没有参数。 |
def main():
# Drive forward then stop
drivetrain.drive(FORWARD)
wait(2, SECONDS)
drivetrain.stop()
# VR threads — Do not delete
vr_thread(main)
变异体#
设置标题#
A heading is the direction the robot is facing, measured in degrees. set_heading changes the robot’s current heading to a new heading value.
例如,如果机器人转向右侧,将航向设置为 0 度,则该右侧位置将成为新的 0 度航向。然后,机器人可以根据新的航向转向其他位置。
Usage:
drivetrain.set_heading(heading, units)
参数 |
描述 |
|---|---|
|
The heading value, in degrees, to set for the robot. This can be an |
|
The heading unit: |
def main():
# Face the new 0 degrees
drivetrain.set_heading(90, DEGREES)
drivetrain.turn_to_heading(0, DEGREES)
# VR threads — Do not delete
vr_thread(main)
设置旋转#
Rotation is how much the robot has turned, measured in degrees. At the beginning of a project, the rotation value is set to 0 degrees. set_rotation changes the robot’s current rotation to a new value.
例如,如果机器人向右转了两圈,它的旋转角度将为 720 度。将旋转角度设置为 0 度会将旋转角度从 720 度重置为 0 度。然后,机器人可以根据这个新的旋转角度进行旋转。
Usage:
drivetrain.set_rotation(rotation, units)
参数 |
描述 |
|---|---|
|
The rotation value, in degrees, to set for the robot. This can be an |
|
The rotation unit: |
def main():
# Spin counterclockwise two times
drivetrain.set_rotation(720, DEGREES)
drivetrain.turn_to_rotation(0, DEGREES)
# VR threads — Do not delete
vr_thread(main)
设置超时#
set_timeout sets how much time the robot will try to finish a movement. If the robot cannot finish in that time, it will stop trying and move on to the next line of code. This keeps the robot from getting stuck on a movement.
Usage:
drivetrain.set_timeout(value, units)
参数 |
描述 |
|---|---|
|
The amount of time the robot can try to finish a movement. This can be a positive |
|
The time unit: |
def main():
# Turn right after driving forward
drivetrain.set_timeout(1, SECONDS)
drivetrain.drive_for(FORWARD, 25, INCHES)
drivetrain.turn_for(RIGHT, 90)
# VR threads — Do not delete
vr_thread(main)
设置驱动速度#
set_drive_velocity tells the robot how fast to drive. A higher percentage makes the robot drive faster and a lower percentage makes the robot drive slower.
每个项目开始时,机器人默认以 50% 的速度行驶。
**注意:**速度越高,机器人行驶速度越快,但精度可能越低;速度越低,机器人行驶速度越慢,但精度可能越高。
Usage:
drivetrain.set_drive_velocity(velocity, units)
参数 |
描述 |
|---|---|
|
The velocity to drive with from 0% to 100%. This can be an |
|
The velocity unit: |
def main():
# Drive forward at different velocities
# Default velocity
drivetrain.drive_for(FORWARD, 150, MM)
wait(1, SECONDS)
# Drive slower
drivetrain.set_drive_velocity(20, PERCENT)
drivetrain.drive_for(FORWARD, 150, MM)
wait(1, SECONDS)
# Drive faster
drivetrain.set_drive_velocity(100, PERCENT)
drivetrain.drive_for(FORWARD, 150, MM)
# VR threads — Do not delete
vr_thread(main)
设置转弯速度#
set_turn_velocity tells the robot how fast to turn. A higher percentage makes the robot turn faster and a lower percentage makes the robot turn slower.
每个项目开始时,机器人默认以 50% 的速度旋转。
**注意:**速度越高,机器人转弯越快,但精度可能越低;速度越低,机器人转弯越慢,但精度可能越高。
Usage:
drivetrain.set_turn_velocity(velocity, units)
参数 |
描述 |
|---|---|
|
The velocity to turn with from 0% to 100%. This can be an |
|
The velocity unit: |
def main():
# Turn at different velocities
# Default velocity
drivetrain.turn_for(RIGHT, 120, DEGREES)
wait(1, SECONDS)
# Turn slower
drivetrain.set_turn_velocity(20, PERCENT)
drivetrain.turn_for(RIGHT, 120, DEGREES)
wait(1, SECONDS)
# Turn faster
drivetrain.set_turn_velocity(100, PERCENT)
drivetrain.turn_for(RIGHT, 120, DEGREES)
# VR threads — Do not delete
vr_thread(main)
获取器#
标题#
A heading is the direction the robot is facing, measured in degrees. heading returns the robot’s current heading from 0 to 359.99 degrees.
机器人的初始航向角为0度。
Usage:
drivetrain.heading(units)
参数 |
描述 |
|---|---|
|
The heading unit: |
def main():
# Display the heading after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print("Heading: ")
brain.screen.print(drivetrain.heading(DEGREES))
# VR threads — Do not delete
vr_thread(main)
旋转#
Rotation is how much the robot has turned, measured in degrees. At the beginning of a project, the rotation value is set to 0 degrees. rotation returns the robot’s current rotation.
向右转会增加旋转角度,向左转会减少旋转角度。例如,向右转两圈将得到 720 度的旋转角度。
Usage:
drivetrain.rotation(units)
参数 |
描述 |
|---|---|
|
The rotation unit: |
def main():
# Display the rotation after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print("Rotation: ")
brain.screen.print(drivetrain.rotation(DEGREES))
# VR threads — Do not delete
vr_thread(main)
已完成#
is_done returns whether the robot is finished moving, as a Boolean value. This can be used to control the timing of other behaviors based on the robot’s movement.
True— The robot is finished moving.False— The robot is still moving.
This method works together with the following Drivetrain methods that have the wait parameter: drive_for, turn_for, turn_to_heading, and turn_to_rotation.
Usage:
drivetrain.is_done()
参数 |
描述 |
|---|---|
此方法没有参数。 |
正在移动#
is_moving returns whether the robot is moving, as a Boolean value. This can be used to control the timing of other behaviors based on the robot’s movement.
True— The robot is moving.False— The robot is not moving.
Usage:
drivetrain.is_moving()
参数 |
描述 |
|---|---|
此方法没有参数。 |
速度#
velocity returns how fast the robot is driving, as a percentage from -100% to 100%.
Usage:
drivetrain.velocity(units)
参数 |
描述 |
|---|---|
|
The velocity unit: |
例子
def main():
# Display the velocity after driving
drivetrain.drive_for(FORWARD, 150, MM)
brain.screen.print("Velocity: ")
brain.screen.print(drivetrain.velocity(PERCENT))
运动#
Swish 利用进料马达收集球体并将其输送到弹射器上。弹射器马达组下降并释放弹射臂,从而发射球体。
每个电机和电机组都有其自身的运动方向。方向描述解释了Swish上每个方向对应的电机或电机组的运动方式。
行动#
旋转#
spin spins a motor or motor group forward or reverse forever. The motor or motor group will continue to spin until it is given another action, like spinning in a different direction or stopping.
用法:
此方法可以使用以下两种可用的电机或电机组对象之一:
发动机 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
|
The direction the motor or motor group spins:
|
def main():
# Spin the Intake Motor to bring a Ball onto the Catapult
intake_motor.spin(FORWARD)
wait(2, SECONDS)
intake_motor.stop()
# VR threads — Do not delete
vr_thread(main)
spin_for#
spin_for spins a motor or motor group for a specific distance. The spin is relative to the current position of the motor or motor group. The project will wait until the motor or motor group is done spinning before the next line of code runs.
用法:
此方法可以使用以下两种可用的电机或电机组对象之一:
发动机 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
|
The direction the motor or motor group spins:
|
|
The distance the motor or motor group spins. |
|
Optional. The distance unit: |
|
Optional.
|
def main():
# Release the preloaded Ball
catapult_group.spin_for(REVERSE, 5, DEGREES)
# Lower the Catapult to prepare to load another Ball
catapult_group.spin_for(FORWARD, 1350, DEGREES)
# VR threads — Do not delete
vr_thread(main)
自旋至位置#
spin_to_position spins a motor or motor group to a specific position.
A motor or motor group’s position is how far it has spun, measured in DEGREES or TURNS. One turn is equal to 360 degrees. At the beginning of a project, the motor or motor group position is set to 0 degrees. The motor or motor group position can also be set using the set_position method.
位置值是绝对值。这意味着旋转方向取决于电机或电机组的当前位置。
例如,如果电机或电机组从 0 度开始旋转到 720 度,它将正转两圈。如果之后它旋转到 360 度,它将反转一圈,因为 360 小于 720。
用法:
此方法可以使用以下两种可用的电机或电机组对象之一:
发动机 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
|
The position value the motor or motor group will spin to. |
|
Optional. The position unit: |
|
Optional.
|
def main():
# Release the preloaded Ball
catapult_group.spin_to_position(-90, DEGREES)
# Lower the Catapult to prepare to load another Ball
catapult_group.spin_to_position(1260, DEGREES)
# VR threads — Do not delete
vr_thread(main)
停止电机#
stop stops a motor or motor group from spinning.
用法:
此方法可以使用以下两种可用的电机或电机组对象之一:
发动机 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
此方法没有参数。 |
def main():
# Spin the Intake Motor to bring a Ball onto the Catapult
intake_motor.spin(FORWARD)
wait(2, SECONDS)
intake_motor.stop()
# VR threads — Do not delete
vr_thread(main)
变异体#
设置位置#
A motor or motor group’s position is how far it has spun, measured in DEGREES or TURNS. One turn is equal to 360 degrees. set_position changes the motor or motor group’s current position to a new value.
例如,如果电机或电机组旋转了 180 度,将其位置设置为 0 度会将该位置从 180 度重置为 0 度。然后,电机或电机组可以根据该新值旋转到相应位置。
用法:
此方法可以使用以下两种可用的电机或电机组对象之一:
发动机 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
|
The position value to set for the motor or motor group. |
|
Optional. The position unit: |
def main():
# Lower the Catapult to prepare to load another Ball
catapult_group.spin_to_position(1260, DEGREES)
# Set the new Catapult position to be 0 degrees
catapult_group.set_position(0, DEGREES)
# VR threads — Do not delete
vr_thread(main)
设置速度#
set_velocity tells a motor or motor group how fast to spin. A higher percentage makes the motor or motor group spin faster and a lower percentage makes the motor or motor group spin slower.
每个项目开始时,每个电机或电机组默认以 50% 的速度旋转。
**注意:**更高的速度会使电机或电机组旋转得更快,但精度可能会降低。更低的速度会使电机或电机组旋转得更慢,但精度可能会更高。
用法:
此方法可以使用以下两种可用的电机或电机组对象之一:
发动机 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
|
The velocity to spin with from -100% to 100%. This can be an |
|
The velocity unit: |
def main():
# Start spinning the Intake Motor to prepare for picking up a Ball
intake_motor.set_velocity(100, PERCENT)
intake_motor.spin(FORWARD)
# VR threads — Do not delete
vr_thread(main)
设置超时#
set_timeout sets how much time a motor or motor group will try to finish a movement. If the motor or motor group cannot finish in that time, it will stop trying and move on to the next line of code. This keeps the motor or motor group from getting stuck on a movement.
用法:
此方法可以使用以下两种可用的电机或电机组对象之一:
发动机 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
|
The amount of time the motor or motor group can try to finish a movement. This can be a positive |
|
The time unit: |
def main():
# Limit how long the Catapult waits to reach its target
catapult_group.set_timeout(2, SECONDS)
catapult_group.spin_for(FORWARD, 6, TURNS)
# VR threads — Do not delete
vr_thread(main)
获取器#
已完成#
is_done returns whether the motor or motor group is finished moving, as a Boolean value. This can be used to control the timing of other behaviors based on the motor or motor group’s movement.
True— The motor or motor group is finished moving.False— The motor or motor group is still moving.
This method works together with the following Motion methods that have the wait parameter: spin_for and spin_to_position.
用法:
此方法可以使用以下两种可用的电机或电机组对象之一:
发动机 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
此方法没有参数。 |
正在旋转#
is_spinning returns whether the motor or motor group is spinning, as a Boolean value. This can be used to control the timing of other behaviors based on the motor or motor group’s movement.
True— The motor or motor group is spinning.False— The motor or motor group is not spinning.
This method works together with the following Motion methods that have the wait parameter: spin_for and spin_to_position.
用法:
此方法可以使用以下两种可用的电机或电机组对象之一:
发动机 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
此方法没有参数。 |
位置#
A motor or motor group’s position is how far it has spun, measured in DEGREES or TURNS. One turn is equal to 360 degrees. position returns the motor or motor group’s current position.
项目开始时,电机或电机组的位置设置为 0 度。如果电机或电机组正向旋转一整圈,则位置为 360 度或 1 圈。如果电机或电机组反向旋转,则位置为负值。
用法:
此方法可以使用以下两种可用的电机或电机组对象之一:
发动机 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
|
The unit to return the motor or motor group position in: |
def main():
# Lower the Catapult to prepare to load another Ball
while not catapult_group.position(DEGREES) == 1260:
catapult_group.spin(FORWARD)
# VR threads — Do not delete
vr_thread(main)
速度#
velocity returns how fast the motor or motor group is spinning.
正值表示电机或电机组正转;负值表示电机或电机组反转。
用法:
此方法可以使用以下两种可用的电机或电机组对象之一:
发动机 |
命令 |
|---|---|
|
|
|
|
参数 |
描述 |
|---|---|
|
The velocity unit to return: |
def main():
# Print the current Catapult velocity
brain.screen.print(catapult_group.velocity(PERCENT))
# VR threads — Do not delete
vr_thread(main)
安慰#
项目运行时,控制台会显示文本、数字和变量值。控制台方法还可以将光标移动到新行、清除控制台以及向“监视器”选项卡添加变量或传感器值。
打印#
脑屏幕打印#
brain.screen.print displays text, numbers, or variable values in the Console using the current cursor position. It behaves like print on the Console API page.
Use brain.screen.next_row when you want the next printed value to start on a new row.
Usage:
brain.screen.print(value)
参数 |
描述 |
|---|---|
|
要在控制台中显示的文本、数字或变量值。 |
def main():
# Display a message in the Console
brain.screen.print("Hello, robot!")
# VR threads — Do not delete
vr_thread(main)

brain.screen.下一行#
brain.screen.next_row moves the cursor to column 1 on the next row in the Console. The next value printed will appear on that row.
Usage:
brain.screen.next_row()
参数 |
描述 |
|---|---|
此方法没有参数。 |
def main():
# Print on two rows
brain.screen.print("Row 1")
brain.screen.next_row()
brain.screen.print("Row 2")
# VR threads — Do not delete
vr_thread(main)

brain.screen.clear_screen#
brain.screen.clear_screen clears all rows from the Console and moves the cursor back to Row 1.
Usage:
brain.screen.clear_screen()
参数 |
描述 |
|---|---|
此方法没有参数。 |
def main():
# Display text, then clear it after two seconds
brain.screen.print("This will disappear...")
wait(2, SECONDS)
brain.screen.clear_screen()
# VR threads — Do not delete
vr_thread(main)
监视器#
监控变量#
monitor_variable adds one or more predefined variables to the Monitor tab in VEXcode. This lets you watch a variable’s value change while a project is running.
Variables must be global for monitor_variable to monitor them successfully. Provide each variable name as a string.
Usage:
monitor_variable(“variable”)
monitor_variable(“variable1”, “variable2”)
参数 |
描述 |
|---|---|
|
要监控的预定义全局变量的名称,以字符串形式给出。要监控多个变量,请用逗号分隔每个变量名。 |
def main():
# Monitor the amount of loops
global loops
monitor_variable("loops")
# Drive in a square 3 times
for loops in range(12):
drivetrain.turn_for(RIGHT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 150, MM)
# VR threads — Do not delete
vr_thread(main)
监控传感器#
monitor_sensor adds one or more sensor values to the Monitor tab in VEXcode. This lets you watch sensor values change while a project is running.
请以字符串形式提供每个传感器的值。
Usage:
monitor_sensor(“sensor”)
monitor_sensor(“sensor1”, “sensor2”)
参数 |
描述 |
|---|---|
|
The sensor value to monitor, given as a string. To monitor more than one sensor value, separate each sensor value with a comma. Supported sensor identifiers include:
|
def main():
# Monitor the robot's heading
monitor_sensor("drivetrain.heading")
drivetrain.turn_for(RIGHT, 450, DEGREES)
# VR threads — Do not delete
vr_thread(main)
传感#
光学的#
靠近对象#
is_near_object returns a Boolean indicating whether or not the Optical Sensor detects an object within range.
True– The Optical Sensor detects an object.False– The Optical Sensor does not detect an object.
Usage:
intake_optical.is_near_object()
参数 |
描述 |
|---|---|
此方法没有参数。 |
def main():
# Launch a loaded Ball once the Intake Optical Sensor detects a Ball
if intake_optical.is_near_object():
catapult_group.spin_for(REVERSE, 5, DEGREES)
# VR threads — Do not delete
vr_thread(main)
颜色#
color returns an instance of a Color class, based on the detected hue value. These can be compared to predefined Color objects to create conditional statements.
可选颜色有:
NONERED- A hue value between 340° - 20°GREEN- A hue value between 80° - 140°BLUE- A hue value between 200° - 240°YELLOW- A hue value between 40° - 60°ORANGE- A hue value between 20° - 40°PURPLE- A hue value between 240° - 280°CYAN- A hue value between 140° - 200°
Note: The Optical Sensor is looking for hue ranges that match the specified color. For detecting specific hue ranges, see hue.
Usage:
intake_optical.color()
参数 |
描述 |
|---|---|
此方法没有参数。 |
def main():
# Launch a loaded Ball once yellow is detected in the Intake
if intake_optical.color() == YELLOW:
catapult_group.spin_for(REVERSE, 5, DEGREES)
# VR threads — Do not delete
vr_thread(main)
亮度#
brightness returns the amount of light reflected from the object as a decimal (float) representing a percent.
百分比越高,意味着反射回光学传感器的光线越多;百分比越低,意味着反射回的光线越少。
Usage:
intake_optical.brightness()
参数 |
描述 |
|---|---|
此方法没有参数。 |
def main():
# Launch a loaded Ball once the Intake Optical Sensor sees a Ball
if intake_optical.brightness() > 0:
catapult_group.spin_for(REVERSE, 5, DEGREES)
# VR threads — Do not delete
vr_thread(main)
色调#
hue returns the hue value of the detected color as a decimal (float) from 0 to 359.
色相是用色轮上的数字来描述颜色的一种方法。

Usage:
intake_optical.hue()
参数 |
描述 |
|---|---|
此方法没有参数。 |
def main():
# Launch a loaded Ball once the Intake Optical Sensor reports a hue value
if intake_optical.hue() > 100:
catapult_group.spin_for(REVERSE, 5, DEGREES)
# VR threads — Do not delete
vr_thread(main)
检测到目标#
object_detected registers a function to be called whenever the Optical Sensor detects a new object.
Usage:
intake_optical.object_detected(callback, arg)
参数 |
描述 |
|---|---|
|
先前定义的 函数,当光学传感器检测到新物体时执行。 |
|
def launch_ball():
catapult_group.spin_for(REVERSE, 5, DEGREES)
def main():
# Launch the Ball when the Intake Optical Sensor detects an object
intake_optical.object_detected(launch_ball)
# VR threads — Do not delete
vr_thread(main)
丢失对象#
object_lost registers a function to be called whenever the Optical Sensor loses a detected object.
Usage:
intake_optical.object_lost(callback, arg)
参数 |
描述 |
|---|---|
|
先前定义的 函数,当光学传感器丢失检测到的对象时执行。 |
|
def lower_catapult():
catapult_group.spin_to_position(1260, DEGREES)
def main():
# Lower the Catapult when the Intake Optical Sensor no longer detects an object
intake_optical.object_lost(lower_catapult)
# VR threads — Do not delete
vr_thread(main)