宏#
介绍#
VEX AIM Python 宏是预先构建的代码块,可以拖放到 Python 项目中。每个宏都是一组 Python 命令,它们协同工作,使机器人完成更复杂的动作。
这些并非可从代码中调用的单个方法。相反,每个宏都作为现成的代码序列插入,可以直接使用,也可以添加到项目后进行编辑。
在此页面上,每个 Python 代码块都显示了使用该宏时插入的实际代码序列。
以下是所有宏的列表:
情感——让机器人表现出丰富的情感。
act happy— Makes the robot act happy.act sad— Makes the robot act sad.act silly— Makes the robot act silly.act angry— Makes the robot act angry.act excited— Makes the robot act excited.
AI视觉(运动球类)——转向或收集运动球类。
turn right until sports ball— Turns right until a sports ball is detected.turn left until sports ball— Turns left until a sports ball is detected.get sports ball— Moves the robot to collect a sports ball.
AI视觉(橙色桶)——转向或收集橙色桶。
turn right until orange barrel— Turns right until an orange barrel is detected.turn left until orange barrel— Turns left until an orange barrel is detected.get orange barrel— Moves the robot to collect an orange barrel.
AI视觉(蓝色木桶)——转向或收集蓝色木桶。
turn right until blue barrel— Turns right until a blue barrel is detected.turn left until blue barrel— Turns left until a blue barrel is detected.get blue barrel— Moves the robot to collect a blue barrel.
人工智能视觉(AIM机器人)——转向或向其他AIM机器人移动。
turn right until AIM robot— Turns right until another AIM Robot is detected.turn left until AIM robot— Turns left until another AIM Robot is detected.move to AIM robot— Moves toward another AIM Robot.
AI视觉(AprilTag)——转向或靠近AprilTags。
turn right until AprilTag— Turns right until an an AprilTag is detected.turn left until AprilTag— Turns left until an an AprilTag is detected.move to AprilTag— Moves toward an AprilTag.
情绪#
act happy#
这个宏可以让机器人表现得开心,包括显示笑脸、播放欢快的声音、变换 LED 灯颜色以及来回移动。当机器人需要以快乐或积极的方式回应时,可以使用它。
以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。
# act happy
robot.screen.show_emoji(HAPPY)
wait(.5,SECONDS)
robot.sound.play(ACT_HAPPY)
wait(115, MSEC)
robot.screen.show_emoji(HAPPY, LOOK_RIGHT)
robot.led.on(ALL_LEDS, YELLOW)
robot.turn_for(RIGHT, 4, 100)
robot.led.on(ALL_LEDS, WHITE)
robot.screen.show_emoji(HAPPY, LOOK_LEFT)
robot.turn_for(RIGHT, -8, 100)
robot.led.on(ALL_LEDS, YELLOW)
robot.screen.show_emoji(HAPPY, LOOK_RIGHT)
robot.turn_for(RIGHT, 4, 100)
wait(100,MSEC)
for i in range(3):
robot.screen.show_emoji(QUIET)
robot.led.on(ALL_LEDS, WHITE)
robot.move_for(1,-90)
robot.screen.show_emoji(HAPPY)
robot.led.on(ALL_LEDS, YELLOW)
robot.move_for(1,90)
robot.stop_all_movement()
robot.led.off(ALL_LEDS)
wait(.5,SECONDS)
act sad#
这个宏可以让机器人表现出悲伤的表情,包括显示悲伤的表情、播放悲伤的声音、LED灯逐渐变暗以及缓慢地朝不同方向移动。当机器人需要表现出失望或低落的情绪时,可以使用它。
以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。
# act sad
robot.screen.show_emoji(SAD)
wait(.5,SECONDS)
robot.sound.play(ACT_SAD)
wait(115, MSEC)
robot.led.on(ALL_LEDS, Color(0, 0, 250))
robot.screen.show_emoji(SAD, LOOK_RIGHT)
robot.move_for(11, 135, 70)
robot.led.on(ALL_LEDS, Color(0, 0, 100))
robot.screen.show_emoji(SAD, LOOK_FORWARD)
robot.move_for(11, 315, 70)
robot.led.on(ALL_LEDS, Color(0, 0, 50))
robot.screen.show_emoji(SAD, LOOK_LEFT)
robot.move_for(11, 225, 70)
robot.led.on(ALL_LEDS, Color(0, 0, 10))
robot.screen.show_emoji(SAD, LOOK_FORWARD)
robot.move_for(11, 45, 70)
wait(.5,SECONDS)
robot.led.off(ALL_LEDS)
act silly#
这个宏可以让机器人做出滑稽的动作,比如旋转、变换表情和闪烁不同颜色的LED灯。当机器人需要做出俏皮或出人意料的反应时,可以使用它。
以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。
# act silly
robot.screen.show_emoji(SILLY)
wait(.5,SECONDS)
robot.sound.play(ACT_SILLY)
wait(115,MSEC)
robot.turn_for(RIGHT, 360, 100, wait=False)
face_list = [SILLY, HAPPY, EXCITED, PROUD, THRILLED, LAUGHING]
colors = [BLUE, CYAN, GREEN, ORANGE, PURPLE, RED, WHITE, YELLOW]
while robot.is_turn_active():
robot.screen.show_emoji(SILLY)
robot.led.on(ALL_LEDS, random.choice(colors))
wait(110,MSEC)
robot.screen.show_emoji(random.choice(face_list))
wait(110,MSEC)
robot.screen.show_emoji(SILLY)
robot.led.off(ALL_LEDS)
wait(.5,SECONDS)
act angry#
这个宏可以让机器人做出生气的反应,包括显示愤怒的表情、播放愤怒的声音、将LED灯变成红色以及快速移动。当机器人需要表现出沮丧或警告的反应时,可以使用它。
以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。
# act angry
robot.screen.show_emoji(ANGRY)
wait(.5,SECONDS)
robot.sound.play(ACT_ANGRY)
wait(115,MSEC)
robot.led.on(ALL_LEDS, RED)
robot.move_for(25, 0, 50)
wait(50,MSEC)
for i in range(5):
robot.screen.show_emoji(ANNOYED)
robot.move_for(5, 180, 50)
robot.screen.show_emoji(ANGRY)
wait(50,MSEC)
wait(.5,SECONDS)
robot.led.off(ALL_LEDS)
act excited#
这个宏可以让机器人表现得兴奋,包括露出兴奋的表情、播放兴奋的声音、快速左右移动以及闪烁橙色LED灯。当机器人需要庆祝或做出充满活力的反应时,可以使用它。
以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。
# act excited
robot.screen.show_emoji(EXCITED)
wait(.5,SECONDS)
robot.sound.play(ACT_EXCITED)
wait(115, MSEC)
for angle in [3,-3,2,-2,2,-2,2,-2,1,-1]:
if angle > 0:
robot.screen.show_emoji(EXCITED, LOOK_RIGHT)
robot.led.on(LED4, ORANGE)
robot.led.on(LED5, ORANGE)
robot.led.on(LED6, ORANGE)
robot.led.off(LED1)
else:
robot.screen.show_emoji(EXCITED, LOOK_LEFT)
robot.led.on(LED1, ORANGE)
robot.led.on(LED2, ORANGE)
robot.led.on(LED3, ORANGE)
robot.led.off(LED6)
robot.turn_for(RIGHT, angle)
robot.led.off(ALL_LEDS)
robot.screen.show_emoji(EXCITED, LOOK_FORWARD)
wait(.5,SECONDS)
AI视觉(运动球)#
turn right until sports ball#
此宏命令会让机器人向右转弯,直到AI视觉传感器检测到运动球,然后机器人转向检测到的物体。当机器人需要在靠近或捡起运动球之前搜索它时,可以使用此宏命令。
Note: If the robot has difficulty detecting objects, try lowering the robot’s turn velocity with set_turn_velocity.
以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。
# Turn right until sports ball is detected
robot.turn(RIGHT)
while True:
vision_data = robot.vision.get_data(SPORTS_BALL)
if vision_data[0].exists:
# Turn to the object by adding your current heading and the vision bearing offset
robot.turn_to(robot.inertial.get_heading() + vision_data[0].bearing)
break
wait(20, MSEC)
turn left until sports ball#
此宏命令会让机器人向左转弯,直到AI视觉传感器检测到运动球,然后机器人转向检测到的物体。当机器人需要在靠近或捡起运动球之前搜索它时,可以使用此宏命令。
Note: If the robot has difficulty detecting objects, try lowering the robot’s turn velocity with set_turn_velocity.
以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。
# Turn left until sports ball is detected
robot.turn(LEFT)
while True:
vision_data = robot.vision.get_data(SPORTS_BALL)
if vision_data[0].exists:
# Turn to the object by adding your current heading and the vision bearing offset
robot.turn_to(robot.inertial.get_heading() + vision_data[0].bearing)
break
wait(20, MSEC)
get sports ball#
此宏命令会让机器人向检测到的运动球移动,直到机器人捡起该球为止。请在机器人能够看到运动球之后,或在使用“转向直到找到运动球”宏找到运动球之后使用此宏命令。
Note: If the robot has difficulty detecting objects, try lowering the robot’s move and turn velocities with set_move_velocity and set_turn_velocity.
以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。
# Get sports ball
while True:
vision_data = robot.vision.get_data(SPORTS_BALL)
if vision_data[0].exists:
if robot.has_sports_ball():
robot.stop_all_movement()
break
else:
robot.move_at(vision_data[0].bearing)
else:
robot.move_at(0)
wait(20, MSEC)
AI视觉(橙色桶)#
turn right until orange barrel#
此宏命令会让机器人向右转弯,直到AI视觉传感器检测到橙色桶,然后机器人转向检测到的物体。当机器人需要在靠近或收集橙色桶之前搜索它时,可以使用此宏命令。
Note: If the robot has difficulty detecting objects, try lowering the robot’s turn velocity with set_turn_velocity.
以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。
# Turn right until orange barrel is detected
robot.turn(RIGHT)
while True:
vision_data = robot.vision.get_data(ORANGE_BARREL)
if vision_data[0].exists:
# Turn to the object by adding your current heading and the vision bearing offset
robot.turn_to(robot.inertial.get_heading() + vision_data[0].bearing)
break
wait(20, MSEC)
turn left until orange barrel#
此宏命令会让机器人向左转弯,直到AI视觉传感器检测到橙色桶,然后机器人转向检测到的物体。当机器人需要先搜索橙色桶,然后再靠近或收集它时,可以使用此宏命令。
Note: If the robot has difficulty detecting objects, try lowering the robot’s turn velocity with set_turn_velocity.
以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。
# Turn left until orange barrel is detected
robot.turn(LEFT)
while True:
vision_data = robot.vision.get_data(ORANGE_BARREL)
if vision_data[0].exists:
# Turn to the object by adding your current heading and the vision bearing offset
robot.turn_to(robot.inertial.get_heading() + vision_data[0].bearing)
break
wait(20, MSEC)
get orange barrel#
此宏命令会让机器人朝着检测到的橙色桶移动,直到机器人拾取到它为止。请在机器人能够看到橙色桶之后,或者在使用“转向直到找到橙色桶”宏找到橙色桶之后使用此宏。
Note: If the robot has difficulty detecting objects, try lowering the robot’s move and turn velocities with set_move_velocity and set_turn_velocity.
以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。
# Get orange barrel
while True:
vision_data = robot.vision.get_data(ORANGE_BARREL)
if vision_data[0].exists:
if robot.has_orange_barrel():
robot.stop_all_movement()
break
else:
robot.move_at(vision_data[0].bearing)
else:
robot.move_at(0)
wait(20, MSEC)
人工智能视觉(蓝桶)#
turn right until blue barrel#
此宏命令会让机器人向右转弯,直到AI视觉传感器检测到蓝色桶,然后机器人转向检测到的物体。当机器人需要先搜索蓝色桶,然后再靠近或收集它时,可以使用此宏命令。
Note: If the robot has difficulty detecting objects, try lowering the robot’s turn velocity with set_turn_velocity.
以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。
# Turn right until blue barrel is detected
robot.turn(RIGHT)
while True:
vision_data = robot.vision.get_data(BLUE_BARREL)
if vision_data[0].exists:
# Turn to the object by adding your current heading and the vision bearing offset
robot.turn_to(robot.inertial.get_heading() + vision_data[0].bearing)
break
wait(20, MSEC)
turn left until blue barrel#
此宏命令会让机器人向左转弯,直到AI视觉传感器检测到蓝色桶,然后机器人转向检测到的物体。当机器人需要先搜索蓝色桶,然后再靠近或收集它时,可以使用此宏命令。
Note: If the robot has difficulty detecting objects, try lowering the robot’s turn velocity with set_turn_velocity.
以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。
# Turn left until blue barrel is detected
robot.turn(LEFT)
while True:
vision_data = robot.vision.get_data(BLUE_BARREL)
if vision_data[0].exists:
# Turn to the object by adding your current heading and the vision bearing offset
robot.turn_to(robot.inertial.get_heading() + vision_data[0].bearing)
break
wait(20, MSEC)
get blue barrel#
此宏命令会让机器人朝着检测到的蓝色桶移动,直到收集到桶为止。请在机器人能够看到蓝色桶之后,或在使用“转向直到找到蓝色桶”宏找到蓝色桶之后使用此宏。
Note: If the robot has difficulty detecting objects, try lowering the robot’s move and turn velocities with set_move_velocity and set_turn_velocity.
以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。
# Get blue barrel
while True:
vision_data = robot.vision.get_data(BLUE_BARREL)
if vision_data[0].exists:
if robot.has_blue_barrel():
robot.stop_all_movement()
break
else:
robot.move_at(vision_data[0].bearing)
else:
robot.move_at(0)
wait(20, MSEC)
人工智能视觉(AIM机器人)#
turn right until AIM robot#
此宏命令会使机器人向右旋转,直到AI视觉传感器检测到另一个VEX AIM编码机器人,然后机器人转向该机器人。当机器人需要先搜索其他机器人才能靠近时,可以使用此宏命令。
Note: If the robot has difficulty detecting objects, try lowering the robot’s turn velocity with set_turn_velocity.
以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。
# Turn right until AIM robot is detected
robot.turn(RIGHT)
while True:
vision_data = robot.vision.get_data(AIM_ROBOT)
if vision_data[0].exists:
# Turn to the object by adding your current heading and the vision bearing offset
robot.turn_to(robot.inertial.get_heading() + vision_data[0].bearing)
break
wait(20, MSEC)
turn left until AIM robot#
此宏命令会使机器人向左转弯,直到AI视觉传感器检测到另一个VEX AIM编码机器人,然后机器人转向该机器人。当机器人需要先搜索其他机器人才能靠近时,可以使用此宏命令。
Note: If the robot has difficulty detecting objects, try lowering the robot’s turn velocity with set_turn_velocity.
以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。
# Turn left until AIM robot is detected
robot.turn(LEFT)
while True:
vision_data = robot.vision.get_data(AIM_ROBOT)
if vision_data[0].exists:
# Turn to the object by adding your current heading and the vision bearing offset
robot.turn_to(robot.inertial.get_heading() + vision_data[0].bearing)
break
wait(20, MSEC)
move to AIM robot#
此宏命令使机器人向另一台 VEX AIM 编码机器人移动,直到距离足够近可以停止。请在机器人能够看到另一台机器人之后,或在使用“转向直到找到 AIM 机器人”宏找到目标机器人之后使用此宏命令。
Note: If the robot has difficulty detecting objects, try lowering the robot’s move and turn velocities with set_move_velocity and set_turn_velocity.
以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。
# Move to AIM robot
while True:
vision_data = robot.vision.get_data(AIM_ROBOT)
if vision_data[0].exists:
if vision_data[0].width >= 140:
robot.stop_all_movement()
break
else:
robot.move_at(vision_data[0].bearing)
else:
robot.move_at(0)
wait(20, MSEC)
人工智能视觉(AprilTag)#
turn right until AprilTag#
This macro turns the robot to the right until the AI Vision Sensor detects AprilTag ID 0, then turns toward the detected AprilTag. Use it when the robot needs to search for an AprilTag before moving toward it. To change which AprilTag the robot will turn until, replace the 0 in TAG0 with any number from 0 to 37 (AprilTags 5 through 37 can be used with printed AprilTags from AIM Printables.)
Note: If the robot has difficulty detecting objects, try lowering the robot’s turn velocity with set_turn_velocity.
以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。
# Turn right until AprilTag ID 0 is detected
robot.turn(RIGHT)
while True:
vision_data = robot.vision.get_data(TAG0)
if vision_data[0].exists:
# Turn to the object by adding your current heading and the vision bearing offset
robot.turn_to(robot.inertial.get_heading() + vision_data[0].bearing)
break
wait(20, MSEC)
turn left until AprilTag#
This macro turns the robot to the left until the AI Vision Sensor detects AprilTag ID 0, then turns toward the detected AprilTag. Use it when the robot needs to search for an AprilTag before moving toward it. To change which AprilTag the robot will turn until, replace the 0 in TAG0 with any number from 0 to 37 (AprilTags 5 through 37 can be used with printed AprilTags from AIM Printables.)
Note: If the robot has difficulty detecting objects, try lowering the robot’s turn velocity with set_turn_velocity.
以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。
# Turn left until AprilTag ID 0 is detected
robot.turn(LEFT)
while True:
vision_data = robot.vision.get_data(TAG0)
if vision_data[0].exists:
# Turn to the object by adding your current heading and the vision bearing offset
robot.turn_to(robot.inertial.get_heading() + vision_data[0].bearing)
break
wait(20, MSEC)
move to AprilTag#
This macro moves the robot toward AprilTag ID 0 until it is close enough to stop. Use this after the robot can see an AprilTag, or after using a turn-until-AprilTag macro to find one. To change which AprilTag the robot will move to, replace the 0 in TAG0 with any number from 0 to 37 (AprilTags 5 through 37 can be used with printed AprilTags from AIM Printables.)
Note: If the robot has difficulty detecting objects, try lowering the robot’s move and turn velocities with set_move_velocity and set_turn_velocity.
以下代码展示了从工具箱拖动此宏后添加到项目中的 Python 代码序列。您可以直接使用此序列,也可以在添加后对其进行编辑。
# Move to AprilTag ID 0
while True:
vision_data = robot.vision.get_data(TAG0)
if vision_data[0].exists:
if vision_data[0].width >= 60:
robot.stop_all_movement()
break
else:
robot.move_at(vision_data[0].bearing)
else:
robot.move_at(0)
wait(20, MSEC)