訊息#

介紹#

兩台 VEX AIM 編程機器人可以連接起來,以便在專案進行過程中進行通訊。訊息傳遞方式允許一台機器人向另一台機器人發送訊息。訊息可以包含純文本,也可以包含最多三個數字的文字。

接收到的訊息會儲存在先進先出(FIFO)佇列中。這意味著最先接收到的訊息將首先被讀取。

使用 is_message_available 檢查訊息是否可讀。然後使用 get_message 讀取下一則文字訊息。如果訊息中還包含數字,則使用 get_message_and_data 同時讀取文字和數字。

注意:要發送和接收訊息,兩個機器人必須已連接,並且兩個機器人必須運行使用訊息方法的專案。

以下是所有方法的清單:

操作——在机器人之间发送消息。

  • send_message — 傳送文字和最多三個數字給關聯的機器人。

取得器 — 讀取訊息或檢查連線狀態。

回調函數-當訊息事件發生時運行函數。

  • handle_message — 當收到特定訊息時執行函數。

  • connected — 當機器人與另一個機器人連接時執行函數。

  • disconnected — 當機器人不再與其他機器人連接時運行函數。

行動#

send_message#

send_message 方法會向已連接的機器人發送文字和最多三個數字。當一個機器人需要接收命令、訊號或簡短訊息時,可以在另一個機器人上使用此方法。如果訊息本身足夠清晰,則只需發送文字即可。當訊息需要相關值時,例如發送包含機器人當前航向值的 “heading” 則需要新增數字。

在接收機器人上,使用 is_message_available 檢查是否有訊息到達。然後使用 get_message 取得純文字訊息,或使用 get_message_and_data 取得包含數字的訊息。

用法:
robot.link.send_message(message, arg1, arg2, arg3)

範圍

描述

message

要傳送給關聯機器人的文字。

arg1

(可選)要隨訊息一起發送的數字。

arg2

(可選)要隨訊息一起發送的第二個號碼。

arg3

可選。要隨訊息一起發送的第三個號碼。

例子

機器人 1的程式碼

# Press the screen to tell Robot 2 which way to turn
robot.screen.print("Robot 1")

while not robot.screen.pressing():
    wait(5, MSEC)

robot.link.send_message("right")

機器人 2的程式碼

# Turn in the direction that Robot 1 sends
robot.screen.print("Robot 2")

while not robot.link.is_message_available():
    wait(5, MSEC)

direction = robot.link.get_message()

if direction == "left":
    robot.turn_for(LEFT, 90)
elif direction == "right":
    robot.turn_for(RIGHT, 90)

# Robot 1: continuously send the current heading to Robot 2
robot.screen.print("Robot 1")

while True:
    robot.link.send_message("heading", robot.inertial.get_heading())
    wait(50, MSEC)

# Robot 2: turn to match Robot 1's heading
robot.screen.print("Robot 2")

while True:
    if robot.link.is_message_available():
        message, heading = robot.link.get_message_and_data()
        robot.turn_to(heading)

    wait(50, MSEC)

獲取器#

get_message#

get_message 返回消息队列中下一条收到的文本消息。当已连接的机器人发送的是纯文本消息时,在接收方机器人上使用此方法。常见的使用顺序是:先用 is_message_available 检查消息是否已到达,然后再使用 get_message,之后可以显示、比较返回的文本,或将其用于其他方法。

在另一個機器人上使用 send_message 發送純文字訊息。

用法:
robot.link.get_message(timeout)

範圍

描述

timeout

可選。等待訊息的最長時間(以毫秒為單位),超過此時間後繼續執行。預設值為 1000。

例子

機器人 1的程式碼

# Press the screen to send a message to Robot 2
robot.screen.print("Robot 1")

while not robot.screen.pressing():
    wait(5, MSEC)

robot.link.send_message("Hello, Robot 2!")

機器人 2的程式碼

# Display the message sent by Robot 1
robot.screen.print("Robot 2")
robot.screen.next_row()

while not robot.link.is_message_available():
    wait(5, MSEC)

message = robot.link.get_message()
robot.screen.print(message)

get_message_and_data#

get_message_and_data 以元组形式从消息队列中返回下一条收到的文本消息及数字。当已连接的机器人发送了带有一个或多个数字的文本时,在接收方机器人上使用此方法。通常的顺序是,先使用 is_message_available 检查消息是否已到达,然后再使用 get_message_and_data,之后可以使用返回的文本和数字。

在另一個機器人上使用 send_message 將文字和數字一起發送。

用法:
robot.link.get_message_and_data(timeout)

