信息#
介绍#
两个 VEX AIM 编码机器人可以连接,以便在项目期间进行通信。通过消息功能,机器人可以发送和接收信息,协调彼此的动作。
**注意:**为了发送和接收消息,两个机器人必须同时运行处理消息的项目。
以下是可用方法的列表:
操作——在机器人之间发送消息。
send_message – 向另一个链接的机器人发送一个字符串和最多三个数字。
Getters – 检查收到的消息和连接状态。
get_message – 检索收到的最新字符串。
get_message_and_data – 检索作为元组接收的最新字符串和数字。
is_connected – 返回一个布尔值,指示机器人是否与另一个机器人连接。
is_message_available – 返回一个布尔值,指示其他链接机器人是否曾经发送过消息。
回调——事件发生时运行函数。
handle_message – 注册一个函数,在收到特定消息时调用。
connected – 注册一个函数,当机器人与另一个机器人连接时调用。
disconnected – 注册一个函数,当机器人与另一个机器人断开连接时调用。
行动#
send_message#
send_message
向另一个链接的机器人发送一个字符串和最多三个数字。
用法:
robot.link.send_message(message, arg1, arg2, arg3)
范围 |
描述 |
---|---|
|
发送给其他链接机器人的字符串。 |
|
可选。要发送给其他链接机器人的浮点数或整数。 |
|
可选。要发送给另一个链接机器人的第二个浮点数或整数。 |
|
可选。要发送给另一个链接机器人的第三个浮点数或整数。 |
# Send a message when the screen is pressed
def robot_send_message():
robot.link.send_message("message")
robot.screen.pressed(robot_send_message)
# Display when a message is received
while not robot.link.is_message_available():
wait(5, MSEC)
robot.screen.print("Received!")
# Send the current heading to the other robot
def robot_send_message():
robot.link.send_message("message", robot.inertial.get_heading())
robot.screen.pressed(robot_send_message)
# Turn to match the other robot's heading
while True:
if robot.link.is_message_available():
message, heading = robot.link.get_message_and_data()
robot.turn_to(heading)
wait(5, MSEC)
吸气剂#
get_message#
get_message
返回从其他链接机器人接收到的最新字符串。
用法:
robot.link.get_message(timeout)
参数 |
描述 |
---|---|
|
可选。函数在没有消息的情况下继续执行之前等待消息的最长时间(以毫秒为单位)。默认值为 1000。 |
# Send a message when the screen is pressed
def robot_send_message():
robot.link.send_message("VEXcode!")
robot.screen.pressed(robot_send_message)
# Display when a message is received
while True:
if robot.link.is_message_available():
response = robot.link.get_message()
robot.screen.print(response)
wait(5, MSEC)
get_message_and_data#
get_message_and_data
返回从另一个链接的机器人接收到的最新字符串和数字,并将其作为元组返回。
用法:
robot.link.get_message_and_data(timeout)
参数 |
描述 |
---|---|
|
可选。函数在没有消息的情况下继续执行之前等待消息的最长时间(以毫秒为单位)。默认值为 1000。 |
# Send the current heading to the other robot
def robot_send_message():
robot.link.send_message("message", robot.inertial.get_heading())
robot.screen.pressed(robot_send_message)
# Turn to match the other robot's heading
while True:
if robot.link.is_message_available():
message, heading = robot.link.get_message_and_data()
robot.turn_to(heading)
wait(5, MSEC)
is_connected#
is_connected
返回一个布尔值,指示机器人当前是否与另一个机器人连接。
True
- 机器人与另一个机器人相连。False
- 机器人未与其他机器人连接。
用法:
robot.link.is_connected()
参数 |
描述 |
---|---|
该方法没有参数。 |
# Turn off one of the linked robots to see the message change
while True:
robot.screen.clear_screen(1, 1)
robot.screen.print(robot.link.is_connected())
wait(5, MSEC)
is_message_available#
is_message_available
返回一个布尔值,指示其他链接机器人是否曾经在当前项目中使用过 send_message。
True
- send_message 已被当前项目中的其他链接机器人使用。False
- send_message 从未被当前项目中的其他链接机器人使用过。
用法:
robot.link.is_message_available()
参数 |
描述 |
---|---|
该方法没有参数。 |
# Send a message when the screen is pressed
def robot_send_message():
robot.link.send_message("message")
robot.screen.pressed(robot_send_message)
# Display when a message is received
while not robot.link.is_message_available():
wait(5, MSEC)
robot.screen.print("Received!")
回调#
handle_message#
handle_message
注册一个当机器人收到特定消息字符串时运行的方法。
用法:robot.link.handle_message(callback, message)
参数 |
描述 |
---|---|
|
当收到的消息与指定的字符串匹配时调用的先前定义的函数。 |
|
用于检查传入消息的字符串。如果匹配,则运行 |
# Show a smiling emoji
def robot_smile():
robot.screen.show_emoji(HAPPY)
# If the message says "smile", show a smile
robot.link.handle_message(robot_smile, "smile")
# Send a message when the screen is pressed
def robot_send_message():
# Change the message to not show the emoji
robot.link.send_message("smile")
robot.screen.pressed(robot_send_message)
connected#
connected
注册一个函数,当机器人链接到另一个机器人时调用。
用法:robot.link.connected(callback, args)
参数 |
描述 |
---|---|
|
当机器人链接到另一个机器人时调用的先前定义的函数。 |
|
可选。包含要传递给回调函数的参数的元组。更多信息请参阅使用带参数的函数。 |
# Show a happy face when connected
def robot_smile():
robot.screen.show_emoji(HAPPY)
# Show a sad face when disconnected
def robot_sad():
robot.screen.show_emoji(SAD)
# Register both callbacks
# Turn off a robot to change the emoji
robot.link.connected(robot_smile)
robot.link.disconnected(robot_sad)
disconnected#
disconnected
注册一个函数,当机器人未链接到另一个机器人时调用。
用法:robot.link.disconnected(callback, args)
参数 |
描述 |
---|---|
|
当机器人未链接到另一个机器人时调用的先前定义的函数。 |
|
可选。包含要传递给回调函数的参数的元组。更多信息请参阅使用带参数的函数。 |
# Show a happy face when connected
def robot_smile():
robot.screen.show_emoji(HAPPY)
# Show a sad face when disconnected
def robot_sad():
robot.screen.show_emoji(SAD)
# Register both callbacks
# Turn off a robot to change the emoji
robot.link.connected(robot_smile)
robot.link.disconnected(robot_sad)