Message#

Introduction#

Two VEX AIM Coding Robots can be linked so they communicate during a project. Using Message, the robots can send and receive information to coordinate their actions.

Note: For messages to be sent and received, both robots must be running projects that handle messaging at the same time.

Below is a list of available methods:

Actions – Send messages between robots.

  • send_message – Sends a string and up to three numbers to the other linked robot.

Getters – Check for received messages and connection status.

  • get_message – Retrieve the most recent string received.

  • get_message_and_data – Retrieve the most recent string and numbers received as a tuple.

  • is_connected – Returns a Boolean indicating whether the robot is linked with another robot.

  • is_message_available – Returns a Boolean indicating whether the other linked robot has ever sent a message.

Callbacks – Run functions when events occur.

  • handle_message – Register a function to be called when a specific message is received.

  • connected – Register a function to be called when the robot is connected with another robot.

  • disconnected – Register a function to be called when the robot is disconnected with another robot.

Actions#

send_message#

send_message sends a string and up to three numbers to the other linked robot.

Usage:
robot.link.send_message(message, arg1, arg2, arg3)

Parameter

Description

message

The string to send to the other linked robot.

arg1

Optional. A float or integer to send to the other linked robot.

arg2

Optional. A second float or integer to send to the other linked robot.

arg3

Optional. A third float or integer to send to the other linked robot.

Code for Robot 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)

# "right" turns right, "left" turns left
robot.link.send_message("right")


Code for Robot 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)


Code for Robot 1

# Continuously send the current heading to Robot 2
robot.screen.print("Robot 1")
while True:
    robot.link.send_message("Turn to heading", robot.inertial.get_heading())
    wait(5, MSEC)


Code for 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(5, MSEC)


Code for Robot 1

robot.screen.show_aivision()
while True:
    # Only return a max of 3 AprilTag IDs for message limit
    detections = robot.vision.get_data(ALL_TAGS, 3)

    # Identify the IDs collected
    ids = [det.id for det in detections]

    # Send IDs as separate arguments
    robot.link.send_message("Vision Data", *ids)
    # Tuple organized like: ("Vision Data", ID 1, ID 2, ID 3)

    wait(50, MSEC)


Code for Robot 2

while True:
    if robot.link.is_message_available():
        # Receive the message from Robot 1
        data = robot.link.get_message_and_data()

        # Separate out the received message
        message = data[0]
        arg1 = data[1] if len(data) > 1 else None
        arg2 = data[2] if len(data) > 2 else None
        arg3 = data[3] if len(data) > 3 else None

        # Reset the screen
        robot.screen.clear_screen()
        robot.screen.set_cursor(1, 1)
        robot.screen.print("Detected IDs:")
        robot.screen.next_row()

        # Display any IDs that exist from the data
        if message == "Vision Data":
            for tag_id in (arg1, arg2, arg3):
                if tag_id is not None:
                    robot.screen.print(tag_id)
                    robot.screen.next_row()

    wait(50, MSEC)

Getters#

get_message#

get_message returns the most recent string received from the other linked robot.

Usage:
robot.link.get_message(timeout)

Parameters

Description

timeout

Optional. The maximum time (in milliseconds) the function will wait for a message before continuing without one. The default is 1000.

Code for Robot 1

# Press the screen to send a message to Robot 2
robot.screen.print("Robot 1")
while not robot.screen.pressing():
    wait(5, MSEC)

# Change the string to display a different message
robot.link.send_message("Hello, Robot 2!")


Code for Robot 2

# Display what message Robot 1 sends
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 most recent string and numbers received from the other linked robot and returns it as a tuple.

Usage:
robot.link.get_message_and_data(timeout)

Parameters

Description

timeout

Optional. The maximum time (in milliseconds) the function will wait for a message before continuing without one. The default is 1000.

Code for Robot 1

# Code for Robot 1

# Continuously send the current heading to Robot 2
robot.screen.print("Robot 1")
while True:
    robot.link.send_message("Turn to heading", robot.inertial.get_heading())
    wait(5, MSEC)


Code for 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(5, MSEC)

is_connected#

is_connected returns a Boolean indicating whether the robot is currently linked with another robot.

  • True - The robot is linked with another robot.

  • False - The robot is not linked with another robot.

Usage:
robot.link.is_connected()

Parameters

Description

This method has no parameters.

# 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 returns a Boolean indicating whether the other linked robot has ever used send_message yet in the current project.

  • True - send_message has been used by the other linked robot in the current project.

  • False - send_message has never been used by the other linked robot in the current project.

Usage:
robot.link.is_message_available()

Parameters

Description

This method has no parameters.

Code for Robot 1

# Press the screen to send a message to Robot 2
robot.screen.print("Robot 1")
while not robot.screen.pressing():
    wait(5, MSEC)

# Change the string to display a different message
robot.link.send_message("Hello, Robot 2!")


Code for Robot 2

# Display what message Robot 1 sends
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)

Callbacks#

handle_message#

handle_message registers a method that runs when the robot receives a specific message string.

Usage:
robot.link.handle_message(callback, message)

Parameters

Description

callback

A previously defined function that is called when the received message matches the specified string.

message

The string to check incoming messages against. If it matches, the callback runs.

Code for Robot 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("smile")


Code for Robot 2

# Show a smiling emoji
def robot_smile():
    robot.screen.show_emoji(HAPPY)

# If Robot 1 sends "smile", show a smiling emoji
robot.screen.print("Robot 2")
while not robot.link.is_message_available():
    wait(5, MSEC)

robot.link.handle_message(robot_smile, "smile")

connected#

connected registers a function to be called when the robot is linked to another robot.

Usage:
robot.link.connected(callback, args)

Parameters

Description

callback

A previously defined function that is called when the robot is linked to another robot.

args

Optional. A tuple containing arguments to pass to the callback function. See Using Functions with Parameters for more information.

# 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 registers a function to be called when the robot is not linked to another robot.

Usage:
robot.link.disconnected(callback, args)

Parameters

Description

callback

A previously defined function that is called when the robot is not linked to another robot.

args

Optional. A tuple containing arguments to pass to the callback function. See Using Functions with Parameters for more information.

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