Mesaj Bağlantısı#
giriiş#
MessageLink, iki EXP Brain’in isteğe bağlı küçük verilerle birlikte kısa dize mesajları göndererek iletişim kurmasını sağlar. Robotlar arası koordinasyon için tasarlanmıştır; örneğin rutinleri başlatmak, sensör sonuçlarını paylaşmak veya başka bir robottaki davranışları tetiklemek gibi.
send’nin her kullanımı, bağlantılı EXP Beyninin kuyruğuna bir mesaj ekler ve kuyruk ilk giren ilk çıkar (FIFO) prensibiyle okunur. Diğer EXP Beyni receive’i kullanmadan önce birden fazla mesaj gönderilirse, bunlar depolanır ve gönderildikleri sırayla tek tek geri döndürülür. Mesajlar kuyruğa alınabildiğinden, her döngüde aynı durumu tekrar tekrar göndermek birikmeye neden olabilir; zaman açısından kritik mantık için, yalnızca değerler değiştiğinde gönderin.
Önemli: Her iki robot da aynı anda MessageLink kullanan projeleri çalıştırıyor olmalıdır, aksi takdirde hiçbir mesaj gönderilmez/alınmaz.
Bu sayfada örnek olarak link MessageLink adı kullanılmaktadır. Gerektiğinde kendi yapılandırdığınız adla değiştirin.
Aşağıda mevcut yöntemlerin listesi bulunmaktadır:
Bağlantı Oluştur#
Her iki EXP Beyninde de bir MesajBağlantısı oluşturarak, robotlar birbirlerine mesaj gönderebilsinler diye, V5 Robot Radyoları ile kablosuz olarak veya kablolu Akıllı Kablo aracılığıyla aralarında bir iletişim kanalı kurun.
MessageLink(smartport, name, linktype, wired)
Parametre |
Tanım |
|---|---|
|
Bu bağlantı için kullanılan Akıllı Bağlantı Noktası; V5 Robot Radyosunun bağlı olduğu (kablosuz) veya Akıllı Kablonun takıldığı (kablolu) bağlantı noktasıdır. |
|
Bu bağlantıya verilecek benzersiz ad. |
|
Bu Beyin, bu bağlantı çifti için kullanacağı rolü belirtir. Her çiftte bir Yönetici ve bir İşçi bulunmalıdır:
|
|
İsteğe bağlı. Bu bağlantının kablolu mu yoksa kablosuz mu olduğu:
|
Robot 1 için Kod
""" Create a wireless link in Port 1 name: VEXRoboticsLink123456789 linktype: Manager """ link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER)Robot 2 için Kod
""" Create a wireless link in Port 1 name: VEXRoboticsLink123456789 linktype: Worker """ link = MessageLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER)
Mevcut Yöntemler#
MessageLink’iniz kurulduktan ve Brain’ler birbirine bağlandıktan sonra, mesaj göndermek, almak ve yanıtlamak için aşağıdaki yöntemleri kullanabilirsiniz.
is_linked#
is_linked yöntemi, bir MessageLink üzerindeki EXP Beyinlerinin birbiriyle eşleştirilip eşleştirilmediğini döndürür.
True– İki EXP Beyni eşleştirilmiş ve bu bağlantı üzerinden iletişim kuruyor.False– Bu bağlantıda iki EXP Beyni eşleştirilmemiş.
Not: Herhangi bir kod çalıştırmadan önce projenin başında EXP Brains’in bağlantılı olduğundan emin olmak her zaman iyi bir uygulamadır.
Usage:
link.is_linked()
Parametreler |
Tanım |
|---|---|
Bu yöntemin parametresi yoktur. |
Robot 1 için Kod
# 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)Robot 2 için Kod
# 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 , bağlantılı diğer EXP Beynine bir metin ve/veya bir tamsayı ve/veya ondalık sayı gönderir.
Usage:
send(string, integer, float)
Parametreler |
Tanım |
|---|---|
|
Diğer bağlantılı EXP Beynine gönderilecek metin. |
|
İsteğe bağlı. Bağlantılı diğer EXP Beynine gönderilecek bir tamsayı. |
|
İsteğe bağlı. Diğer bağlantılı EXP Beynine gönderilecek ondalık sayı. Ondalık sayılar 4 ondalık basamağa kadar biçimlendirilir; daha az ondalık basamak verilirse, 4 gösterilene kadar sonuna sıfırlar eklenir. |
Yalnızca string gönderilirse, bağlantılı diğer EXP Beyni tam olarak bu dizeyi alır.
Eğer integer ve/veya float dahil edilirse, alıcı Beyin tüm alanları şu sırayla birleştiren tek bir kodlanmış dize alır: .string_integer_float
Örnek: link.send(“string”, 1, 2.55) .string_1_2.5500 olarak alınır.
Robot 1 için Kod
# 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)Robot 2 için Kod
# 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)
Robot 1 için Kod
# 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)Robot 2 için Kod
# 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 diğer bağlantılı EXP Beyninden kuyruğa alınmış bir sonraki mesajı döndürür. Mesajlar FIFO sırasına göre (en eski okunmamış olan önce) okunur. receive çağrıldığında kuyruk boşsa, belirtilen timeout kadar yeni bir mesaj bekler. Bu süre içinde hiçbir mesaj gelmezse, receive Nonedöndürür ve daha sonra gönderilen herhangi bir mesaj, bir sonraki receive kullanıldığında okunmak üzere kuyrukta kalır.
Eğer diğer bağlantılı EXP Brain yalnızca stringve receive.
Diğer bağlantılı EXP Brain bir tamsayı ve/veya ondalık sayı gönderdiyse, receive alanları şu sırayla birleştiren tek bir kodlanmış dize döndürür: .string_integer_float
Örneğin, diğer bağlantılı EXP Brain link.send(“string”, 1, 2.55)kullanıyorsa, link.receive() kullanmak .string_1_2.5500gibi kodlanmış bir dize döndürür. Bu kodlanmış dizeyi (aşağıdaki örneklere bakın) bölerek mesaj adını, tamsayı ve ondalık sayı değerlerini çıkarabilirsiniz.
Eğer manuel bölme/ayrıştırmadan kaçınmayı tercih ederseniz, belirli bir mesaj adı için bir işleyici kaydetmek üzere received kullanın; işleyici gönderilen değerleri argüman olarak alır.
Usage:
receive(timeout)
Parametreler |
Tanım |
|---|---|
|
İsteğe bağlı. Milisaniye cinsinden süre: |
Robot 1 için Kod
# 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)Robot 2 için Kod
# 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)
Robot 1 için Kod
# 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)Robot 2 için Kod
# 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 kaydedildi; bu, EXP Beyni gönderilen bir mesaj aldığında çağrılacak bir fonksiyondur.
Usage:
link.received(message, arg)
Parametreler |
Tanım |
|---|---|
|
Eşleştirilecek mesaj adı (dize). Alınan mesajın adı bu değerle eşleştiğinde, |
|
EXP Beyni |
received geri çağırma işlemi dört argümanla çağrılıyor:
Geri Çağrı İmzası:
callback(message, linkname, index, value)
Argüman |
Tanım |
|---|---|
|
Alınan mesajın adı (örneğin, |
|
Mesajın alındığı bağlantının adı. |
|
Mesajla birlikte gönderilen tamsayı değeri (eğer belirtilmişse). |
|
Mesajla birlikte gönderilen ondalık sayı değeri (eğer belirtilmişse). |
Robot 1 için Kod
# 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)Robot 2 için Kod
# 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)