Σύνδεσμος Μηνύματος#
Εισαγωγή#
Το MessageLink επιτρέπει σε δύο εγκεφάλους EXP να επικοινωνούν στέλνοντας σύντομα μηνύματα συμβολοσειράς με προαιρετικά μικρά δεδομένα. Έχει σχεδιαστεί για συντονισμό ρομπότ με ρομπότ — όπως η έναρξη ρουτινών, η κοινοποίηση αποτελεσμάτων αισθητήρων ή η ενεργοποίηση συμπεριφορών σε ένα άλλο ρομπότ.
Every usage of send adds a message to the linked EXP Brain’s queue, and the queue is read first-in, first-out (FIFO). If multiple messages are sent before the other EXP Brain uses receive, they will be stored and returned one at a time in the order they were sent. Because messages can queue, repeatedly sending the same status every loop may create backlog; for time-critical logic, send only when values change.
Important: Both robots must be running projects that use MessageLink at the same time, or no messages will be sent/received.
This page uses link as the example MessageLink name. Replace it with your own configured name as needed.
Παρακάτω είναι μια λίστα με τις διαθέσιμες μεθόδους:
Δημιουργία συνδέσμου#
Δημιουργήστε ένα MessageLink και στους δύο εγκεφάλους EXP για να δημιουργήσετε ένα κανάλι επικοινωνίας μεταξύ τους —είτε ασύρματα με τα V5 Robot Radios είτε μέσω ενός ενσύρματου έξυπνου καλωδίου— ώστε τα ρομπότ να μπορούν να στέλνουν μηνύματα μεταξύ τους.
MessageLink(smartport, name, linktype, wired)
Παράμετρος |
Περιγραφή |
|---|---|
|
The Smart Port used for this link—the port the V5 Robot Radio is connected to (wireless) or the Smart Cable is plugged into (wired). Written as |
|
Το μοναδικό όνομα που θα δοθεί σε αυτόν τον σύνδεσμο ως συμβολοσειρά. |
|
The role this Brain will use for this link pair. Each pair must include one Manager and one Worker:
|
|
Optional. Whether this link is wired or wireless:
|
Κωδικός για το Ρομπότ 1
""" Create a wireless link in Port 1 name: VEXRoboticsLink123456789 linktype: Manager """ link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER)Κωδικός για το Ρομπότ 2
""" Create a wireless link in Port 1 name: VEXRoboticsLink123456789 linktype: Worker """ link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER)
Διαθέσιμες μέθοδοι#
Μόλις ρυθμιστεί το MessageLink και συνδεθούν οι Brains, μπορείτε να χρησιμοποιήσετε τις παρακάτω μεθόδους για να στέλνετε, να λαμβάνετε και να απαντάτε σε μηνύματα.
is_linked#
is_linked method returns whether the EXP Brains on a MessageLink are paired with one another.
True– The two EXP Brains are paired and communicating on this link.False– The two EXP Brains are not paired on this link.
Σημείωση: Είναι καλή πρακτική να ελέγχετε πάντα για να βεβαιωθείτε ότι οι εγκέφαλοι EXP είναι συνδεδεμένοι στην αρχή ενός έργου πριν από την εκτέλεση οποιουδήποτε περαιτέρω κώδικα.
Usage:
link.is_linked()
Παράμετροι |
Περιγραφή |
|---|---|
Αυτή η μέθοδος δεν έχει παραμέτρους. |
Κωδικός για το Ρομπότ 1
# Tell the other robot when the screen is being pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") # Continuously send if the screen is being pressed while True: if brain.screen.pressing(): link.send("pressed") else: link.send("released") wait(50, MSEC)Κωδικός για το Ρομπότ 2
# Display if the other robot's screen is being pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) # Continuously check if Robot 1 has sent "pressed" while True: brain.screen.clear_screen() brain.screen.set_cursor(1, 1) brain.screen.print("Robot 2 - Worker") brain.screen.next_row() if link.receive() == "pressed": brain.screen.new_line() brain.screen.print("Manager is being pressed!") wait(50, MSEC)
send#
send sends a string and/or an integer and/or float to the other linked EXP Brain.
Usage:
send(string, integer, float)
Παράμετροι |
Περιγραφή |
|---|---|
|
Η συμβολοσειρά που θα σταλεί στον άλλο συνδεδεμένο εγκέφαλο EXP. |
|
Προαιρετικό. Ένας ακέραιος αριθμός για αποστολή στον άλλο συνδεδεμένο εγκέφαλο EXP. |
|
Προαιρετικό. Ένας αριθμός κινητής υποδιαστολής για αποστολή στον άλλο συνδεδεμένο εγκέφαλο EXP. Οι αριθμοί κινητής υποδιαστολής μορφοποιούνται σε 4 δεκαδικά ψηφία. Εάν παρέχονται λιγότερα δεκαδικά ψηφία, προστίθενται μηδενικά στο τέλος μέχρι να εμφανιστούν 4. |
If only string is sent, the other linked EXP Brain receives that exact string.
If integer and/or float are included, the receiving Brain gets a single encoded string that combines all fields in this order: .string_integer_float
Example: Using link.send(“string”, 1, 2.55) is received as .string_1_2.5500.
Κωδικός για το Ρομπότ 1
# Tell the other robot when the screen is being pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") # Continuously send if the screen is being pressed while True: if brain.screen.pressing(): link.send("pressed") else: link.send("released") wait(50, MSEC)Κωδικός για το Ρομπότ 2
# Display if the other robot's screen is being pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) # Continuously check if Robot 1 has sent "pressed" while True: brain.screen.clear_screen() brain.screen.set_cursor(1, 1) brain.screen.print("Robot 2 - Worker") brain.screen.next_row() if link.receive() == "pressed": brain.screen.new_line() brain.screen.print("Manager is being pressed!") wait(50, MSEC)
Κωδικός για το Ρομπότ 1
# Tell the other robot where the screen was pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") brain.screen.next_row() brain.screen.print("Tap to send circles") # Send the coordinates whenever the screen is pressed while True: if brain.screen.pressing(): x = brain.screen.x_position() y = brain.screen.y_position() link.send("touch", x, y) wait(10, MSEC)Κωδικός για το Ρομπότ 2
# Draw a circle where Robot 1's screen was pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 2 - Worker") while True: # Get the full encoded string circle_coordinates = link.receive(200) if circle_coordinates: # Split the encoded string by _ parts = circle_coordinates.split("_") if len(parts) == 3: # Convert the coordinates back into numbers x = float(parts[1]) y = float(parts[2]) brain.screen.set_fill_color(Color.WHITE) brain.screen.draw_circle(x, y, 20) wait(10, MSEC)
receive#
receive returns the next queued message from the other linked EXP Brain. Messages are read in FIFO order (oldest unread first). If the queue is empty when receive is called, it waits up to the specified timeout for a new message. If no message arrives in that window, receive returns None, and any message sent afterward remains in the queue to be read the next time receive is used.
If the other linked EXP Brain sent only a string, receive returns that exact string.
If the other linked EXP Brain sent an integer and/or float, receive returns a single encoded string that combines all fields in this order: .string_integer_float
For example, if the other linked EXP Brain uses link.send(“string”, 1, 2.55), then using link.receive() returns an encoded string such as .string_1_2.5500. You can split this encoded string (see the examples below) to extract the message name, integer, and float values.
If you’d rather avoid manual splitting/parsing, use received to register a handler for a specific message name; the handler receives the sent values as arguments.
Usage:
receive(timeout)
Παράμετροι |
Περιγραφή |
|---|---|
|
Optional. How long in milliseconds |
Κωδικός για το Ρομπότ 1
# Tell the other robot when the screen is being pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") # Continuously send if the screen is being pressed while True: if brain.screen.pressing(): link.send("pressed") else: link.send("released") wait(50, MSEC)Κωδικός για το Ρομπότ 2
# Display if the other robot's screen is being pressed # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) # Continuously check if Robot 1 has sent "pressed" while True: brain.screen.clear_screen() brain.screen.set_cursor(1, 1) brain.screen.print("Robot 2 - Worker") brain.screen.next_row() if link.receive() == "pressed": brain.screen.new_line() brain.screen.print("Manager is being pressed!") wait(50, MSEC)
Κωδικός για το Ρομπότ 1
# Send a math problem for the other Brain to solve # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") """ Change whether to "add" or "subtract", then what numbers to use in the operation. """ link.send("add", 7, 2.5)Κωδικός για το Ρομπότ 2
# Solve the math problem that the other Brain sends # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 2 - Worker") brain.screen.next_row() math_problem = link.receive() if math_problem: # Split the encoded string by _ parts = math_problem.split("_") if len(parts) == 3: # Remove the . from the beginning of the string operator = "".join([c for c in parts[0] if c.isalpha()]) # Separate out the numbers for the operation a = int(parts[1]) b = float(parts[2]) # Use what operator was sent if operator == "add": result = a + b elif operator == "subtract": result = a - b # If neither add or subtract is sent, do nothing else: pass brain.screen.print("Result = ", result)
received#
received registers a function to be called whenever the EXP Brain receives a sent message.
Usage:
link.received(message, arg)
Παράμετροι |
Περιγραφή |
|---|---|
|
The message name (string) to match. When a received message’s name matches this value, the |
|
A previously defined function that runs when the EXP Brain receives a message matching |
received callbacks are called with four arguments:
Callback Signature:
callback(message, linkname, index, value)
Επιχείρημα |
Περιγραφή |
|---|---|
|
The message name that was received (for example, |
|
Το όνομα του συνδέσμου από τον οποίο ελήφθη το μήνυμα. |
|
Η ακέραια τιμή που αποστέλλεται με το μήνυμα (εάν παρέχεται). |
|
Η τιμή float που αποστέλλεται με το μήνυμα (εάν παρέχεται). |
Κωδικός για το Ρομπότ 1
# Send a math problem for the other Brain to solve # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") """ Change whether to "add" or "subtract", then what numbers to use in the operation. """ link.send("add", 7, 2.5)Κωδικός για το Ρομπότ 2
# Solve the math problem that the other Brain sends # Create the link link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 2 - Worker") brain.screen.next_row() # add if "add" is received def add_cmd(message, linkname, a, b): result = a + b brain.screen.print("Result = ", result) # subtract if ""subtract" is received def subtract_cmd(message, linkname, a, b): result = a - b brain.screen.print("Result = ", result) # Create event handlers for "add" and "subtract" link.received("add", add_cmd) link.received("subtract", subtract_cmd)