Python spécifique aux robots#

Introduction#

Le robot héros Flop comprend un moteur d’admission, un moteur de bras et un capteur de vision IA IQ.

Toutes les commandes Python VR standard de VEXcode sont disponibles pour une utilisation dans le terrain de jeu IQ 26-27 Level Up.

Vous trouverez ci-dessous la liste de toutes les commandes Python spécifiques à Robot disponibles :

Mouvement – ​​Déplacer et suivre les moteurs du robot.

  • Actes

    • spin – Fait tourner un moteur dans une direction pour toujours.

    • spin_for – Fait tourner un moteur pendant un nombre spécifique de degrés ou de tours.

    • spin_to_position – Fait tourner un moteur jusqu’à une position spécifique.

    • stop – Arrête la rotation d’un moteur.

  • Paramètres

    • set_velocity – Définit la vitesse de rotation du moteur.

    • set_timeout – Définit la durée pendant laquelle un moteur essaiera de terminer un mouvement.

    • set_position – Modifie la position actuelle du moteur à une nouvelle valeur.

  • Valeurs

    • is_done – Renvoie si le moteur a fini de bouger.

    • is_spinning – Renvoie si le moteur tourne.

    • position – Renvoie la position actuelle du moteur.

    • vitesse – Renvoie la vitesse de rotation du moteur.

Vision par IA – Capturez et analysez des objets à l’aide du capteur de vision par IA IQ.

  • Attrapeurs

    • take_snapshot – Filtre l’image actuelle du capteur selon une signature spécifique et renvoie un tuple d’objets détectés.

  • Propriétés

    • .width – Renvoie la largeur de l’objet détecté en pixels.

    • .height – Renvoie la hauteur de l’objet détecté en pixels.

    • .centerX – Renvoie la coordonnée x du centre de l’objet détecté.

    • .centerY – Renvoie la coordonnée y du centre de l’objet détecté.

    • .originX – Renvoie la coordonnée x du coin supérieur gauche de la boîte englobante de l’objet détecté.

    • .originY – Renvoie la coordonnée y du coin supérieur gauche de la boîte englobante de l’objet détecté.

    • .id – Renvoie l’ID de la classification IA détectée.

Les exemples de cette page utilisent la position de départ par défaut du Playground.

Mouvement#

Flop possède deux moteurs spécifiques au robot : intake_motor et arm_motorLes deux utilisent des commandes de moteur standard.

Constantes de direction :

  • intake_motor: FORWARD = admission (collecte) ; REVERSE = évacuation (éjecte)

  • arm_motor: FORWARD = haut (lève) ; REVERSE = bas (abaisse)

rotation#

La commande spin fait tourner le moteur indéfiniment dans le sens indiqué. Le moteur continuera de tourner jusqu’à réception d’une nouvelle commande.

Usage:
motor.spin(direction)

Paramètres

Description

direction

Sens de rotation : FORWARD ou REVERSE.

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)

tourner_pour#

La commande spin_for spin_for fait tourner un moteur sur une distance spécifique. Le programme attend la fin de la rotation du moteur avant d’exécuter la commande suivante, sauf si l’option wait=False est spécifiée.

Usage:
motor.spin_for(direction, amount, unit)

Paramètres

Description

direction

Sens de rotation : FORWARD ou REVERSE.

amount

Distance de rotation. Il peut s’agir d’un nombre entier ou décimal.

unit

L’unité de mesure : DEGREES ou TURNS.

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)

rotation_vers_position#

La commande spin_to_position fait tourner un moteur jusqu’à une position spécifique. Le programme attend que le moteur ait terminé avant d’exécuter la commande suivante, sauf si wait=False est spécifiée.

Usage:
motor.spin_to_position(position, unit)

Paramètres

Description

position

La position de rotation. Il peut s’agir d’un entier ou d’un nombre décimal.

unit

L’unité de mesure : DEGREES ou TURNS.

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)

arrêt#

stop empêche le moteur de tourner.

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)

définir_vitesse#

set_velocity définit la vitesse de rotation d’un moteur en pourcentage, de 0 à 100.

