メッセージリンク#
導入#
MessageLinkは、2つのEXP Brainが短い文字列メッセージとオプションで少量のデータを送信することで通信できるようにする機能です。ルーチンの開始、センサー結果の共有、別のロボットの動作のトリガーなど、ロボット間の連携を目的として設計されています。
send を使用するたびに、リンクされた EXP Brain のキューにメッセージが追加され、キューは先入れ先出し (FIFO) で読み取られます。他の EXP Brain が receive を使用する前に複数のメッセージが送信された場合、それらは保存され、送信された順序で 1 つずつ返されます。メッセージはキューに格納できるため、ループごとに同じステータスを繰り返し送信するとバックログが発生する可能性があります。時間制約のあるロジックの場合は、値が変更されたときのみ送信してください。
重要: 両方のロボットが同時に MessageLink を使用するプロジェクトを実行している必要があります。そうでない場合、メッセージの送受信は行われません。
This page uses link as the example MessageLink name. Replace it with your own configured name as needed.
利用可能なメソッドの一覧は以下のとおりです。
リンクを作成する#
両方のEXP BrainにMessageLinkを作成して、V5 Robot Radioを使用した無線接続、またはスマートケーブルを使用した有線接続のいずれかで、両者間の通信チャネルを確立し、ロボット同士がメッセージを送受信できるようにします。
MessageLink(smartport, name, linktype, wired)
パラメータ |
説明 |
|---|---|
|
このリンクに使用されるスマートポートとは、V5 Robot Radioが接続されているポート(無線)またはスマートケーブルが差し込まれているポート(有線)のことです。0 |
|
このリンク |
|
このリンクペアで使用するブレインの役割。各ペアには、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の設定が完了し、Brainがリンクされると、以下の方法を使用してメッセージの送受信や返信を行うことができます。
is_linked#
is_linked メソッドは、MessageLink 上の EXP Brain が互いにペアになっているかどうかを返します。
True– 2 つの EXP Brain はペアになっており、このリンクで通信しています。False– このリンクでは、2 つの EXP Brain はペアになっていません。
注: プロジェクトの開始時にEXP Brainがリンクされていることを常に確認してから、さらにコードを実行することをお勧めします。
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 文字列、整数、浮動小数点数をもう一方の EXP Brain に送信します。
Usage:
send(string, integer, float)
パラメータ |
説明 |
|---|---|
|
他のリンクされたEXP Brainに送信する文字列。 |
|
オプション。リンク先のEXP Brainに送信する整数値。 |
|
オプション。リンク先のEXP Brainに送信する浮動小数点数。浮動小数点数は小数点以下4桁まででフォーマットされます。小数点以下の桁数が少ない場合は、4桁になるまで末尾にゼロが追加されます。 |
If only string is sent, the other linked EXP Brain receives that exact string.
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 returns the next queued message from the other linked EXP Brain. Messages are read in FIFO order (oldest unread first). If the queue is empty when receive is called, it waits up to the specified timeout for a new message. If no message arrives in that window, receive returns None, and any message sent afterward remains in the queue to be read the next time receive is used.
もしリンクされた別のEXP Brainが文字列 string receive はその文字列を正確に返します。
他のリンクされた EXP Brain が整数および/または浮動小数点数を送信した場合、 receive を受信し、すべてのフィールドを次の順序で組み合わせた単一のエンコードされた文字列を返します: .string_integer_float
例えば、リンクされた別の EXP 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 は、EXP Brain が送信メッセージを受信するたびに呼び出される関数を登録します。
Usage:
link.received(message, arg)
パラメータ |
説明 |
|---|---|
|
照合するメッセージ名(文字列)。受信したメッセージ名がこの値と一致すると、コールバック関数 |
|
EXP Brainがメッセージ |
received のコールバックが4つの引数で呼び出されました。
コールバック署名:
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)