消息链接#
介绍#
MessageLink允许两个V5大脑通过发送带有可选少量数据的短字符串消息进行通信。它专为机器人之间的协调而设计,例如启动程序、共享传感器结果或触发另一个机器人的行为。
每次调用 send 都会向关联的 V5 Brain 的队列中添加一条消息,队列采用先进先出 (FIFO) 的读取方式。如果在另一个 V5 Brain 使用 receive 之前发送了多条消息,这些消息将被存储,并按发送顺序逐条返回。由于消息可以排队,因此在每次循环中重复发送相同的状态可能会导致积压;对于时间关键型逻辑,仅当值发生变化时才发送消息。
重要提示:两个机器人必须同时运行使用 MessageLink 项目,否则将不会发送/接收任何消息。
本页面使用 link 作为示例链接 MessageLink 名称。请根据需要将其替换为您自己配置的名称。
以下是可用方法列表:
创建链接#
在两个 V5 大脑上创建 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 上的 V5 Brains 是否彼此配对。
True– 两个 V5 大脑已配对并通过此链路进行通信。False– 此链接中的两个 V5 大脑未配对。
注意:在项目开始时,运行任何其他代码之前,最好始终检查以确保 V5 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 向另一个连接的 V5 Brain 发送字符串和/或整数和/或浮点数。
Usage:
send(string, integer, float)
参数 |
描述 |
|---|---|
|
要发送给另一个已连接的 V5 大脑的字符串。 |
|
可选。要发送给另一个已连接的 V5 大脑的整数。 |
|
可选。要发送到另一个已连接的 V5 Brain 的浮点数。浮点数默认显示 4 位小数;如果提供的位数少于 4 位,则会在末尾添加零,直到显示 4 位小数为止。 |
如果只发送字符串 string ,则另一个连接的 V5 大脑会收到完全相同的字符串。
如果包含 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 返回来自另一个已连接的 V5 Brain 的下一条已排队的消息。消息按先进先出 (FIFO) 顺序读取(最旧的未读消息优先)。如果在调用 receive 时队列为空,则会等待最多指定的 receive 时间以接收新消息。如果在该时间内没有消息到达, receive 返回 None,之后发送的任何消息都将保留在队列中,以便在下次使用 timeout 时读取。
If the other linked V5 Brain sent only a string, receive returns that exact string.
如果另一个连接的 V5 Brain 发送的是整数和/或浮点数, receive 返回一个编码字符串,该字符串按以下顺序组合所有字段: .string_integer_float
例如,如果另一个链接的 V5 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 注册一个函数,当 V5 大脑收到发送的消息时,该函数将被调用。
Usage:
link.received(message, arg)
参数 |
描述 |
|---|---|
|
The message name (string) to match. When a received message’s name matches this value, the |
|
先前定义的 函数 在 V5 大脑收到与消息 |
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)