訊息連結#
介紹#
MessageLink 允許兩個 EXP Brain 透過發送帶有可選少量資料的短字串訊息進行通訊。它專為機器人之間的協調而設計,例如啟動程式、共享感測器結果或觸發另一個機器人的行為。
每次使用 send 都會在關聯的 EXP Brain 的佇列中加入一則訊息,佇列採用先進先出 (FIFO) 的讀取方式。如果在另一個 EXP Brain 使用 receive 之前發送了多個訊息,這些訊息將被存儲,並按發送順序逐條返回。由於訊息可以排隊,因此在每次循環中重複發送相同的狀態可能會導致積壓;對於時間關鍵型邏輯,僅當值發生變化時才發送訊息。
重要提示:兩個機器人必須同時執行使用 MessageLink 項目,否則將不會發送/接收任何訊息。
本頁面使用 link 作為範例連結 MessageLink 名稱。請根據需要將其替換為您自己配置的名稱。
以下是可用方法清單:
建立連結#
在兩個 EXP 大腦上創建 MessageLink,以在它們之間建立通訊通道——可以透過 V5 機器人無線電進行無線連接,也可以透過有線智慧電纜進行連接——以便機器人可以互相發送訊息。
MessageLink(smartport, name, linktype, wired)
範圍 |
描述 |
|---|---|
|
此連接使用的智慧型連接埠-V5 機器人無線電連接的連接埠(無線)或智慧電纜插入的連接埠(有線)。格式為 |
|
此連結的唯一名稱(字串形式) |
|
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 方法傳回 MessageLink 上的 EXP Brains 是否彼此配對。
True– 兩個 EXP 大腦已配對並透過此連結進行通訊。False– 此連結中的兩個 EXP 大腦並未配對。
注意:在專案開始時,運行任何其他程式碼之前,最好始終檢查以確保 EXP Brains 已連接。
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 向另一個連接的 EXP Brain 發送字串和/或整數和/或浮點數。
Usage:
send(string, integer, float)
參數 |
描述 |
|---|---|
|
要傳送給另一個已連接的EXP Brain的字串。 |
|
可選。要傳送給另一個已連接的經驗大腦的整數。 |
|
可選。要傳送給另一個已連接的 EXP Brain 的浮點數。浮點數預設顯示 4 位小數;如果提供的位數少於 4 位,則會在末尾添加零,直到顯示 4 位小數為止。 |
如果只傳送字串 string ,則另一個連接的 EXP Brain 將收到該字串。
如果包含 integer 和/或 float ,則接收端 Brain 將獲得一個按以下順序組合所有欄位的單一編碼字串: .string_integer_float
範例: 使用 link.send(“string”, 1, 2.55) 被接收為 .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 傳回來自另一個已連線的 EXP Brain 的下一封已排隊的訊息。訊息以先進先出 (FIFO) 順序讀取(最舊的未讀訊息優先)。如果在呼叫 receive 時隊列為空,則會等待最多 receive 時間以接收新訊息。如果在該時間內沒有訊息到達, receive 返回 None,之後發送的任何訊息都將保留在佇列中,以便在下次使用 timeout 時讀取。
如果另一個連結的 EXP Brain 只發送了字串 string receive 回傳該字串。
如果另一個關聯的 EXP Brain 發送的是整數和/或浮點數, receive 傳回編碼字串,該字串會依以下順序組合所有欄位: .string_integer_float
例如,如果另一個連結的 EXP Brain 使用 link.send(“string”, 1, 2.55),那麼使用 link.receive() 將傳回一個編碼字串,例如 .string_1_2.5500您可以拆分此編碼字串(請參閱下面的範例)以提取訊息名稱、整數和浮點值。
如果您不想手動分割/解析,請使用 received 為特定訊息名稱註冊處理程序;處理程序接收傳送的值作為參數。
Usage:
receive(timeout)
參數 |
描述 |
|---|---|
|
可選。等待新訊息的毫秒數 |
機器人 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 註冊一個函數,每當 EXP Brain 收到發送的訊息時,函數將會被呼叫。
Usage:
link.received(message, arg)
參數 |
描述 |
|---|---|
|
The message name (string) to match. When a received message’s name matches this value, the |
|
先前定義的 函數 在 EXP Brain 收到 與訊息 |
received 回呼被調用,並帶有四個參數:
回呼函數簽章:
callback(message, linkname, index, value)
爭論 |
描述 |
|---|---|
|
收到的訊息名稱(例如, |
|
收到該訊息的連結名稱。 |
|
訊息中發送的整數值(如果提供)。 |
|
訊息中發送的浮點值(如果提供)。 |
機器人 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)