주요 콘텐츠

ROS 2 패키지에서 사용자 지정 메시지 만들기

사용자 지정 메시지를 사용하여 현재 ROS 2에서 지원되는 메시지 유형 세트를 확장할 수 있습니다. 사용자 지정 메시지는 사용자가 정의하는 메시지입니다. 현재 지원되는 메시지 유형을 보내고 받고 있다면 사용자 지정 메시지를 사용할 필요가 없습니다. 지원되는 메시지 유형의 목록을 보려면 MATLAB® 명령 창에 ros2 msg list를 입력합니다. 지원되는 ROS 2 메시지에 대한 자세한 내용은 Work with Basic ROS 2 Messages 항목을 참조하십시오.

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를 제공했습니다. 이 예제에서는 여러 메시지가 포함된 폴더를 사용하여 이들을 동시에 생성할 수 있음을 보여줍니다.

새 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 Publisher 및 ROS 2 Subscriber를 사용해 데이터 교환하기 항목을 참조하십시오.

example_b_msgs/Standalone 메시지를 사용할 publisher를 생성합니다.

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

동일한 토픽에 대한 subscriber를 생성합니다.

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 필드를 사용하여 subscriber가 받은 최근 메시지를 확인합니다.

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 파일)가 사용자 지정 메시지 패키지 폴더에 포함되어 있는지 확인해야 합니다.

참고 항목

| | | |

도움말 항목