Žinutės nuoroda#
Įvadas#
„MessageLink“ leidžia dviem „V5 Brains“ bendrauti siunčiant trumpus pranešimus su pasirinktinai smulkiais duomenimis. Jis skirtas robotų tarpusavio koordinavimui – pavyzdžiui, paleisti rutinas, dalytis jutiklių rezultatais arba suaktyvinti kito roboto elgesį.
Kiekvieną kartą panaudojus send, prie susietų V5 smegenų eilės pridedamas pranešimas, o eilė skaitoma FIFO principu. Jei prieš tai, kai kita V5 smegenų sistema panaudoja receive, išsiunčiami keli pranešimai, jie bus saugomi ir grąžinami po vieną tokia tvarka, kokia buvo išsiųsti. Kadangi pranešimai gali būti eilėje, pakartotinis tos pačios būsenos siuntimas kiekviename cikle gali sukelti vėlavimą; laiko požiūriu kritinei logikai siųskite tik tada, kai pasikeičia reikšmės.
Svarbu: Abu robotai vienu metu turi vykdyti projektus, kurie naudoja MessageLink , kitaip pranešimai nebus siunčiami / gaunami.
Šiame puslapyje kaip pavyzdyje MessageLink “ naudojamas link . Jei reikia, pakeiskite jį savo sukonfigūruotu pavadinimu.
Žemiau pateikiamas galimų metodų sąrašas:
Sukurti nuorodą#
Sukurkite „MessageLink“ abiejuose „V5 Brain“ įrenginiuose, kad užmegztumėte ryšio kanalą tarp jų – belaidžiu būdu naudojant „V5 Robot Radios“ arba laidiniu „Smart Cable“ – kad robotai galėtų siųsti vienas kitam žinutes.
MessageLink(smartport, name, linktype, wired)
Parametras |
Aprašymas |
|---|---|
|
Šiam ryšiui naudojamas išmanusis prievadas – prievadas, prie kurio prijungtas V5 roboto radijas (belaidis ryšys) arba išmanusis kabelis (laidinis ryšys). Parašytas taip: |
|
Unikalus šios nuorodos pavadinimas eilutės forma. |
|
Rolė, kurią šios smegenys naudos šiai ryšių porai. Kiekvienoje poroje turi būti vienas vadovas ir vienas darbuotojas:
|
|
Pasirinktinai. Ar ši jungtis laidinė, ar belaidė:
|
1 roboto kodas
""" Create a wireless link in Port 1 name: VEXRoboticsLink123456789 linktype: Manager """ link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER)Roboto 2 kodas
""" Create a wireless link in Port 1 name: VEXRoboticsLink123456789 linktype: Worker """ link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER)
Galimi metodai#
Kai jūsų „MessageLink“ bus nustatytas ir smegenys susietos, galėsite naudoti toliau nurodytus metodus žinutėms siųsti, gauti ir į jas atsakyti.
is_linked#
is_linked metodas grąžina, ar V5 smegenys „MessageLink“ tinkle yra suporuotos viena su kita.
True– Du V5 smegenys yra suporuotos ir bendrauja šiuo ryšiu.False– Šioje jungtyje dvi V5 smegenys nėra suporuotos.
Pastaba: Prieš paleidžiant bet kokį tolesnį kodą, projekto pradžioje visada rekomenduojama patikrinti, ar V5 smegenys yra susietos.
Usage:
link.is_linked()
Parametrai |
Aprašymas |
|---|---|
Šis metodas neturi parametrų. |
1 roboto kodas
# 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)Roboto 2 kodas
# 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 siunčia eilutę ir (arba) sveikąjį skaičių ir (arba) slankiojo skaičiaus reikšmę į kitą susietą V5 smegenų sistemą.
Usage:
send(string, integer, float)
Parametrai |
Aprašymas |
|---|---|
|
Eilutė, kurią reikia siųsti į kitą susietą V5 smegenų dalį. |
|
Pasirinktinai. Sveikasis skaičius, kurį reikia siųsti į kitą susietą V5 smegenų branduolį. |
|
Pasirinktinai. Slankiojo skaičiaus skaičius, siunčiamas į kitą susietą V5 smegenų sistemą. Slankiojo skaičiaus skaičiai suformatuoti iki 4 skaitmenų po kablelio; jei nurodyta mažiau skaitmenų po kablelio, pridedami nuliai, kol rodoma 4. |
Jei siunčiama tik string , kita susieta V5 smegenys gauna tikslią eilutę.
Jei įtraukiami integer ir (arba) float , gaunantis smegenų tinklas gauna vieną užkoduotą eilutę, kurioje visi laukai sujungiami tokia tvarka: .string_integer_float
Pavyzdys: Naudojant link.send(“string”, 1, 2.55) gaunamas kaip .string_1_2.5500.
1 roboto kodas
# 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)Roboto 2 kodas
# 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 roboto kodas
# 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)Roboto 2 kodas
# 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 V5 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.
Jei kitas susietas V5 Brain išsiuntė tik string, receive grąžina tikslią eilutę.
Jei kitas susietas V5 Brain išsiuntė sveikąjį ir (arba) slankiojo skaičiaus reikšmę, receive grąžina vieną užkoduotą eilutę, kurioje visi laukai sujungiami tokia tvarka: .string_integer_float
Pavyzdžiui, jei kitas susietas V5 Brain naudoja link.send(“string”, 1, 2.55), tada naudojant link.receive() grąžina užkoduotą eilutę, pvz. .string_1_2.5500Šią užkoduotą eilutę galite padalinti (žr. pavyzdžius toliau), kad išgautumėte pranešimo pavadinimą, sveikąjį skaičių ir slankiojo skaičiaus reikšmes.
Jei norite išvengti rankinio skaidymo / analizavimo, naudokite received, kad užregistruotumėte konkretaus pranešimo pavadinimo tvarkyklę; tvarkyklė gauna išsiųstas reikšmes kaip argumentus.
Usage:
receive(timeout)
Parametrai |
Aprašymas |
|---|---|
|
Pasirinktinai. Kiek milisekundėmis |
1 roboto kodas
# 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)Roboto 2 kodas
# 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 roboto kodas
# 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)Roboto 2 kodas
# 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 užregistruoja funkciją, kuri bus iškviečiama kaskart, kai V5 Brain gaus išsiųstą pranešimą.
Usage:
link.received(message, arg)
Parametrai |
Aprašymas |
|---|---|
|
Atitinkamas pranešimo pavadinimas (eilutė). Kai gauto pranešimo pavadinimas atitinka šią reikšmę, iškviečiama atgalinio iškvietimo |
|
Anksčiau apibrėžta function, kuri paleidžiama, kai V5 smegenys gauna pranešimą, atitinkantį |
received atgalinių iškvietimų iškviečiami su keturiais argumentais:
Atgalinio iškvietimo parašas:
callback(message, linkname, index, value)
Argumentas |
Aprašymas |
|---|---|
|
Gautos žinutės pavadinimas (pavyzdžiui, |
|
Nuorodos, iš kurios buvo gautas pranešimas, pavadinimas. |
|
Sveikasis skaičius, išsiųstas su pranešimu (jei nurodyta). |
|
Su pranešimu siunčiama slankiojo skaičiaus reikšmė (jei nurodyta). |
1 roboto kodas
# 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)Roboto 2 kodas
# 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)