消息#

简介#

两台 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] (#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)