Python específico para robots#
Introducción#
El robot héroe, Flop, incluye un motor de admisión, un motor de brazo y un sensor de visión con IA IQ.
Todos los comandos estándar de VEXcode VR Python están disponibles para su uso en el IQ 26-27 Level Up.
A continuación se muestra una lista de todos los comandos de Python disponibles específicos para Robot:
Movimiento: Mueve y rastrea los motores del robot.
Comportamiento
spin – Hace girar un motor en una dirección indefinidamente.
spin_for – Hace girar un motor durante un número específico de grados o vueltas.
spin_to_position – Hace girar un motor a una posición específica.
stop – Detiene el giro de un motor.
Ajustes
set_velocity – Establece la velocidad de giro de un motor.
set_timeout – Establece cuánto tiempo un motor intentará terminar un movimiento.
set_position – Cambia la posición actual del motor a un nuevo valor.
Valores
is_done – Devuelve si el motor ha terminado de moverse.
is_spinning – Devuelve si el motor está girando.
posición – Devuelve la posición actual del motor.
velocidad – Devuelve la velocidad de giro del motor.
Visión por IA: Captura y analiza objetos utilizando el sensor de visión por IA IQ.
Obtenidos
take_snapshot – Filtra el fotograma actual del sensor a una firma específica y devuelve una tupla de objetos detectados.
Propiedades
.width – Devuelve el ancho del objeto detectado en píxeles.
.altura – Devuelve la altura del objeto detectado en píxeles.
.centerX – Devuelve la coordenada x del centro del objeto detectado.
.centerY – Devuelve la coordenada y del centro del objeto detectado.
.originX – Devuelve la coordenada x de la esquina superior izquierda del cuadro delimitador del objeto detectado.
.originY – Devuelve la coordenada y de la esquina superior izquierda del cuadro delimitador del objeto detectado.
.id – Devuelve el ID de la clasificación de IA detectada.
Los ejemplos de esta página utilizan la posición de inicio predeterminada del Playground.
Movimiento#
Flop has two robot-specific motors: intake_motor and arm_motor. Both use standard motor commands.
Constantes de dirección:
intake_motor:FORWARD= intake (collects);REVERSE= outtake (ejects)arm_motor:FORWARD= up (raises);REVERSE= down (lowers)
girar#
spin spins a motor in the given direction forever. The motor will continue to spin until it is given another command.
Usage:
motor.spin(direction)
Parámetros |
Descripción |
|---|---|
|
The direction to spin: |
def main():
# Score a Bean Bag in a Blue Goal
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 35, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
arm_motor.spin_for(FORWARD, 1, TURNS)
drivetrain.drive_for(FORWARD, 40, INCHES)
intake_motor.spin(REVERSE)
# VR threads — Do not delete
vr_thread(main)
girar_para#
spin_for spins a motor for a specific distance. The project waits until the motor is done before the next command runs unless wait=False is passed.
Usage:
motor.spin_for(direction, amount, unit)
Parámetros |
Descripción |
|---|---|
|
The direction to spin: |
|
La distancia a girar. Puede ser un número entero o decimal. |
|
The unit of measurement: |
def main():
# Score a Bean Bag in a Blue Goal
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 35, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
arm_motor.spin_for(FORWARD, 1, TURNS)
drivetrain.drive_for(FORWARD, 40, INCHES)
intake_motor.spin(REVERSE)
# VR threads — Do not delete
vr_thread(main)
girar_a_posición#
spin_to_position spins a motor to a specific position. The project waits until the motor is done before the next command runs unless wait=False is passed.
Usage:
motor.spin_to_position(position, unit)
Parámetros |
Descripción |
|---|---|
|
La posición a la que girar. Puede ser un número entero o decimal. |
|
The unit of measurement: |
def main():
# Score a Bean Bag in a Blue Goal
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 35, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
arm_motor.spin_to_position(1, TURNS)
drivetrain.drive_for(FORWARD, 40, INCHES)
intake_motor.spin(REVERSE)
# VR threads — Do not delete
vr_thread(main)
detener#
stop stops a motor from spinning.
Usage:
motor.stop()
def main():
# Score a Bean Bag in a Blue Goal
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 35, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
arm_motor.spin_for(FORWARD, 1, TURNS)
drivetrain.drive_for(FORWARD, 40, INCHES)
intake_motor.spin(REVERSE)
wait(1, SECONDS)
intake_motor.stop()
# VR threads — Do not delete
vr_thread(main)
establecer_velocidad#
set_velocity sets how fast a motor will spin as a percentage from 0 to 100.
Usage:
motor.set_velocity(velocity, unit)
Parámetros |
Descripción |
|---|---|
|
La velocidad del motor como porcentaje de 0 a 100. |
|
The unit of measurement: |
def main():
# Score a Bean Bag in a Blue Goal
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 35, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
arm_motor.spin_for(FORWARD, 1, TURNS)
drivetrain.drive_for(FORWARD, 40, INCHES)
intake_motor.set_velocity(100, PERCENT)
intake_motor.spin(REVERSE)
# VR threads — Do not delete
vr_thread(main)
establecer_tiempo_de_espera#
set_timeout sets how many seconds a motor will try to finish a movement before stopping.
Usage:
motor.set_timeout(timeout, unit)
Parámetros |
Descripción |
|---|---|
|
El número de segundos que el motor intentará completar un movimiento. |
|
The unit of measurement: |
def main():
# Score a Bean Bag in a Blue Goal
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 35, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
arm_motor.set_timeout(2, SECONDS)
arm_motor.spin_for(FORWARD, 1, TURNS)
drivetrain.drive_for(FORWARD, 40, INCHES)
intake_motor.spin(REVERSE)
# VR threads — Do not delete
vr_thread(main)
posición_de_establecer#
set_position changes the motor’s current position to a new value, resetting its encoder.
Usage:
motor.set_position(position, unit)
Parámetros |
Descripción |
|---|---|
|
El nuevo valor de posición. |
|
The unit of measurement: |
def main():
# Score a Bean Bag in a Blue Goal
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 35, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
arm_motor.set_position(0, DEGREES)
arm_motor.spin_for(FORWARD, 1, TURNS)
drivetrain.drive_for(FORWARD, 40, INCHES)
intake_motor.spin(REVERSE)
# VR threads — Do not delete
vr_thread(main)
está_hecho#
is_done returns a Boolean indicating whether the motor has finished its movement.
True– The motor has finished moving.False– The motor is still moving.
Usage:
motor.is_done()
def main():
# Score a Bean Bag in a Blue Goal
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 35, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
arm_motor.spin_for(FORWARD, 1, TURNS, wait=False)
while not arm_motor.is_done():
wait(5, MSEC)
drivetrain.drive_for(FORWARD, 40, INCHES)
intake_motor.spin(REVERSE)
# VR threads — Do not delete
vr_thread(main)
está_girando#
is_spinning returns a Boolean indicating whether the motor is currently spinning.
True– The motor is spinning.False– The motor is not spinning.
Usage:
motor.is_spinning()
def main():
# Score a Bean Bag in a Blue Goal
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 35, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
arm_motor.spin_for(FORWARD, 1, TURNS)
drivetrain.drive_for(FORWARD, 40, INCHES)
intake_motor.spin(REVERSE)
while intake_motor.is_spinning():
wait(5, MSEC)
intake_motor.stop()
# VR threads — Do not delete
vr_thread(main)
posición#
position returns the motor’s current position.
Usage:
motor.position(unit)
Parámetros |
Descripción |
|---|---|
|
The unit of measurement: |
def main():
# Score a Bean Bag in a Blue Goal
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 35, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
while arm_motor.position(DEGREES) < 360:
arm_motor.spin(FORWARD)
wait(2, MSEC)
arm_motor.stop()
drivetrain.drive_for(FORWARD, 40, INCHES)
intake_motor.spin(REVERSE)
# VR threads — Do not delete
vr_thread(main)
velocidad#
velocity returns how fast the motor is currently spinning as a percentage from -100 to 100.
Usage:
motor.velocity(unit)
Parámetros |
Descripción |
|---|---|
|
The unit of measurement: |
def main():
# Score a Bean Bag in a Blue Goal
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 35, INCHES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
arm_motor.spin_for(FORWARD, 1, TURNS)
drivetrain.drive_for(FORWARD, 40, INCHES)
intake_motor.spin(REVERSE)
wait(0.2, SECONDS)
brain.screen.print(intake_motor.velocity(PERCENT))
# VR threads — Do not delete
vr_thread(main)
Visión por IA#
tomar_instantánea#
take_snapshot filters data from the IQ AI Vision Sensor frame to a single signature — a saved description of something the sensor can recognize, such as a game element on the field — and returns a tuple.
La tupla almacena objetos ordenados de mayor a menor ancho, comenzando en el índice#propertiesSe puede acceder a las propiedades de cada objeto mediante su índice. Se devuelve una tupla vacía si no se detectan objetos coincidentes.
Usage:
ai_vision.take_snapshot(signature)
Parámetros |
Descripción |
|---|---|
|
Filters the dataset to only include data of the given signature. Available signatures are:
|
def main():
# Drop the preloaded Bean Bag
intake_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 90, DEGREES)
# Turn to face a red Bean Bag
drivetrain.set_turn_velocity(30, PERCENT)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
for ai_object in ai_objects:
if ai_object.id == GameElements.RED_BEANBAG:
if ai_object.centerX < 140:
drivetrain.turn(LEFT)
elif ai_object.centerX > 180:
drivetrain.turn(RIGHT)
else:
drivetrain.stop()
return
break
# VR threads — Do not delete
vr_thread(main)
Propiedades#
There are seven properties that are included with each object stored in a tuple after take_snapshot is used.
All property values except .id describe the detected object’s position and size in the IQ AI Vision Sensor’s view at the moment take_snapshot was used. These values are measured in pixels, based on the sensor’s 320 by 240 pixel resolution.
.ancho#
.width returns the width of the detected object in pixels, which is an integer between 1 and 320.
def main():
# Drop the preloaded Bean Bag
intake_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 90, DEGREES)
# Approach the Bean Bag, then pick it up
drivetrain.set_drive_velocity(30, PERCENT)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
for ai_object in ai_objects:
if ai_object.id == GameElements.RED_BEANBAG:
if ai_object.width > 200:
drivetrain.stop()
intake_motor.spin(FORWARD)
return
else:
drivetrain.drive(FORWARD)
break
# VR threads — Do not delete
vr_thread(main)
.altura#
.height returns the height of the detected object in pixels, which is an integer between 1 and 240.
def main():
# Drop the preloaded Bean Bag
intake_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 90, DEGREES)
# Approach the Bean Bag, then pick it up
drivetrain.set_drive_velocity(30, PERCENT)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
for ai_object in ai_objects:
if ai_object.id == GameElements.RED_BEANBAG:
if ai_object.height > 110:
drivetrain.stop()
intake_motor.spin(FORWARD)
return
else:
drivetrain.drive(FORWARD)
break
# VR threads — Do not delete
vr_thread(main)
.centerX#
.centerX returns the x-coordinate of the detected object’s center in pixels, which is an integer between 0 and 320.
def main():
# Drop the preloaded Bean Bag
intake_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 90, DEGREES)
# Steer to keep the Bean Bag centered
drivetrain.set_turn_velocity(30, PERCENT)
drivetrain.set_drive_velocity(30, PERCENT)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
for ai_object in ai_objects:
if ai_object.id == GameElements.RED_BEANBAG:
if ai_object.centerX < 140:
drivetrain.turn_for(LEFT, 5, DEGREES)
else:
drivetrain.drive_for(FORWARD, 150, MM)
intake_motor.spin(FORWARD)
return
break
# VR threads — Do not delete
vr_thread(main)
.centroY#
.centerY returns the y-coordinate of the detected object’s center in pixels, which is an integer between 0 and 240.
def main():
# Drop the preloaded Bean Bag
intake_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 90, DEGREES)
# Print the y-position of the Bean Bag
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
for ai_object in ai_objects:
if ai_object.id == GameElements.RED_BEANBAG:
brain.screen.print(ai_object.centerY)
break
# VR threads — Do not delete
vr_thread(main)
.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():
# Drop the preloaded Bean Bag
intake_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 90, DEGREES)
# Steer to keep the left edge centered
drivetrain.set_turn_velocity(30, PERCENT)
drivetrain.set_drive_velocity(30, PERCENT)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
for ai_object in ai_objects:
if ai_object.id == GameElements.RED_BEANBAG:
if ai_object.originX < 100:
drivetrain.turn_for(LEFT, 5, DEGREES)
else:
drivetrain.drive_for(FORWARD, 150, MM)
intake_motor.spin(FORWARD)
return
break
# VR threads — Do not delete
vr_thread(main)
.origenY#
.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():
# Drop the preloaded Bean Bag
intake_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 90, DEGREES)
# Print the y-position of the Bean Bag's top edge
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
for ai_object in ai_objects:
if ai_object.id == GameElements.RED_BEANBAG:
brain.screen.print(ai_object.originY)
break
# VR threads — Do not delete
vr_thread(main)
.identificación#
.id returns the ID of the detected AI Classification as an integer.
Clasificación de IA |
identificación |
|---|---|
BlueBeanBag |
0 |
Puf rojo |
1 |
Bolsa de frijoles amarilla |
2 |
def main():
# Drop the preloaded Bean Bag
intake_motor.spin_for(REVERSE, 180, DEGREES)
drivetrain.turn_for(LEFT, 90, DEGREES)
# Pick up the Bean Bag if it's red
drivetrain.set_drive_velocity(30, PERCENT)
ai_objects = ai_vision.take_snapshot(AiVision.ALL_AIOBJS)
for ai_object in ai_objects:
if ai_object.id == GameElements.RED_BEANBAG:
intake_motor.spin(FORWARD)
drivetrain.drive_for(FORWARD, 150, MM)
break
# VR threads — Do not delete
vr_thread(main)
def main():
# Stop when you see the Red Goal
drivetrain.set_turn_velocity(30, PERCENT)
drivetrain.turn(RIGHT)
while True:
ai_objects = ai_vision.take_snapshot(AiVision.ALL_TAGS)
for ai_object in ai_objects:
if ai_object.id == 1:
drivetrain.stop()
brain.screen.print("Found the Red Goal!")
return
# VR threads — Do not delete
vr_thread(main)