Python ειδικά για ρομπότ#
Εισαγωγή#
Το Hero Bot, Flop, περιλαμβάνει έναν κινητήρα εισαγωγής, έναν βραχίονα και έναν αισθητήρα όρασης IQ AI.
Όλες οι τυπικές εντολές VEXcode VR Python είναι διαθέσιμες για χρήση στο IQ 26-27 Level Up Playground.
Παρακάτω είναι μια λίστα με όλες τις διαθέσιμες εντολές Python ειδικά για Robot:
Κίνηση – Μετακίνηση και παρακολούθηση των κινητήρων του ρομπότ.
Ενέργειες
spin – Περιστρέφει έναν κινητήρα προς μία κατεύθυνση για πάντα.
spin_for – Περιστρέφει έναν κινητήρα για έναν συγκεκριμένο αριθμό μοιρών ή στροφών.
spin_to_position – Περιστρέφει έναν κινητήρα σε μια συγκεκριμένη θέση.
stop – Σταματά την περιστροφή ενός κινητήρα.
Ρυθμίσεις
set_velocity – Ορίζει την ταχύτητα περιστροφής ενός κινητήρα.
set_timeout – Ορίζει το χρονικό διάστημα που θα χρειαστεί ένας κινητήρας για να ολοκληρώσει μια κίνηση.
set_position – Αλλάζει την τρέχουσα θέση του κινητήρα σε μια νέα τιμή.
Τιμές
είναι_ολοκληρωμένο – Επιστρέφει εάν ο κινητήρας έχει ολοκληρώσει την κίνηση.
είναι_περιστρεφόμενος – Επιστρέφει εάν ο κινητήρας περιστρέφεται.
θέση – Επιστρέφει την τρέχουσα θέση του κινητήρα.
ταχύτητα – Επιστρέφει την ταχύτητα περιστροφής του κινητήρα.
AI Vision – Καταγραφή και ανάλυση αντικειμένων χρησιμοποιώντας τον αισθητήρα AI Vision Sensor IQ.
Αποκτητές
λήψη_στιγμιότυπου – Φιλτράρει το τρέχον καρέ του αισθητήρα σε μια συγκεκριμένη υπογραφή και επιστρέφει μια πλειάδα ανιχνευμένων αντικειμένων.
Σκηνικά θέατρου
.width – Επιστρέφει το πλάτος του ανιχνευμένου αντικειμένου σε pixel.
.height – Επιστρέφει το ύψος του ανιχνευμένου αντικειμένου σε pixel.
.centerX – Επιστρέφει τη συντεταγμένη x του κέντρου του ανιχνευμένου αντικειμένου.
.centerY – Επιστρέφει τη συντεταγμένη y του κέντρου του ανιχνευμένου αντικειμένου.
.originX – Επιστρέφει τη συντεταγμένη x της επάνω αριστερής γωνίας του πλαισίου οριοθέτησης του ανιχνευμένου αντικειμένου.
.originY – Επιστρέφει τη συντεταγμένη y της επάνω αριστερής γωνίας του πλαισίου οριοθέτησης του ανιχνευμένου αντικειμένου.
.id – Επιστρέφει το ID της ανιχνευμένης ταξινόμησης AI.
Τα παραδείγματα σε αυτήν τη σελίδα χρησιμοποιούν την προεπιλεγμένη θέση έναρξης της Παιδικής Χαράς.
Κίνηση#
Flop has two robot-specific motors: intake_motor and arm_motor. Both use standard motor commands.
Σταθερές κατεύθυνσης:
intake_motor:FORWARD= intake (collects);REVERSE= outtake (ejects)arm_motor:FORWARD= up (raises);REVERSE= down (lowers)
γνέθω#
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)
Παράμετροι |
Περιγραφή |
|---|---|
|
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)
περιστροφή_για#
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)
Παράμετροι |
Περιγραφή |
|---|---|
|
The direction to spin: |
|
Η απόσταση περιστροφής. Αυτή μπορεί να είναι ακέραιος ή δεκαδικός αριθμός. |
|
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)
περιστροφή_σε_θέση#
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)
Παράμετροι |
Περιγραφή |
|---|---|
|
Η θέση στην οποία θα γίνει η περιστροφή. Αυτή μπορεί να είναι ακέραιος ή δεκαδικός αριθμός. |
|
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)
στάση#
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)
set_velocity#
set_velocity sets how fast a motor will spin as a percentage from 0 to 100.
Usage:
motor.set_velocity(velocity, unit)
Παράμετροι |
Περιγραφή |
|---|---|
|
Η ταχύτητα του κινητήρα ως ποσοστό από 0 έως 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)
set_timeout#
set_timeout sets how many seconds a motor will try to finish a movement before stopping.
Usage:
motor.set_timeout(timeout, unit)
Παράμετροι |
Περιγραφή |
|---|---|
|
Ο αριθμός των δευτερολέπτων που θα προσπαθήσει ο κινητήρας να ολοκληρώσει μια κίνηση. |
|
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)
ορισμός_θέσης#
set_position changes the motor’s current position to a new value, resetting its encoder.
Usage:
motor.set_position(position, unit)
Παράμετροι |
Περιγραφή |
|---|---|
|
Η νέα τιμή θέσης. |
|
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)
είναι_ολοκληρωμένο#
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)
περιστρέφεται#
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)
θέση#
position returns the motor’s current position.
Usage:
motor.position(unit)
Παράμετροι |
Περιγραφή |
|---|---|
|
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)
ταχύτητα#
velocity returns how fast the motor is currently spinning as a percentage from -100 to 100.
Usage:
motor.velocity(unit)
Παράμετροι |
Περιγραφή |
|---|---|
|
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)
Όραμα Τεχνητής Νοημοσύνης#
λήψη_στιγμιότυπου#
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.
Η πλειάδα αποθηκεύει αντικείμενα ταξινομημένα από το μεγαλύτερο στο μικρότερο κατά πλάτος, ξεκινώντας από τον δείκτη 0. Η πρόσβαση στις ιδιότητες κάθε αντικειμένου είναι δυνατή χρησιμοποιώντας τον δείκτη του. Επιστρέφεται μια κενή πλειάδα εάν δεν εντοπιστούν αντικείμενα που να ταιριάζουν.
Usage:
ai_vision.take_snapshot(signature)
Παράμετροι |
Περιγραφή |
|---|---|
|
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)
Σκηνικά θέατρου#
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.
.πλάτος#
.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)
.ύψος#
.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)
.centerY#
.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)
.originY#
.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)
.ταυτότητα#
.id returns the ID of the detected AI Classification as an integer.
Ταξινόμηση Τεχνητής Νοημοσύνης |
ταυτότητα |
|---|---|
ΜπλεΦασόλια |
0 |
Κόκκινη Τσάντα Φασολιών |
1 |
Κίτρινη Τσάντα Φασολιών |
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)