机器人专用 Python#
介绍#
VIQRC 25-26 混合搭配游乐场采用专为此游乐场设计的构造方法,包括两个电机选项、光学传感器和触摸 LED。
所有标准 VEXcode VR 方法均可用于 VIQRC 25-26 Mix & Match 游乐场。
以下是所有可用的 Playground 特定方法的列表:
运动——移动并跟踪机器人的马达。
行动
spin – 无限旋转选定的电机或电机组。
spin_for – 使电机或电机组旋转特定距离(以度或圈为单位)。
spin_to_position – 将电机或电机组旋转到设定位置。
stop – 停止特定电机或电机组旋转。
修改器
set_position – 设置电机或电机组的编码器值。
set_velocity – 以百分比设置电机或电机组的速度。
set_timeout – 限制运动受阻时电机阻止在放弃之前等待的时间。
吸气剂
is_done – 返回一个布尔值,指示电机是否不再旋转。
is_spinning – 返回一个布尔值,指示电机当前是否正在旋转。
位置 – 以度数或圈数返回电机当前的旋转位置。
velocity – 以 % 或 rpm 为单位返回电机当前速度。
感知——利用机器人的各种传感器。
触摸LED
set_color – 将 TouchLED 设置为选定的颜色。
光学的
is_near_object – 返回检测到的物体是否靠近光学传感器。
color – 返回从光学传感器检测到的颜色。
亮度 – 返回传感器检测到的亮度百分比。
色调 - 返回检测到的颜色的色调值。
object_detected – 当光学传感器检测到物体时注册一个回调函数。
object_lost – 注册光学传感器丢失物体时的回调函数。
运动#
行动#
旋转#
spin
使马达无限旋转。
用法:
两个可用电机对象之一可与此方法一起使用,如下所示:
发动机 |
命令 |
---|---|
|
|
|
|
参数 |
描述 |
---|---|
|
电机旋转
|
def main():
# Place a Pin atop another Pin
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
spin_for#
spin_for
使电机旋转给定的度数或圈数。
用法:
两个可用电机对象之一可与此方法一起使用,如下所示:
发动机 |
命令 |
---|---|
|
|
|
|
参数 |
描述 |
---|---|
|
电机旋转
|
|
电机旋转的距离,为整数。 |
|
表示旋转距离的单位:
|
|
可选
|
def main():
# Place a Pin atop another Pin
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
旋转到位置#
spin_to
将电机旋转到给定位置。
用法:
两个可用电机对象之一可与此方法一起使用,如下所示:
发动机 |
命令 |
---|---|
|
|
|
|
参数 |
描述 |
---|---|
|
电机旋转的特定角度或圈数。 |
|
表示旋转角度的单位:
|
|
可选
|
def main():
# Place a Pin atop another Pin
lift_motor.spin_to_position(-2, TURNS)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
停止#
“stop” 停止电机旋转。
用法:
两个可用电机对象之一可与此方法一起使用,如下所示:
发动机 |
命令 |
---|---|
|
|
|
|
参数 |
描述 |
---|---|
该方法没有参数。 |
def main():
# Place a Pin atop another Pin
lift_motor.spin(REVERSE)
wait(2, SECONDS)
lift_motor.stop()
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
修改器#
设置位置#
set_position
将电机的编码器位置设置为给定的位置值。
用法:
两个可用电机对象之一可与此方法一起使用,如下所示:
发动机 |
命令 |
---|---|
|
|
|
|
参数 |
描述 |
---|---|
|
要设置的电机编码器的具体整数。 |
|
表示旋转角度的单位:
|
def main():
# Place a Pin atop another Pin
lift_motor.set_position(100, DEGREES)
lift_motor.spin_to_position(-500, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
设置速度#
set_velocity
设置电机的速度。
用法:
两个可用电机对象之一可与此方法一起使用,如下所示:
发动机 |
命令 |
---|---|
|
|
|
|
参数 |
描述 |
---|---|
|
IQ 电机旋转的速度,范围从 0 到 100。 |
|
表示新速度的单位:
|
def main():
# Place a Pin atop another Pin
lift_motor.set_velocity(100, PERCENT)
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
设置超时#
set_timeout
为电机的运动命令设置时间限制。这可以防止未到达预定位置的运动命令阻止后续命令运行。
用法:
两个可用电机对象之一可与此方法一起使用,如下所示:
发动机 |
命令 |
---|---|
|
|
|
|
参数 |
描述 |
---|---|
|
电机停止前等待的时间。 |
|
表示超时的单位:
|
def main():
# Place a Pin atop another Pin
lift_motor.set_timeout(2, SECONDS)
lift_motor.spin_for(REVERSE, 5, TURNS)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
吸气剂#
完成#
is_done
返回一个布尔值,指示指定电机是否没有旋转。
True
– 指定的电机没有旋转。False
– 指定的电机正在旋转。
用法:
两个可用电机对象之一可与此方法一起使用,如下所示:
发动机 |
命令 |
---|---|
|
|
|
|
参数 |
描述 |
---|---|
该方法没有参数。 |
def main():
# Place a Pin atop another Pin
lift_motor.spin_for(REVERSE, 600, DEGREES, wait=False)
wait(0.1, SECONDS)
while not lift_motor.is_done():
touchled.set_color(RED)
wait(0.5, SECONDS)
touchled.set_color(NONE)
wait(0.5, SECONDS)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
正在旋转#
is_spinning
返回一个布尔值,指示指定电机是否正在旋转。
True
– 指定的电机正在旋转。False
– 指定的电机没有旋转。
用法:
两个可用电机对象之一可与此方法一起使用,如下所示:
发动机 |
命令 |
---|---|
|
|
|
|
参数 |
描述 |
---|---|
该方法没有参数。 |
def main():
# Place a Pin atop another Pin
lift_motor.spin_for(REVERSE, 600, DEGREES, wait=False)
wait(0.1, SECONDS)
while lift_motor.is_spinning():
touchled.set_color(RED)
wait(0.5, SECONDS)
touchled.set_color(NONE)
wait(0.5, SECONDS)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
位置#
position
返回指定电机旋转的总距离。
用法:
两个可用电机对象之一可与此方法一起使用,如下所示:
发动机 |
命令 |
---|---|
|
|
|
|
参数 |
描述 |
---|---|
|
表示电机位置的单位:
|
def main():
# Place a Pin atop another Pin
lift_motor.spin(REVERSE)
while not -600 > claw_motor.position(DEGREES):
wait(2, MSEC)
lift_motor.stop()
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
速度#
velocity
返回电机的当前转速。
用法:
两个可用电机对象之一可与此方法一起使用,如下所示:
发动机 |
命令 |
---|---|
|
|
|
|
参数 |
描述 |
---|---|
|
表示电机位置的单位:
|
def main():
# Place a Pin atop another Pin
lift_motor.set_velocity(100, PERCENT)
lift_motor.spin_for(REVERSE, 600, DEGREES, wait=False)
wait(0.5, SECONDS)
brain.screen.print(lift_motor.velocity(PERCENT))
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
传感#
触摸LED#
设置颜色#
set_color
设置触摸 LED 的颜色。
用法:
touchled.set_color(color)
参数 |
描述 |
---|---|
|
将触摸 LED 设置为的颜色:
|
def main():
# Place a Pin atop another Pin
touchled.set_color(RED)
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
touchled.set_color(NONE)
光学的#
靠近物体#
is_near_object
返回一个布尔值,指示光学传感器是否检测到靠近传感器的物体。
“真”——物体靠近光学传感器。
“False”——物体离光学传感器太远。
用法:
optical.is_near_object()
参数 |
描述 |
---|---|
该方法没有参数。 |
def main():
# Place a Pin atop another Pin
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
while not not optical.is_near_object():
wait(2, MSEC)
lift_motor.spin_to_position(0, DEGREES)
颜色#
color
返回光学传感器检测到的颜色:
返回的颜色: |
---|
<ul><li>`NONE` – 未检测到颜色。2 </li>`RED`</li><li>`GREEN`</li><li>`BLUE`</li><li>`<li></li><li>`ORANGE`</li><li>`PURPLE`</li><li>`WHITE`</li><li>`RED_VIOLET`</li><li>`VIOLET`</li><li>`BLUE_VIOLET`</li><li>`BLUE_GREEN`</li><li>`YELLOW_GREEN`</li><li>`YELLOW_ORANGE`</li><li>`RED_ORANGE`</li></ul> |
用法:
optical.color()
参数 |
描述 |
---|---|
该方法没有参数。 |
def main():
# Place a Pin atop another Pin
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
while not optical.color() == RED:
wait(5, MSEC)
lift_motor.spin_to_position(0, DEGREES)
亮度#
brightness
返回光学传感器检测到的亮度值,以 0% 到 100% 的百分比表示。
用法:
optical.brightness()
参数 |
描述 |
---|---|
该方法没有参数。 |
def main():
# Place a Pin atop another Pin
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
while not 30 > optical.brightness():
wait(5, MSEC)
lift_motor.spin_to_position(0, DEGREES)
色调#
hue
返回光学传感器检测到的色调。
色调值范围从 0 到 359 度,对应于下面显示的色轮上的位置。
用法:
optical.hue()
参数 |
描述 |
---|---|
该方法没有参数。 |
def main():
# Place a Pin atop another Pin
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
while not optical.hue() > 0:
wait(5, MSEC)
lift_motor.spin_to_position(0, DEGREES)
检测到物体#
当光学传感器检测到物体时,object_detected
注册一个回调函数。
用法:
optical.object_detected(callback, arg)
参数 |
描述 |
---|---|
|
当检测到物体时调用的函数。 |
|
可选。用于将参数传递给回调函数的元组。 |
def pin_in_open_claw():
# Change color when claw isn't holding anything
touchled.set_color(RED)
def main():
# Place a Pin atop another Pin
optical.object_detected(pin_in_open_claw)
touchled.set_color(GREEN)
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)
object_lost#
object_lost
注册光学传感器丢失物体时的回调函数。
用法:
optical.object_lost(回调,arg)
参数 |
描述 |
---|---|
|
当对象丢失时调用的函数。 |
|
可选。用于将参数传递给回调函数的元组。 |
def lower_lift():
# Lower lift when Pin not in claw
lift_motor.spin_to_position(0, DEGREES)
def main():
# Place a Pin atop another Pin
optical.object_lost(lower_lift)
lift_motor.spin_for(REVERSE, 600, DEGREES)
drivetrain.drive_for(FORWARD, 140, MM)
claw_motor.spin(FORWARD)