Usage:
motor.set_velocity(velocity, unit)

Paramètres

Description

velocity

La vitesse du moteur en pourcentage, de 0 à 100.

unit

L’unité de mesure : PERCENT.

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)

définir_délai#

set_timeout définit le nombre de secondes pendant lesquelles un moteur tentera de terminer un mouvement avant de s’arrêter.

Usage:
motor.set_timeout(timeout, unit)

Paramètres

Description

timeout

Le nombre de secondes pendant lesquelles le moteur tentera de terminer un mouvement.

unit

L’unité de mesure : SECONDS.

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)

définir_position#

La commande set_position modifie la position actuelle du moteur en une nouvelle valeur, réinitialisant ainsi son encodeur.

Usage:
motor.set_position(position, unit)

Paramètres

Description

position

La nouvelle valeur de position.

unit

L’unité de mesure : DEGREES ou TURNS.

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_terminé#

is_done renvoie une valeur booléenne indiquant si le moteur a terminé son mouvement.

  • True – Le moteur a fini de tourner.

  • False – Le moteur est toujours en mouvement.

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_en_roulement#

is_spinning renvoie une valeur booléenne indiquant si le moteur est actuellement en rotation.

  • True – Le moteur tourne.

  • False – Le moteur ne tourne pas.

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)

position#

La position position renvoie la position actuelle du moteur.

Usage:
motor.position(unit)

Paramètres

Description

unit

L’unité de mesure : DEGREES ou TURNS.

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)

vitesse#

La vitesse velocity renvoie la vitesse de rotation actuelle du moteur en pourcentage, de -100 à 100.

Usage:
motor.velocity(unit)

Paramètres

Description

unit

L’unité de mesure : PERCENT.

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)

Vision IA#

prendre_un_instantané#

take_snapshot filtre les données de la trame du capteur de vision IQ AI en une seule signature — une description enregistrée de quelque chose que le capteur peut reconnaître, comme un élément de jeu sur le terrain — et renvoie un tuple.

The tuple stores objects ordered from largest to smallest by width, starting at index 0. Each object’s properties can be accessed using its index. An empty tuple is returned if no matching objects are detected.

Usage:
ai_vision.take_snapshot(signature)

Paramètres

Description

signature

Filtre l’ensemble de données pour n’inclure que les données de la signature spécifiée. Les signatures disponibles sont :

  • AiVision.ALL_AIOBJS - Détecte tous les Bean Bags.
  • AiVision.ALL_TAGS - Détecte les AprilTags, situés sur les côtés des buts.

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)

Propriétés#

Il existe sept propriétés qui sont incluses avec chaque objet stocké dans un tuple après l’utilisation de take_snapshot.

Toutes les valeurs de propriété, à l’exception .id décrivent la position et la taille de l’objet détecté dans le champ de vision du capteur IQ AI Vision au moment take_snapshot . Ces valeurs sont exprimées en pixels, selon la résolution du capteur (320 x 240 pixels).

.largeur#

.width renvoie la largeur de l’objet détecté en pixels, qui est un entier compris entre 1 et 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)

.hauteur#

.height renvoie la hauteur de l’objet détecté en pixels, qui est un entier compris entre 1 et 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)

.centreX#

.centerX renvoie la coordonnée x du centre de l’objet détecté en pixels, qui est un entier compris entre 0 et 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)

.centreY#

.centerY renvoie la coordonnée y du centre de l’objet détecté en pixels, qui est un entier compris entre 0 et 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)

.origineX#

.originX renvoie la coordonnée x du coin supérieur gauche de la boîte englobante de l’objet détecté en pixels, qui est un entier compris entre 0 et 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)

.origine#

.originY renvoie la coordonnée y du coin supérieur gauche de la boîte englobante de l’objet détecté en pixels, qui est un entier compris entre 0 et 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)

.identifiant#

.id renvoie l’ID de la classification IA détectée sous forme d’entier.

Classification par IA

identifiant

Sac à haricots bleus

0

Sac à haricots rouges

1

Sac à haricots jaunes

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)