Python específico para robots#

Introducción#

El patio de juegos VIQRC 25-26 Mix & Match presenta métodos exclusivos para la construcción diseñada para este patio de juegos, incluidas dos opciones de motor, sensor óptico y LED táctil.

Todos los métodos estándar de VEXcode VR están disponibles para su uso en el patio de juegos VIQRC 25-26 Mix & Match.

A continuación se muestra una lista de todos los métodos específicos de Playground disponibles:

Movimiento: mueve y rastrea los motores del robot.

  • Comportamiento

    • spin – Hace girar el motor o el grupo de motores seleccionado indefinidamente.

    • spin_for – Hace girar un motor o grupo durante una distancia específica en grados o vueltas.

    • spin_to_position – Gira un motor o un grupo de motores a una posición establecida.

    • stop – Detiene el giro de un motor o un grupo de motores específico.

  • Mutadores

    • set_position – Establece el valor del codificador de un motor o grupo de motores.

    • set_velocity – Establece la velocidad de un motor o grupo de motores como un porcentaje.

    • set_timeout – Limita el tiempo que un bloque de motor espera antes de darse por vencido si el movimiento está bloqueado.

  • Conseguidores

    • is_done – Devuelve un valor booleano que indica si el motor ya no está girando.

    • is_spinning – Devuelve un valor booleano que indica si el motor está girando actualmente.

    • posición – Devuelve la posición de rotación actual del motor en grados o vueltas.

    • velocidad – Devuelve la velocidad actual del motor en % o rpm.

Detección: utilice los distintos sensores del robot.

  • LED táctil

    • set_color – Establece el TouchLED en un color seleccionado.

  • Óptico

    • is_near_object – Devuelve si un objeto detectado está cerca del sensor óptico.

    • color – Devuelve el color detectado por el sensor óptico.

    • brillo – Devuelve el porcentaje de brillo detectado por el sensor.

    • hue – Devuelve el valor de tono del color detectado.

    • object_detected – Registra una función de devolución de llamada para cuando el sensor óptico detecta un objeto.

    • object_lost – Registra una función de devolución de llamada para cuando el sensor óptico pierde un objeto.

Movimiento#

Actions#

girar#

spin spins a motor indefinitely.

Uso:
Se puede utilizar uno de los dos objetos de motor disponibles con este método, como se muestra a continuación:

motor

Dominio

claw_motor

claw_motor.spin(direction) — The Claw Motor

lift_motor

lift_motor.spin(direction) — The Lift Motor

Parámetros

Descripción

direction

The direction for the motor to spin:

  • FORWARD – Opens claw or lowers the lift.
  • REVERSE – Closes claw or raises the lift.
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)

girar_para#

spin_for spins a motor for a given amount of degrees or turns.

Uso:
Se puede utilizar uno de los dos objetos de motor disponibles con este método, como se muestra a continuación:

motor

Dominio

claw_motor

claw_motor.spin_for(direction, distance, units, wait=True) — The Claw Motor

lift_motor

lift_motor.spin_for(direction, distance, units, wait=True) — The Lift Motor

Parámetros

Descripción

direction

The direction for the motor to spin:

  • FORWARD – Opens claw or lowers the lift.
  • REVERSE – Closes claw or raises the lift.

distance

La distancia que debe girar el motor, expresada en un número entero.

units

The unit that represents the distance to rotate:

  • DEGREES
  • TURNS

wait

Optional.

  • wait=True (default) - The robot waits until spin_for is complete before executing the next line of code.
  • wait=False - The robot starts the action and moves on to the next line of code right away, without waiting for spin_for to finish.

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)

girar a la posición#

spin_to spins a motor to a given position.

Uso:
Se puede utilizar uno de los dos objetos de motor disponibles con este método, como se muestra a continuación:

motor

Dominio

claw_motor

claw_motor.spin_to_position(angle, units, wait=True) — The Claw Motor

lift_motor

lift_motor.spin_to_position(angle, units, wait=True) — The Lift Motor

Parámetros

Descripción

angle

El ángulo específico o número de vueltas que dará el motor.

units

The unit that represents the angle to rotate to:

  • DEGREES
  • TURNS

wait

Optional.

  • wait=True (default) - The robot waits until spin_to_position is complete before executing the next line of code.
  • wait=False - The robot starts the action and moves on to the next line of code right away, without waiting for spin_to_position to finish.

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)

detener#

stop stops a motor from spinning.

Uso:
Se puede utilizar uno de los dos objetos de motor disponibles con este método, como se muestra a continuación:

motor

Dominio

claw_motor

claw_motor.stop() — The Claw Motor

lift_motor

lift_motor.stop() — The Lift Motor

Parámetros

Descripción

Este método no tiene parámetros.

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)

Mutators#

posición_establecida#

set_position sets a motor’s encoder position to the given position value.

Uso:
Se puede utilizar uno de los dos objetos de motor disponibles con este método, como se muestra a continuación:

motor

Dominio

claw_motor

claw_motor.set_position(position, units) — The Claw Motor

lift_motor

lift_motor.set_position(position, units) — The Lift Motor

Parámetros

Descripción

position

El entero específico que se debe configurar para el codificador del motor.

units

The unit that represents the angle to rotate to:

  • DEGREES
  • TURNS
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)

establecer_velocidad#

set_velocity sets the speed of a motor.

Uso:
Se puede utilizar uno de los dos objetos de motor disponibles con este método, como se muestra a continuación:

