메시지#

소개#

두 대의 VEX AIM 코딩 로봇은 프로젝트 진행 중 서로 통신할 수 있도록 연결할 수 있습니다. 메시지 전송 방식을 통해 한 로봇이 다른 로봇에게 메시지를 보낼 수 있습니다. 메시지에는 텍스트만 포함하거나 최대 세 자리 숫자가 포함된 텍스트를 보낼 수 있습니다.

수신된 메시지는 선입선출(FIFO) 큐에 저장됩니다. 즉, 먼저 수신된 메시지가 가장 먼저 읽힙니다.

is_message_available를 사용하여 메시지를 읽을 준비가 되었는지 확인합니다. 그런 다음 get_message를 사용하여 다음 텍스트 메시지를 읽습니다. 메시지에 숫자도 포함된 경우 get_message_and_data를 사용하여 텍스트와 숫자를 함께 읽습니다.

참고: 메시지를 주고받으려면 두 로봇이 연결되어 있어야 하며, 두 로봇 모두 메시지 메서드를 사용하는 프로젝트를 실행 중이어야 합니다.

다음은 모든 방법 목록입니다.

동작 — 로봇 간 메시지 전송.

  • send_message — 연결된 로봇에게 텍스트와 최대 3개의 숫자를 보냅니다.

게터 — 메시지를 읽거나 연결 상태를 확인합니다.

  • get_message — 다음으로 수신된 문자 메시지를 반환합니다.

  • get_message_and_data — 다음으로 수신된 문자 메시지와 번호를 반환합니다.

  • is_connected — 로봇이 다른 로봇과 연결되어 있는지 여부를 반환합니다.

  • is_message_available — 메시지를 읽을 수 있는지 여부를 반환합니다.

  • get_name — 연결된 로봇의 이름을 반환합니다.

콜백 — 메시지 이벤트가 발생할 때 함수를 실행합니다.

  • handle_message — 특정 메시지가 수신되면 함수를 실행합니다.

  • connected — 로봇이 다른 로봇과 연결될 때 기능을 실행합니다.

  • disconnected — 로봇이 더 이상 다른 로봇과 연결되지 않은 경우 함수를 실행합니다.

행동#

send_message#

send_message 은 연결된 로봇에게 텍스트와 최대 세 개의 숫자를 전송합니다. 다른 로봇이 명령, 신호 또는 간단한 정보를 수신해야 할 때 한 로봇에서 이 메서드를 사용합니다. 메시지 내용이 텍스트만으로 충분할 경우 텍스트만 전송합니다. 메시지에 관련 값이 필요한 경우 숫자를 추가합니다. 예를 들어 “heading” 과 함께 로봇의 현재 방향 값을 전송할 수 있습니다.

수신 로봇에서는 is_message_available를 사용하여 메시지가 도착했는지 확인합니다. 그런 다음 텍스트만 있는 메시지의 경우 get_message를 사용하고 숫자가 포함된 메시지의 경우 get_message_and_data를 사용합니다.

Usage:
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를 사용하세요.

Usage:
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를 사용하여 텍스트와 숫자를 함께 전송합니다.

Usage:
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 — 로봇이 다른 로봇과 연결되어 있지 않습니다.

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_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)

매개변수

설명

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 사용하여 등록하십시오.

Usage:
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 사용하여 등록합니다.

Usage:
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)