消息#
简介#
两台 VEX AIM 编程机器人可以连接起来,以便在项目进行过程中进行通信。消息传递方式允许一台机器人向另一台机器人发送消息。消息可以包含纯文本,也可以包含最多三个数字的文本。
接收到的消息会存储在先进先出(FIFO)队列中。这意味着最先接收到的消息将首先被读取。
使用 is_message_available 检查是否有消息待读取。然后使用 get_message 读取下一条文本消息。如果消息还包含数字,则使用 get_message_and_data 同时读取文本和数字。
**注意:**要发送和接收消息,两个机器人必须已连接,并且两个机器人必须运行使用消息方法的项目。
以下是所有方法的列表:
操作——在机器人之间发送消息。
[
send_message] (#send_message) —向链接的机器人发送文本和最多三个数字。
获取器 — 读取消息或检查连接状态。
[
get_message] (#get_message) —返回下一条收到的短信。[
get_message_and_data] (#get_message_and_data) —返回下一条收到的短信和数字。[
is_connected] (#is_connected) —返回机器人是否与其他机器人链接。[
is_message_available] (#is_message_available) —返回消息是否可读。[
get_name] (#get_name) —返回链接机器人的名称。
Callbacks — Run functions when message events occur.
[
handle_message] (#handle_message) —在收到特定消息时运行函数。[
connected] (#connected) —当机器人与其他机器人链接时运行函数。[
disconnected] (#disconnected) —当机器人不再与其他机器人链接时运行函数。
操作#
send_message#
send_message 向已连接的机器人发送文本以及最多三个数字。当另一台机器人需要接收命令、信号或简短信息时,在其中一台机器人上使用此方法。如果消息本身已经足够,只发送文本即可。当消息需要附带相关数值时,可以添加数字,例如发送 “heading” 以及机器人当前的航向值。
在接收方机器人上,使用 is_message_available 检查消息是否已到达。然后,对于纯文本消息使用 get_message,对于包含数字的消息使用 get_message_and_data。
Usage:
robot.link.send_message(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 returns the next received text message from the message queue. Use this method on the receiving robot when the linked robot sent a text-only message. A common order is to use is_message_available to check that a message has arrived, then use get_message before displaying, comparing, or using the returned text in another method.
Use send_message on the other robot to send a text-only message.
Usage:
robot.link.get_message(timeout)
参数 |
描述 |
|---|---|
|
Optional. The maximum time, in milliseconds, to wait for a message before continuing. The default is |
示例
机器人 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 returns the next received text message and numbers from the message queue as a tuple. Use this method on the receiving robot when the linked robot sent text with one or more numbers. A common order is to use is_message_available to check that a message has arrived, then use get_message_and_data before using the returned text and numbers.
Use send_message on the other robot to send the text and numbers together.
Usage:
robot.link.get_message_and_data(timeout)
参数 |
描述 |
|---|---|
|
Optional. The maximum time, in milliseconds, to wait for a message before continuing. The default is |
示例
机器人 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 returns whether the robot is linked with another robot. Use this method before sending or waiting for messages if the project needs to check that the robot-to-robot connection is ready.
True— The robot is linked with another robot.False— The robot is not linked with another robot.
Usage:
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)或[get_message_and_data] (#get_message_and_data)之前使用此方法,以便接收机器人仅在到达后尝试读取消息。
True— 有一条可以读取的消息。False—没有可供阅读的消息。
Usage:
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。
Usage:
robot.link.get_name()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Display the name of the linked robot
robot.screen.print(robot.link.get_name())
回调#
handle_message#
handle_message 注册一个在机器人接收到特定文本消息时运行的函数。当机器人应自动响应已知消息而不是在循环中检查消息队列时,请使用此方法。首先定义回调函数,然后使用 handle_message 和消息文本进行注册。
Usage:
robot.link.handle_message(callback, 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。
Usage:
robot.link.connected(callback, args)
参数 |
描述 |
|---|---|
|
预先定义的函数,当机器人与其他机器人连接时运行。 |
|
可选。包含要传递给回调函数的参数的元组。更多信息请参阅使用带参数的函数。 |
# Show a happy emoji when connected
def robot_smile():
robot.screen.show_emoji(HAPPY)
robot.link.connected(robot_smile)
disconnected#
disconnected 注册当机器人不再与其他机器人链接时运行的函数。当机器人到机器人连接丢失后,项目应立即响应时,请使用此方法。首先定义回调函数,然后将其注册为 disconnected。
Usage:
robot.link.disconnected(callback, args)
参数 |
描述 |
|---|---|
|
预先定义的函数,当机器人不再与其他机器人连接时运行。 |
|
可选。包含要传递给回调函数的参数的元组。更多信息请参阅使用带参数的函数。 |
# Show a sad emoji when disconnected
def robot_sad():
robot.screen.show_emoji(SAD)
robot.link.disconnected(robot_sad)