motor

Dominio

claw_motor

claw_motor.set_velocity(velocity, units) — The Claw Motor

lift_motor

lift_motor.set_velocity(velocity, units) — The Lift Motor

Parámetros

Descripción

velocity

La velocidad a la que girará el motor IQ, que varía de 0 a 100.

units

The unit that represents the new velocity:

  • PERCENT
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)

establecer_tiempo_de_espera#

set_timeout sets a time limit for a motor’s movement commands. This prevents motion commands that do not reach their intended position from preventing subsequent commands from running.

Uso:
Se puede utilizar uno de los dos objetos de motor disponibles con este método, como se muestra a continuación:

motor

Dominio

claw_motor

claw_motor.set_timeout(value, units) — The Claw Motor

lift_motor

lift_motor.set_timeout(value, units) — The Lift Motor

Parámetros

Descripción

value

La cantidad de tiempo que el motor esperará antes de detenerse.

units

The unit to represent the timeout:

  • SECONDS
  • MSEC – milliseconds
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)

Getters#

está_hecho#

is_done returns a Boolean indicating whether the specified motor is not spinning.

  • True – The specified motor is not spinning.

  • False – The specified motor is spinning.

Uso:
Se puede utilizar uno de los dos objetos de motor disponibles con este método, como se muestra a continuación:

motor

Dominio

claw_motor

claw_motor.is_done() — The Claw Motor

lift_motor

lift_motor.is_done() — The Lift Motor

Parámetros

Descripción

Este método no tiene parámetros.

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)

está girando#

is_spinning returns a Boolean indicating whether the specified motor is spinning.

  • True – The specified motor is spinning.

  • False – The specified motor is not spinning.

Uso:
Se puede utilizar uno de los dos objetos de motor disponibles con este método, como se muestra a continuación:

motor

Dominio

claw_motor

claw_motor.is_spinning() — The Claw Motor

lift_motor

lift_motor.is_spinning() — The Lift Motor

Parámetros

Descripción

Este método no tiene parámetros.

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)

posición#

position returns the total distance the specified motor has rotated.

Uso:
Se puede utilizar uno de los dos objetos de motor disponibles con este método, como se muestra a continuación:

motor

Dominio

claw_motor

claw_motor.position(units) — The Claw Motor

lift_motor

lift_motor.position(units) — The Lift Motor

Parámetros

Descripción

units

The units that represent the motor’s position:

  • DEGREES
  • TURNS
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)

velocidad#

velocity returns the current rotational speed of the motor.

Uso:
Se puede utilizar uno de los dos objetos de motor disponibles con este método, como se muestra a continuación:

motor

Dominio

claw_motor

claw_motor.velocity(units) — The Claw Motor

lift_motor

lift_motor.velocity(units) — The Lift Motor

Parámetros

Descripción

units

The unit that represent the motor’s position:

  • PERCENT
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)

Detección#

Touch LED#

establecer_color#

set_color sets the color of the Touch LED.

Usage:
touchled.set_color(color)

Parámetros

Descripción

color

The color to set the Touch LED to:

  • NONE – Turns off the Touch LED
  • RED
  • GREEN
  • BLUE
  • YELLOW
  • ORANGE
  • PURPLE
  • WHITE
  • RED_VIOLET
  • VIOLET
  • BLUE_VIOLET
  • BLUE_GREEN
  • YELLOW_GREEN
  • YELLOW_ORANGE
  • RED_ORANGE

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)

Optical#

está_cerca_del_objeto#

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()

Parámetros

Descripción

Este método no tiene parámetros.

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#

color returns the color detected by the Optical Sensor:

Color devuelto:

  • NONE – No color detected.
  • RED
  • GREEN
  • BLUE
  • YELLOW
  • ORANGE
  • PURPLE
  • WHITE
  • RED_VIOLET
  • VIOLET
  • BLUE_VIOLET
  • BLUE_GREEN
  • YELLOW_GREEN
  • YELLOW_ORANGE
  • RED_ORANGE

Usage:
optical.color()

Parámetros

Descripción

Este método no tiene parámetros.

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)

brillo#

brightness returns the brightness value detected by the Optical Sensor as a percent from 0% to 100%.

Usage:
optical.brightness()

Parámetros

Descripción

Este método no tiene parámetros.

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)

matiz#

hue returns the hue detected by the Optical Sensor.

Los valores de tono varían de 0 a 359 grados, correspondientes a las posiciones en la rueda de color que se muestran a continuación.

Una rueda de color circular que muestra un espectro completo de tonos etiquetados con valores de grados alrededor del perímetro, que aumentan en incrementos de 30 grados desde 0° en la parte superior hasta 360°.

Usage:
optical.hue()

Parámetros

Descripción

Este método no tiene parámetros.

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)

objeto_detectado#

object_detected registers a callback function for when the Optical Sensor detects an object.

Usage:
optical.object_detected(callback, arg)

Parámetros

Descripción

callback

Una función que se llamará cuando se detecte un objeto.

arg

Opcional. Una tupla que se utiliza para pasar argumentos a la función de devolución de llamada.

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)

objeto_perdido#

object_lost registers a callback function for when the Optical Sensor loses an object.

Usage:
optical.object_lost(callback, arg)

Parámetros

Descripción

callback

Una función que se llamará cuando se pierda un objeto.

arg

Opcional. Una tupla que se utiliza para pasar argumentos a la función de devolución de llamada.

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)