메시지 링크#
소개#
MessageLink는 두 대의 V5 Brain 로봇이 짧은 문자열 메시지와 선택적으로 소량의 데이터를 주고받으며 통신할 수 있도록 해줍니다. 이 기능은 로봇 간의 협업, 예를 들어 루틴 시작, 센서 결과 공유 또는 다른 로봇의 동작 실행 등을 위해 설계되었습니다.
send을 사용할 때마다 연결된 V5 Brain의 큐에 메시지가 추가되며, 큐는 선입선출(FIFO) 방식으로 읽힙니다. 다른 V5 Brain이 receive을 사용하기 전에 여러 메시지가 전송되면 해당 메시지는 전송된 순서대로 하나씩 저장되고 반환됩니다. 메시지는 큐에 저장될 수 있으므로 매 루프마다 동일한 상태를 반복적으로 전송하면 백로그가 발생할 수 있습니다. 시간 제약이 있는 로직의 경우 값이 변경될 때만 전송하십시오.
중요: 두 로봇 모두 동시에 MessageLink MessageLink 사용하는 프로젝트를 실행해야 합니다. 그렇지 않으면 메시지가 송수신되지 않습니다.
This page uses link as the example MessageLink name. Replace it with your own configured name as needed.
다음은 사용 가능한 방법 목록입니다.
링크 생성#
두 대의 V5 브레인에 메시지링크를 생성하여 두 로봇 간의 통신 채널을 구축하십시오. 무선으로는 V5 로봇 라디오를 사용하거나 유선 스마트 케이블을 통해 연결할 수 있습니다. 이렇게 하면 로봇들이 서로 메시지를 주고받을 수 있습니다.
MessageLink(smartport, name, linktype, wired)
매개변수 |
설명 |
|---|---|
|
이 링크에 사용되는 스마트 포트는 V5 로봇 라디오가 연결된 포트(무선) 또는 스마트 케이블이 꽂힌 포트(유선)입니다. 형식은 |
|
이 링크에 부여할 고유한 문자열 이름입니다. |
|
이 브레인이 해당 링크 쌍에 사용할 역할입니다. 각 쌍에는 관리자 1명과 작업자 1명이 포함되어야 합니다.
|
|
선택 사항입니다. 이 연결이 유선인지 무선인지 여부를 지정합니다.
|
로봇 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 Brain이 서로 쌍을 이루는지 여부를 반환합니다.
True– 두 개의 V5 브레인이 페어링되어 이 링크를 통해 통신하고 있습니다.False– 이 링크에서는 두 개의 V5 브레인이 쌍을 이루지 않습니다.
참고: 프로젝트 시작 시 추가 코드를 실행하기 전에 V5 브레인이 제대로 연결되었는지 항상 확인하는 것이 좋습니다.
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 브레인으로 문자열 및/또는 정수 및/또는 부동 소수점을 전송합니다.
Usage:
send(string, integer, float)
매개변수 |
설명 |
|---|---|
|
연결된 다른 V5 브레인으로 보낼 문자열입니다. |
|
선택 사항입니다. 연결된 다른 V5 브레인으로 보낼 정수입니다. |
|
선택 사항입니다. 연결된 다른 V5 브레인으로 보낼 부동 소수점 값입니다. 부동 소수점 값은 소수점 이하 4자리까지 형식이 지정됩니다. 4자리보다 적은 자릿수를 제공하면 4자리가 표시될 때까지 끝에 0이 추가됩니다. |
If only string is sent, the other linked V5 Brain receives that exact string.
만약 integer 및/또는 float 이 포함되면, 수신하는 브레인은 모든 필드를 다음 순서로 결합한 단일 인코딩된 문자열을 받습니다: .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 브레인에서 대기 중인 다음 메시지를 반환합니다. 메시지는 FIFO 순서(가장 오래된 미읽기 메시지부터)로 읽습니다. receive 호출될 때 큐가 비어 있으면 지정된 timeout 까지 새 메시지를 기다립니다. 해당 시간 내에 메시지가 도착하지 않으면 receive 은 None을 반환하고, 이후에 전송된 메시지는 다음에 receive 사용될 때 읽히도록 큐에 남아 있습니다.
만약 다른 연결된 V5 브레인이 string만 보냈다면, receive 수신하고 정확히 그 문자열을 반환합니다.
다른 연결된 V5 브레인이 정수 및/또는 부동 소수점을 전송한 경우, receive 수신하고 모든 필드를 다음 순서로 결합한 단일 인코딩된 문자열을 반환합니다: .string_integer_float
예를 들어, 연결된 다른 V5 브레인이 link.send(“string”, 1, 2.55)사용하는 경우, <code>link.receive()</code> 를 사용하면<code>.string_1_2.5500</code> 같은 인코딩된 문자열이 반환됩니다. 이 인코딩된 문자열을 분할하여(아래 예 참조) 메시지 이름, 정수 및 부동 소수점 값을 추출할 수 있습니다.
수동 분할/구문 분석을 피하려면 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 등록합니다.
Usage:
link.received(message, arg)
매개변수 |
설명 |
|---|---|
|
일치시킬 메시지 이름(문자열). 수신된 메시지의 이름이 이 값과 일치하면 |
|
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)