範圍

描述

timeout

可選。等待訊息的最長時間(以毫秒為單位),超過此時間後繼續執行。預設值為 1000。

例子

機器人 1的程式碼

# Continuously send the current heading to Robot 2
robot.screen.print("Robot 1")

while True:
    robot.link.send_message("heading", robot.inertial.get_heading())
    wait(50, MSEC)

機器人 2的程式碼

# Turn to match Robot 1's heading
robot.screen.print("Robot 2")

while True:
    if robot.link.is_message_available():
        message, heading = robot.link.get_message_and_data()
        robot.turn_to(heading)

    wait(50, MSEC)

is_connected#

is_connected 傳回機器人是否已與其他機器人連線。如果專案需要檢查機器人之間的連接是否已就緒,請在發送或等待訊息之前使用此方法。

  • True — 該機器人與另一個機器人連接。

  • False — 該機器人未與其他機器人連接。

用法:
robot.link.is_connected()

參數

描述

此方法沒有參數。

# Display whether the robot is connected
while True:
    robot.screen.clear_screen()
    robot.screen.set_cursor(1, 1)
    robot.screen.print(robot.link.is_connected())
    wait(50, MSEC)

is_message_available#

is_message_available 返回消息是否在消息队列中等待。在get_message或get_message_and_data之前使用此方法,以便接收机器人仅在到达后尝试读取消息。

  • True — 有訊息可供閱讀。

  • False — 沒有可讀的訊息。

用法:
robot.link.is_message_available()

參數

描述

此方法沒有參數。

例子

機器人 1的程式碼

# Press the screen to send a message to Robot 2
robot.screen.print("Robot 1")

while not robot.screen.pressing():
    wait(5, MSEC)

robot.link.send_message("Hello, Robot 2!")

機器人 2的程式碼

# Wait until a message is available, then display it
robot.screen.print("Robot 2")
robot.screen.next_row()

while not robot.link.is_message_available():
    wait(5, MSEC)

message = robot.link.get_message()
robot.screen.print(message)

get_name#

get_name 傳回已連接機器人的名稱。使用此方法可在發送或接收訊息之前顯示已連接的機器人。如果沒有連接任何機器人,此方法返回 None。

用法:
robot.link.get_name()

參數

描述

此方法沒有參數。

# Display the name of the linked robot
robot.screen.print(robot.link.get_name())

回呼函數#

handle_message#

handle_message 註冊一個函數,當機器人收到特定的文字訊息時運行。當機器人需要自動回應已知訊息而不是循環檢查訊息佇列時,可以使用此方法。首先定義回呼函數,然後使用 handle_message 和要監視的訊息文字註冊函數。

用法:
robot.link.handle_message(callback, message)

範圍

描述

callback

先前定義的函數,當接收到的訊息與 message匹配時運行。

message

用於核對收到的訊息的文字。

例子

機器人 1的程式碼

# Press the screen to send "smile" to Robot 2
robot.screen.print("Robot 1")

while not robot.screen.pressing():
    wait(5, MSEC)

robot.link.send_message("smile")

機器人 2的程式碼

# Show a happy emoji when Robot 1 sends "smile"
def robot_smile():
    robot.screen.show_emoji(HAPPY)

robot.link.handle_message(robot_smile, "smile")

while True:
    wait(50, MSEC)

connected#

connected 註冊一個在機器人與其他機器人連接時運行的函數。當專案需要在機器人之間建立連接後立即做出回應時,請使用此方法。首先定義回呼函數,然後將其註冊到 connected中。

用法:
robot.link.connected(callback, args)

範圍

描述

callback

預先定義的函數,當機器人與其他機器人連接時運行。

args

可選。包含要傳遞給回呼函數的參數的元組。有關更多信息,請參閱使用帶參數的函數。

# Show a happy emoji when connected
def robot_smile():
    robot.screen.show_emoji(HAPPY)

robot.link.connected(robot_smile)

disconnected#

disconnected 註冊一個函數,該函數在機器人與其他機器人斷開連接時運行。當專案需要在機器人間連線遺失後立即回應時,請使用此方法。首先定義回呼函數,然後將其註冊到 disconnected。

用法:
robot.link.disconnected(callback, args)

範圍

描述

callback

預先定義的函數,當機器人不再與其他機器人連接時運作。

args

可選。包含要傳遞給回呼函數的參數的元組。有關更多信息,請參閱使用帶參數的函數。

# Show a sad emoji when disconnected
def robot_sad():
    robot.screen.show_emoji(SAD)

robot.link.disconnected(robot_sad)