机器人专用 Python#
介绍#
V5RC 25-26 Push Back 游乐场采用专为此游乐场设计的专属方法,包括两个电机选项、AI 视觉传感器、光学传感器和游戏定位系统 (GPS) 传感器。
所有标准 VEXcode VR 方法均可用于 V5RC 25-26 Push Back 游乐场。
以下是所有可用的 Playground 特定方法的列表:
运动——移动并跟踪机器人的马达。
行动
spin – 无限旋转选定的电机或电机组。
spin_for – 使电机或电机组旋转特定距离(以度或圈为单位)。
spin_to_position – 将电机或电机组旋转到设定位置。
stop – 停止特定电机或电机组旋转。
修改器
set_position – 设置电机或电机组的编码器值。
set_velocity – 以百分比设置电机或电机组的速度。
set_timeout – 限制运动受阻时电机阻止在放弃之前等待的时间。
吸气剂
is_done – 返回一个布尔值,指示电机是否不再旋转。
is_spinning – 返回一个布尔值,指示电机当前是否正在旋转。
位置 – 以度数或圈数返回电机当前的旋转位置。
velocity – 以 % 或 rpm 为单位返回电机当前速度。
AI 视觉 - 使用 AI 视觉传感器捕捉和分析物体。
吸气剂
take_snapshot – 根据给定的签名返回检测到的对象元组。
特性
感知——利用机器人的各种传感器。
光学的
is_near_object – 返回检测到的物体是否靠近光学传感器。
color – 返回从光学传感器检测到的颜色。
亮度 – 返回传感器检测到的亮度百分比。
色调 - 返回检测到的颜色的色调值。
object_detected – 当光学传感器检测到物体时注册一个回调函数。
object_lost – 注册光学传感器丢失物体时的回调函数。
全球定位系统
x_position – 返回场地上 GPS 传感器的当前 x 坐标。
y_position – 返回场地上 GPS 传感器的当前 y 坐标。
heading – 根据 GPS 传感器的读数(0 到 359 度)返回机器人当前面向的航向。
运动#
Actions#
旋转#
spin
spins a motor or motor group in the specified direction indefinitely.
用法:
两个可用电机对象之一可与此方法一起使用,如下所示:
发动机 |
命令 |
---|---|
|
|
|
|
参数 |
描述 |
---|---|
方向 |
The direction for the motor to spin:
|
def main():
# Pick up a second Block
conveyor_motor.spin_for(FORWARD, 150, DEGREES)
intake_motor.spin(FORWARD)
drivetrain.drive_for(FORWARD, 350, MM)
spin_for#
spin_for
spins a motor or motor group for a given amount of degrees or turns.
用法:
两个可用电机对象之一可与此方法一起使用,如下所示:
发动机 |
命令 |
---|---|
|
|
|
|
参数 |
描述 |
---|---|
|
The direction for the motor to spin:
|
|
电机旋转的距离,为整数。 |
|
The unit that represents the distance to rotate:
|
|
Optional.
|
def main():
# Pick up a second Block
conveyor_motor.spin_for(FORWARD, 150, DEGREES)
intake_motor.spin(FORWARD)
drivetrain.drive_for(FORWARD, 350, MM)
旋转到位置#
spin_to
spins a motor or motor group to a given position.
用法:
两个可用电机对象之一可与此方法一起使用,如下所示:
发动机 |
命令 |
---|---|
|
|
|
|
参数 |
描述 |
---|---|
|
电机旋转的特定角度或圈数。 |
|
The unit that represents the angle to rotate to:
|
|
Optional.
|
def main():
# Pick up a second Block
conveyor_motor.spin_to_position(150, DEGREES)
intake_motor.spin(FORWARD)
drivetrain.drive_for(FORWARD, 350, MM)
停止#
stop
stops a motor or motor group from spinning.
用法:
两个可用电机对象之一可与此方法一起使用,如下所示:
发动机 |
命令 |
---|---|
|
|
|
|
参数 |
描述 |
---|---|
该方法没有参数。 |
def main():
# Pick up a second Block
conveyor_motor.spin_for(FORWARD, 150, DEGREES)
intake_motor.spin(FORWARD)
drivetrain.drive_for(FORWARD, 350, MM)
intake_motor.stop()
Mutators#
设置位置#
set_position
sets a motor’s or motor group’s encoder position to the given position value.
用法:
两个可用电机对象之一可与此方法一起使用,如下所示:
发动机 |
命令 |
---|---|
|
|
|
|
参数 |
描述 |
---|---|
|
要设置的电机编码器的具体整数。 |
|
The unit that represents the angle to rotate to:
|
def main():
# Pick up a second Block
conveyor_motor.set_position(-150, DEGREES)
conveyor_motor.spin_to_position(0, DEGREES)
intake_motor.spin(FORWARD)
drivetrain.drive_for(FORWARD, 350, MM)
设置速度#
set_velocity
sets the speed of a motor or motor group.
用法:
两个可用电机对象之一可与此方法一起使用,如下所示:
发动机 |
命令 |
---|---|
|
|
|
|
参数 |
描述 |
---|---|
|
V5 电机旋转的速度,范围从 0 到 100。 |
|
The unit that represents the new velocity:
|
def main():
# Pick up a second Block
conveyor_motor.set_velocity(90, PERCENT)
conveyor_motor.spin_for(FORWARD, 200, DEGREES)
intake_motor.spin(FORWARD)
drivetrain.drive_for(FORWARD, 400, MM)
设置超时#
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.
用法:
两个可用电机对象之一可与此方法一起使用,如下所示:
发动机 |
命令 |
---|---|
|
|
|
|
参数 |
描述 |
---|---|
|
电机停止前等待的时间。 |
|
The unit to represent the timeout:
|
def main():
# Pick up a second Block
conveyor_motor.set_timeout(0.5, SECONDS)
conveyor_motor.spin_to_position(1000, DEGREES)
intake_motor.spin(FORWARD)
drivetrain.drive_for(FORWARD, 400, MM)
Getters#
完成#
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.
用法:
两个可用电机对象之一可与此方法一起使用,如下所示:
发动机 |
命令 |
---|---|
|
|
|
|
参数 |
描述 |
---|---|
该方法没有参数。 |
def main():
# Pick up a second Block
conveyor_motor.spin_to_position(300, DEGREES, wait=False)
wait(0.1, SECONDS)
while not conveyor_motor.is_done():
drivetrain.drive(FORWARD)
intake_motor.spin(FORWARD)
wait(5, MSEC)
drivetrain.stop()
intake_motor.stop()
正在旋转#
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.
用法:
两个可用电机对象之一可与此方法一起使用,如下所示:
发动机 |
命令 |
---|---|
|
|
|
|
参数 |
描述 |
---|---|
该方法没有参数。 |
def main():
# Pick up a second Block
conveyor_motor.spin_to_position(300, DEGREES, wait=False)
wait(0.1, SECONDS)
while conveyor_motor.is_spinning():
drivetrain.drive_for(FORWARD, 200, MM)
intake_motor.spin(FORWARD)
位置#
position
returns the total distance the specified motor or motor group has rotated.
用法:
两个可用电机对象之一可与此方法一起使用,如下所示:
发动机 |
命令 |
---|---|
|
|
|
|
参数 |
描述 |
---|---|
|
The units that represent the motor’s position:
|
def main():
# Pick up a second Block
while conveyor_motor.position(DEGREES) < 150:
conveyor_motor.spin(FORWARD)
wait(2, MSEC)
conveyor_motor.stop()
intake_motor.spin(FORWARD)
drivetrain.drive_for(FORWARD, 400, MM)
速度#
velocity
returns the current rotational speed of the motor or motor group.
用法:
两个可用电机对象之一可与此方法一起使用,如下所示:
发动机 |
命令 |
---|---|
|
|
|
|
参数 |
描述 |
---|---|
|
The unit that represent the motor’s position:
|
def main():
# Pick up a second block
conveyor_motor.set_velocity(100, PERCENT)
conveyor_motor.spin_to_position(300, DEGREES, wait=False)
wait(0.2, SECONDS)
brain.screen.print(conveyor_motor.velocity(PERCENT))
intake_motor.spin(FORWARD)
drivetrain.drive_for(FORWARD, 400, MM)
人工智能视觉#
Getters#
拍摄快照#
take_snapshot
filters the data from the AI Vision Sensor frame to return a tuple.
该元组存储的对象按宽度从大到小排序,从索引 0 开始。每个对象的 属性 可以通过其索引访问。如果未检测到匹配的对象,则返回一个空元组。
Usage:
ai_vision.take_snapshot(signature)
参数 |
描述 |
---|---|
|
获取哪个签名的数据。唯一可用的签名是:
|
def main():
# If an object is detected, pick it up
conveyor_motor.spin_for(FORWARD, 150, DEGREES)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
if ai_objects:
intake_motor.spin(FORWARD)
drivetrain.drive(FORWARD)
while not bumper.pressing():
wait(2, MSEC)
drivetrain.stop()
break
Properties#
使用 take_snapshot
后,存储在元组中的每个对象包含七个属性。
Some property values are based off of the detected object’s position in the AI Vision Sensor’s view at the time that take_snapshot
was used. The 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():
# If an object is detected, approach and pick it up
conveyor_motor.spin_for(FORWARD, 150, DEGREES)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
if ai_objects:
if ai_objects[0].width > 65:
drivetrain.stop()
break
else:
intake_motor.spin(FORWARD)
drivetrain.drive(FORWARD)
。高度#
.height
returns the height of the detected object in pixels, which is an integer between 1 and 240.
def main():
# If an object is detected, approach and pick it up
conveyor_motor.spin_for(FORWARD, 150, DEGREES)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
if ai_objects:
if ai_objects[0].height > 55:
drivetrain.stop()
break
else:
intake_motor.spin(FORWARD)
drivetrain.drive(FORWARD)
.centerX#
.centerX
returns the x-coordinate of the detected object’s center in pixels, which is an integer between 0 and 320.
def main():
# Pick up a Block from the top left group
conveyor_motor.spin_for(FORWARD, 150, DEGREES)
drivetrain.turn_for(LEFT, 80, DEGREES)
drivetrain.drive_for(FORWARD, 200, MM)
drivetrain.set_turn_velocity(20, PERCENT)
drivetrain.turn(LEFT)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
if ai_objects:
if 140 < ai_objects[0].centerX < 180:
intake_motor.spin(FORWARD)
drivetrain.drive(FORWARD)
while not bumper.pressing():
wait(2, MSEC)
drivetrain.stop()
.centerY#
.centerY
returns the y-coordinate of the detected object’s center in pixels, which is an integer between 0 and 240.
def main():
# If an object is detected, approach and pick it up
conveyor_motor.spin_for(FORWARD, 150, DEGREES)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
if ai_objects:
if ai_objects[0].centerY > 140:
drivetrain.stop()
break
else:
intake_motor.spin(FORWARD)
drivetrain.drive(FORWARD)
.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():
# Pick up a Block from the top left group
conveyor_motor.spin_for(FORWARD, 150, DEGREES)
drivetrain.turn_for(LEFT, 80, DEGREES)
drivetrain.drive_for(FORWARD, 300, MM)
drivetrain.set_turn_velocity(20, PERCENT)
drivetrain.turn(RIGHT)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
if ai_objects:
if 100 < ai_objects[0].originX < 140:
intake_motor.spin(FORWARD)
drivetrain.drive(FORWARD)
while not bumper.pressing():
wait(2, MSEC)
drivetrain.stop()
.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():
# If an object is detected, approach and pick it up
conveyor_motor.spin_for(FORWARD, 150, DEGREES)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
if ai_objects:
if ai_objects[0].originY > 120:
drivetrain.stop()
break
else:
intake_motor.spin(FORWARD)
drivetrain.drive(FORWARD)
。ID#
.id
returns the ID of the detected AI Classification as an integer.
人工智能分类 |
ID |
签名 |
---|---|---|
蓝块 |
1 |
|
红块 |
2 |
|
def main():
# Pick up a Red Block from the top left group
conveyor_motor.spin_for(FORWARD, 150, DEGREES)
drivetrain.turn_for(LEFT, 80, DEGREES)
drivetrain.drive_for(FORWARD, 300, MM)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
if ai_objects:
drivetrain.drive(FORWARD)
if (ai_objects[0].id) == 2:
intake_motor.spin(FORWARD)
while not bumper.pressing():
wait(2, MSEC)
drivetrain.stop()
break
传感#
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():
# Move the preloaded Block to the top of the Conveyor
conveyor_motor.spin(FORWARD)
while not optical.is_near_object():
wait(2, MSEC)
conveyor_motor.stop()
颜色#
color
returns the color detected by the Optical Sensor:
返回的颜色: |
---|
|
Usage:
optical.color()
参数 |
描述 |
---|---|
该方法没有参数。 |
def main():
# Pick up and move a Blue Block to the top of the Conveyor
conveyor_motor.spin_for(FORWARD, 150, DEGREES)
intake_motor.spin(FORWARD)
drivetrain.drive_for(FORWARD, 360, MM)
conveyor_motor.spin(FORWARD)
while not optical.color() == BLUE:
wait(2, MSEC)
conveyor_motor.stop()
亮度#
brightness
returns the brightness value detected by the Optical Sensor as a percent from 0% to 100%.
Usage:
optical.brightness()
参数 |
描述 |
---|---|
该方法没有参数。 |
def main():
# Move the preloaded Block to the top of the Conveyor
conveyor_motor.spin(FORWARD)
while not 0 < optical.brightness():
wait(2, MSEC)
conveyor_motor.stop()
色调#
hue
returns the hue detected by the Optical Sensor.
色调值范围从 0 到 359 度,对应于下面显示的色轮上的位置。
Usage:
optical.hue()
参数 |
描述 |
---|---|
该方法没有参数。 |
def main():
# Pick up and move a Blue Block to the top of the Conveyor
conveyor_motor.spin_for(FORWARD, 150, DEGREES)
intake_motor.spin(FORWARD)
drivetrain.drive_for(FORWARD, 360, MM)
conveyor_motor.spin(FORWARD)
while not 220 < optical.hue() < 260:
wait(2, MSEC)
conveyor_motor.stop()
检测到物体#
object_detected
registers a callback function for when the Optical Sensor detects an object.
Usage:
optical.object_detected(callback, arg)
参数 |
描述 |
---|---|
|
当检测到物体时调用的函数。 |
|
可选。用于将参数传递给回调函数的元组。 |
def ready_block():
# Stop the conveyor when Block is at the Sensor
conveyor_motor.stop()
def main():
# Move the preloaded Block near the top of the Conveyor
conveyor_motor.spin(FORWARD)
optical.object_detected(ready_block)
object_lost#
object_lost
registers a callback function for when the Optical Sensor loses a previously detected object.
Usage:
optical.object_lost(callback, arg)
参数 |
描述 |
---|---|
|
当检测到的对象丢失时将调用的函数。 |
|
可选。用于将参数传递给回调函数的元组。 |
def ready_block():
# Stop the conveyor when Block is at the top
conveyor_motor.stop()
def main():
# Move the preloaded Block to the top of the Conveyor
conveyor_motor.spin(FORWARD)
optical.object_lost(ready_block)
GPS#
本页上的所有 GPS(游戏定位系统™)传感器示例均使用默认的 Playground 起始位置 C。
x_位置#
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, |
def main():
# Pick up a second Block
conveyor_motor.spin_for(FORWARD, 150, DEGREES)
intake_motor.spin(FORWARD)
drivetrain.drive(FORWARD)
while not gps.x_position(MM) > -795:
wait(2, MSEC)
drivetrain.stop()
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, |
def main():
# Pick up a second Block
conveyor_motor.spin_for(FORWARD, 150, DEGREES)
intake_motor.spin(FORWARD)
drivetrain.drive(FORWARD)
while not -450 > gps.y_position(MM):
wait(2, MSEC)
drivetrain.stop()
标题#
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()
参数 |
描述 |
---|---|
该方法没有参数。 |
def main():
# Score the preloaded Block into a Center Goal
drivetrain.set_turn_velocity(30, PERCENT)
drivetrain.turn(LEFT)
while not 40 > gps.heading():
wait(2, MSEC)
drivetrain.drive_for(FORWARD, 1000, MM)
drivetrain.turn(RIGHT)
while not gps.heading() > 310:
wait(2, MSEC)
drivetrain.drive_for(REVERSE, 400, MM)
conveyor_motor.spin(FORWARD)