メインコンテンツ

ROS 2 パッケージからのカスタム メッセージの作成

カスタム メッセージを使用して、ROS 2 で現在サポートされているメッセージ タイプのセットを拡張します。カスタム メッセージはユーザー定義のメッセージです。サポートされているメッセージ タイプを送受信する場合は、カスタム メッセージを使用する必要はありません。サポートされているメッセージ タイプのリストを表示するには、MATLAB® コマンド ウィンドウで「ros2 msg list」と入力します。サポートされている ROS 2 メッセージの詳細については、基本的な ROS 2 メッセージの操作を参照してください。

ROS 2 カスタム メッセージをはじめて取り扱う場合は、ROS Toolbox システム要件を参照してください。

ROS 2 カスタム メッセージは、msg という名前のフォルダーを含む ROS 2 パッケージ フォルダー内に指定されます。msg フォルダーには、すべてのカスタム メッセージ タイプ定義が含まれています。たとえば、custom フォルダーにある example_b_msgs パッケージのフォルダーとファイルの構造は次のとおりです。

ROS 2 custom message folder structure

このパッケージにはカスタム メッセージ タイプ Standalone.msg が含まれています。MATLAB はこれらのファイルを使用して、パッケージに含まれるカスタム メッセージを使用するために必要なファイルを生成します。

この例では、MATLAB で ROS 2 カスタム メッセージを作成します。必要な msg ファイルが含まれる ROS 2 パッケージがなければなりません。

カスタム メッセージ パッケージが正しいことを確認したら、親フォルダーへのパスを指定し、指定したパスで ros2genmsg を呼び出します。次の例では、依存関係にある example_package_a,example_package_b、および example_package_c という 3 つのメッセージが指定されています。また、この例は、複数のメッセージが含まれるフォルダーを使用して、すべてのメッセージを同時に生成できることも示しています。

新しい MATLAB セッションを開き、カスタム メッセージ フォルダーをローカル フォルダーに作成します。

folderPath = fullfile(pwd,"custom");
copyfile("example_*_msgs",folderPath);

カスタム メッセージ ファイルのフォルダー パスを指定し、ros2genmsg を使用してカスタム メッセージを作成します。

ros2genmsg(folderPath)
Identifying message files in folder 'C:/Work/custom'.Done.
Removing previous version of Python virtual environment.Done.
Creating a Python virtual environment.Done.
Adding required Python packages to virtual environment.Done.
Copying include folders.Done.
Copying libraries.Done.
Validating message files in folder 'C:/Work/custom'.Done.
[3/3] Generating MATLAB interfaces for custom message packages... Done.
Running colcon build in folder 'C:/Work/custom/matlab_msg_gen/win64'.
Build in progress. This may take several minutes...
Build succeeded.build log

ros2 msg list を呼び出して、新しいカスタム メッセージの作成を確認します。

ros2 msg list
action_msgs/CancelGoalRequest
action_msgs/GoalInfo
action_msgs/GoalStatusArray
actionlib_msgs/GoalID
actionlib_msgs/GoalStatus
builtin_interfaces/Duration
builtin_interfaces/Time composition_interfaces/ListNodesRequest
composition_interfaces/ListNodesResponse
diagnostic_msgs/AddDiagnosticsRequest
diagnostic_msgs/AddDiagnosticsResponse
example_a_msgs/DependsOnB
example_b_msgs/Standalone
example_interfaces/AddTwoIntsRequest
example_interfaces/AddTwoIntsResponse
example_interfaces/Bool
example_interfaces/Byte...

これで、上で作成したカスタム メッセージを標準メッセージとして使用できます。メッセージ送受信の詳細については、ROS 2 のパブリッシャーとサブスクライバーとのデータ交換を参照してください。

example_b_msgs/Standalone メッセージを使用するパブリッシャーを作成します。

node = ros2node("/node_1");
pub = ros2publisher(node,"/example_topic","example_b_msgs/Standalone");

同じトピックでサブスクライバーを作成します。

sub = ros2subscriber(node,"/example_topic");

メッセージを作成し、そのメッセージを送信します。

custom_msg = ros2message("example_b_msgs/Standalone");
custom_msg.int_property = uint32(12);
custom_msg.string_property='This is ROS 2 custom message example';
send(pub,custom_msg);
pause(3) % Allow a few seconds for the message to arrive

LatestMessage フィールドを使用すると、サブスクライバーが最近受信したメッセージがわかります。

sub.LatestMessage
ans = struct with fields:
        MessageType: 'example_b_msgs/Standalone'
       int_property: 12
    string_property: 'This is ROS 2 custom message example'

作成した ROS オブジェクトを削除します。

clear node pub sub

組み込みメッセージ定義のカスタム定義との置き換え

MATLAB は、多くの組み込み ROS 2 メッセージ タイプを提供しています。これらのメッセージ タイプの定義を、上記で詳述した同じカスタム メッセージ作成ワークフローを使用して、新しい定義と置き換えることができます。組み込みのメッセージ パッケージの定義を置き換える場合は、カスタム メッセージ パッケージ フォルダーに、対応する組み込みメッセージ パッケージのすべてのメッセージ タイプに対する新しい定義 (.msg ファイル) が含まれていることを確認する必要があります。

参考

| | | |

トピック