Ziņojuma saite#
Ievads#
MessageLink ļauj diviem V5 Brains sazināties, nosūtot īsas virknes ziņojumus ar papildu nelieliem datiem. Tas ir paredzēts robotu savstarpējai koordinācijai — piemēram, rutīnu palaišanai, sensoru rezultātu koplietošanai vai uzvedības aktivizēšanai citā robotā.
Katru reizi, kad tiek izmantota send, saistītās V5 smadzeņu rindai tiek pievienots ziņojums, un rinda tiek lasīta pēc principa “pirmais iekšā, pirmais ārā” (FIFO). Ja pirms otras V5 smadzeņu sistēmas tiek izmantota receive, tiek nosūtīti vairāki ziņojumi, tie tiks saglabāti un atgriezti pa vienam tādā secībā, kādā tie tika nosūtīti. Tā kā ziņojumi var tikt ievietoti rindā, atkārtota viena un tā paša statusa nosūtīšana katrā ciklā var radīt uzkrāšanos; laika ziņā kritiskas loģikas gadījumā sūtiet tikai tad, kad mainās vērtības.
Svarīgi: Abiem robotiem vienlaikus jādarbojas ar projektiem, kas izmanto MessageLink , pretējā gadījumā ziņojumi netiks nosūtīti/saņemti.
Šajā lapā kā piemēra MessageLink nosaukums tiek izmantots link Ja nepieciešams, nomainiet to ar savu konfigurēto nosaukumu.
Zemāk ir pieejamo metožu saraksts:
Izveidot saiti#
Izveidojiet MessageLink abās V5 smadzenēs, lai izveidotu saziņas kanālu starp tām — vai nu bezvadu režīmā, izmantojot V5 robotu radio, vai izmantojot vadu viedkabeli —, lai roboti varētu sūtīt ziņojumus viens otram.
MessageLink(smartport, name, linktype, wired)
Parametrs |
Apraksts |
|---|---|
|
Šai saitei izmantotā viedā pieslēgvieta — pieslēgvieta, kurai ir pievienots V5 robota radio (bezvadu savienojums) vai kurai ir pievienots viedais kabelis (vadu savienojums). Rakstīts kā |
|
Unikālais nosaukums, kas šai saitei jāpiešķir kā virkne. |
|
Loma, ko šīs smadzenes izmantos šim saišu pārim. Katrā pārī jāiekļauj viens vadītājs un viens darbinieks:
|
|
Pēc izvēles. Vai šī saite ir vadu vai bezvadu:
|
1. robota kods
""" Create a wireless link in Port 1 name: VEXRoboticsLink123456789 linktype: Manager """ link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER)Robots 2 kods
""" Create a wireless link in Port 1 name: VEXRoboticsLink123456789 linktype: Worker """ link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER)
Pieejamās metodes#
Kad jūsu MessageLink ir iestatīts un smadzenes ir savienotas, varat izmantot tālāk norādītās metodes, lai sūtītu, saņemtu un atbildētu uz ziņojumiem.
is_linked#
is_linked metode atgriež, vai V5 smadzenes MessageLink saitē ir savienotas pārī viena ar otru.
True– Abas V5 smadzenes ir savienotas pārī un sazinās, izmantojot šo saiti.False– Abas V5 smadzenes šajā saitē nav savienotas pārī.
Piezīme: Pirms jebkāda turpmāka koda palaišanas projekta sākumā vienmēr ir jāpārbauda, vai V5 smadzenes ir saistītas.
Usage:
link.is_linked()
Parametri |
Apraksts |
|---|---|
Šai metodei nav parametru. |
1. robota kods
# 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)Robots 2 kods
# 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 nosūta virkni un/vai veselu skaitli un/vai peldošo skaitli uz otru saistīto V5 Brain.
Usage:
send(string, integer, float)
Parametri |
Apraksts |
|---|---|
|
Virkne, kas jānosūta uz otru saistīto V5 Brain. |
|
Neobligāti. Vesels skaitlis, kas jānosūta otrai saistītajai V5 smadzenei. |
|
Neobligāti. Peldskaitlis, kas jānosūta uz otru saistīto V5 Brain. Peldskaitļi tiek formatēti ar 4 zīmēm aiz komata; ja norādīts mazāk zīmju aiz komata, tiek pievienotas nulles, līdz tiek parādīti 4. |
Ja tiek nosūtīta tikai string , otra saistītā V5 Brain saņem tieši šo virkni.
Ja ir iekļauts integer un/vai float , saņēmēja smadzenes saņem vienu kodētu virkni, kas apvieno visus laukus šādā secībā: .string_integer_float
Piemērs: Izmantojot link.send(“string”, 1, 2.55) tiek saņemts kā .string_1_2.5500.
1. robota kods
# 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)Robots 2 kods
# 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. robota kods
# 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)Robots 2 kods
# 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 atgriež nākamo rindā esošo ziņojumu no otras saistītās V5 Brain. Ziņojumi tiek lasīti FIFO secībā (vispirms vecākie nelasītie). Ja rinda ir tukša, kad tiek izsaukta receive , tā gaida jaunu ziņojumu līdz norādītajam timeout Ja šajā logā netiek saņemts neviens ziņojums, receive atgriež None, un visi pēc tam nosūtītie ziņojumi paliek rindā, lai tos nolasītu nākamajā reizē, kad tiks izmantota receive.
Ja otra saistītā V5 Brain nosūtīja tikai string, receive atgriež precīzu virkni.
Ja otra saistītā V5 Brain nosūtīja veselu skaitli un/vai peldošo skaitli, receive atgriež vienu kodētu virkni, kas apvieno visus laukus šādā secībā: .string_integer_float
Piemēram, ja otra saistītā V5 Brain izmanto link.send(“string”, 1, 2.55), tad, izmantojot link.receive() tiek atgriezta kodēta virkne, piemēram, .string_1_2.5500Šo kodēto virkni var sadalīt (skatiet tālāk sniegtos piemērus), lai iegūtu ziņojuma nosaukumu, veselu skaitli un peldošās vērtības.
Ja vēlaties izvairīties no manuālas sadalīšanas/parsēšanas, izmantojiet received, lai reģistrētu apstrādātāju konkrētam ziņojuma nosaukumam; apstrādātājs saņem nosūtītās vērtības kā argumentus.
Usage:
receive(timeout)
Parametri |
Apraksts |
|---|---|
|
Neobligāti. Cik ilgi milisekundēs |
1. robota kods
# 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)Robots 2 kods
# 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. robota kods
# 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)Robots 2 kods
# 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 reģistrē funkciju, kas tiks izsaukta ikreiz, kad V5 Brain saņem nosūtītu ziņojumu.
Usage:
link.received(message, arg)
Parametri |
Apraksts |
|---|---|
|
Ziņojuma nosaukums (virkne), kam jāatrod atbilstība. Ja saņemtā ziņojuma nosaukums atbilst šai vērtībai, tiek izsaukta atzvanīšanas funkcija |
|
Iepriekš definēta funkcija, kas darbojas, kad V5 smadzenes saņem ziņojumu, kas atbilst |
received atzvanīšanas signāli tiek izsaukti ar četriem argumentiem:
Atzvanīšanas paraksts:
callback(message, linkname, index, value)
Arguments |
Apraksts |
|---|---|
|
Saņemtā ziņojuma nosaukums (piemēram, |
|
Saites nosaukums, no kuras tika saņemts ziņojums. |
|
Vesela skaitļa vērtība, kas nosūtīta kopā ar ziņojumu (ja tāda ir norādīta). |
|
Ziņojumā nosūtītā peldošā vērtība (ja tāda ir norādīta). |
1. robota kods
# 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)Robots 2 kods
# 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)